Java Code Examples for androidx.test.InstrumentationRegistry#getContext()

The following examples show how to use androidx.test.InstrumentationRegistry#getContext() . 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: SqliteFunctionalTest.java    From requery with Apache License 2.0 6 votes vote down vote up
public SqliteFunctionalTest(Type type) {
    EntityModel model = Models.DEFAULT;
    Context context = InstrumentationRegistry.getContext();
    dbName = type.toString().toLowerCase() + ".db";
    switch (type) {
        default:
        case ANDROID:
            dataSource = new DatabaseSource(context, model, dbName, 1);
            break;
        case SUPPORT:
            dataSource = new SqlitexDatabaseSource(context, model, dbName, 1);
            break;
        case SQLCIPHER:
            dataSource = new SqlCipherDatabaseSource(context, model, dbName, "test123", 1);
            break;
    }
}
 
Example 2
Source File: EnableComponentsTestRule.java    From android-browser-helper with Apache License 2.0 5 votes vote down vote up
private static void setComponentEnabled(Class clazz, boolean enabled) {
    Context context = InstrumentationRegistry.getContext();
    PackageManager pm = context.getPackageManager();
    ComponentName name = new ComponentName(context, clazz);

    int newState = enabled
            ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
            : PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
    int flags = PackageManager.DONT_KILL_APP;

    if (pm.getComponentEnabledSetting(name) != newState) {
        pm.setComponentEnabledSetting(name, newState, flags);
    }
}
 
Example 3
Source File: BaseJdkHashCalculatorTest.java    From hash-checker with Apache License 2.0 5 votes vote down vote up
@Before
public void initializeResources() throws NoSuchAlgorithmException {
    context = InstrumentationRegistry.getContext();
    HashType hashType = getHashType();

    assertNotNull(hashType);

    jdkHashCalculator = new JdkHashCalculator();
    jdkHashCalculator.setHashType(hashType);
}
 
Example 4
Source File: SqliteFunctionalTest.java    From requery with Apache License 2.0 5 votes vote down vote up
@Override
public void setup() {
    dataSource.setLoggingEnabled(true);
    Context context = InstrumentationRegistry.getContext();
    context.deleteDatabase(dbName);
    data = new EntityDataStore<>(dataSource.getConfiguration());
}
 
Example 5
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 6
Source File: OnCheckedChangedTest.java    From butterknife with Apache License 2.0 5 votes vote down vote up
@UiThreadTest
@Test public void argumentCast() {
  class MyView extends ToggleButton implements ArgumentCast.MyInterface {
    MyView(Context context) {
      super(context);
    }
  }

  View view1 = new MyView(InstrumentationRegistry.getContext());
  view1.setId(1);
  View view2 = new MyView(InstrumentationRegistry.getContext());
  view2.setId(2);
  View view3 = new MyView(InstrumentationRegistry.getContext());
  view3.setId(3);
  ViewGroup tree = new FrameLayout(InstrumentationRegistry.getContext());
  tree.addView(view1);
  tree.addView(view2);
  tree.addView(view3);

  ArgumentCast target = new ArgumentCast();
  ButterKnife.bind(target, tree);

  view1.performClick();
  assertSame(view1, target.last);

  view2.performClick();
  assertSame(view2, target.last);

  view3.performClick();
  assertSame(view3, target.last);
}
 
Example 7
Source File: OnLongClickTest.java    From butterknife with Apache License 2.0 5 votes vote down vote up
@UiThreadTest
@Test public void argumentCast() {
  class MyView extends Button implements ArgumentCast.MyInterface {
    MyView(Context context) {
      super(context);
    }
  }

  View view1 = new MyView(InstrumentationRegistry.getContext());
  view1.setId(1);
  View view2 = new MyView(InstrumentationRegistry.getContext());
  view2.setId(2);
  View view3 = new MyView(InstrumentationRegistry.getContext());
  view3.setId(3);
  View view4 = new MyView(InstrumentationRegistry.getContext());
  view4.setId(4);
  ViewGroup tree = new FrameLayout(InstrumentationRegistry.getContext());
  tree.addView(view1);
  tree.addView(view2);
  tree.addView(view3);
  tree.addView(view4);

  ArgumentCast target = new ArgumentCast();
  ButterKnife.bind(target, tree);

  view1.performLongClick();
  assertSame(view1, target.last);

  view2.performLongClick();
  assertSame(view2, target.last);

  view3.performLongClick();
  assertSame(view3, target.last);

  view4.performLongClick();
  assertSame(view4, target.last);
}
 
Example 8
Source File: UtilsTest.java    From butterknife with Apache License 2.0 5 votes vote down vote up
@Test public void finderThrowsNiceError() {
  Context context = InstrumentationRegistry.getContext();
  View view = new View(context);
  try {
    Utils.findRequiredView(view, android.R.id.button1, "yo mama");
    fail();
  } catch (IllegalStateException e) {
    assertThat(e).hasMessage("Required view 'button1' with ID "
        + android.R.id.button1
        + " for yo mama was not found. If this view is optional add '@Nullable' (fields) or '@Optional' (methods) annotation.");
  }
}
 
Example 9
Source File: UtilsTest.java    From butterknife with Apache License 2.0 5 votes vote down vote up
@Test public void finderThrowsLessNiceErrorInEditMode() {
  Context context = InstrumentationRegistry.getContext();
  View view = new EditModeView(context);
  try {
    Utils.findRequiredView(view, android.R.id.button1, "yo mama");
    fail();
  } catch (IllegalStateException e) {
    assertThat(e).hasMessage("Required view '<unavailable while editing>' "
        + "with ID " + android.R.id.button1
        + " for yo mama was not found. If this view is optional add '@Nullable' (fields) or '@Optional' (methods) annotation.");
  }
}
 
Example 10
Source File: AndroidTestApplicationModule.java    From ground-android with Apache License 2.0 4 votes vote down vote up
@Provides
static Context contextProvider() {
  return InstrumentationRegistry.getContext();
}
 
Example 11
Source File: SketchUtilsTest.java    From sketch with Apache License 2.0 4 votes vote down vote up
@Test
public void testReadApkIcon() {
    Context context = InstrumentationRegistry.getContext();
    PackageInfo packageInfo;
    try {
        packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
        return;
    }
    String apkPath = packageInfo.applicationInfo.sourceDir;

    if (!new File(apkPath).exists()) {
        Assert.fail("Backup test app failed. " + apkPath);
    }

    Bitmap highQualityApkIconBitmap = SketchUtils.readApkIcon(context, apkPath, false, "testReadApkIcon", null);
    if (highQualityApkIconBitmap == null) {
        Assert.fail("Read high quality apk icon result is null");
    }
    if (highQualityApkIconBitmap.isRecycled()) {
        Assert.fail("High quality apk icon bitmap recycled");
    }

    Bitmap lowQualityApkIconBitmap = SketchUtils.readApkIcon(context, apkPath, true, "testReadApkIcon", null);
    if (lowQualityApkIconBitmap == null) {
        highQualityApkIconBitmap.recycle();
        Assert.fail("Read low quality apk icon result is null");
    }
    if (lowQualityApkIconBitmap.isRecycled()) {
        highQualityApkIconBitmap.recycle();
        Assert.fail("Low quality apk icon bitmap recycled");
    }

    int highQualityByteCount = SketchUtils.getByteCount(highQualityApkIconBitmap);
    int lowQualityByteCount = SketchUtils.getByteCount(lowQualityApkIconBitmap);

    highQualityApkIconBitmap.recycle();
    lowQualityApkIconBitmap.recycle();

    // KITKAT 以上不再支持 ARGB4444,因此 lowQualityImage 参数应该无效
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        if (highQualityByteCount != lowQualityByteCount) {
            Assert.fail("lowQualityImage attr invalid");
        }
    } else {
        if (highQualityByteCount <= lowQualityByteCount) {
            Assert.fail("lowQualityImage attr invalid");
        }
    }
}
 
Example 12
Source File: SketchUtilsTest.java    From sketch with Apache License 2.0 4 votes vote down vote up
@Test
public void testDrawableToBitmap() {
    Context context = InstrumentationRegistry.getContext();
    //noinspection deprecation
    Drawable drawable = context.getResources().getDrawable(R.drawable.shape_round_rect);

    Bitmap highQualityApkIconBitmap = SketchUtils.drawableToBitmap(drawable, false, null);
    if (highQualityApkIconBitmap == null) {
        Assert.fail("Read quality high apk icon result is null");
    }
    if (highQualityApkIconBitmap.isRecycled()) {
        Assert.fail("High quality apk icon bitmap recycled");
    }

    Bitmap lowQualityApkIconBitmap = SketchUtils.drawableToBitmap(drawable, true, null);
    if (lowQualityApkIconBitmap == null) {
        highQualityApkIconBitmap.recycle();
        Assert.fail("Read low quality apk icon result is null");
    }
    if (lowQualityApkIconBitmap.isRecycled()) {
        highQualityApkIconBitmap.recycle();
        Assert.fail("Low quality apk icon bitmap recycled");
    }

    int highQualityByteCount = SketchUtils.getByteCount(highQualityApkIconBitmap);
    int lowQualityByteCount = SketchUtils.getByteCount(lowQualityApkIconBitmap);

    highQualityApkIconBitmap.recycle();
    lowQualityApkIconBitmap.recycle();

    // KITKAT 以上不再支持 ARGB4444,因此 lowQualityImage 参数应该无效
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        if (highQualityByteCount != lowQualityByteCount) {
            Assert.fail("lowQualityImage attr invalid");
        }
    } else {
        if (highQualityByteCount <= lowQualityByteCount) {
            Assert.fail("lowQualityImage attr invalid");
        }
    }
}
 
Example 13
Source File: OnClickTest.java    From butterknife with Apache License 2.0 4 votes vote down vote up
@UiThreadTest
@Test public void argumentCast() {
  class MyView extends Button implements ArgumentCast.MyInterface {
    MyView(Context context) {
      super(context);
    }

    @Override public boolean post(Runnable action) {
      // Because of DebouncingOnClickListener, we run any posted Runnables synchronously.
      action.run();
      return true;
    }
  }

  View view1 = new MyView(InstrumentationRegistry.getContext());
  view1.setId(1);
  View view2 = new MyView(InstrumentationRegistry.getContext());
  view2.setId(2);
  View view3 = new MyView(InstrumentationRegistry.getContext());
  view3.setId(3);
  View view4 = new MyView(InstrumentationRegistry.getContext());
  view4.setId(4);
  ViewGroup tree = new FrameLayout(InstrumentationRegistry.getContext());
  tree.addView(view1);
  tree.addView(view2);
  tree.addView(view3);
  tree.addView(view4);

  ArgumentCast target = new ArgumentCast();
  ButterKnife.bind(target, tree);

  view1.performClick();
  assertSame(view1, target.last);

  view2.performClick();
  assertSame(view2, target.last);

  view3.performClick();
  assertSame(view3, target.last);

  view4.performClick();
  assertSame(view4, target.last);
}