@Test @Feature({"GCore"}) public void willUseConnectionBackgroundTest() { int disconnectionTimeout = 5000; int arbitraryNumberOfSeconds = 42; GoogleApiClientHelper helper = new GoogleApiClientHelper(mMockClient); ShadowLooper.runUiThreadTasksIncludingDelayedTasks(); Activity mockActivity = mock(Activity.class); ApplicationStatus.onStateChangeForTesting(mockActivity, ActivityState.CREATED); helper.setDisconnectionDelay(disconnectionTimeout); // We have a connected client when(mMockClient.isConnected()).thenReturn(true); // We go in the background and extend the delay ApplicationStatus.onStateChangeForTesting(mockActivity, ActivityState.STOPPED); ShadowLooper.idleMainLooper(disconnectionTimeout - arbitraryNumberOfSeconds); helper.willUseConnection(); // The client should not have been disconnected. ShadowLooper.idleMainLooper(disconnectionTimeout - arbitraryNumberOfSeconds); verify(mMockClient, never()).disconnect(); // After the full timeout it should still disconnect though ShadowLooper.idleMainLooper(arbitraryNumberOfSeconds); verify(mMockClient).disconnect(); // The client is now disconnected then when(mMockClient.isConnected()).thenReturn(false); // The call should reconnect a disconnected client helper.willUseConnection(); verify(mMockClient).connect(); }
// Should pass @Test public void testButtonOneClick_shouldShowToast() { MainActivity activity = Robolectric.setupActivity(MainActivity.class); activity.findViewById(R.id.button_1).performClick(); ShadowLooper.idleMainLooper(); // Check toast correctness Assert.assertTrue(ShadowToast.getTextOfLatestToast().equals("Button #1 Clicked")); // Check textView text TextView textView = (TextView) activity.findViewById(R.id.test_text); Assert.assertTrue("Button 1 Clicked".equals(textView.getText())); }
@Test @Feature({"GCore"}) public void disconnectionCancellingTest() { int disconnectionTimeout = 5000; GoogleApiClientHelper helper = new GoogleApiClientHelper(mMockClient); ShadowLooper.runUiThreadTasksIncludingDelayedTasks(); Activity mockActivity = mock(Activity.class); ApplicationStatus.onStateChangeForTesting(mockActivity, ActivityState.CREATED); helper.setDisconnectionDelay(disconnectionTimeout); // We have a connected client when(mMockClient.isConnected()).thenReturn(true); // We go in the background and come back before the end of the timeout. ApplicationStatus.onStateChangeForTesting(mockActivity, ActivityState.STOPPED); ShadowLooper.idleMainLooper(disconnectionTimeout - 42); ApplicationStatus.onStateChangeForTesting(mockActivity, ActivityState.STARTED); ShadowLooper.runUiThreadTasksIncludingDelayedTasks(); // The client should not have been disconnected, which would drop requests otherwise. verify(mMockClient, never()).disconnect(); }