Here are the examples of the java api class android.content.ContextWrapper taken from open source projects.
1. MusicPlayer#bindToService()
Project: Muzesto
File: MusicPlayer.java
File: MusicPlayer.java
public static final ServiceToken bindToService(final Context context, final ServiceConnection callback) { Activity realActivity = ((Activity) context).getParent(); if (realActivity == null) { realActivity = (Activity) context; } final ContextWrapper contextWrapper = new ContextWrapper(realActivity); contextWrapper.startService(new Intent(contextWrapper, MusicService.class)); final ServiceBinder binder = new ServiceBinder(callback, contextWrapper.getApplicationContext()); if (contextWrapper.bindService(new Intent().setClass(contextWrapper, MusicService.class), binder, 0)) { mConnectionMap.put(contextWrapper, binder); return new ServiceToken(contextWrapper); } return null; }
2. MusicPlayer#unbindFromService()
Project: Muzesto
File: MusicPlayer.java
File: MusicPlayer.java
public static void unbindFromService(final ServiceToken token) { if (token == null) { return; } final ContextWrapper mContextWrapper = token.mWrappedContext; final ServiceBinder mBinder = mConnectionMap.remove(mContextWrapper); if (mBinder == null) { return; } mContextWrapper.unbindService(mBinder); if (mConnectionMap.isEmpty()) { mService = null; } }
3. AlertDialogTest#shouldFindViewsByIdIfAViewIsSet()
Project: robolectric
File: AlertDialogTest.java
File: AlertDialogTest.java
@Test public void shouldFindViewsByIdIfAViewIsSet() throws Exception { ContextWrapper context = new ContextWrapper(null); AlertDialog dialog = new AlertDialog.Builder(context).create(); assertThat(dialog.findViewById(99), nullValue()); View view = new View(context); view.setId(99); dialog.setView(view); assertThat(dialog.findViewById(99), sameInstance(view)); assertThat(dialog.findViewById(66), nullValue()); }
4. MerlinServiceShould#useCustomNetworkStatusRetrieverWhenPingingCustomEndpoint()
Project: merlin
File: MerlinServiceShould.java
File: MerlinServiceShould.java
@Test public void useCustomNetworkStatusRetrieverWhenPingingCustomEndpoint() { ContextWrapper contextWrapper = stubbedContextWrapper(); NetworkStatusRetriever mockRetriever = mock(NetworkStatusRetriever.class); HostPinger mockDefaultPinger = mock(HostPinger.class); HostPinger mockCustomPinger = mock(HostPinger.class); MerlinService merlinService = buildStubbedMerlinService(contextWrapper, mockRetriever, mockDefaultPinger, mockCustomPinger); merlinService.onCreate(); merlinService.setHostname("some new host name", mock(ResponseCodeValidator.class)); merlinService.onConnectivityChanged(networkAvailableEvent()); verify(mockRetriever).fetchWithPing(mockCustomPinger); }
5. MerlinServiceShould#useDefaultNetworkStatusRetrieverWhenPingingDefaultEndpoint()
Project: merlin
File: MerlinServiceShould.java
File: MerlinServiceShould.java
@Test public void useDefaultNetworkStatusRetrieverWhenPingingDefaultEndpoint() { ContextWrapper contextWrapper = stubbedContextWrapper(); NetworkStatusRetriever mockRetriever = mock(NetworkStatusRetriever.class); HostPinger mockDefaultPinger = mock(HostPinger.class); HostPinger mockCustomPinger = mock(HostPinger.class); MerlinService merlinService = buildStubbedMerlinService(contextWrapper, mockRetriever, mockDefaultPinger, mockCustomPinger); merlinService.onCreate(); merlinService.onConnectivityChanged(networkAvailableEvent()); verify(mockRetriever).fetchWithPing(mockDefaultPinger); }
6. NucleusLayoutTest#getActivityFromWrappedContext()
Project: nucleus
File: NucleusLayoutTest.java
File: NucleusLayoutTest.java
@Test public void getActivityFromWrappedContext() throws Exception { Activity activity = mock(Activity.class); ContextWrapper wrapper = mock(ContextWrapper.class); when(wrapper.getBaseContext()).thenReturn(activity); ContextWrapper wrapper2 = mock(ContextWrapper.class); when(wrapper2.getBaseContext()).thenReturn(wrapper); stub(method(BASE_VIEW_CLASS, "getContext")).toReturn(wrapper2); assertEquals(activity, tested.getActivity()); }
7. ContextWrapperTest#setUp()
Project: robolectric
File: ContextWrapperTest.java
File: ContextWrapperTest.java
@Before public void setUp() throws Exception { transcript = new Transcript(); contextWrapper = new ContextWrapper(new Activity()); }
8. AlertDialogTest#shouldSetCustomTitleView()
Project: robolectric
File: AlertDialogTest.java
File: AlertDialogTest.java
@Test public void shouldSetCustomTitleView() throws Exception { ContextWrapper context = new ContextWrapper(null); AlertDialog.Builder builder = new AlertDialog.Builder(context); View view = new View(context); assertThat(builder.setCustomTitle(view), sameInstance(builder)); AlertDialog alert = builder.create(); assertThat(shadowOf(alert).getCustomTitleView(), equalTo((View) view)); }
9. AlertDialogTest#shouldSetView()
Project: robolectric
File: AlertDialogTest.java
File: AlertDialogTest.java
@Test public void shouldSetView() throws Exception { ContextWrapper context = new ContextWrapper(null); AlertDialog.Builder builder = new AlertDialog.Builder(context); EditText view = new EditText(context); builder.setView(view); AlertDialog alert = builder.create(); assertThat(shadowOf(alert).getView(), equalTo((View) view)); }
10. MerlinServiceShould#stubbedContextWrapper()
Project: merlin
File: MerlinServiceShould.java
File: MerlinServiceShould.java
@NonNull private ContextWrapper stubbedContextWrapper() { Context context = mock(Context.class); ContextWrapper contextWrapper = mock(ContextWrapper.class); ConnectivityManager connectivityManager = mock(ConnectivityManager.class); when(contextWrapper.getApplicationContext()).thenReturn(context); when(context.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(connectivityManager); return contextWrapper; }
11. AppLinkTest#setUp()
Project: Bolts-Android
File: AppLinkTest.java
File: AppLinkTest.java
@Override protected void setUp() throws Exception { super.setUp(); openedIntents = new ArrayList<>(); activityInterceptor = new ContextWrapper(getInstrumentation().getTargetContext()) { @Override public void startActivity(Intent intent) { openedIntents.add(intent); } }; }
12. ACDDHacks#allClasses()
Project: ACDD
File: ACDDHacks.java
File: ACDDHacks.java
/** * hack all defined classes ***/ private static void allClasses() throws HackAssertionException { if (VERSION.SDK_INT <= 8) { LoadedApk = Hack.into("android.app.ActivityThread$PackageInfo"); } else { LoadedApk = Hack.into("android.app.LoadedApk"); } ActivityThread = Hack.into("android.app.ActivityThread"); Resources = Hack.into(Resources.class); Application = Hack.into(Application.class); AssetManager = Hack.into(AssetManager.class); IPackageManager = Hack.into("android.content.pm.IPackageManager"); Service = Hack.into(Service.class); ContextImpl = Hack.into("android.app.ContextImpl"); ContextThemeWrapper = Hack.into(ContextThemeWrapper.class); ContextWrapper = Hack.into("android.content.ContextWrapper"); sIsIgnoreFailure = true; ClassLoader = Hack.into(ClassLoader.class); DexClassLoader = Hack.into(DexClassLoader.class); LexFile = Hack.into("dalvik.system.LexFile"); PackageParser$Component = Hack.into("android.content.pm.PackageParser$Component"); PackageParser$Activity = Hack.into("android.content.pm.PackageParser$Activity"); PackageParser$Provider = Hack.into("android.content.pm.PackageParser$Provider"); PackageParser = Hack.into("android.content.pm.PackageParser"); PackageParser$Package = Hack.into("android.content.pm.PackageParser$Package"); PackageParser$ActivityIntentInfo = Hack.into("android.content.pm.PackageParser$ActivityIntentInfo"); sIsIgnoreFailure = false; }