Search in sources :

Example 36 with ContextWrapper

use of android.content.ContextWrapper in project Bolts-Android by BoltsFramework.

the class AppLinkTest method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    openedIntents = new ArrayList<>();
    instrumentation = InstrumentationRegistry.getInstrumentation();
    activityInterceptor = new ContextWrapper(instrumentation.getTargetContext()) {

        @Override
        public void startActivity(Intent intent) {
            openedIntents.add(intent);
        }
    };
}
Also used : Intent(android.content.Intent) ContextWrapper(android.content.ContextWrapper)

Example 37 with ContextWrapper

use of android.content.ContextWrapper in project Resurrection_packages_apps_Settings by ResurrectionRemix.

the class ExternalSourcesDetailsTest method refreshUi_bothUnknownSourcesUserRestrictions_disableSwitchPreference.

@Test
public void refreshUi_bothUnknownSourcesUserRestrictions_disableSwitchPreference() {
    // Mocks set up
    final ExternalSourcesDetails fragment = new ExternalSourcesDetails();
    final ContextWrapper context = RuntimeEnvironment.application;
    final UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
    final ShadowUserManager shadowUserManager = Shadow.extract(userManager);
    ReflectionHelpers.setField(fragment, "mSwitchPref", mSwitchPref);
    ReflectionHelpers.setField(fragment, "mPackageInfo", mPackageInfo);
    mPackageInfo.applicationInfo = new ApplicationInfo();
    ReflectionHelpers.setField(fragment, "mUserManager", userManager);
    ReflectionHelpers.setField(mSwitchPref, "mHelper", mHelper);
    final AppStateInstallAppsBridge appBridge = mock(AppStateInstallAppsBridge.class);
    ReflectionHelpers.setField(fragment, "mAppBridge", appBridge);
    when(appBridge.createInstallAppsStateFor(nullable(String.class), anyInt())).thenReturn(mock(InstallAppsState.class));
    // Test restriction set up
    shadowUserManager.setUserRestriction(UserHandle.of(UserHandle.myUserId()), UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES_GLOBALLY, true);
    shadowUserManager.setUserRestriction(UserHandle.of(UserHandle.myUserId()), UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES, true);
    doAnswer((answer) -> {
        when(mSwitchPref.isDisabledByAdmin()).thenReturn(true);
        return null;
    }).when(mSwitchPref).checkRestrictionAndSetDisabled(UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES_GLOBALLY);
    // Code execution
    assertThat(fragment.refreshUi()).isTrue();
    // Assertions
    assertThat(userManager.hasUserRestriction(UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES_GLOBALLY, UserHandle.of(UserHandle.myUserId()))).isTrue();
    assertThat(userManager.hasUserRestriction(UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES, UserHandle.of(UserHandle.myUserId()))).isTrue();
    assertThat(mSwitchPref.isDisabledByAdmin()).isTrue();
}
Also used : ShadowUserManager(com.android.settings.testutils.shadow.ShadowUserManager) AppStateInstallAppsBridge(com.android.settings.applications.AppStateInstallAppsBridge) UserManager(android.os.UserManager) ShadowUserManager(com.android.settings.testutils.shadow.ShadowUserManager) ApplicationInfo(android.content.pm.ApplicationInfo) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ContextWrapper(android.content.ContextWrapper) InstallAppsState(com.android.settings.applications.AppStateInstallAppsBridge.InstallAppsState) Test(org.junit.Test)

Example 38 with ContextWrapper

use of android.content.ContextWrapper in project Resurrection_packages_apps_Settings by ResurrectionRemix.

the class ExternalSourcesDetailsTest method refreshUi_userRestrictionsUnknownSourcesGlobally_disablesSwitchPreference.

@Test
public void refreshUi_userRestrictionsUnknownSourcesGlobally_disablesSwitchPreference() {
    // Mocks set up
    final ExternalSourcesDetails fragment = new ExternalSourcesDetails();
    final ContextWrapper context = RuntimeEnvironment.application;
    final UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
    final ShadowUserManager shadowUserManager = Shadow.extract(userManager);
    ReflectionHelpers.setField(fragment, "mSwitchPref", mSwitchPref);
    ReflectionHelpers.setField(fragment, "mPackageInfo", mPackageInfo);
    mPackageInfo.applicationInfo = new ApplicationInfo();
    ReflectionHelpers.setField(fragment, "mUserManager", userManager);
    ReflectionHelpers.setField(mSwitchPref, "mHelper", mHelper);
    final AppStateInstallAppsBridge appBridge = mock(AppStateInstallAppsBridge.class);
    ReflectionHelpers.setField(fragment, "mAppBridge", appBridge);
    when(appBridge.createInstallAppsStateFor(nullable(String.class), anyInt())).thenReturn(mock(InstallAppsState.class));
    // Test restriction set up
    shadowUserManager.setUserRestriction(UserHandle.of(UserHandle.myUserId()), UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES_GLOBALLY, true);
    doAnswer((answer) -> {
        when(mSwitchPref.isDisabledByAdmin()).thenReturn(true);
        return null;
    }).when(mSwitchPref).checkRestrictionAndSetDisabled(UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES_GLOBALLY);
    // Code execution
    assertThat(fragment.refreshUi()).isTrue();
    // Assertions
    assertThat(userManager.hasUserRestriction(UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES_GLOBALLY, UserHandle.of(UserHandle.myUserId()))).isTrue();
    assertThat(mSwitchPref.isDisabledByAdmin()).isTrue();
}
Also used : ShadowUserManager(com.android.settings.testutils.shadow.ShadowUserManager) AppStateInstallAppsBridge(com.android.settings.applications.AppStateInstallAppsBridge) UserManager(android.os.UserManager) ShadowUserManager(com.android.settings.testutils.shadow.ShadowUserManager) ApplicationInfo(android.content.pm.ApplicationInfo) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ContextWrapper(android.content.ContextWrapper) InstallAppsState(com.android.settings.applications.AppStateInstallAppsBridge.InstallAppsState) Test(org.junit.Test)

Example 39 with ContextWrapper

use of android.content.ContextWrapper in project Resurrection_packages_apps_Settings by ResurrectionRemix.

the class ExternalSourcesDetailsTest method refreshUi_userRestrictionsUnknownSources_disablesSwitchPreference.

@Test
public void refreshUi_userRestrictionsUnknownSources_disablesSwitchPreference() {
    // Mocks set up
    final ExternalSourcesDetails fragment = new ExternalSourcesDetails();
    final ContextWrapper context = RuntimeEnvironment.application;
    final UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
    final ShadowUserManager shadowUserManager = Shadow.extract(userManager);
    ReflectionHelpers.setField(fragment, "mSwitchPref", mSwitchPref);
    ReflectionHelpers.setField(fragment, "mPackageInfo", mPackageInfo);
    mPackageInfo.applicationInfo = new ApplicationInfo();
    ReflectionHelpers.setField(fragment, "mUserManager", userManager);
    ReflectionHelpers.setField(mSwitchPref, "mHelper", mHelper);
    final AppStateInstallAppsBridge appBridge = mock(AppStateInstallAppsBridge.class);
    ReflectionHelpers.setField(fragment, "mAppBridge", appBridge);
    when(appBridge.createInstallAppsStateFor(nullable(String.class), anyInt())).thenReturn(mock(InstallAppsState.class));
    // Test restriction set up
    shadowUserManager.setUserRestriction(UserHandle.of(UserHandle.myUserId()), UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES, true);
    doAnswer((answer) -> {
        when(mSwitchPref.isDisabledByAdmin()).thenReturn(true);
        return null;
    }).when(mSwitchPref).checkRestrictionAndSetDisabled(UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES);
    // Code execution
    assertThat(fragment.refreshUi()).isTrue();
    // Assertions
    assertThat(userManager.hasUserRestriction(UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES, UserHandle.of(UserHandle.myUserId()))).isTrue();
    assertThat(mSwitchPref.isDisabledByAdmin()).isTrue();
}
Also used : ShadowUserManager(com.android.settings.testutils.shadow.ShadowUserManager) AppStateInstallAppsBridge(com.android.settings.applications.AppStateInstallAppsBridge) UserManager(android.os.UserManager) ShadowUserManager(com.android.settings.testutils.shadow.ShadowUserManager) ApplicationInfo(android.content.pm.ApplicationInfo) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ContextWrapper(android.content.ContextWrapper) InstallAppsState(com.android.settings.applications.AppStateInstallAppsBridge.InstallAppsState) Test(org.junit.Test)

Example 40 with ContextWrapper

use of android.content.ContextWrapper in project Presentation by StanKocken.

the class BasePresenterTest method testShouldReturnActivityFromContextWrapper.

@Test
public void testShouldReturnActivityFromContextWrapper() {
    ContextWrapper contextWrapper = Mockito.mock(ContextWrapper.class);
    Activity activity = Mockito.mock(Activity.class);
    TestDef.IView view = Mockito.mock(TestDef.IView.class);
    when(view.getContext()).thenReturn(contextWrapper);
    when(contextWrapper.getBaseContext()).thenReturn(activity);
    TestPresenter presenter = new TestPresenter();
    presenter.setView(view);
    assertEquals(contextWrapper, presenter.getContext());
    assertEquals(activity, presenter.getActivity());
}
Also used : Activity(android.app.Activity) TestDef(com.skocken.presentation.definition.TestDef) ContextWrapper(android.content.ContextWrapper) Test(org.junit.Test)

Aggregations

ContextWrapper (android.content.ContextWrapper)108 Context (android.content.Context)37 Activity (android.app.Activity)25 File (java.io.File)16 Test (org.junit.Test)13 Intent (android.content.Intent)11 MockContentResolver (android.test.mock.MockContentResolver)11 PackageManager (android.content.pm.PackageManager)10 Resources (android.content.res.Resources)9 View (android.view.View)9 IOException (java.io.IOException)8 Before (org.junit.Before)8 ContentResolver (android.content.ContentResolver)6 UserManager (android.os.UserManager)6 ThemePreferenceController (com.android.settings.display.ThemePreferenceController)6 OverlayManager (com.android.settings.display.ThemePreferenceController.OverlayManager)6 ArrayList (java.util.ArrayList)6 AppWidgetHostView (android.appwidget.AppWidgetHostView)5 LayoutInflater (android.view.LayoutInflater)5 JsonParser (com.google.gson.JsonParser)5