androidx.test.InstrumentationRegistry Java Examples

The following examples show how to use androidx.test.InstrumentationRegistry. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RunnerArgsTest.java    From android-test with Apache License 2.0 6 votes vote down vote up
/** Test that a custom filter with bundle is created */
@Test
public void testFromBundle_customFilterWithBundle() {
  Bundle b = new Bundle();
  b.putString(RunnerArgs.ARGUMENT_FILTER, CustomTestFilterTakesBundle.class.getName());
  b.putString(CustomTestFilterTakesBundle.class.getName(), "test");
  RunnerArgs args =
      new RunnerArgs.Builder()
          .fromBundle(InstrumentationRegistry.getInstrumentation(), b)
          .build();
  assertEquals("Mismatch in number of filters created", 1, args.filters.size());
  Filter filter = args.filters.get(0);
  assertTrue("Filter not of correct type", filter instanceof CustomTestFilterTakesBundle);
  CustomTestFilterTakesBundle customTestFilterTakesBundle = (CustomTestFilterTakesBundle) filter;
  assertEquals("Filter not of correct type", "test", customTestFilterTakesBundle.getTest());
}
 
Example #2
Source File: MockIntegrationTestObjects.java    From dhis2-android-sdk with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public MockIntegrationTestObjects(MockIntegrationTestDatabaseContent content) throws Exception {
    this.content = content;

    Context context = InstrumentationRegistry.getTargetContext().getApplicationContext();
    Stetho.initializeWithDefaults(context);

    dhis2MockServer = new Dhis2MockServer(0);
    CalendarProviderFactory.setFixed();

    d2 = D2Factory.forNewDatabase();

    databaseAdapter = d2.databaseAdapter();
    d2DIComponent = d2.d2DIComponent;

    resourceHandler = ResourceHandler.create(databaseAdapter);
    resourceHandler.setServerDate(serverDate);
}
 
Example #3
Source File: PFSecurityUtilsTest.java    From PFLockScreen-Android with Apache License 2.0 6 votes vote down vote up
@Test
public void pfSecurityUtilsOld() throws Exception {
    // Context of the app under test.
    final String alias = "test_alias_old";
    final String pinCode = "1234";

    final Context appContext = InstrumentationRegistry.getTargetContext();

    PFSecurityUtilsOld.getInstance().deleteKey(alias);

    final boolean isAliasFalse = PFSecurityUtilsOld.getInstance().isKeystoreContainAlias(alias);
    assertFalse(isAliasFalse);

    final String encoded = PFSecurityUtilsOld.getInstance().encode(appContext, alias, pinCode, false);
    assertNotNull(encoded);

    final boolean isAliasTrue = PFSecurityUtilsOld.getInstance().isKeystoreContainAlias(alias);
    assertTrue(isAliasTrue);

    final String decoded = PFSecurityUtilsOld.getInstance().decode(alias, encoded);
    assertEquals(decoded, pinCode);

    PFSecurityUtilsOld.getInstance().deleteKey(alias);
    final boolean isAliasFalse2 = PFSecurityUtilsOld.getInstance().isKeystoreContainAlias(alias);
    assertFalse(isAliasFalse2);
}
 
Example #4
Source File: SettingsScreenshots.java    From focus-android with Mozilla Public License 2.0 6 votes vote down vote up
@Before
public void clearSettings() {
    PreferenceManager.getDefaultSharedPreferences(
            InstrumentationRegistry.getInstrumentation().getTargetContext())
            .edit()
            .clear()
            .apply();
    CustomDomains.INSTANCE.save(
            InstrumentationRegistry.getInstrumentation().getTargetContext(),
            Collections.emptyList());

    final Context appContext = InstrumentationRegistry.getInstrumentation()
            .getTargetContext()
            .getApplicationContext();
    PreferenceManager.getDefaultSharedPreferences(appContext)
            .edit()
            .putBoolean(FIRSTRUN_PREF, true)
            .apply();
}
 
Example #5
Source File: RunnerArgsTest.java    From android-test with Apache License 2.0 6 votes vote down vote up
/** Test parsing the boolean idle argument */
@Test
public void testFromBundle_targetProcess() {
  Bundle b = new Bundle();
  b.putString(RunnerArgs.ARGUMENT_TARGET_PROCESS, "com.foo.bar");
  RunnerArgs args =
      new RunnerArgs.Builder()
          .fromBundle(InstrumentationRegistry.getInstrumentation(), b)
          .build();
  assertEquals("com.foo.bar", args.targetProcess);

  b.putString(RunnerArgs.ARGUMENT_TARGET_PROCESS, "com.foo.bar:ui");
  args =
      new RunnerArgs.Builder()
          .fromBundle(InstrumentationRegistry.getInstrumentation(), b)
          .build();
  assertEquals("com.foo.bar:ui", args.targetProcess);
}
 
Example #6
Source File: RunnerArgsTest.java    From android-test with Apache License 2.0 6 votes vote down vote up
/** Test that a custom runner builder is loaded */
@Test
public void fromBundle_notRunnerBuilder() {
  Bundle b = new Bundle();
  String className = CustomTestFilter.class.getName();
  b.putString(RunnerArgs.ARGUMENT_RUNNER_BUILDER, className);
  try {
    new RunnerArgs.Builder().fromBundle(InstrumentationRegistry.getInstrumentation(), b).build();
    fail("Did not detect invalid runner builder");
  } catch (IllegalArgumentException e) {
    assertEquals(
        "Unexpected exception",
        className + " does not extend " + RunnerBuilder.class.getName(),
        e.getMessage());
  }
}
 
Example #7
Source File: FirebaseArrayOfObjectsTest.java    From FirebaseUI-Android with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
    FirebaseApp app = getAppInstance(InstrumentationRegistry.getContext());
    mRef = FirebaseDatabase.getInstance(app)
            .getReference()
            .child("firebasearray")
            .child("objects");
    mArray = new FirebaseArray<>(mRef, new ClassSnapshotParser<>(Bean.class));
    mRef.removeValue();
    mListener = runAndWaitUntil(mArray, new Runnable() {
        @Override
        public void run() {
            for (int i = 1; i <= INITIAL_SIZE; i++) {
                mRef.push().setValue(new Bean(i, "Text " + i, i % 2 == 0), i);
            }
        }
    }, new Callable<Boolean>() {
        @Override
        public Boolean call() {
            return mArray.size() == INITIAL_SIZE;
        }
    });
}
 
Example #8
Source File: OkHttp3MemorizationTests.java    From cwac-netsecurity with Apache License 2.0 6 votes vote down vote up
@Test
public void testSingleItemPublic() throws Exception {
  MemorizingTrustManager memo=new MemorizingTrustManager.Builder()
    .saveTo(memoDir, "sekrit".toCharArray())
    .noTOFU()
    .onlySingleItemChains()
    .build();

  final TrustManagerBuilder tmb=new TrustManagerBuilder()
    .withConfig(InstrumentationRegistry.getContext(),
      R.xml.comodo, true)
    .and()
    .add(memo);

  OkHttp3Integrator.applyTo(tmb, builder);
  OkHttpClient client=builder.build();

  Response response=client.newCall(buildPublicRequest()).execute();
  Assert.assertEquals(getExpectedResponse(), response.body().string());
}
 
Example #9
Source File: ExampleInstrumentedTest.java    From PlayerBase with Apache License 2.0 5 votes vote down vote up
@Test
public void useAppContext() throws Exception {
    // Context of the app under test.
    Context appContext = InstrumentationRegistry.getTargetContext();

    assertEquals("com.kk.taurus.avplayer", appContext.getPackageName());
}
 
Example #10
Source File: ExampleInstrumentedTest.java    From DMAudioStreamer with Apache License 2.0 5 votes vote down vote up
@Test
public void useAppContext() throws Exception {
    // Context of the app under test.
    Context appContext = InstrumentationRegistry.getTargetContext();

    assertEquals("dm.audiostreamerdemo", appContext.getPackageName());
}
 
Example #11
Source File: ExampleInstrumentedTest.java    From journaldev with MIT License 5 votes vote down vote up
@Test
public void useAppContext() {
    // Context of the app under test.
    Context appContext = InstrumentationRegistry.getTargetContext();

    assertEquals("com.journaldev.androidchipsandchipgroup", appContext.getPackageName());
}
 
Example #12
Source File: ExampleInstrumentedTest.java    From FilePicker with Apache License 2.0 5 votes vote down vote up
@Test
public void useAppContext() {
    // Context of the app under test.
    Context appContext = InstrumentationRegistry.getTargetContext();

    assertEquals("com.jaiselrahman.filepickersample", appContext.getPackageName());
}
 
Example #13
Source File: WindowAttachmentTest.java    From screenshot-tests-for-android with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
  mContext = InstrumentationRegistry.getTargetContext();
  KeyguardManager km = (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
  mLock = km.newKeyguardLock("SelectAtTagActivityTest");
  mLock.disableKeyguard();
}
 
Example #14
Source File: KaliumWalletTest.java    From natrium-android-wallet with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Before
@UiThreadTest
public void setUp() throws Exception {
    // build the activity component
    testActivityComponent = DaggerTestActivityComponent
            .builder()
            .applicationComponent(KaliumApplication.getApplication(InstrumentationRegistry.getTargetContext().getApplicationContext()).getApplicationComponent())
            .activityModule(new ActivityModule(InstrumentationRegistry.getTargetContext()))
            .build();

    testActivityComponent.inject(this);
}
 
Example #15
Source File: OrAndTest.java    From cwac-netsecurity with Apache License 2.0 5 votes vote down vote up
@Override
protected TrustManagerBuilder getBuilder() throws Exception {
  return(new TrustManagerBuilder()
    .withConfig(InstrumentationRegistry.getContext(),
      R.xml.okhttp3_selfsigned_mismatch, false)
    .or()
    .useDefault()
    .and()
    .denyAll());
}
 
Example #16
Source File: ExampleInstrumentedTest.java    From Study_Android_Demo with Apache License 2.0 5 votes vote down vote up
@Test
public void useAppContext() {
    // Context of the app under test.
    Context appContext = InstrumentationRegistry.getTargetContext();

    assertEquals("com.realmo.demo.multiscreen", appContext.getPackageName());
}
 
Example #17
Source File: FirebaseInAppMessagingFlowableTest.java    From firebase-android-sdk with Apache License 2.0 5 votes vote down vote up
@Test
public void afterCacheLoad_returnsValueFromMemory() throws IOException {
  analyticsConnector.invokeListenerOnEvent(ANALYTICS_EVENT_NAME);
  waitUntilNotified(subscriber); // this value will be cached
  clearProtoDiskCache(InstrumentationRegistry.getContext());

  analyticsConnector.invokeListenerOnEvent(ANALYTICS_EVENT_NAME);
  await().timeout(2, SECONDS).until(() -> subscriber.valueCount() > 1);

  List<Object> triggeredMessages = getPlainValues(subscriber);
  assertThat(triggeredMessages.size()).isEqualTo(2);
  for (Object o : triggeredMessages) {
    assertThat(o).isEqualTo(MODAL_MESSAGE_MODEL);
  }
}
 
Example #18
Source File: ExampleInstrumentedTest.java    From journaldev with MIT License 5 votes vote down vote up
@Test
public void useAppContext() {
    // Context of the app under test.
    Context appContext = InstrumentationRegistry.getTargetContext();

    assertEquals("com.journaldev.androidqbubbles", appContext.getPackageName());
}
 
Example #19
Source File: RunnerArgsTest.java    From android-test with Apache License 2.0 5 votes vote down vote up
/** Test parsing bundle when a negative test timeout is provided */
@Test(expected = NumberFormatException.class)
@SuppressWarnings("TestExceptionChecker")
public void testFromBundle_timeoutWithNegativeValue() {
  Bundle b = new Bundle();
  b.putString(RunnerArgs.ARGUMENT_TIMEOUT, "-500");
  new RunnerArgs.Builder().fromBundle(InstrumentationRegistry.getInstrumentation(), b);
}
 
Example #20
Source File: ExampleInstrumentedTest.java    From DateTimePicker with Apache License 2.0 5 votes vote down vote up
@Test
public void useAppContext() throws Exception {
    // Context of the app under test.
    Context appContext = InstrumentationRegistry.getTargetContext();

    assertEquals("com.takisoft.datetimepicker.test", appContext.getPackageName());
}
 
Example #21
Source File: LocaleRule.java    From BaldPhone with Apache License 2.0 5 votes vote down vote up
private void setLocale(Locale locale) {
    Resources resources = InstrumentationRegistry.getTargetContext().getResources();
    Locale.setDefault(locale);
    Configuration config = resources.getConfiguration();
    config.setLocale(locale);
    DisplayMetrics displayMetrics = resources.getDisplayMetrics();
    resources.updateConfiguration(config, displayMetrics);
}
 
Example #22
Source File: ExampleInstrumentedTest.java    From Android-9-Development-Cookbook with MIT License 5 votes vote down vote up
@Test
public void useAppContext() {
    // Context of the app under test.
    Context appContext = InstrumentationRegistry.getTargetContext();

    assertEquals("com.packtpub.projectionandcamera", appContext.getPackageName());
}
 
Example #23
Source File: ExampleInstrumentedTest.java    From bottomsheets with Apache License 2.0 5 votes vote down vote up
@Test
public void useAppContext() {
    // Context of the app under test.
    Context appContext = InstrumentationRegistry.getTargetContext();

    assertEquals("com.arthurivanets.bottomsheet.test", appContext.getPackageName());
}
 
Example #24
Source File: ExampleInstrumentedTest.java    From journaldev with MIT License 5 votes vote down vote up
@Test
public void useAppContext() {
    // Context of the app under test.
    Context appContext = InstrumentationRegistry.getTargetContext();

    assertEquals("com.journaldev.androidmotionlayout", appContext.getPackageName());
}
 
Example #25
Source File: TestDatabaseAdapterFactory.java    From dhis2-android-sdk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static DatabaseAdapter create() {
    Context context = InstrumentationRegistry.getTargetContext().getApplicationContext();
    Stetho.initializeWithDefaults(context);
    DatabaseAdapterFactory databaseAdapterFactory = DatabaseAdapterFactory.create(context,
            new InMemorySecureStore());
    DatabaseAdapter parentDatabaseAdapter = databaseAdapterFactory.newParentDatabaseAdapter();
    databaseAdapterFactory.createOrOpenDatabase(parentDatabaseAdapter, dbName,false);
    parentDatabaseAdapter.setForeignKeyConstraintsEnabled(false);
    return parentDatabaseAdapter;
}
 
Example #26
Source File: ExampleInstrumentedTest.java    From Android-BLE-Common-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void useAppContext() {
	// Context of the app under test.
	Context appContext = InstrumentationRegistry.getTargetContext();

	assertEquals("no.nordicsemi.android.ble.common.test", appContext.getPackageName());
}
 
Example #27
Source File: ExampleInstrumentedTest.java    From AndroidUSBCamera with Apache License 2.0 5 votes vote down vote up
@Test
public void useAppContext() throws Exception {
    // Context of the app under test.
    Context appContext = InstrumentationRegistry.getTargetContext();

    assertEquals("com.jiangdg.androidusbcamera", appContext.getPackageName());
}
 
Example #28
Source File: ExampleInstrumentedTest.java    From journaldev with MIT License 5 votes vote down vote up
@Test
public void useAppContext() {
    // Context of the app under test.
    Context appContext = InstrumentationRegistry.getTargetContext();

    assertEquals("com.journaldev.androidarcoredistancecamera", appContext.getPackageName());
}
 
Example #29
Source File: ShellExecutorTest.java    From android-test with Apache License 2.0 5 votes vote down vote up
@Before
public void initShellExec() {
  this.shellExecutor =
      new ShellExecutorImpl(
          InstrumentationRegistry.getContext(),
          InstrumentationRegistry.getArguments().getString(ShellExecSharedConstants.BINDER_KEY));
}
 
Example #30
Source File: RunnerArgsTest.java    From android-test with Apache License 2.0 5 votes vote down vote up
/**
 * Test {@link androidx.test.internal.runner.RunnerArgs.Builder#fromBundle(Bundle)} when
 * class name and method name is provided along with an additional class name.
 */
@Test
public void testFromBundle_notClassAndMethodCombo_different() {
  Bundle b = new Bundle();
  b.putString(RunnerArgs.ARGUMENT_NOT_TEST_CLASS, "ClassName1#method,ClassName2");
  RunnerArgs args =
      new RunnerArgs.Builder()
          .fromBundle(InstrumentationRegistry.getInstrumentation(), b)
          .build();
  assertEquals(2, args.notTests.size());
  assertEquals("ClassName1", args.notTests.get(0).testClassName);
  assertEquals("method", args.notTests.get(0).methodName);
  assertEquals("ClassName2", args.notTests.get(1).testClassName);
  assertNull(args.notTests.get(1).methodName);
}