android.test.mock.MockContext Java Examples

The following examples show how to use android.test.mock.MockContext. 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: EspScreenshotToolTest.java    From espresso-macchiato with MIT License 6 votes vote down vote up
@Test
public void testGetFileDirFailure() {
    EspScreenshotTool espScreenshotTool = new EspScreenshotTool() {
        @Override
        protected Context getTargetContext() {
            return new MockContext() {
                @Override
                public File getFilesDir() {
                    // may happen when storage is not setup properly on emulator
                    return null;
                }
            };
        }
    };

    exception.expect(IllegalStateException.class);
    exception.expectMessage("could not find directory to store screenshot");
    espScreenshotTool.takeWithNameInternal("does not work");
}
 
Example #2
Source File: SimpleOperationTest.java    From arca-android with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void testSimpleOperationNotifiesChangeOnSuccess() {
    final AssertionLatch latch = new AssertionLatch(1);
    final MockContext context = new MockContext() {
        @Override
        public ContentResolver getContentResolver() {
            return new MockContentResolver() {
                @Override
                public void notifyChange(final Uri u, final ContentObserver o) {
                    latch.countDown();
                }
            };
        }
    };
    final TestSimpleOperation operation = new TestSimpleOperation(Uri.EMPTY);
    operation.onSuccess(context, null);
    latch.assertComplete();
}
 
Example #3
Source File: UpholdClientTest.java    From uphold-sdk-android with MIT License 6 votes vote down vote up
@Test
public void beginAuthorizationShouldCallUriParse() throws Exception {
    UpholdClient.initialize(new MockSharedPreferencesContext());

    UpholdClient upholdClient = new UpholdClient();
    ArrayList<String> scopes = new ArrayList<String>() {{
        add("foo");
    }};

    PowerMockito.mockStatic(TextUtils.class);
    PowerMockito.when(TextUtils.join(" ", scopes)).thenReturn("foo");

    PowerMockito.mockStatic(Uri.class);
    upholdClient.beginAuthorization(new MockContext(), "foo", scopes, "bar");

    PowerMockito.verifyStatic();
    Uri.parse(String.format("%s/authorize/foo?scope=foo&state=bar", BuildConfig.AUTHORIZATION_SERVER_URL));
}
 
Example #4
Source File: DeviceTest.java    From android-job with Apache License 2.0 6 votes vote down vote up
@Test
public void testNetworkStateWifiAndRoaming() {
    NetworkInfo networkInfo = mock(NetworkInfo.class);
    when(networkInfo.isConnected()).thenReturn(true);
    when(networkInfo.isConnectedOrConnecting()).thenReturn(true);
    when(networkInfo.getType()).thenReturn(ConnectivityManager.TYPE_WIFI);
    when(networkInfo.isRoaming()).thenReturn(true);

    ConnectivityManager connectivityManager = mock(ConnectivityManager.class);
    when(connectivityManager.getActiveNetworkInfo()).thenReturn(networkInfo);

    Context context = mock(MockContext.class);
    when(context.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(connectivityManager);

    assertThat(Device.getNetworkType(context)).isEqualTo(JobRequest.NetworkType.UNMETERED);
}
 
Example #5
Source File: BaseJobManagerTest.java    From android-job with Apache License 2.0 6 votes vote down vote up
/**
 * @return A mocked context which returns a spy of {@link RuntimeEnvironment#application} in
 * {@link Context#getApplicationContext()}.
 */
public static Context createMockContext() {
    // otherwise the JobScheduler isn't supported we check if the service is enabled
    // Robolectric doesn't parse services from the manifest, see https://github.com/robolectric/robolectric/issues/416
    PackageManager packageManager = mock(PackageManager.class);
    when(packageManager.queryBroadcastReceivers(any(Intent.class), anyInt())).thenReturn(Collections.singletonList(new ResolveInfo()));

    ResolveInfo resolveInfo = new ResolveInfo();
    resolveInfo.serviceInfo = new ServiceInfo();
    resolveInfo.serviceInfo.permission = "android.permission.BIND_JOB_SERVICE";
    when(packageManager.queryIntentServices(any(Intent.class), anyInt())).thenReturn(Collections.singletonList(resolveInfo));

    Context context = spy(ApplicationProvider.getApplicationContext());
    when(context.getPackageManager()).thenReturn(packageManager);
    when(context.getApplicationContext()).thenReturn(context);

    Context mockContext = mock(MockContext.class);
    when(mockContext.getApplicationContext()).thenReturn(context);
    return mockContext;
}
 
Example #6
Source File: DeviceTest.java    From android-job with Apache License 2.0 6 votes vote down vote up
@Test
public void testNetworkStateWifiAndMobile() {
    NetworkInfo networkInfo = mock(NetworkInfo.class);
    when(networkInfo.isConnected()).thenReturn(true);
    when(networkInfo.isConnectedOrConnecting()).thenReturn(true);
    when(networkInfo.getType()).thenReturn(ConnectivityManager.TYPE_WIFI);
    when(networkInfo.isRoaming()).thenReturn(false);

    ConnectivityManager connectivityManager = mock(ConnectivityManager.class);
    when(connectivityManager.getActiveNetworkInfo()).thenReturn(networkInfo);

    Context context = mock(MockContext.class);
    when(context.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(connectivityManager);

    assertThat(Device.getNetworkType(context)).isEqualTo(JobRequest.NetworkType.UNMETERED);
}
 
Example #7
Source File: DeviceTest.java    From android-job with Apache License 2.0 6 votes vote down vote up
@Test
public void testNetworkStateRoaming() {
    NetworkInfo networkInfo = mock(NetworkInfo.class);
    when(networkInfo.isConnected()).thenReturn(true);
    when(networkInfo.isConnectedOrConnecting()).thenReturn(true);
    when(networkInfo.getType()).thenReturn(ConnectivityManager.TYPE_MOBILE);
    when(networkInfo.isRoaming()).thenReturn(true);

    ConnectivityManager connectivityManager = mock(ConnectivityManager.class);
    when(connectivityManager.getActiveNetworkInfo()).thenReturn(networkInfo);

    Context context = mock(MockContext.class);
    when(context.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(connectivityManager);

    assertThat(Device.getNetworkType(context)).isEqualTo(JobRequest.NetworkType.CONNECTED);
}
 
Example #8
Source File: DeviceTest.java    From android-job with Apache License 2.0 6 votes vote down vote up
@Test
public void testNetworkStateMeteredNotRoaming() {
    NetworkInfo networkInfo = mock(NetworkInfo.class);
    when(networkInfo.isConnected()).thenReturn(true);
    when(networkInfo.isConnectedOrConnecting()).thenReturn(true);
    when(networkInfo.getType()).thenReturn(ConnectivityManager.TYPE_MOBILE);
    when(networkInfo.isRoaming()).thenReturn(false);

    ConnectivityManager connectivityManager = mock(ConnectivityManager.class);
    when(connectivityManager.getActiveNetworkInfo()).thenReturn(networkInfo);

    Context context = mock(MockContext.class);
    when(context.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(connectivityManager);

    assertThat(Device.getNetworkType(context)).isEqualTo(JobRequest.NetworkType.NOT_ROAMING);
}
 
Example #9
Source File: DeviceTest.java    From android-job with Apache License 2.0 5 votes vote down vote up
@Test
public void testNetworkStateNotConnected() {
    NetworkInfo networkInfo = mock(NetworkInfo.class);
    when(networkInfo.isConnected()).thenReturn(false);
    when(networkInfo.isConnectedOrConnecting()).thenReturn(false);

    ConnectivityManager connectivityManager = mock(ConnectivityManager.class);
    when(connectivityManager.getActiveNetworkInfo()).thenReturn(networkInfo);

    Context context = mock(MockContext.class);
    when(context.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(connectivityManager);

    assertThat(Device.getNetworkType(context)).isEqualTo(JobRequest.NetworkType.ANY);
}
 
Example #10
Source File: SimpleOperationTest.java    From arca-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void testSimpleOperationInsertsDataOnSuccess() throws Exception {
    final AssertionLatch latch = new AssertionLatch(1);
    final MockContext context = new MockContextWithProvider(new MockContentProvider() {
        @Override
        public int bulkInsert(Uri u, ContentValues[] v) {
            latch.countDown();
            assertEquals(URI, u);
            return 0;
        }
    });
    final TestSimpleOperation operation = new TestSimpleOperation(URI);
    operation.onPostExecute(context, new ContentValues[0]);
    latch.assertComplete();
}
 
Example #11
Source File: DeviceTest.java    From android-job with Apache License 2.0 5 votes vote down vote up
@Test
public void testPlatformBug() {
    ConnectivityManager connectivityManager = mock(ConnectivityManager.class);
    when(connectivityManager.getActiveNetworkInfo()).thenThrow(new NullPointerException());

    Context context = mock(MockContext.class);
    when(context.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(connectivityManager);

    assertThat(Device.getNetworkType(context)).isEqualTo(JobRequest.NetworkType.ANY);
}
 
Example #12
Source File: DeviceTest.java    From android-job with Apache License 2.0 5 votes vote down vote up
@Test
public void testNetworkStateVpn() {
    NetworkInfo networkInfo = mock(NetworkInfo.class);
    when(networkInfo.isConnected()).thenReturn(true);
    when(networkInfo.isConnectedOrConnecting()).thenReturn(true);
    when(networkInfo.getType()).thenReturn(ConnectivityManager.TYPE_VPN);

    ConnectivityManager connectivityManager = mock(ConnectivityManager.class);
    when(connectivityManager.getActiveNetworkInfo()).thenReturn(networkInfo);

    Context context = mock(MockContext.class);
    when(context.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(connectivityManager);

    assertThat(Device.getNetworkType(context)).isEqualTo(JobRequest.NetworkType.NOT_ROAMING);
}
 
Example #13
Source File: DeviceTest.java    From android-job with Apache License 2.0 5 votes vote down vote up
@Test
public void testNetworkStateUnmeteredWifi() {
    NetworkInfo networkInfo = mock(NetworkInfo.class);
    when(networkInfo.isConnected()).thenReturn(true);
    when(networkInfo.isConnectedOrConnecting()).thenReturn(true);
    when(networkInfo.getType()).thenReturn(ConnectivityManager.TYPE_WIFI);

    ConnectivityManager connectivityManager = mock(ConnectivityManager.class);
    when(connectivityManager.getActiveNetworkInfo()).thenReturn(networkInfo);

    Context context = mock(MockContext.class);
    when(context.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(connectivityManager);

    assertThat(Device.getNetworkType(context)).isEqualTo(JobRequest.NetworkType.UNMETERED);
}
 
Example #14
Source File: SwipeLayoutTest.java    From swipe-maker with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
    swipeLayout = new SwipeLayout(new MockContext());
    orientationStrategy = mock(OrientationStrategy.class);
    OrientationStrategyFactory orientationStrategyFactory = new OrientationStrategyFactory() {
        @Override
        public OrientationStrategy make(View view) {
            return orientationStrategy;
        }
    };
    swipeLayout.setOrientation(orientationStrategyFactory);
    verifyNoMoreInteractions(orientationStrategy);
}
 
Example #15
Source File: DeviceTest.java    From android-job with Apache License 2.0 5 votes vote down vote up
@Test
public void testNetworkStateNotConnectedWithNullNetworkInfo() {
    ConnectivityManager connectivityManager = mock(ConnectivityManager.class);

    Context context = mock(MockContext.class);
    when(context.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(connectivityManager);

    assertThat(Device.getNetworkType(context)).isEqualTo(JobRequest.NetworkType.ANY);
}
 
Example #16
Source File: JobRequirementTest.java    From android-job with Apache License 2.0 5 votes vote down vote up
private Job createMockedJob() {
    Context context = mock(MockContext.class);

    JobRequest request = mock(JobRequest.class);
    Job.Params params = mock(Job.Params.class);
    when(params.getRequest()).thenReturn(request);

    Job job = spy(new DummyJobs.SuccessJob());
    when(job.getParams()).thenReturn(params);
    doReturn(context).when(job).getContext();

    return job;
}
 
Example #17
Source File: JobManagerCreateTest.java    From android-job with Apache License 2.0 5 votes vote down vote up
private Context mockContext() {
    SharedPreferences preferences = mock(SharedPreferences.class);
    when(preferences.getStringSet(anyString(), ArgumentMatchers.<String>anySet())).thenReturn(new HashSet<String>());

    Context context = mock(MockContext.class);
    when(context.getApplicationContext()).thenReturn(context);
    when(context.getSharedPreferences(anyString(), anyInt())).thenReturn(preferences);
    return context;
}
 
Example #18
Source File: ApiTest.java    From fingerpoetry-android with Apache License 2.0 5 votes vote down vote up
@Test
public void login() throws IOException {
    Context context = new MockContext();
    Observable<ApiResult<LoginData>> observable= bookRetrofit.getAccountApi().login("18301441595", "123456");
    observable.observeOn(Schedulers.immediate())
            .subscribeOn(Schedulers.immediate())
            .subscribe(observer);
}
 
Example #19
Source File: BaseJobManagerTest.java    From android-job with Apache License 2.0 4 votes vote down vote up
protected final JobManager createManager() {
    Context mockContext = mock(MockContext.class);
    when(mockContext.getApplicationContext()).thenReturn(mContext);
    return JobManager.create(mockContext);
}
 
Example #20
Source File: AcceptSDKApiClientTest.java    From accept-sdk-android with MIT License 4 votes vote down vote up
@Before public void setUp() throws Exception {
  context = new MockContext();
  apiLoginID = API_LOGIN_ID;
  apiClient =
      new AcceptSDKApiClient.Builder(context, AcceptSDKApiClient.Environment.SANDBOX).build();
}
 
Example #21
Source File: MainActivityTest.java    From Inside_Android_Testing with Apache License 2.0 3 votes vote down vote up
public void testFakeMethod() {
	MockContext mockContext = new MockContext();

	int actualValue = mainActivity.fakeMethod(mockContext);

	assertEquals(1, actualValue);
}