当前位置: 首页>>代码示例>>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;未经允许,请勿转载。