| 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | package org.chromium.chrome.browser.signin; |
| 6 | |
| 7 | import android.accounts.Account; |
| 8 | import android.test.InstrumentationTestCase; |
| 9 | import android.test.suitebuilder.annotation.SmallTest; |
| 10 | |
| 11 | import org.chromium.base.test.util.AdvancedMockContext; |
| 12 | import org.chromium.base.test.util.Feature; |
| 13 | import org.chromium.sync.signin.AccountManagerHelper; |
| 14 | import org.chromium.sync.test.util.AccountHolder; |
| 15 | import org.chromium.sync.test.util.MockAccountManager; |
| 16 | |
| 17 | import java.util.concurrent.TimeUnit; |
| 18 | |
| 19 | public class AndroidProfileOAuth2TokenServiceHelperTest extends InstrumentationTestCase { |
| 20 | |
| 21 | private AdvancedMockContext mContext; |
| 22 | private MockAccountManager mAccountManager; |
| 23 | |
| 24 | @Override |
| 25 | protected void setUp() throws Exception { |
| 26 | super.setUp(); |
| 27 | |
| 28 | // Mock out the account manager on the device. |
| 29 | mContext = new AdvancedMockContext(getInstrumentation().getTargetContext()); |
| 30 | mAccountManager = new MockAccountManager(mContext, getInstrumentation().getContext()); |
| 31 | AccountManagerHelper.overrideAccountManagerHelperForTests(mContext, mAccountManager); |
| 32 | } |
| 33 | |
| 34 | @SmallTest |
| 35 | @Feature({"Sync"}) |
| 36 | public void testGetOAuth2AccessTokenWithTimeoutOnSuccess() { |
| 37 | String authToken = "someToken"; |
| 38 | // Auth token should be successfully received. |
| 39 | runTestOfGetOAuth2AccessTokenWithTimeout(authToken); |
| 40 | } |
| 41 | |
| 42 | @SmallTest |
| 43 | @Feature({"Sync"}) |
| 44 | public void testGetOAuth2AccessTokenWithTimeoutOnError() { |
| 45 | String authToken = null; |
| 46 | // Should not crash when auth token is null. |
| 47 | runTestOfGetOAuth2AccessTokenWithTimeout(authToken); |
| 48 | } |
| 49 | |
| 50 | private void runTestOfGetOAuth2AccessTokenWithTimeout(String expectedToken) { |
| 51 | String scope = "http://example.com/scope"; |
| 52 | Account account = AccountManagerHelper.createAccountFromName("test@gmail.com"); |
| 53 | String oauth2Scope = "oauth2:" + scope; |
| 54 | |
| 55 | // Add an account with given auth token for the given scope, already accepted auth popup. |
| 56 | AccountHolder accountHolder = |
| 57 | AccountHolder.create() |
| 58 | .account(account) |
| 59 | .hasBeenAccepted(oauth2Scope, true) |
| 60 | .authToken(oauth2Scope, expectedToken).build(); |
| 61 | mAccountManager.addAccountHolderExplicitly(accountHolder); |
| 62 | |
| 63 | String accessToken = |
| 64 | AndroidProfileOAuth2TokenServiceHelper.getOAuth2AccessTokenWithTimeout( |
| 65 | mContext, null, account, scope, 5, TimeUnit.SECONDS); |
| 66 | assertEquals(expectedToken, accessToken); |
| 67 | } |
| 68 | } |