Index: chrome/android/junit/src/org/chromium/chrome/browser/gcore/GoogleApiClientHelperTest.java |
diff --git a/chrome/android/junit/src/org/chromium/chrome/browser/gcore/GoogleApiClientHelperTest.java b/chrome/android/junit/src/org/chromium/chrome/browser/gcore/GoogleApiClientHelperTest.java |
index cc8e786735c47d198937b35400faa160e5606c8f..270fc670fbcec9f48f2a287e2bf124000ebf8205 100644 |
--- a/chrome/android/junit/src/org/chromium/chrome/browser/gcore/GoogleApiClientHelperTest.java |
+++ b/chrome/android/junit/src/org/chromium/chrome/browser/gcore/GoogleApiClientHelperTest.java |
@@ -23,8 +23,8 @@ import org.junit.After; |
import org.junit.Before; |
import org.junit.Test; |
import org.junit.runner.RunWith; |
-import org.robolectric.Robolectric; |
import org.robolectric.annotation.Config; |
+import org.robolectric.shadows.ShadowLooper; |
/** |
* Tests for {@link GoogleApiClientHelper} |
@@ -52,16 +52,14 @@ public class GoogleApiClientHelperTest { |
@Feature({"GCore"}) |
public void connectionAttemptDelayTest() { |
GoogleApiClientHelper helper = new GoogleApiClientHelper(mMockClient); |
- ConnectionResult mockResult = mock(ConnectionResult.class); |
- when(mockResult.getErrorCode()).thenReturn(ConnectionResult.SERVICE_UPDATING); |
- Robolectric.pauseMainLooper(); |
- helper.onConnectionFailed(mockResult); |
- verify(mMockClient, never()).connect(); |
- Robolectric.unPauseMainLooper(); |
+ ShadowLooper.pauseMainLooper(); |
+ helper.onConnectionFailed(new ConnectionResult(ConnectionResult.SERVICE_UPDATING)); |
+ verify(mMockClient, times(0)).connect(); |
+ ShadowLooper.unPauseMainLooper(); |
- Robolectric.runUiThreadTasksIncludingDelayedTasks(); |
- verify(mMockClient).connect(); |
+ ShadowLooper.runUiThreadTasksIncludingDelayedTasks(); |
+ verify(mMockClient, times(1)).connect(); |
} |
/** Tests that the connection handler gives up after a number of connection attempts. */ |
@@ -70,28 +68,24 @@ public class GoogleApiClientHelperTest { |
public void connectionFailureTest() { |
GoogleApiClientHelper helper = new GoogleApiClientHelper(mMockClient); |
- ConnectionResult mockResult = mock(ConnectionResult.class); |
- when(mockResult.getErrorCode()).thenReturn(ConnectionResult.DEVELOPER_ERROR); |
- |
- helper.onConnectionFailed(mockResult); |
- Robolectric.runUiThreadTasksIncludingDelayedTasks(); |
+ helper.onConnectionFailed(new ConnectionResult(ConnectionResult.DEVELOPER_ERROR)); |
+ ShadowLooper.runUiThreadTasksIncludingDelayedTasks(); |
// Should not retry on unrecoverable errors |
verify(mMockClient, never()).connect(); |
// Connection attempts |
- when(mockResult.getErrorCode()).thenReturn(ConnectionResult.SERVICE_UPDATING); |
for (int i = 0; i < ConnectedTask.RETRY_NUMBER_LIMIT; i++) { |
- helper.onConnectionFailed(mockResult); |
- Robolectric.runUiThreadTasksIncludingDelayedTasks(); |
+ helper.onConnectionFailed(new ConnectionResult(ConnectionResult.SERVICE_UPDATING)); |
+ ShadowLooper.runUiThreadTasksIncludingDelayedTasks(); |
} |
// Should have tried to connect every time. |
verify(mMockClient, times(ConnectedTask.RETRY_NUMBER_LIMIT)).connect(); |
// Try again |
- helper.onConnectionFailed(mockResult); |
- Robolectric.runUiThreadTasksIncludingDelayedTasks(); |
+ helper.onConnectionFailed(new ConnectionResult(ConnectionResult.SERVICE_UPDATING)); |
+ ShadowLooper.runUiThreadTasksIncludingDelayedTasks(); |
// The connection handler should have given up, no new call. |
verify(mMockClient, times(ConnectedTask.RETRY_NUMBER_LIMIT)).connect(); |
@@ -102,13 +96,11 @@ public class GoogleApiClientHelperTest { |
@Feature({"GCore"}) |
public void connectionAttemptsResetTest() { |
GoogleApiClientHelper helper = new GoogleApiClientHelper(mMockClient); |
- ConnectionResult mockResult = mock(ConnectionResult.class); |
- when(mockResult.getErrorCode()).thenReturn(ConnectionResult.SERVICE_UPDATING); |
// Connection attempts |
for (int i = 0; i < ConnectedTask.RETRY_NUMBER_LIMIT - 1; i++) { |
- helper.onConnectionFailed(mockResult); |
- Robolectric.runUiThreadTasksIncludingDelayedTasks(); |
+ helper.onConnectionFailed(new ConnectionResult(ConnectionResult.SERVICE_UPDATING)); |
+ ShadowLooper.runUiThreadTasksIncludingDelayedTasks(); |
} |
// Should have tried to connect every time. |
@@ -116,19 +108,19 @@ public class GoogleApiClientHelperTest { |
// Connection successful now |
helper.onConnected(null); |
- Robolectric.runUiThreadTasksIncludingDelayedTasks(); |
+ ShadowLooper.runUiThreadTasksIncludingDelayedTasks(); |
for (int i = 0; i < ConnectedTask.RETRY_NUMBER_LIMIT; i++) { |
- helper.onConnectionFailed(mockResult); |
- Robolectric.runUiThreadTasksIncludingDelayedTasks(); |
+ helper.onConnectionFailed(new ConnectionResult(ConnectionResult.SERVICE_UPDATING)); |
+ ShadowLooper.runUiThreadTasksIncludingDelayedTasks(); |
} |
// A success should allow for more connection attempts. |
verify(mMockClient, times(ConnectedTask.RETRY_NUMBER_LIMIT * 2 - 1)).connect(); |
// This should not result in a connection attempt, the limit is still there. |
- helper.onConnectionFailed(mockResult); |
- Robolectric.runUiThreadTasksIncludingDelayedTasks(); |
+ helper.onConnectionFailed(new ConnectionResult(ConnectionResult.SERVICE_UPDATING)); |
+ ShadowLooper.runUiThreadTasksIncludingDelayedTasks(); |
// The connection handler should have given up, no new call. |
verify(mMockClient, times(ConnectedTask.RETRY_NUMBER_LIMIT * 2 - 1)).connect(); |
@@ -138,7 +130,7 @@ public class GoogleApiClientHelperTest { |
@Feature({"GCore"}) |
public void lifecycleManagementTest() { |
GoogleApiClientHelper helper = new GoogleApiClientHelper(mMockClient); |
- Robolectric.runUiThreadTasksIncludingDelayedTasks(); |
+ ShadowLooper.runUiThreadTasksIncludingDelayedTasks(); |
Activity mockActivity = mock(Activity.class); |
ApplicationStatus.onStateChangeForTesting(mockActivity, ActivityState.CREATED); |
@@ -155,8 +147,8 @@ public class GoogleApiClientHelperTest { |
// Should be disconnected when we go in the background |
ApplicationStatus.onStateChangeForTesting(mockActivity, ActivityState.STOPPED); |
- Robolectric.runUiThreadTasksIncludingDelayedTasks(); |
- verify(mMockClient).disconnect(); |
+ ShadowLooper.runUiThreadTasksIncludingDelayedTasks(); |
+ verify(mMockClient, times(1)).disconnect(); |
// Should be reconnected when we come in the foreground |
ApplicationStatus.onStateChangeForTesting(mockActivity, ActivityState.STARTED); |
@@ -177,7 +169,7 @@ public class GoogleApiClientHelperTest { |
@Feature({"GCore"}) |
public void lifecycleManagementDelayTest() { |
GoogleApiClientHelper helper = new GoogleApiClientHelper(mMockClient); |
- Robolectric.runUiThreadTasksIncludingDelayedTasks(); |
+ ShadowLooper.runUiThreadTasksIncludingDelayedTasks(); |
Activity mockActivity = mock(Activity.class); |
ApplicationStatus.onStateChangeForTesting(mockActivity, ActivityState.CREATED); |
helper.setDisconnectionDelay(5000); |
@@ -187,12 +179,12 @@ public class GoogleApiClientHelperTest { |
// Should not be disconnected when we go in the background |
ApplicationStatus.onStateChangeForTesting(mockActivity, ActivityState.STOPPED); |
- Robolectric.runUiThreadTasks(); |
- verify(mMockClient, never()).disconnect(); |
+ ShadowLooper.runUiThreadTasks(); |
+ verify(mMockClient, times(0)).disconnect(); |
// Should be disconnected when we wait. |
- Robolectric.runUiThreadTasksIncludingDelayedTasks(); |
- verify(mMockClient).disconnect(); |
+ ShadowLooper.runUiThreadTasksIncludingDelayedTasks(); |
+ verify(mMockClient, times(1)).disconnect(); |
// Should be reconnected when we come in the foreground |
ApplicationStatus.onStateChangeForTesting(mockActivity, ActivityState.STARTED); |
@@ -200,11 +192,11 @@ public class GoogleApiClientHelperTest { |
// Should not disconnect when we became visible during the delay |
ApplicationStatus.onStateChangeForTesting(mockActivity, ActivityState.STOPPED); |
- Robolectric.runUiThreadTasks(); |
- verify(mMockClient).disconnect(); |
+ ShadowLooper.runUiThreadTasks(); |
+ verify(mMockClient, times(1)).disconnect(); |
ApplicationStatus.onStateChangeForTesting(mockActivity, ActivityState.STARTED); |
- Robolectric.runUiThreadTasksIncludingDelayedTasks(); |
- verify(mMockClient).disconnect(); |
+ ShadowLooper.runUiThreadTasksIncludingDelayedTasks(); |
+ verify(mMockClient, times(1)).disconnect(); |
} |
@Test |
@@ -212,7 +204,7 @@ public class GoogleApiClientHelperTest { |
public void disconnectionCancellingTest() { |
int disconnectionTimeout = 5000; |
GoogleApiClientHelper helper = new GoogleApiClientHelper(mMockClient); |
- Robolectric.runUiThreadTasksIncludingDelayedTasks(); |
+ ShadowLooper.runUiThreadTasksIncludingDelayedTasks(); |
Activity mockActivity = mock(Activity.class); |
ApplicationStatus.onStateChangeForTesting(mockActivity, ActivityState.CREATED); |
helper.setDisconnectionDelay(disconnectionTimeout); |
@@ -222,9 +214,9 @@ public class GoogleApiClientHelperTest { |
// We go in the background and come back before the end of the timeout. |
ApplicationStatus.onStateChangeForTesting(mockActivity, ActivityState.STOPPED); |
- Robolectric.idleMainLooper(disconnectionTimeout - 42); |
+ ShadowLooper.idleMainLooper(disconnectionTimeout - 42); |
ApplicationStatus.onStateChangeForTesting(mockActivity, ActivityState.STARTED); |
- Robolectric.runUiThreadTasksIncludingDelayedTasks(); |
+ ShadowLooper.runUiThreadTasksIncludingDelayedTasks(); |
// The client should not have been disconnected, which would drop requests otherwise. |
verify(mMockClient, never()).disconnect(); |
@@ -236,7 +228,7 @@ public class GoogleApiClientHelperTest { |
int disconnectionTimeout = 5000; |
int arbitraryNumberOfSeconds = 42; |
GoogleApiClientHelper helper = new GoogleApiClientHelper(mMockClient); |
- Robolectric.runUiThreadTasksIncludingDelayedTasks(); |
+ ShadowLooper.runUiThreadTasksIncludingDelayedTasks(); |
Activity mockActivity = mock(Activity.class); |
ApplicationStatus.onStateChangeForTesting(mockActivity, ActivityState.CREATED); |
helper.setDisconnectionDelay(disconnectionTimeout); |
@@ -246,15 +238,15 @@ public class GoogleApiClientHelperTest { |
// We go in the background and extend the delay |
ApplicationStatus.onStateChangeForTesting(mockActivity, ActivityState.STOPPED); |
- Robolectric.idleMainLooper(disconnectionTimeout - arbitraryNumberOfSeconds); |
+ ShadowLooper.idleMainLooper(disconnectionTimeout - arbitraryNumberOfSeconds); |
helper.willUseConnection(); |
// The client should not have been disconnected. |
- Robolectric.idleMainLooper(disconnectionTimeout - arbitraryNumberOfSeconds); |
+ ShadowLooper.idleMainLooper(disconnectionTimeout - arbitraryNumberOfSeconds); |
verify(mMockClient, never()).disconnect(); |
// After the full timeout it should still disconnect though |
- Robolectric.idleMainLooper(arbitraryNumberOfSeconds); |
+ ShadowLooper.idleMainLooper(arbitraryNumberOfSeconds); |
verify(mMockClient).disconnect(); |
// The client is now disconnected then |
@@ -269,7 +261,7 @@ public class GoogleApiClientHelperTest { |
@Feature({"GCore"}) |
public void willUseConnectionForegroundTest() { |
GoogleApiClientHelper helper = new GoogleApiClientHelper(mMockClient); |
- Robolectric.runUiThreadTasksIncludingDelayedTasks(); |
+ ShadowLooper.runUiThreadTasksIncludingDelayedTasks(); |
Activity mockActivity = mock(Activity.class); |
ApplicationStatus.onStateChangeForTesting(mockActivity, ActivityState.CREATED); |
helper.setDisconnectionDelay(5000); |
@@ -279,11 +271,11 @@ public class GoogleApiClientHelperTest { |
// We are in the foreground |
ApplicationStatus.onStateChangeForTesting(mockActivity, ActivityState.STARTED); |
- Robolectric.runUiThreadTasksIncludingDelayedTasks(); |
+ ShadowLooper.runUiThreadTasksIncludingDelayedTasks(); |
// Disconnections should not be scheduled when in the foreground. |
helper.willUseConnection(); |
- Robolectric.runUiThreadTasksIncludingDelayedTasks(); |
+ ShadowLooper.runUiThreadTasksIncludingDelayedTasks(); |
verify(mMockClient, never()).disconnect(); |
} |
} |