當前位置: 首頁>>代碼示例>>Java>>正文


Java MockContext類代碼示例

本文整理匯總了Java中android.test.mock.MockContext的典型用法代碼示例。如果您正苦於以下問題:Java MockContext類的具體用法?Java MockContext怎麽用?Java MockContext使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


MockContext類屬於android.test.mock包,在下文中一共展示了MockContext類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: testNetworkChangeReceiver

import android.test.mock.MockContext; //導入依賴的package包/類
@Test
public void testNetworkChangeReceiver() throws Exception {
    latch = new CountDownLatch(1);

    NetworkConnectionListener listener = new NetworkConnectionListener() {
        @Override
        public void networkChanged(NetworkConnectionType newConnection) {
            latch.countDown();
        }
    };

    NetworkMonitor mockedMonitor = mock(NetworkMonitor.class);
    when(mockedMonitor.getCurrentConnectionType()).thenReturn(NetworkConnectionType.WIFI);

    Intent mockedIntent = mock(Intent.class);
    when(mockedIntent.getAction()).thenReturn(ConnectivityManager.CONNECTIVITY_ACTION);

    NetworkMonitor.NetworkChangeReceiver receiver = mockedMonitor.new NetworkChangeReceiver(listener);
    receiver.onReceive(new MockContext(), mockedIntent);

    assertTrue(latch.await(100, TimeUnit.MILLISECONDS));
}
 
開發者ID:ibm-bluemix-mobile-services,項目名稱:bms-clientsdk-android-core,代碼行數:23,代碼來源:NetworkMonitorTests.java

示例2: setup

import android.test.mock.MockContext; //導入依賴的package包/類
@Before
public void setup() {
    MockitoAnnotations.initMocks(this);
    WitchTestUtils.testInit(core);
    items.add(EXISTING_BINDER_ITEM_POSITION, itemWithBinder);
    items.add(NON_EXISTING_BINDER_ITEM_POSITION, itemWithoutBinder);

    // Layout inflater stuff
    when(itemView.getTag(anyInt())).thenReturn(tagContainer);
    emptyViewHolder = new EmptyViewHolder(itemView);

    when(inflater.inflate(anyInt(), any(ViewGroup.class), anyBoolean())).thenReturn(itemView);
    MockContext context = mock(MockContext.class);
    when(context.getSystemService(eq(Service.LAYOUT_INFLATER_SERVICE))).thenReturn(inflater);
    when(parent.getContext()).thenReturn(context);

    List<RecyclerViewBinderAdapter.Binder<?>> binders = new ArrayList<>();
    binders.add(itemBinder);
    adapter = new TestRecyclerViewBinderAdapter<>(items, binders);
}
 
開發者ID:sedstrom,項目名稱:Witch-Android,代碼行數:21,代碼來源:RecyclerViewBinderAdapterTest.java

示例3: testGetFileDirFailure

import android.test.mock.MockContext; //導入依賴的package包/類
@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");
}
 
開發者ID:nenick,項目名稱:espresso-macchiato,代碼行數:20,代碼來源:EspScreenshotToolTest.java

示例4: beginAuthorizationShouldCallUriParse

import android.test.mock.MockContext; //導入依賴的package包/類
@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));
}
 
開發者ID:uphold,項目名稱:uphold-sdk-android,代碼行數:19,代碼來源:UpholdClientTest.java

示例5: setupNetworkRequirement

import android.test.mock.MockContext; //導入依賴的package包/類
private Job setupNetworkRequirement(JobRequest.NetworkType requirement, boolean connected, int networkType, boolean roaming) {
    NetworkInfo networkInfo = mock(NetworkInfo.class);
    when(networkInfo.isConnected()).thenReturn(connected);
    when(networkInfo.isConnectedOrConnecting()).thenReturn(connected);
    when(networkInfo.getType()).thenReturn(networkType);
    when(networkInfo.isRoaming()).thenReturn(roaming);

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

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

    JobRequest request = mock(JobRequest.class);
    when(request.requiredNetworkType()).thenReturn(requirement);

    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;
}
 
開發者ID:evernote,項目名稱:android-job,代碼行數:26,代碼來源:JobRequirementTest.java

示例6: createMockContext

import android.test.mock.MockContext; //導入依賴的package包/類
/**
 * @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 enable
    // 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(RuntimeEnvironment.application);
    when(context.getPackageManager()).thenReturn(packageManager);
    when(context.getApplicationContext()).thenReturn(context);

    Context mockContext = mock(MockContext.class);
    when(mockContext.getApplicationContext()).thenReturn(context);
    return mockContext;
}
 
開發者ID:evernote,項目名稱:android-job,代碼行數:24,代碼來源:BaseJobManagerTest.java

示例7: testNetworkStateMeteredNotRoaming

import android.test.mock.MockContext; //導入依賴的package包/類
@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);
}
 
開發者ID:evernote,項目名稱:android-job,代碼行數:17,代碼來源:DeviceTest.java

示例8: testNetworkStateRoaming

import android.test.mock.MockContext; //導入依賴的package包/類
@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);
}
 
開發者ID:evernote,項目名稱:android-job,代碼行數:17,代碼來源:DeviceTest.java

示例9: testNetworkStateWifiAndMobile

import android.test.mock.MockContext; //導入依賴的package包/類
@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);
}
 
開發者ID:evernote,項目名稱:android-job,代碼行數:17,代碼來源:DeviceTest.java

示例10: testNetworkStateWifiAndRoaming

import android.test.mock.MockContext; //導入依賴的package包/類
@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);
}
 
開發者ID:evernote,項目名稱:android-job,代碼行數:17,代碼來源:DeviceTest.java

示例11: testStartMonitoringNetworkChanges

import android.test.mock.MockContext; //導入依賴的package包/類
@Test
public void testStartMonitoringNetworkChanges() throws Exception {
    latch = new CountDownLatch(1);

    class TestContext extends MockContext {
        @Override
        public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
            assertTrue(receiver instanceof NetworkMonitor.NetworkChangeReceiver);
            latch.countDown();
            return null;
        }
    }

    Context context = new TestContext();
    NetworkMonitor networkMonitor = new NetworkMonitor(context, getNetworkListener());
    networkMonitor.startMonitoringNetworkChanges();

    assertTrue(latch.await(100, TimeUnit.MILLISECONDS));
}
 
開發者ID:ibm-bluemix-mobile-services,項目名稱:bms-clientsdk-android-core,代碼行數:20,代碼來源:NetworkMonitorTests.java

示例12: testStopMonitoringNetworkChanges

import android.test.mock.MockContext; //導入依賴的package包/類
@Test
public void testStopMonitoringNetworkChanges() throws Exception {
    latch = new CountDownLatch(1);

    class TestContext extends MockContext {
        @Override
        public void unregisterReceiver(BroadcastReceiver receiver) {
            assertTrue(receiver instanceof NetworkMonitor.NetworkChangeReceiver);
            latch.countDown();
        }
    }

    Context context = new TestContext();
    NetworkMonitor networkMonitor = new NetworkMonitor(context, getNetworkListener());
    networkMonitor.startMonitoringNetworkChanges();
    networkMonitor.stopMonitoringNetworkChanges();

    assertTrue(latch.await(100, TimeUnit.MILLISECONDS));
}
 
開發者ID:ibm-bluemix-mobile-services,項目名稱:bms-clientsdk-android-core,代碼行數:20,代碼來源:NetworkMonitorTests.java

示例13: testStopMonitoringNetworkChangesWithNullListener

import android.test.mock.MockContext; //導入依賴的package包/類
@Test
public void testStopMonitoringNetworkChangesWithNullListener() {

    class TestContext extends MockContext {
        @Override
        public void unregisterReceiver(BroadcastReceiver receiver) {
            fail("Should not have a receiver without a NetworkChangeListener given to NetworkMonitor.");
        }
    }

    // Test with null Listener
    Context context = new TestContext();
    NetworkMonitor networkMonitor = new NetworkMonitor(context, null);
    networkMonitor.startMonitoringNetworkChanges();
    networkMonitor.stopMonitoringNetworkChanges();

    // Test misuse of the API
    context = new TestContext();
    networkMonitor = new NetworkMonitor(context, null);
    networkMonitor.stopMonitoringNetworkChanges();
    networkMonitor.stopMonitoringNetworkChanges();
    networkMonitor.stopMonitoringNetworkChanges();
}
 
開發者ID:ibm-bluemix-mobile-services,項目名稱:bms-clientsdk-android-core,代碼行數:24,代碼來源:NetworkMonitorTests.java

示例14: testSimpleOperationNotifiesChangeOnSuccess

import android.test.mock.MockContext; //導入依賴的package包/類
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();
}
 
開發者ID:cfmobile,項目名稱:arca-android,代碼行數:18,代碼來源:SimpleOperationTest.java

示例15: JsonDataToBirthdayConverterTest

import android.test.mock.MockContext; //導入依賴的package包/類
@Test
public void JsonDataToBirthdayConverterTest() throws Exception {
    String response = "{\"Data\":[{\"Birthday\":{\"Id\":0,\"Name\":\"Jonas Schubert\",\"Group\":\"Family\",\"RemindMe\":0,\"SentMail\":0,\"Date\":{\"Day\":2,\"Month\":1,\"Year\":1990}}},{\"Birthday\":{\"Id\":1,\"Name\":\"Artur Rychter\",\"Group\":\"Friends\",\"RemindMe\":1,\"SentMail\":0,\"Date\":{\"Day\":21,\"Month\":3,\"Year\":1990}}}]} ";
    MockContext mockContext = new MockContext();

    SerializableList<LucaBirthday> birthdayList = JsonDataToBirthdayConverter.getInstance().GetList(response, mockContext);

    assertNotNull(birthdayList);
    assertEquals(birthdayList.getSize(), 2);
    assertEquals(birthdayList.getValue(0).GetName(), "Jonas Schubert");
}
 
開發者ID:GuepardoApps,項目名稱:LucaHome-AndroidApplication,代碼行數:12,代碼來源:JsonConverterTest.java


注:本文中的android.test.mock.MockContext類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。