| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.chrome.browser.externalauth; | 5 package org.chromium.chrome.browser.externalauth; |
| 6 | 6 |
| 7 import static org.junit.Assert.assertFalse; | 7 import static org.junit.Assert.assertFalse; |
| 8 import static org.junit.Assert.assertTrue; | 8 import static org.junit.Assert.assertTrue; |
| 9 import static org.mockito.ArgumentMatchers.any; | 9 import static org.mockito.ArgumentMatchers.any; |
| 10 import static org.mockito.ArgumentMatchers.anyInt; | 10 import static org.mockito.ArgumentMatchers.anyInt; |
| 11 import static org.mockito.Mockito.doNothing; | 11 import static org.mockito.Mockito.doNothing; |
| 12 import static org.mockito.Mockito.inOrder; | 12 import static org.mockito.Mockito.inOrder; |
| 13 import static org.mockito.Mockito.never; | 13 import static org.mockito.Mockito.never; |
| 14 import static org.mockito.Mockito.verifyZeroInteractions; | 14 import static org.mockito.Mockito.verifyZeroInteractions; |
| 15 import static org.mockito.Mockito.when; | 15 import static org.mockito.Mockito.when; |
| 16 | 16 |
| 17 import android.content.Context; | 17 import android.content.Context; |
| 18 | 18 |
| 19 import com.google.android.gms.common.ConnectionResult; | 19 import com.google.android.gms.common.ConnectionResult; |
| 20 | 20 |
| 21 import org.chromium.base.test.util.Feature; | |
| 22 import org.chromium.testing.local.LocalRobolectricTestRunner; | |
| 23 import org.junit.Before; | 21 import org.junit.Before; |
| 24 import org.junit.Test; | 22 import org.junit.Test; |
| 25 import org.junit.runner.RunWith; | 23 import org.junit.runner.RunWith; |
| 26 import org.mockito.InOrder; | 24 import org.mockito.InOrder; |
| 27 import org.mockito.Mock; | 25 import org.mockito.Mock; |
| 28 import org.mockito.MockitoAnnotations; | 26 import org.mockito.MockitoAnnotations; |
| 29 import org.robolectric.annotation.Config; | 27 import org.robolectric.annotation.Config; |
| 30 | 28 |
| 29 import org.chromium.base.test.util.Feature; |
| 30 import org.chromium.testing.local.LocalRobolectricTestRunner; |
| 31 |
| 31 /** | 32 /** |
| 32 * Robolectric tests for {@link ExternalAuthUtils}. | 33 * Robolectric tests for {@link ExternalAuthUtils}. |
| 33 */ | 34 */ |
| 34 @RunWith(LocalRobolectricTestRunner.class) | 35 @RunWith(LocalRobolectricTestRunner.class) |
| 35 @Config(manifest = Config.NONE) | 36 @Config(manifest = Config.NONE) |
| 36 public class ExternalAuthUtilsTest { | 37 public class ExternalAuthUtilsTest { |
| 37 private static final int ERR = 999; | 38 private static final int ERR = 999; |
| 38 @Mock private Context mContext; | 39 @Mock private Context mContext; |
| 39 @Mock private ExternalAuthUtils mExternalAuthUtils; | 40 @Mock private ExternalAuthUtils mExternalAuthUtils; |
| 40 @Mock private UserRecoverableErrorHandler mUserRecoverableErrorHandler; | 41 @Mock private UserRecoverableErrorHandler mUserRecoverableErrorHandler; |
| 41 | 42 |
| 42 @Before | 43 @Before |
| 43 public void setUp() { | 44 public void setUp() { |
| 44 MockitoAnnotations.initMocks(this); | 45 MockitoAnnotations.initMocks(this); |
| 45 } | 46 } |
| 46 | 47 |
| 47 @Test | 48 @Test |
| 48 @Feature({"GooglePlayServices"}) | 49 @Feature({"GooglePlayServices"}) |
| 49 public void testCanUseGooglePlayServicesSuccess() { | 50 public void testCanUseGooglePlayServicesSuccess() { |
| 50 when(mExternalAuthUtils.canUseGooglePlayServices(any(Context.class), | 51 when(mExternalAuthUtils.canUseGooglePlayServices(any(Context.class), |
| 51 any(UserRecoverableErrorHandler.class))).thenCallRealMethod
(); | 52 any(UserRecoverableErrorHandler.class))).thenCallRealMethod(); |
| 53 when(mExternalAuthUtils.canUseGooglePlayServicesResultCode(any(Context.c
lass), |
| 54 any(UserRecoverableErrorHandler.class))).thenCallRealMethod(); |
| 52 when(mExternalAuthUtils.checkGooglePlayServicesAvailable(mContext)).then
Return( | 55 when(mExternalAuthUtils.checkGooglePlayServicesAvailable(mContext)).then
Return( |
| 53 ConnectionResult.SUCCESS); | 56 ConnectionResult.SUCCESS); |
| 54 assertTrue(mExternalAuthUtils.canUseGooglePlayServices( | 57 assertTrue(mExternalAuthUtils.canUseGooglePlayServices( |
| 55 mContext, mUserRecoverableErrorHandler)); | 58 mContext, mUserRecoverableErrorHandler)); |
| 56 verifyZeroInteractions(mUserRecoverableErrorHandler); | 59 verifyZeroInteractions(mUserRecoverableErrorHandler); |
| 57 | 60 |
| 58 // Verifying stubs can be an anti-pattern but here it is important to | 61 // Verifying stubs can be an anti-pattern but here it is important to |
| 59 // test that the real method canUseGooglePlayServices did invoke these | 62 // test that the real method canUseGooglePlayServices did invoke these |
| 60 // methods, which subclasses are expected to be able to override. | 63 // methods, which subclasses are expected to be able to override. |
| 61 InOrder inOrder = inOrder(mExternalAuthUtils); | 64 InOrder inOrder = inOrder(mExternalAuthUtils); |
| 62 inOrder.verify(mExternalAuthUtils).checkGooglePlayServicesAvailable(mCon
text); | 65 inOrder.verify(mExternalAuthUtils).checkGooglePlayServicesAvailable(mCon
text); |
| 63 inOrder.verify(mExternalAuthUtils).recordConnectionResult(ConnectionResu
lt.SUCCESS); | 66 inOrder.verify(mExternalAuthUtils).recordConnectionResult(ConnectionResu
lt.SUCCESS); |
| 64 inOrder.verify(mExternalAuthUtils, never()).isUserRecoverableError(anyIn
t()); | 67 inOrder.verify(mExternalAuthUtils, never()).isUserRecoverableError(anyIn
t()); |
| 65 inOrder.verify(mExternalAuthUtils, never()).describeError(anyInt()); | 68 inOrder.verify(mExternalAuthUtils, never()).describeError(anyInt()); |
| 66 } | 69 } |
| 67 | 70 |
| 68 @Test | 71 @Test |
| 69 @Feature({"GooglePlayServices"}) | 72 @Feature({"GooglePlayServices"}) |
| 70 public void testCanUseGooglePlayServicesNonUserRecoverableFailure() { | 73 public void testCanUseGooglePlayServicesNonUserRecoverableFailure() { |
| 71 when(mExternalAuthUtils.canUseGooglePlayServices(any(Context.class), | 74 when(mExternalAuthUtils.canUseGooglePlayServices(any(Context.class), |
| 72 any(UserRecoverableErrorHandler.class))).thenCallRealMethod
(); | 75 any(UserRecoverableErrorHandler.class))).thenCallRealMethod(); |
| 76 when(mExternalAuthUtils.canUseGooglePlayServicesResultCode(any(Context.c
lass), |
| 77 any(UserRecoverableErrorHandler.class))).thenCallRealMethod(); |
| 73 when(mExternalAuthUtils.checkGooglePlayServicesAvailable(mContext)).then
Return(ERR); | 78 when(mExternalAuthUtils.checkGooglePlayServicesAvailable(mContext)).then
Return(ERR); |
| 74 when(mExternalAuthUtils.isUserRecoverableError(ERR)).thenReturn(false);
// Non-recoverable | 79 when(mExternalAuthUtils.isUserRecoverableError(ERR)).thenReturn(false);
// Non-recoverable |
| 75 assertFalse(mExternalAuthUtils.canUseGooglePlayServices( | 80 assertFalse(mExternalAuthUtils.canUseGooglePlayServices( |
| 76 mContext, mUserRecoverableErrorHandler)); | 81 mContext, mUserRecoverableErrorHandler)); |
| 77 verifyZeroInteractions(mUserRecoverableErrorHandler); | 82 verifyZeroInteractions(mUserRecoverableErrorHandler); |
| 78 | 83 |
| 79 // Verifying stubs can be an anti-pattern but here it is important to | 84 // Verifying stubs can be an anti-pattern but here it is important to |
| 80 // test that the real method canUseGooglePlayServices did invoke these | 85 // test that the real method canUseGooglePlayServices did invoke these |
| 81 // methods, which subclasses are expected to be able to override. | 86 // methods, which subclasses are expected to be able to override. |
| 82 InOrder inOrder = inOrder(mExternalAuthUtils); | 87 InOrder inOrder = inOrder(mExternalAuthUtils); |
| 83 inOrder.verify(mExternalAuthUtils).checkGooglePlayServicesAvailable(mCon
text); | 88 inOrder.verify(mExternalAuthUtils).checkGooglePlayServicesAvailable(mCon
text); |
| 84 inOrder.verify(mExternalAuthUtils).recordConnectionResult(ERR); | 89 inOrder.verify(mExternalAuthUtils).recordConnectionResult(ERR); |
| 85 inOrder.verify(mExternalAuthUtils).isUserRecoverableError(ERR); | 90 inOrder.verify(mExternalAuthUtils).isUserRecoverableError(ERR); |
| 86 } | 91 } |
| 87 | 92 |
| 88 @Test | 93 @Test |
| 89 @Feature({"GooglePlayServices"}) | 94 @Feature({"GooglePlayServices"}) |
| 90 public void testCanUseGooglePlayServicesUserRecoverableFailure() { | 95 public void testCanUseGooglePlayServicesUserRecoverableFailure() { |
| 91 when(mExternalAuthUtils.canUseGooglePlayServices(any(Context.class), | 96 when(mExternalAuthUtils.canUseGooglePlayServices(any(Context.class), |
| 92 any(UserRecoverableErrorHandler.class))).thenCallRealMethod
(); | 97 any(UserRecoverableErrorHandler.class))).thenCallRealMethod(); |
| 98 when(mExternalAuthUtils.canUseGooglePlayServicesResultCode(any(Context.c
lass), |
| 99 any(UserRecoverableErrorHandler.class))).thenCallRealMethod(); |
| 93 doNothing().when(mUserRecoverableErrorHandler).handleError(mContext, ERR
); | 100 doNothing().when(mUserRecoverableErrorHandler).handleError(mContext, ERR
); |
| 94 when(mExternalAuthUtils.checkGooglePlayServicesAvailable(mContext)).then
Return(ERR); | 101 when(mExternalAuthUtils.checkGooglePlayServicesAvailable(mContext)).then
Return(ERR); |
| 95 when(mExternalAuthUtils.isUserRecoverableError(ERR)).thenReturn(true);
// Recoverable | 102 when(mExternalAuthUtils.isUserRecoverableError(ERR)).thenReturn(true);
// Recoverable |
| 96 when(mExternalAuthUtils.describeError(anyInt())).thenReturn("unused");
// For completeness | 103 when(mExternalAuthUtils.describeError(anyInt())).thenReturn("unused");
// For completeness |
| 97 assertFalse(mExternalAuthUtils.canUseGooglePlayServices( | 104 assertFalse(mExternalAuthUtils.canUseGooglePlayServices( |
| 98 mContext, mUserRecoverableErrorHandler)); | 105 mContext, mUserRecoverableErrorHandler)); |
| 99 | 106 |
| 100 // Verifying stubs can be an anti-pattern but here it is important to | 107 // Verifying stubs can be an anti-pattern but here it is important to |
| 101 // test that the real method canUseGooglePlayServices did invoke these | 108 // test that the real method canUseGooglePlayServices did invoke these |
| 102 // methods, which subclasses are expected to be able to override. | 109 // methods, which subclasses are expected to be able to override. |
| 103 InOrder inOrder = inOrder(mExternalAuthUtils, mUserRecoverableErrorHandl
er); | 110 InOrder inOrder = inOrder(mExternalAuthUtils, mUserRecoverableErrorHandl
er); |
| 104 inOrder.verify(mExternalAuthUtils).checkGooglePlayServicesAvailable(mCon
text); | 111 inOrder.verify(mExternalAuthUtils).checkGooglePlayServicesAvailable(mCon
text); |
| 105 inOrder.verify(mExternalAuthUtils).recordConnectionResult(ERR); | 112 inOrder.verify(mExternalAuthUtils).recordConnectionResult(ERR); |
| 106 inOrder.verify(mExternalAuthUtils).isUserRecoverableError(ERR); | 113 inOrder.verify(mExternalAuthUtils).isUserRecoverableError(ERR); |
| 107 inOrder.verify(mUserRecoverableErrorHandler).handleError(mContext, ERR); | 114 inOrder.verify(mUserRecoverableErrorHandler).handleError(mContext, ERR); |
| 108 } | 115 } |
| 109 } | 116 } |
| OLD | NEW |