Java Code Examples for android.support.test.InstrumentationRegistry#getContext()

The following examples show how to use android.support.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: PermissionsTest.java    From espresso-samples with Apache License 2.0 6 votes vote down vote up
@Before
public void startMainActivityFromHomeScreen() {
    // Initialize UiDevice instance
    device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());

    // Start from the home screen
    device.pressHome();

    // Wait for launcher
    final String launcherPackage = getLauncherPackageName();
    MatcherAssert.assertThat(launcherPackage, IsNull.notNullValue());
    device.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)), LAUNCH_TIMEOUT);

    // Launch the app
    Context context = InstrumentationRegistry.getContext();
    final Intent intent = context.getPackageManager()
                                 .getLaunchIntentForPackage(APP_PACKAGE_NAME);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);    // Clear out any previous instances
    context.startActivity(intent);

    // Wait for the app to appear
    device.wait(Until.hasObject(By.pkg(APP_PACKAGE_NAME).depth(0)), LAUNCH_TIMEOUT);
}
 
Example 2
Source File: EspAppDataToolTest.java    From espresso-macchiato with MIT License 6 votes vote down vote up
@Test
@Ignore
// TODO need a way to throw error if any connection is open. better way would be to close them all
public void testClearDatabaseWithOpenConnection() {
    EspAppDataTool.clearDatabase();

    PersonDbHelper personDbHelper = new PersonDbHelper(InstrumentationRegistry.getContext());
    givenDatabaseEntry(personDbHelper);

    // simulate: forget to close database connection
    personDbHelper.getWritableDatabase();
    EspAppDataTool.clearDatabase();
    /*
    thenDatabaseHasAnEntry(personDbHelper);

    personDbHelper.close();
    EspAppDataTool.clearDatabase();
    */
    thenDatabaseIsEmpty(personDbHelper);
}
 
Example 3
Source File: MainEndToEndTest.java    From Building-Professional-Android-Applications with MIT License 6 votes vote down vote up
@Before
public void startMainActivityFromHomeScreen() {
    mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
    mDevice.pressHome();

    final String launcherPackage = mDevice.getLauncherPackageName();
    assertThat(launcherPackage, notNullValue());
    mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)),
            LAUNCH_TIMEOUT);

    Context context = InstrumentationRegistry.getContext();
    final Intent intent = context.getPackageManager().getLaunchIntentForPackage(TARGET_PACKAGE);

    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    context.startActivity(intent);

    mDevice.wait(Until.hasObject(By.pkg(TARGET_PACKAGE).depth(0)), LAUNCH_TIMEOUT);
}
 
Example 4
Source File: UiHelper.java    From AppCrawler with Apache License 2.0 6 votes vote down vote up
public static boolean launchApp(String targetPackage) {
    FileLog.i(TAG_MAIN, "{Launch} " + targetPackage);

    UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
    String launcherPackage = device.getLauncherPackageName();
    if (launcherPackage.compareToIgnoreCase(targetPackage) == 0) {
        launchHome();
        return true;
    }
    Context context = InstrumentationRegistry.getContext();
    final Intent intent = context.getPackageManager().getLaunchIntentForPackage(targetPackage);
    if (intent != null) {
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); // Make sure each launch is a new task
        context.startActivity(intent);
        device.wait(Until.hasObject(By.pkg(Config.sTargetPackage).depth(0)), Config.sLaunchTimeout);
    } else {
        String err = String.format("(%s) No launchable Activity.\n", targetPackage);
        Log.e(TAG, err);
        Bundle bundle = new Bundle();
        bundle.putString("ERROR", err);
        InstrumentationRegistry.getInstrumentation().finish(1, bundle);
    }
    return true;
}
 
Example 5
Source File: ExampleInstrumentedTest.java    From za-Farmer with MIT License 5 votes vote down vote up
@Before
public void setUp() throws RemoteException {
    mUIDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());  //获得device对象
    mContext = InstrumentationRegistry.getContext();
    KeyguardManager km = (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
    KeyguardManager.KeyguardLock kl = km.newKeyguardLock("farmer");
    //解锁
    kl.disableKeyguard();
    if (!mUIDevice.isScreenOn()) {
        mUIDevice.wakeUp();
    }

}
 
Example 6
Source File: TrustedWebActivityServiceConnectionManagerTest.java    From custom-tabs-client with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
    mContext = InstrumentationRegistry.getContext();
    mManager = new TrustedWebActivityServiceConnectionManager(mContext);

    TrustedWebActivityServiceConnectionManager
            .registerClient(mContext, ORIGIN, mContext.getPackageName());
}
 
Example 7
Source File: SdlRemoteDisplayTest.java    From sdl_java_suite with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void testConstructor(){
    VirtualDisplayEncoder encoder = createVDE();
    assertNotNull(encoder);
    MockRemoteDisplay remoteDisplay = new MockRemoteDisplay(InstrumentationRegistry.getContext(), encoder.getVirtualDisplay());
    assertNotNull(remoteDisplay);

    encoder.shutDown();
}
 
Example 8
Source File: ItIntroScreensActivity.java    From SmoothClicker with MIT License 5 votes vote down vote up
/**
 * Starts the main activity to test from the home screen
 */
@Before
public void startMainActivityFromHomeScreen() {

    l(this, "@Before startMainActivityFromHomeScreen");

    Context context = InstrumentationRegistry.getContext();

    // Initialize UiDevice instance
    mDevice = UiDevice.getInstance(getInstrumentation());

    // Start from the home screen
    mDevice.pressHome();

    // Wait for launcher
    final String launcherPackage = mDevice.getLauncherPackageName();
    assertThat(launcherPackage, notNullValue());
    mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)), LAUNCH_TIMEOUT_MS);

    // Launch the app
    final Intent intent = context.getPackageManager().getLaunchIntentForPackage(PACKAGE_APP_PATH);

    // Clear out any previous instances
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    context.startActivity(intent);

    // Wait for the app to appear
    mDevice.wait(Until.hasObject(By.pkg(PACKAGE_APP_PATH).depth(0)), LAUNCH_TIMEOUT_MS);

}
 
Example 9
Source File: ViewPagerAdapterTest.java    From android-mvvm with Apache License 2.0 5 votes vote down vote up
@Test
@UiThreadTest
public void instantiateItemAddsBindedView() throws Exception {
    ViewGroup container = new LinearLayout(InstrumentationRegistry.getContext());
    sut.instantiateItem(container, 0);
    View child = container.getChildAt(0);

    ViewDataBinding binding = DataBindingUtil.bind(child);

    assertEquals(1, container.getChildCount());
    assertEquals("com.manaschaudhari.android_mvvm.test.databinding.LayoutTestBinding", binding.getClass().getName());
    assertTrue(testBinder.lastBinding == binding);
    assertTrue(testBinder.lastViewModel == viewModelsSource.getValue().get(0));
}
 
Example 10
Source File: VoiceHatPeripheralInstrumentationTest.java    From contrib-drivers with Apache License 2.0 5 votes vote down vote up
/**
 * Verify that the manage input drivers permission is granted.
 */
@Test
public void testInputPermissionGranted() {
    String manageInputDrivers = "com.google.android.things.permission.MANAGE_INPUT_DRIVERS";
    Context context = InstrumentationRegistry.getContext();
    Assert.assertEquals(PackageManager.PERMISSION_GRANTED,
        context.checkSelfPermission(manageInputDrivers));
}
 
Example 11
Source File: WebAuthnCryptographyTest.java    From android-webauthn-authenticator with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    Context ctx = InstrumentationRegistry.getContext();
    this.credentialSafe = new CredentialSafe(ctx, false, false);
    this.crypto = new WebAuthnCryptography(this.credentialSafe);

}
 
Example 12
Source File: PlatformsTest.java    From api-android-java with MIT License 4 votes vote down vote up
public void setUp() throws InterruptedException {
    context = InstrumentationRegistry.getContext();
    String key = System.getProperty("API_KEY");
    wrapper = new IGDBWrapper(context, key, Version.PRO, true);

}
 
Example 13
Source File: IGDBWrapperTest.java    From api-android-java with MIT License 4 votes vote down vote up
public void setUp(){
    context = InstrumentationRegistry.getContext();
    String key = System.getProperty("API_KEY");
    wrapper = new IGDBWrapper(context, key, Version.PRO, true);
}
 
Example 14
Source File: GameEnginesTest.java    From api-android-java with MIT License 4 votes vote down vote up
public void setUp() throws InterruptedException {
    context = InstrumentationRegistry.getContext();
    String key = System.getProperty("API_KEY");
    wrapper = new IGDBWrapper(context, key, Version.PRO, true);

}
 
Example 15
Source File: CipherStorageApi23Test.java    From keystore-ultimate with Apache License 2.0 4 votes vote down vote up
@Override
CipherStorage init() {
    return new CipherStorageAndroidKeystore(InstrumentationRegistry.getContext(),
            new CipherPreferencesStorage(InstrumentationRegistry.getContext()));
}
 
Example 16
Source File: ExternalStorageTest.java    From OpenMapKitAndroid with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Before
public void init() {
    context = InstrumentationRegistry.getContext();
    launchIntent = ApplicationTest.getLaunchOMKIntent();
    createOdkMediaDirectory();
}
 
Example 17
Source File: PulseSourcesTest.java    From api-android-java with MIT License 4 votes vote down vote up
public void setUp() throws InterruptedException {
    context = InstrumentationRegistry.getContext();
    String key = System.getProperty("API_KEY");
    wrapper = new IGDBWrapper(context, key, Version.PRO, true);

}
 
Example 18
Source File: TitlesTest.java    From api-android-java with MIT License 4 votes vote down vote up
public void setUp() throws InterruptedException {
    context = InstrumentationRegistry.getContext();
    String key = System.getProperty("API_KEY");
    wrapper = new IGDBWrapper(context, key, Version.PRO, true);

}
 
Example 19
Source File: EspTextViewMatcherTest.java    From espresso-macchiato with MIT License 4 votes vote down vote up
@Test
public void testOnlyMatchTextView() {
    Matcher<View> colorMatcher = EspTextViewMatcher.withTextColor(0xff000000);
    View nonTextView = new View(InstrumentationRegistry.getContext());
    assertFalse(colorMatcher.matches(nonTextView));
}
 
Example 20
Source File: BaseSettingsProviderTest.java    From Study_Android_Demo with Apache License 2.0 4 votes vote down vote up
protected Context getContext() {
    return InstrumentationRegistry.getContext();
}