| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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.signin; | 5 package org.chromium.chrome.browser.signin; |
| 6 | 6 |
| 7 import android.accounts.Account; | 7 import android.accounts.Account; |
| 8 import android.support.test.InstrumentationRegistry; |
| 8 import android.support.test.filters.SmallTest; | 9 import android.support.test.filters.SmallTest; |
| 9 import android.test.InstrumentationTestCase; | 10 |
| 11 import org.junit.Assert; |
| 12 import org.junit.Before; |
| 13 import org.junit.Test; |
| 14 import org.junit.runner.RunWith; |
| 10 | 15 |
| 11 import org.chromium.base.test.util.AdvancedMockContext; | 16 import org.chromium.base.test.util.AdvancedMockContext; |
| 12 import org.chromium.base.test.util.DisabledTest; | 17 import org.chromium.base.test.util.DisabledTest; |
| 13 import org.chromium.base.test.util.Feature; | 18 import org.chromium.base.test.util.Feature; |
| 19 import org.chromium.chrome.test.ChromeJUnit4ClassRunner; |
| 14 import org.chromium.components.signin.AccountManagerHelper; | 20 import org.chromium.components.signin.AccountManagerHelper; |
| 15 import org.chromium.components.signin.test.util.AccountHolder; | 21 import org.chromium.components.signin.test.util.AccountHolder; |
| 16 import org.chromium.components.signin.test.util.MockAccountManager; | 22 import org.chromium.components.signin.test.util.MockAccountManager; |
| 17 | 23 |
| 18 import java.util.Arrays; | 24 import java.util.Arrays; |
| 19 import java.util.concurrent.TimeUnit; | 25 import java.util.concurrent.TimeUnit; |
| 20 | 26 |
| 21 /** Tests for OAuth2TokenService. */ | 27 /** Tests for OAuth2TokenService. */ |
| 22 public class OAuth2TokenServiceTest extends InstrumentationTestCase { | 28 @RunWith(ChromeJUnit4ClassRunner.class) |
| 23 | 29 public class OAuth2TokenServiceTest { |
| 24 private AdvancedMockContext mContext; | 30 private AdvancedMockContext mContext; |
| 25 private MockAccountManager mAccountManager; | 31 private MockAccountManager mAccountManager; |
| 26 | 32 |
| 27 @Override | 33 @Before |
| 28 protected void setUp() throws Exception { | 34 public void setUp() throws Exception { |
| 29 super.setUp(); | |
| 30 | |
| 31 // Mock out the account manager on the device. | 35 // Mock out the account manager on the device. |
| 32 mContext = new AdvancedMockContext(getInstrumentation().getTargetContext
()); | 36 mContext = new AdvancedMockContext( |
| 33 mAccountManager = new MockAccountManager(mContext, getInstrumentation().
getContext()); | 37 InstrumentationRegistry.getInstrumentation().getTargetContext())
; |
| 38 mAccountManager = new MockAccountManager( |
| 39 mContext, InstrumentationRegistry.getInstrumentation().getContex
t()); |
| 34 AccountManagerHelper.overrideAccountManagerHelperForTests(mContext, mAcc
ountManager); | 40 AccountManagerHelper.overrideAccountManagerHelperForTests(mContext, mAcc
ountManager); |
| 35 } | 41 } |
| 36 | 42 |
| 37 /* | 43 /* |
| 38 * @SmallTest | 44 * @SmallTest |
| 39 * @Feature({"Sync"}) | 45 * @Feature({"Sync"}) |
| 40 */ | 46 */ |
| 47 @Test |
| 41 @DisabledTest(message = "crbug.com/533417") | 48 @DisabledTest(message = "crbug.com/533417") |
| 42 public void testGetAccountsNoAccountsRegistered() { | 49 public void testGetAccountsNoAccountsRegistered() { |
| 43 String[] accounts = OAuth2TokenService.getAccounts(mContext); | 50 String[] accounts = OAuth2TokenService.getAccounts(mContext); |
| 44 assertEquals("There should be no accounts registered", 0, accounts.lengt
h); | 51 Assert.assertEquals("There should be no accounts registered", 0, account
s.length); |
| 45 } | 52 } |
| 46 | 53 |
| 47 /*@SmallTest | 54 /*@SmallTest |
| 48 @Feature({"Sync"})*/ | 55 @Feature({"Sync"})*/ |
| 56 @Test |
| 49 @DisabledTest(message = "crbug.com/527852") | 57 @DisabledTest(message = "crbug.com/527852") |
| 50 public void testGetAccountsOneAccountRegistered() { | 58 public void testGetAccountsOneAccountRegistered() { |
| 51 Account account1 = AccountManagerHelper.createAccountFromName("foo@gmail
.com"); | 59 Account account1 = AccountManagerHelper.createAccountFromName("foo@gmail
.com"); |
| 52 AccountHolder accountHolder1 = AccountHolder.builder(account1).build(); | 60 AccountHolder accountHolder1 = AccountHolder.builder(account1).build(); |
| 53 mAccountManager.addAccountHolderExplicitly(accountHolder1); | 61 mAccountManager.addAccountHolderExplicitly(accountHolder1); |
| 54 | 62 |
| 55 String[] sysAccounts = OAuth2TokenService.getSystemAccountNames(mContext
); | 63 String[] sysAccounts = OAuth2TokenService.getSystemAccountNames(mContext
); |
| 56 assertEquals("There should be one registered account", 1, sysAccounts.le
ngth); | 64 Assert.assertEquals("There should be one registered account", 1, sysAcco
unts.length); |
| 57 assertEquals("The account should be " + account1, account1.name, sysAcco
unts[0]); | 65 Assert.assertEquals("The account should be " + account1, account1.name,
sysAccounts[0]); |
| 58 | 66 |
| 59 String[] accounts = OAuth2TokenService.getAccounts(mContext); | 67 String[] accounts = OAuth2TokenService.getAccounts(mContext); |
| 60 assertEquals("There should be zero registered account", 0, accounts.leng
th); | 68 Assert.assertEquals("There should be zero registered account", 0, accoun
ts.length); |
| 61 } | 69 } |
| 62 | 70 |
| 63 /*@SmallTest | 71 /*@SmallTest |
| 64 @Feature({"Sync"})*/ | 72 @Feature({"Sync"})*/ |
| 73 @Test |
| 65 @DisabledTest(message = "crbug.com/527852") | 74 @DisabledTest(message = "crbug.com/527852") |
| 66 public void testGetAccountsTwoAccountsRegistered() { | 75 public void testGetAccountsTwoAccountsRegistered() { |
| 67 Account account1 = AccountManagerHelper.createAccountFromName("foo@gmail
.com"); | 76 Account account1 = AccountManagerHelper.createAccountFromName("foo@gmail
.com"); |
| 68 AccountHolder accountHolder1 = AccountHolder.builder(account1).build(); | 77 AccountHolder accountHolder1 = AccountHolder.builder(account1).build(); |
| 69 mAccountManager.addAccountHolderExplicitly(accountHolder1); | 78 mAccountManager.addAccountHolderExplicitly(accountHolder1); |
| 70 Account account2 = AccountManagerHelper.createAccountFromName("bar@gmail
.com"); | 79 Account account2 = AccountManagerHelper.createAccountFromName("bar@gmail
.com"); |
| 71 AccountHolder accountHolder2 = AccountHolder.builder(account2).build(); | 80 AccountHolder accountHolder2 = AccountHolder.builder(account2).build(); |
| 72 mAccountManager.addAccountHolderExplicitly(accountHolder2); | 81 mAccountManager.addAccountHolderExplicitly(accountHolder2); |
| 73 | 82 |
| 74 String[] sysAccounts = OAuth2TokenService.getSystemAccountNames(mContext
); | 83 String[] sysAccounts = OAuth2TokenService.getSystemAccountNames(mContext
); |
| 75 assertEquals("There should be one registered account", 2, sysAccounts.le
ngth); | 84 Assert.assertEquals("There should be one registered account", 2, sysAcco
unts.length); |
| 76 assertTrue("The list should contain " + account1, | 85 Assert.assertTrue("The list should contain " + account1, |
| 77 Arrays.asList(sysAccounts).contains(account1.name)); | 86 Arrays.asList(sysAccounts).contains(account1.name)); |
| 78 assertTrue("The list should contain " + account2, | 87 Assert.assertTrue("The list should contain " + account2, |
| 79 Arrays.asList(sysAccounts).contains(account2.name)); | 88 Arrays.asList(sysAccounts).contains(account2.name)); |
| 80 | 89 |
| 81 String[] accounts = OAuth2TokenService.getAccounts(mContext); | 90 String[] accounts = OAuth2TokenService.getAccounts(mContext); |
| 82 assertEquals("There should be zero registered account", 0, accounts.leng
th); | 91 Assert.assertEquals("There should be zero registered account", 0, accoun
ts.length); |
| 83 } | 92 } |
| 84 | 93 |
| 94 @Test |
| 85 @DisabledTest(message = "crbug.com/568620") | 95 @DisabledTest(message = "crbug.com/568620") |
| 86 @SmallTest | 96 @SmallTest |
| 87 @Feature({"Sync"}) | 97 @Feature({"Sync"}) |
| 88 public void testGetOAuth2AccessTokenWithTimeoutOnSuccess() { | 98 public void testGetOAuth2AccessTokenWithTimeoutOnSuccess() { |
| 89 String authToken = "someToken"; | 99 String authToken = "someToken"; |
| 90 // Auth token should be successfully received. | 100 // Auth token should be successfully received. |
| 91 runTestOfGetOAuth2AccessTokenWithTimeout(authToken); | 101 runTestOfGetOAuth2AccessTokenWithTimeout(authToken); |
| 92 } | 102 } |
| 93 | 103 |
| 94 /*@SmallTest | 104 /*@SmallTest |
| 95 @Feature({"Sync"})*/ | 105 @Feature({"Sync"})*/ |
| 106 @Test |
| 96 @DisabledTest(message = "crbug.com/527852") | 107 @DisabledTest(message = "crbug.com/527852") |
| 97 public void testGetOAuth2AccessTokenWithTimeoutOnError() { | 108 public void testGetOAuth2AccessTokenWithTimeoutOnError() { |
| 98 String authToken = null; | 109 String authToken = null; |
| 99 // Should not crash when auth token is null. | 110 // Should not crash when auth token is null. |
| 100 runTestOfGetOAuth2AccessTokenWithTimeout(authToken); | 111 runTestOfGetOAuth2AccessTokenWithTimeout(authToken); |
| 101 } | 112 } |
| 102 | 113 |
| 103 private void runTestOfGetOAuth2AccessTokenWithTimeout(String expectedToken)
{ | 114 private void runTestOfGetOAuth2AccessTokenWithTimeout(String expectedToken)
{ |
| 104 String scope = "http://example.com/scope"; | 115 String scope = "http://example.com/scope"; |
| 105 Account account = AccountManagerHelper.createAccountFromName("test@gmail
.com"); | 116 Account account = AccountManagerHelper.createAccountFromName("test@gmail
.com"); |
| 106 String oauth2Scope = "oauth2:" + scope; | 117 String oauth2Scope = "oauth2:" + scope; |
| 107 | 118 |
| 108 // Add an account with given auth token for the given scope, already acc
epted auth popup. | 119 // Add an account with given auth token for the given scope, already acc
epted auth popup. |
| 109 AccountHolder accountHolder = AccountHolder.builder(account) | 120 AccountHolder accountHolder = AccountHolder.builder(account) |
| 110 .hasBeenAccepted(oauth2Scope, true
) | 121 .hasBeenAccepted(oauth2Scope, true
) |
| 111 .authToken(oauth2Scope, expectedTo
ken) | 122 .authToken(oauth2Scope, expectedTo
ken) |
| 112 .build(); | 123 .build(); |
| 113 mAccountManager.addAccountHolderExplicitly(accountHolder); | 124 mAccountManager.addAccountHolderExplicitly(accountHolder); |
| 114 | 125 |
| 115 String accessToken = OAuth2TokenService.getOAuth2AccessTokenWithTimeout( | 126 String accessToken = OAuth2TokenService.getOAuth2AccessTokenWithTimeout( |
| 116 mContext, account, scope, 5, TimeUnit.SECONDS); | 127 mContext, account, scope, 5, TimeUnit.SECONDS); |
| 117 assertEquals(expectedToken, accessToken); | 128 Assert.assertEquals(expectedToken, accessToken); |
| 118 } | 129 } |
| 119 } | 130 } |
| OLD | NEW |