| 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.test.util.browser.signin; | 5 package org.chromium.chrome.test.util.browser.signin; |
| 6 | 6 |
| 7 import android.accounts.Account; | 7 import android.accounts.Account; |
| 8 import android.app.Instrumentation; | 8 import android.app.Instrumentation; |
| 9 import android.content.Context; | 9 import android.content.Context; |
| 10 | 10 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 AccountManagerHelper.overrideAccountManagerHelperForTests(sContext, sAcc
ountManager); | 50 AccountManagerHelper.overrideAccountManagerHelperForTests(sContext, sAcc
ountManager); |
| 51 overrideAccountIdProvider(); | 51 overrideAccountIdProvider(); |
| 52 resetSigninState(); | 52 resetSigninState(); |
| 53 } | 53 } |
| 54 | 54 |
| 55 /** | 55 /** |
| 56 * Returns the currently signed in account. | 56 * Returns the currently signed in account. |
| 57 */ | 57 */ |
| 58 public static Account getCurrentAccount() { | 58 public static Account getCurrentAccount() { |
| 59 assert sContext != null; | 59 assert sContext != null; |
| 60 return ChromeSigninController.get().getSignedInUser(); | 60 return ChromeSigninController.get(sContext).getSignedInUser(); |
| 61 } | 61 } |
| 62 | 62 |
| 63 /** | 63 /** |
| 64 * Add an account with the default name. | 64 * Add an account with the default name. |
| 65 */ | 65 */ |
| 66 public static Account addTestAccount() { | 66 public static Account addTestAccount() { |
| 67 return addTestAccount(DEFAULT_ACCOUNT); | 67 return addTestAccount(DEFAULT_ACCOUNT); |
| 68 } | 68 } |
| 69 | 69 |
| 70 /** | 70 /** |
| 71 * Add an account with a given name. | 71 * Add an account with a given name. |
| 72 */ | 72 */ |
| 73 public static Account addTestAccount(String name) { | 73 public static Account addTestAccount(String name) { |
| 74 Account account = createTestAccount(name); | 74 Account account = createTestAccount(name); |
| 75 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 75 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 76 @Override | 76 @Override |
| 77 public void run() { | 77 public void run() { |
| 78 AccountTrackerService.get().invalidateAccountSeedStatus(true); | 78 AccountTrackerService.get(sContext).invalidateAccountSeedStatus(
true); |
| 79 } | 79 } |
| 80 }); | 80 }); |
| 81 return account; | 81 return account; |
| 82 } | 82 } |
| 83 | 83 |
| 84 /** | 84 /** |
| 85 * Add and sign in an account with the default name. | 85 * Add and sign in an account with the default name. |
| 86 */ | 86 */ |
| 87 public static Account addAndSignInTestAccount() { | 87 public static Account addAndSignInTestAccount() { |
| 88 Account account = createTestAccount(DEFAULT_ACCOUNT); | 88 Account account = createTestAccount(DEFAULT_ACCOUNT); |
| 89 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 89 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 90 @Override | 90 @Override |
| 91 public void run() { | 91 public void run() { |
| 92 ChromeSigninController.get().setSignedInAccountName(DEFAULT_ACCO
UNT); | 92 ChromeSigninController.get(sContext).setSignedInAccountName(DEFA
ULT_ACCOUNT); |
| 93 AccountTrackerService.get().invalidateAccountSeedStatus(true); | 93 AccountTrackerService.get(sContext).invalidateAccountSeedStatus(
true); |
| 94 } | 94 } |
| 95 }); | 95 }); |
| 96 return account; | 96 return account; |
| 97 } | 97 } |
| 98 | 98 |
| 99 private static Account createTestAccount(String accountName) { | 99 private static Account createTestAccount(String accountName) { |
| 100 assert sContext != null; | 100 assert sContext != null; |
| 101 Account account = AccountManagerHelper.createAccountFromName(accountName
); | 101 Account account = AccountManagerHelper.createAccountFromName(accountName
); |
| 102 AccountHolder.Builder accountHolder = AccountHolder.builder(account).alw
aysAccept(true); | 102 AccountHolder.Builder accountHolder = AccountHolder.builder(account).alw
aysAccept(true); |
| 103 sAccountManager.addAccountHolderExplicitly(accountHolder.build()); | 103 sAccountManager.addAccountHolderExplicitly(accountHolder.build()); |
| 104 return account; | 104 return account; |
| 105 } | 105 } |
| 106 | 106 |
| 107 private static void overrideAccountIdProvider() { | 107 private static void overrideAccountIdProvider() { |
| 108 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 108 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 109 @Override | 109 @Override |
| 110 public void run() { | 110 public void run() { |
| 111 AccountIdProvider.setInstanceForTest(new AccountIdProvider() { | 111 AccountIdProvider.setInstanceForTest(new AccountIdProvider() { |
| 112 @Override | 112 @Override |
| 113 public String getAccountId(String accountName) { | 113 public String getAccountId(Context ctx, String accountName)
{ |
| 114 return "gaia-id-" + accountName; | 114 return "gaia-id-" + accountName; |
| 115 } | 115 } |
| 116 | 116 |
| 117 @Override | 117 @Override |
| 118 public boolean canBeUsed() { | 118 public boolean canBeUsed(Context ctx) { |
| 119 return true; | 119 return true; |
| 120 } | 120 } |
| 121 }); | 121 }); |
| 122 } | 122 } |
| 123 }); | 123 }); |
| 124 } | 124 } |
| 125 | 125 |
| 126 /** | 126 /** |
| 127 * Should be called at setUp and tearDown so that the signin state is not le
aked across tests. | 127 * Should be called at setUp and tearDown so that the signin state is not le
aked across tests. |
| 128 * The setUp call is implicit inside the constructor. | 128 * The setUp call is implicit inside the constructor. |
| 129 */ | 129 */ |
| 130 public static void resetSigninState() { | 130 public static void resetSigninState() { |
| 131 // Clear cached signed account name and accounts list. | 131 // Clear cached signed account name and accounts list. |
| 132 ChromeSigninController.get().setSignedInAccountName(null); | 132 ChromeSigninController.get(sContext).setSignedInAccountName(null); |
| 133 ContextUtils.getAppSharedPreferences() | 133 ContextUtils.getAppSharedPreferences() |
| 134 .edit() | 134 .edit() |
| 135 .putStringSet(OAuth2TokenService.STORED_ACCOUNTS_KEY, new HashSe
t<String>()) | 135 .putStringSet(OAuth2TokenService.STORED_ACCOUNTS_KEY, new HashSe
t<String>()) |
| 136 .apply(); | 136 .apply(); |
| 137 } | 137 } |
| 138 | 138 |
| 139 private SigninTestUtil() {} | 139 private SigninTestUtil() {} |
| 140 } | 140 } |
| OLD | NEW |