| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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.util; | 5 package org.chromium.chrome.browser.util; |
| 6 | 6 |
| 7 import android.accounts.Account; | 7 import android.accounts.Account; |
| 8 import android.accounts.AuthenticatorDescription; | 8 import android.accounts.AuthenticatorDescription; |
| 9 import android.content.Context; | 9 import android.content.Context; |
| 10 import android.content.Intent; | 10 import android.content.Intent; |
| 11 import android.content.pm.ResolveInfo; | 11 import android.content.pm.ResolveInfo; |
| 12 import android.speech.RecognizerIntent; | 12 import android.speech.RecognizerIntent; |
| 13 import android.support.test.InstrumentationRegistry; |
| 13 import android.support.test.filters.SmallTest; | 14 import android.support.test.filters.SmallTest; |
| 14 import android.test.InstrumentationTestCase; | |
| 15 import android.test.mock.MockContext; | 15 import android.test.mock.MockContext; |
| 16 import android.test.mock.MockPackageManager; | 16 import android.test.mock.MockPackageManager; |
| 17 | 17 |
| 18 import org.junit.Assert; |
| 19 import org.junit.Before; |
| 20 import org.junit.Test; |
| 21 import org.junit.runner.RunWith; |
| 22 |
| 18 import org.chromium.base.ThreadUtils; | 23 import org.chromium.base.ThreadUtils; |
| 19 import org.chromium.base.test.util.AdvancedMockContext; | 24 import org.chromium.base.test.util.AdvancedMockContext; |
| 20 import org.chromium.base.test.util.Feature; | 25 import org.chromium.base.test.util.Feature; |
| 26 import org.chromium.chrome.test.ChromeJUnit4ClassRunner; |
| 21 import org.chromium.components.signin.AccountManagerHelper; | 27 import org.chromium.components.signin.AccountManagerHelper; |
| 22 import org.chromium.components.signin.test.util.MockAccountManager; | 28 import org.chromium.components.signin.test.util.MockAccountManager; |
| 23 | 29 |
| 24 import java.util.ArrayList; | 30 import java.util.ArrayList; |
| 25 import java.util.List; | 31 import java.util.List; |
| 26 import java.util.concurrent.Callable; | 32 import java.util.concurrent.Callable; |
| 27 | 33 |
| 28 /** | 34 /** |
| 29 * Unit Test for FeatureUtilities. | 35 * Unit Test for FeatureUtilities. |
| 30 */ | 36 */ |
| 31 public class FeatureUtilitiesTest extends InstrumentationTestCase { | 37 @RunWith(ChromeJUnit4ClassRunner.class) |
| 32 | 38 public class FeatureUtilitiesTest { |
| 33 private IntentTestMockContext mContextWithSpeech; | 39 private IntentTestMockContext mContextWithSpeech; |
| 34 private IntentTestMockContext mContextWithoutSpeech; | 40 private IntentTestMockContext mContextWithoutSpeech; |
| 35 private MockAuthenticationAccountManager mAccountManager; | 41 private MockAuthenticationAccountManager mAccountManager; |
| 36 private AdvancedMockContext mAccountTestingContext; | 42 private AdvancedMockContext mAccountTestingContext; |
| 37 private Account mTestAccount; | 43 private Account mTestAccount; |
| 38 | 44 |
| 39 public FeatureUtilitiesTest() { | 45 public FeatureUtilitiesTest() { |
| 40 mContextWithSpeech = new IntentTestMockContext( | 46 mContextWithSpeech = new IntentTestMockContext( |
| 41 RecognizerIntent.ACTION_RECOGNIZE_SPEECH); | 47 RecognizerIntent.ACTION_RECOGNIZE_SPEECH); |
| 42 | 48 |
| 43 mContextWithoutSpeech = new IntentTestMockContext( | 49 mContextWithoutSpeech = new IntentTestMockContext( |
| 44 RecognizerIntent.ACTION_WEB_SEARCH); | 50 RecognizerIntent.ACTION_WEB_SEARCH); |
| 45 | 51 |
| 46 mTestAccount = AccountManagerHelper.createAccountFromName("Dummy"); | 52 mTestAccount = AccountManagerHelper.createAccountFromName("Dummy"); |
| 47 } | 53 } |
| 48 | 54 |
| 49 @Override | 55 @Before |
| 50 public void setUp() { | 56 public void setUp() { |
| 51 // GetInstrumentation().getTargetContext() cannot be called in | 57 // GetInstrumentation().getTargetContext() cannot be called in |
| 52 // constructor due to external dependencies. | 58 // constructor due to external dependencies. |
| 53 mAccountTestingContext = new AdvancedMockContext( | 59 mAccountTestingContext = new AdvancedMockContext( |
| 54 getInstrumentation().getTargetContext()); | 60 InstrumentationRegistry.getInstrumentation().getTargetContext())
; |
| 55 } | 61 } |
| 56 | 62 |
| 57 private static class IntentTestPackageManager extends MockPackageManager { | 63 private static class IntentTestPackageManager extends MockPackageManager { |
| 58 | 64 |
| 59 private final String mAction; | 65 private final String mAction; |
| 60 | 66 |
| 61 public IntentTestPackageManager(String recognizesAction) { | 67 public IntentTestPackageManager(String recognizesAction) { |
| 62 super(); | 68 super(); |
| 63 mAction = recognizesAction; | 69 mAction = recognizesAction; |
| 64 } | 70 } |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 @Override | 138 @Override |
| 133 public Boolean call() { | 139 public Boolean call() { |
| 134 return FeatureUtilities.isRecognitionIntentPresent( | 140 return FeatureUtilities.isRecognitionIntentPresent( |
| 135 context, | 141 context, |
| 136 useCachedResult); | 142 useCachedResult); |
| 137 } | 143 } |
| 138 }); | 144 }); |
| 139 } | 145 } |
| 140 | 146 |
| 141 private void setUpAccount(final String accountType) { | 147 private void setUpAccount(final String accountType) { |
| 142 mAccountManager = new MockAuthenticationAccountManager( | 148 mAccountManager = new MockAuthenticationAccountManager(mAccountTestingCo
ntext, |
| 143 mAccountTestingContext, | 149 InstrumentationRegistry.getInstrumentation().getContext(), accou
ntType, |
| 144 getInstrumentation().getContext(), | |
| 145 accountType, | |
| 146 mTestAccount); | 150 mTestAccount); |
| 147 | 151 |
| 148 AccountManagerHelper.overrideAccountManagerHelperForTests( | 152 AccountManagerHelper.overrideAccountManagerHelperForTests( |
| 149 mAccountTestingContext, | 153 mAccountTestingContext, |
| 150 mAccountManager); | 154 mAccountManager); |
| 151 } | 155 } |
| 152 | 156 |
| 157 @Test |
| 153 @SmallTest | 158 @SmallTest |
| 154 @Feature({"FeatureUtilities", "Speech"}) | 159 @Feature({"FeatureUtilities", "Speech"}) |
| 155 public void testSpeechFeatureAvailable() { | 160 public void testSpeechFeatureAvailable() { |
| 156 | |
| 157 final boolean doNotUseCachedResult = false; | 161 final boolean doNotUseCachedResult = false; |
| 158 final boolean recognizesSpeech = isRecognitionIntentPresent( | 162 final boolean recognizesSpeech = isRecognitionIntentPresent( |
| 159 mContextWithSpeech, | 163 mContextWithSpeech, |
| 160 doNotUseCachedResult); | 164 doNotUseCachedResult); |
| 161 | 165 |
| 162 assertTrue(recognizesSpeech); | 166 Assert.assertTrue(recognizesSpeech); |
| 163 } | 167 } |
| 164 | 168 |
| 169 @Test |
| 165 @SmallTest | 170 @SmallTest |
| 166 @Feature({"FeatureUtilities", "Speech"}) | 171 @Feature({"FeatureUtilities", "Speech"}) |
| 167 public void testSpeechFeatureUnavailable() { | 172 public void testSpeechFeatureUnavailable() { |
| 168 | |
| 169 final boolean doNotUseCachedResult = false; | 173 final boolean doNotUseCachedResult = false; |
| 170 final boolean recognizesSpeech = isRecognitionIntentPresent( | 174 final boolean recognizesSpeech = isRecognitionIntentPresent( |
| 171 mContextWithoutSpeech, | 175 mContextWithoutSpeech, |
| 172 doNotUseCachedResult); | 176 doNotUseCachedResult); |
| 173 | 177 |
| 174 assertFalse(recognizesSpeech); | 178 Assert.assertFalse(recognizesSpeech); |
| 175 } | 179 } |
| 176 | 180 |
| 181 @Test |
| 177 @SmallTest | 182 @SmallTest |
| 178 @Feature({"FeatureUtilities", "Speech"}) | 183 @Feature({"FeatureUtilities", "Speech"}) |
| 179 public void testCachedSpeechFeatureAvailability() { | 184 public void testCachedSpeechFeatureAvailability() { |
| 180 | |
| 181 // Initial call will cache the fact that speech is recognized. | 185 // Initial call will cache the fact that speech is recognized. |
| 182 final boolean doNotUseCachedResult = false; | 186 final boolean doNotUseCachedResult = false; |
| 183 isRecognitionIntentPresent( | 187 isRecognitionIntentPresent( |
| 184 mContextWithSpeech, | 188 mContextWithSpeech, |
| 185 doNotUseCachedResult); | 189 doNotUseCachedResult); |
| 186 | 190 |
| 187 // Pass a context that does not recognize speech, but use cached result | 191 // Pass a context that does not recognize speech, but use cached result |
| 188 // which does recognize speech. | 192 // which does recognize speech. |
| 189 final boolean useCachedResult = true; | 193 final boolean useCachedResult = true; |
| 190 final boolean recognizesSpeech = isRecognitionIntentPresent( | 194 final boolean recognizesSpeech = isRecognitionIntentPresent( |
| 191 mContextWithoutSpeech, | 195 mContextWithoutSpeech, |
| 192 useCachedResult); | 196 useCachedResult); |
| 193 | 197 |
| 194 // Check that we still recognize speech as we're using cached result. | 198 // Check that we still recognize speech as we're using cached result. |
| 195 assertTrue(recognizesSpeech); | 199 Assert.assertTrue(recognizesSpeech); |
| 196 | 200 |
| 197 // Check if we can turn cached result off again. | 201 // Check if we can turn cached result off again. |
| 198 final boolean RecognizesSpeechUncached = isRecognitionIntentPresent( | 202 final boolean RecognizesSpeechUncached = isRecognitionIntentPresent( |
| 199 mContextWithoutSpeech, | 203 mContextWithoutSpeech, |
| 200 doNotUseCachedResult); | 204 doNotUseCachedResult); |
| 201 | 205 |
| 202 assertFalse(RecognizesSpeechUncached); | 206 Assert.assertFalse(RecognizesSpeechUncached); |
| 203 } | 207 } |
| 204 | 208 |
| 205 // This test previously flaked on the try bot: http://crbug.com/543160. | 209 // This test previously flaked on the try bot: http://crbug.com/543160. |
| 206 // Re-enabling this test since there has been related cleanup/refactoring | 210 // Re-enabling this test since there has been related cleanup/refactoring |
| 207 // during the time the test was disabled. If the test starts flaking again, | 211 // during the time the test was disabled. If the test starts flaking again, |
| 208 // re-open the bug. | 212 // re-open the bug. |
| 209 // TODO(nyquist): Remove this if the test is not flaky anymore. | 213 // TODO(nyquist): Remove this if the test is not flaky anymore. |
| 214 @Test |
| 210 @SmallTest | 215 @SmallTest |
| 211 @Feature({"FeatureUtilities", "GoogleAccounts"}) | 216 @Feature({"FeatureUtilities", "GoogleAccounts"}) |
| 212 public void testHasGoogleAccountCorrectlyDetected() { | 217 public void testHasGoogleAccountCorrectlyDetected() { |
| 213 | |
| 214 // Set up an account manager mock that returns Google account types | 218 // Set up an account manager mock that returns Google account types |
| 215 // when queried. | 219 // when queried. |
| 216 setUpAccount(AccountManagerHelper.GOOGLE_ACCOUNT_TYPE); | 220 setUpAccount(AccountManagerHelper.GOOGLE_ACCOUNT_TYPE); |
| 217 | 221 |
| 218 boolean hasAccounts = FeatureUtilities.hasGoogleAccounts( | 222 boolean hasAccounts = FeatureUtilities.hasGoogleAccounts( |
| 219 mAccountTestingContext); | 223 mAccountTestingContext); |
| 220 | 224 |
| 221 assertTrue(hasAccounts); | 225 Assert.assertTrue(hasAccounts); |
| 222 | 226 |
| 223 boolean hasAuthenticator = FeatureUtilities.hasGoogleAccountAuthenticato
r( | 227 boolean hasAuthenticator = FeatureUtilities.hasGoogleAccountAuthenticato
r( |
| 224 mAccountTestingContext); | 228 mAccountTestingContext); |
| 225 | 229 |
| 226 assertTrue(hasAuthenticator); | 230 Assert.assertTrue(hasAuthenticator); |
| 227 } | 231 } |
| 228 | 232 |
| 229 // This test previously flaked on the try bot: http://crbug.com/543160. | 233 // This test previously flaked on the try bot: http://crbug.com/543160. |
| 230 // Re-enabling this test since there has been related cleanup/refactoring | 234 // Re-enabling this test since there has been related cleanup/refactoring |
| 231 // during the time the test was disabled. If the test starts flaking again, | 235 // during the time the test was disabled. If the test starts flaking again, |
| 232 // re-open the bug. | 236 // re-open the bug. |
| 233 // TODO(nyquist): Remove this if the test is not flaky anymore. | 237 // TODO(nyquist): Remove this if the test is not flaky anymore. |
| 238 @Test |
| 234 @SmallTest | 239 @SmallTest |
| 235 @Feature({"FeatureUtilities", "GoogleAccounts"}) | 240 @Feature({"FeatureUtilities", "GoogleAccounts"}) |
| 236 public void testHasNoGoogleAccountCorrectlyDetected() { | 241 public void testHasNoGoogleAccountCorrectlyDetected() { |
| 237 | |
| 238 // Set up an account manager mock that doesn't return Google account | 242 // Set up an account manager mock that doesn't return Google account |
| 239 // types when queried. | 243 // types when queried. |
| 240 setUpAccount("Not A Google Account"); | 244 setUpAccount("Not A Google Account"); |
| 241 | 245 |
| 242 boolean hasAccounts = FeatureUtilities.hasGoogleAccounts( | 246 boolean hasAccounts = FeatureUtilities.hasGoogleAccounts( |
| 243 mAccountTestingContext); | 247 mAccountTestingContext); |
| 244 | 248 |
| 245 assertFalse(hasAccounts); | 249 Assert.assertFalse(hasAccounts); |
| 246 | 250 |
| 247 boolean hasAuthenticator = FeatureUtilities.hasGoogleAccountAuthenticato
r( | 251 boolean hasAuthenticator = FeatureUtilities.hasGoogleAccountAuthenticato
r( |
| 248 mAccountTestingContext); | 252 mAccountTestingContext); |
| 249 | 253 |
| 250 assertFalse(hasAuthenticator); | 254 Assert.assertFalse(hasAuthenticator); |
| 251 } | 255 } |
| 252 } | 256 } |
| OLD | NEW |