133873d2b41a5cd2597b20a4e88eb8942944c0f23Tyler Schultzpackage com.xtremelabs.robolectric.shadows; 26fb1776c3dd0820074e31a750d991ce69a0597d2Christian Williams & Ryan Richard 36fb1776c3dd0820074e31a750d991ce69a0597d2Christian Williams & Ryan Richardimport android.app.Application; 4a915d5453773d2acb8cc72a70bf8460d6fd6cd26Lowell Kirsh & Ryan Richardimport android.app.Notification; 56fb1776c3dd0820074e31a750d991ce69a0597d2Christian Williams & Ryan Richardimport android.app.Service; 6a7e263ae4add03982ef3fe294d72ae43102ce52eChristian Williamsimport android.content.Context; 7f26a90f66bc715bac5510479ea659224d49cc39dRich Humphreyimport android.content.ServiceConnection; 8345fb6b77303f0d3c98cf748fefa0136183bdc79Christian Williamsimport com.xtremelabs.robolectric.Robolectric; 922c22c9aa4ca68c2deac6164edc1d82bc9645310Christian Williams & Phil Goodwinimport com.xtremelabs.robolectric.internal.Implementation; 1022c22c9aa4ca68c2deac6164edc1d82bc9645310Christian Williams & Phil Goodwinimport com.xtremelabs.robolectric.internal.Implements; 1122c22c9aa4ca68c2deac6164edc1d82bc9645310Christian Williams & Phil Goodwinimport com.xtremelabs.robolectric.internal.RealObject; 12a8486da7c7a0b6a10e943c55992f34d9c256456dChristian Williams 13a8486da7c7a0b6a10e943c55992f34d9c256456dChristian Williamsimport static com.xtremelabs.robolectric.Robolectric.shadowOf; 146fb1776c3dd0820074e31a750d991ce69a0597d2Christian Williams & Ryan Richard 156fb1776c3dd0820074e31a750d991ce69a0597d2Christian Williams & Ryan Richard@SuppressWarnings({"UnusedDeclaration"}) 166fb1776c3dd0820074e31a750d991ce69a0597d2Christian Williams & Ryan Richard@Implements(Service.class) 17afe0a89d904a7fe2f5980b9deb26cc3240192459Christian Williamspublic class ShadowService extends ShadowContextWrapper { 18cfbfc594c509dd0b8b70d5dfad9692947f3ef506Christian Williams @RealObject Service realService; 19f26a90f66bc715bac5510479ea659224d49cc39dRich Humphrey 205f52066979dd3a3eb6a0df2cd360611c50fb31ecRyan Richard private Notification lastForegroundNotification; 21d74864ce36171b4a214f47cecf52c5f71d01cc26Michael Portuesi private boolean selfStopped = false; 22f26a90f66bc715bac5510479ea659224d49cc39dRich Humphrey private boolean unbindServiceShouldThrowIllegalArgument = false; 23b83de11b8fdc0c77efa70780cec4d39af33252cbChristian Rackerseder private boolean foregroundStopped; 24b83de11b8fdc0c77efa70780cec4d39af33252cbChristian Rackerseder private boolean notificationShouldRemoved; 25cfbfc594c509dd0b8b70d5dfad9692947f3ef506Christian Williams 266fb1776c3dd0820074e31a750d991ce69a0597d2Christian Williams & Ryan Richard @Implementation 276fb1776c3dd0820074e31a750d991ce69a0597d2Christian Williams & Ryan Richard public final Application getApplication() { 28345fb6b77303f0d3c98cf748fefa0136183bdc79Christian Williams return Robolectric.application; 296fb1776c3dd0820074e31a750d991ce69a0597d2Christian Williams & Ryan Richard } 30a7e263ae4add03982ef3fe294d72ae43102ce52eChristian Williams 31cfbfc594c509dd0b8b70d5dfad9692947f3ef506Christian Williams @Implementation @Override 32a7e263ae4add03982ef3fe294d72ae43102ce52eChristian Williams public Context getApplicationContext() { 33345fb6b77303f0d3c98cf748fefa0136183bdc79Christian Williams return Robolectric.application; 34a7e263ae4add03982ef3fe294d72ae43102ce52eChristian Williams } 35cfbfc594c509dd0b8b70d5dfad9692947f3ef506Christian Williams 36cfbfc594c509dd0b8b70d5dfad9692947f3ef506Christian Williams @Implementation 37cfbfc594c509dd0b8b70d5dfad9692947f3ef506Christian Williams public void onDestroy() { 38cfbfc594c509dd0b8b70d5dfad9692947f3ef506Christian Williams assertNoBroadcastListenersRegistered(); 39cfbfc594c509dd0b8b70d5dfad9692947f3ef506Christian Williams } 40f26a90f66bc715bac5510479ea659224d49cc39dRich Humphrey 41f26a90f66bc715bac5510479ea659224d49cc39dRich Humphrey @Implementation 42f26a90f66bc715bac5510479ea659224d49cc39dRich Humphrey public void unbindService(ServiceConnection conn) { 43f26a90f66bc715bac5510479ea659224d49cc39dRich Humphrey if (unbindServiceShouldThrowIllegalArgument) { 44f26a90f66bc715bac5510479ea659224d49cc39dRich Humphrey throw new IllegalArgumentException(); 45f26a90f66bc715bac5510479ea659224d49cc39dRich Humphrey } 46f26a90f66bc715bac5510479ea659224d49cc39dRich Humphrey } 47f26a90f66bc715bac5510479ea659224d49cc39dRich Humphrey 48d74864ce36171b4a214f47cecf52c5f71d01cc26Michael Portuesi @Implementation 49d74864ce36171b4a214f47cecf52c5f71d01cc26Michael Portuesi public void stopSelf() { 50d74864ce36171b4a214f47cecf52c5f71d01cc26Michael Portuesi selfStopped = true; 51d74864ce36171b4a214f47cecf52c5f71d01cc26Michael Portuesi } 52d74864ce36171b4a214f47cecf52c5f71d01cc26Michael Portuesi 53f26a90f66bc715bac5510479ea659224d49cc39dRich Humphrey public void setUnbindServiceShouldThrowIllegalArgument(boolean flag) { 54f26a90f66bc715bac5510479ea659224d49cc39dRich Humphrey unbindServiceShouldThrowIllegalArgument = flag; 55f26a90f66bc715bac5510479ea659224d49cc39dRich Humphrey } 56cfbfc594c509dd0b8b70d5dfad9692947f3ef506Christian Williams 57a915d5453773d2acb8cc72a70bf8460d6fd6cd26Lowell Kirsh & Ryan Richard @Implementation 58a915d5453773d2acb8cc72a70bf8460d6fd6cd26Lowell Kirsh & Ryan Richard public final void startForeground(int id, Notification notification) { 59a915d5453773d2acb8cc72a70bf8460d6fd6cd26Lowell Kirsh & Ryan Richard lastForegroundNotification = notification; 60a915d5453773d2acb8cc72a70bf8460d6fd6cd26Lowell Kirsh & Ryan Richard } 61b83de11b8fdc0c77efa70780cec4d39af33252cbChristian Rackerseder 62b83de11b8fdc0c77efa70780cec4d39af33252cbChristian Rackerseder @Implementation 63b83de11b8fdc0c77efa70780cec4d39af33252cbChristian Rackerseder public void stopForeground(boolean removeNotification) { 64b83de11b8fdc0c77efa70780cec4d39af33252cbChristian Rackerseder foregroundStopped = true; 65b83de11b8fdc0c77efa70780cec4d39af33252cbChristian Rackerseder notificationShouldRemoved = removeNotification; 66b83de11b8fdc0c77efa70780cec4d39af33252cbChristian Rackerseder } 67a915d5453773d2acb8cc72a70bf8460d6fd6cd26Lowell Kirsh & Ryan Richard 68a915d5453773d2acb8cc72a70bf8460d6fd6cd26Lowell Kirsh & Ryan Richard public Notification getLastForegroundNotification() { 69a915d5453773d2acb8cc72a70bf8460d6fd6cd26Lowell Kirsh & Ryan Richard return lastForegroundNotification; 70a915d5453773d2acb8cc72a70bf8460d6fd6cd26Lowell Kirsh & Ryan Richard } 71a915d5453773d2acb8cc72a70bf8460d6fd6cd26Lowell Kirsh & Ryan Richard 7259d9df0f147b94712b36ad44939ebd2afa4cf1b7Phil Goodwin /** 7359d9df0f147b94712b36ad44939ebd2afa4cf1b7Phil Goodwin * Utility method that throws a {@code RuntimeException} if any {@code BroadcastListener}s are still registered. 7459d9df0f147b94712b36ad44939ebd2afa4cf1b7Phil Goodwin */ 75cfbfc594c509dd0b8b70d5dfad9692947f3ef506Christian Williams public void assertNoBroadcastListenersRegistered() { 76a8486da7c7a0b6a10e943c55992f34d9c256456dChristian Williams ((ShadowApplication) shadowOf(getApplicationContext())).assertNoBroadcastListenersRegistered(realService, "Service"); 77cfbfc594c509dd0b8b70d5dfad9692947f3ef506Christian Williams } 78d74864ce36171b4a214f47cecf52c5f71d01cc26Michael Portuesi 79d74864ce36171b4a214f47cecf52c5f71d01cc26Michael Portuesi /** 80d74864ce36171b4a214f47cecf52c5f71d01cc26Michael Portuesi * Non-Android accessor, to use in assertions. 81d74864ce36171b4a214f47cecf52c5f71d01cc26Michael Portuesi * @return 82d74864ce36171b4a214f47cecf52c5f71d01cc26Michael Portuesi */ 83d74864ce36171b4a214f47cecf52c5f71d01cc26Michael Portuesi public boolean isStoppedBySelf() { 84d74864ce36171b4a214f47cecf52c5f71d01cc26Michael Portuesi return selfStopped; 85d74864ce36171b4a214f47cecf52c5f71d01cc26Michael Portuesi } 86b83de11b8fdc0c77efa70780cec4d39af33252cbChristian Rackerseder 87b83de11b8fdc0c77efa70780cec4d39af33252cbChristian Rackerseder public boolean isForegroundStopped() { 88b83de11b8fdc0c77efa70780cec4d39af33252cbChristian Rackerseder return foregroundStopped; 89b83de11b8fdc0c77efa70780cec4d39af33252cbChristian Rackerseder } 90b83de11b8fdc0c77efa70780cec4d39af33252cbChristian Rackerseder 91b83de11b8fdc0c77efa70780cec4d39af33252cbChristian Rackerseder public boolean getNotificationShouldRemoved() { 92b83de11b8fdc0c77efa70780cec4d39af33252cbChristian Rackerseder return notificationShouldRemoved; 93b83de11b8fdc0c77efa70780cec4d39af33252cbChristian Rackerseder } 94e5bc36c75eaf30aa796d03c1f8697a1eb2393f87Dave}