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.annotation.SuppressLint; | 8 import android.annotation.SuppressLint; |
9 import android.app.Instrumentation; | |
10 import android.content.Context; | |
11 | 9 |
12 import org.chromium.base.ContextUtils; | 10 import org.chromium.base.ContextUtils; |
13 import org.chromium.base.ThreadUtils; | 11 import org.chromium.base.ThreadUtils; |
14 import org.chromium.chrome.browser.init.ProcessInitializationHandler; | |
15 import org.chromium.chrome.browser.signin.AccountIdProvider; | 12 import org.chromium.chrome.browser.signin.AccountIdProvider; |
16 import org.chromium.chrome.browser.signin.AccountTrackerService; | 13 import org.chromium.chrome.browser.signin.AccountTrackerService; |
17 import org.chromium.chrome.browser.signin.OAuth2TokenService; | 14 import org.chromium.chrome.browser.signin.OAuth2TokenService; |
18 import org.chromium.components.signin.AccountManagerHelper; | 15 import org.chromium.components.signin.AccountManagerHelper; |
19 import org.chromium.components.signin.ChromeSigninController; | 16 import org.chromium.components.signin.ChromeSigninController; |
20 import org.chromium.components.signin.test.util.AccountHolder; | 17 import org.chromium.components.signin.test.util.AccountHolder; |
21 import org.chromium.components.signin.test.util.FakeAccountManagerDelegate; | 18 import org.chromium.components.signin.test.util.FakeAccountManagerDelegate; |
22 | 19 |
23 import java.util.HashSet; | 20 import java.util.HashSet; |
24 | 21 |
25 /** | 22 /** |
26 * Utility class for test signin functionality. | 23 * Utility class for test signin functionality. |
27 */ | 24 */ |
28 public final class SigninTestUtil { | 25 public final class SigninTestUtil { |
29 private static final String TAG = "Signin"; | 26 private static final String TAG = "Signin"; |
30 | 27 |
31 private static final String DEFAULT_ACCOUNT = "test@gmail.com"; | 28 private static final String DEFAULT_ACCOUNT = "test@gmail.com"; |
32 | 29 |
33 @SuppressLint("StaticFieldLeak") | 30 @SuppressLint("StaticFieldLeak") |
34 private static Context sContext; | |
35 @SuppressLint("StaticFieldLeak") | |
36 private static FakeAccountManagerDelegate sAccountManager; | 31 private static FakeAccountManagerDelegate sAccountManager; |
37 | 32 |
38 /** | 33 /** |
39 * Sets up the test authentication environment. | 34 * Sets up the test authentication environment. |
40 * | 35 * |
41 * This must be called before native is loaded. | 36 * This must be called before native is loaded. |
42 */ | 37 */ |
43 public static void setUpAuthForTest(Instrumentation instrumentation) { | 38 public static void setUpAuthForTest() { |
44 assert sContext == null; | 39 sAccountManager = new FakeAccountManagerDelegate(); |
45 sContext = instrumentation.getTargetContext(); | 40 AccountManagerHelper.overrideAccountManagerHelperForTests(sAccountManage
r); |
46 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | |
47 @Override | |
48 public void run() { | |
49 ProcessInitializationHandler.getInstance().initializePreNative()
; | |
50 } | |
51 }); | |
52 sAccountManager = new FakeAccountManagerDelegate(sContext); | |
53 AccountManagerHelper.overrideAccountManagerHelperForTests(sContext, sAcc
ountManager); | |
54 overrideAccountIdProvider(); | 41 overrideAccountIdProvider(); |
55 resetSigninState(); | 42 resetSigninState(); |
56 } | 43 } |
57 | 44 |
58 /** | 45 /** |
59 * Tears down the test authentication environment. | |
60 */ | |
61 public static void tearDownAuthForTest() { | |
62 sContext = null; | |
63 } | |
64 | |
65 /** | |
66 * Returns the currently signed in account. | 46 * Returns the currently signed in account. |
67 */ | 47 */ |
68 public static Account getCurrentAccount() { | 48 public static Account getCurrentAccount() { |
69 assert sContext != null; | 49 assert sAccountManager != null; |
70 return ChromeSigninController.get().getSignedInUser(); | 50 return ChromeSigninController.get().getSignedInUser(); |
71 } | 51 } |
72 | 52 |
73 /** | 53 /** |
74 * Add an account with the default name. | 54 * Add an account with the default name. |
75 */ | 55 */ |
76 public static Account addTestAccount() { | 56 public static Account addTestAccount() { |
77 return addTestAccount(DEFAULT_ACCOUNT); | 57 return addTestAccount(DEFAULT_ACCOUNT); |
78 } | 58 } |
79 | 59 |
(...skipping 20 matching lines...) Expand all Loading... |
100 @Override | 80 @Override |
101 public void run() { | 81 public void run() { |
102 ChromeSigninController.get().setSignedInAccountName(DEFAULT_ACCO
UNT); | 82 ChromeSigninController.get().setSignedInAccountName(DEFAULT_ACCO
UNT); |
103 AccountTrackerService.get().invalidateAccountSeedStatus(true); | 83 AccountTrackerService.get().invalidateAccountSeedStatus(true); |
104 } | 84 } |
105 }); | 85 }); |
106 return account; | 86 return account; |
107 } | 87 } |
108 | 88 |
109 private static Account createTestAccount(String accountName) { | 89 private static Account createTestAccount(String accountName) { |
110 assert sContext != null; | 90 assert sAccountManager != null; |
111 Account account = AccountManagerHelper.createAccountFromName(accountName
); | 91 Account account = AccountManagerHelper.createAccountFromName(accountName
); |
112 AccountHolder.Builder accountHolder = AccountHolder.builder(account).alw
aysAccept(true); | 92 AccountHolder.Builder accountHolder = AccountHolder.builder(account).alw
aysAccept(true); |
113 sAccountManager.addAccountHolderExplicitly(accountHolder.build()); | 93 sAccountManager.addAccountHolderExplicitly(accountHolder.build()); |
114 return account; | 94 return account; |
115 } | 95 } |
116 | 96 |
117 private static void overrideAccountIdProvider() { | 97 private static void overrideAccountIdProvider() { |
118 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 98 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
119 @Override | 99 @Override |
120 public void run() { | 100 public void run() { |
(...skipping 20 matching lines...) Expand all Loading... |
141 // Clear cached signed account name and accounts list. | 121 // Clear cached signed account name and accounts list. |
142 ChromeSigninController.get().setSignedInAccountName(null); | 122 ChromeSigninController.get().setSignedInAccountName(null); |
143 ContextUtils.getAppSharedPreferences() | 123 ContextUtils.getAppSharedPreferences() |
144 .edit() | 124 .edit() |
145 .putStringSet(OAuth2TokenService.STORED_ACCOUNTS_KEY, new HashSe
t<String>()) | 125 .putStringSet(OAuth2TokenService.STORED_ACCOUNTS_KEY, new HashSe
t<String>()) |
146 .apply(); | 126 .apply(); |
147 } | 127 } |
148 | 128 |
149 private SigninTestUtil() {} | 129 private SigninTestUtil() {} |
150 } | 130 } |
OLD | NEW |