| 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 #include "base/command_line.h" | 5 #include "base/command_line.h" |
| 6 #include "base/memory/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
| 7 #include "base/metrics/field_trial.h" | 7 #include "base/metrics/field_trial.h" |
| 8 #include "base/prefs/pref_service.h" | 8 #include "base/prefs/pref_service.h" |
| 9 #include "base/test/test_simple_task_runner.h" | 9 #include "base/test/test_simple_task_runner.h" |
| 10 #include "chrome/browser/browser_process.h" | 10 #include "chrome/browser/browser_process.h" |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 151 base::FieldTrialList field_trial_list_; | 151 base::FieldTrialList field_trial_list_; |
| 152 std::string extension_id_; | 152 std::string extension_id_; |
| 153 }; | 153 }; |
| 154 | 154 |
| 155 INSTANTIATE_TEST_CASE_P(HotwordServiceTests, | 155 INSTANTIATE_TEST_CASE_P(HotwordServiceTests, |
| 156 HotwordServiceTest, | 156 HotwordServiceTest, |
| 157 ::testing::Values( | 157 ::testing::Values( |
| 158 extension_misc::kHotwordSharedModuleId)); | 158 extension_misc::kHotwordSharedModuleId)); |
| 159 | 159 |
| 160 TEST_P(HotwordServiceTest, IsHotwordAllowedDisabledFieldTrial) { | 160 TEST_P(HotwordServiceTest, IsHotwordAllowedDisabledFieldTrial) { |
| 161 #if defined(ENABLE_HOTWORDING) |
| 161 TestingProfile::Builder profile_builder; | 162 TestingProfile::Builder profile_builder; |
| 162 scoped_ptr<TestingProfile> profile = profile_builder.Build(); | 163 scoped_ptr<TestingProfile> profile = profile_builder.Build(); |
| 163 | 164 |
| 164 // Check that the service exists so that a NULL service be ruled out in | 165 // Check that the service exists so that a NULL service be ruled out in |
| 165 // following tests. | 166 // following tests. |
| 166 HotwordService* hotword_service = | 167 HotwordService* hotword_service = |
| 167 HotwordServiceFactory::GetForProfile(profile.get()); | 168 HotwordServiceFactory::GetForProfile(profile.get()); |
| 168 EXPECT_TRUE(hotword_service != NULL); | 169 EXPECT_TRUE(hotword_service != NULL); |
| 169 | 170 |
| 170 // When the field trial is empty, it should be allowed. | 171 // When the field trial is empty, it should be allowed. |
| (...skipping 12 matching lines...) Expand all Loading... |
| 183 EXPECT_FALSE(HotwordServiceFactory::IsHotwordAllowed(profile.get())); | 184 EXPECT_FALSE(HotwordServiceFactory::IsHotwordAllowed(profile.get())); |
| 184 | 185 |
| 185 // Set a valid locale with invalid field trial to be sure it is | 186 // Set a valid locale with invalid field trial to be sure it is |
| 186 // still false. | 187 // still false. |
| 187 SetApplicationLocale(static_cast<Profile*>(profile.get()), "en"); | 188 SetApplicationLocale(static_cast<Profile*>(profile.get()), "en"); |
| 188 EXPECT_FALSE(HotwordServiceFactory::IsHotwordAllowed(profile.get())); | 189 EXPECT_FALSE(HotwordServiceFactory::IsHotwordAllowed(profile.get())); |
| 189 | 190 |
| 190 // Test that incognito returns false as well. | 191 // Test that incognito returns false as well. |
| 191 EXPECT_FALSE(HotwordServiceFactory::IsHotwordAllowed( | 192 EXPECT_FALSE(HotwordServiceFactory::IsHotwordAllowed( |
| 192 profile->GetOffTheRecordProfile())); | 193 profile->GetOffTheRecordProfile())); |
| 194 #endif |
| 193 } | 195 } |
| 194 | 196 |
| 195 TEST_P(HotwordServiceTest, IsHotwordAllowedInvalidFieldTrial) { | 197 TEST_P(HotwordServiceTest, IsHotwordAllowedInvalidFieldTrial) { |
| 198 #if defined(ENABLE_HOTWORDING) |
| 196 TestingProfile::Builder profile_builder; | 199 TestingProfile::Builder profile_builder; |
| 197 scoped_ptr<TestingProfile> profile = profile_builder.Build(); | 200 scoped_ptr<TestingProfile> profile = profile_builder.Build(); |
| 198 | 201 |
| 199 // Check that the service exists so that a NULL service be ruled out in | 202 // Check that the service exists so that a NULL service be ruled out in |
| 200 // following tests. | 203 // following tests. |
| 201 HotwordService* hotword_service = | 204 HotwordService* hotword_service = |
| 202 HotwordServiceFactory::GetForProfile(profile.get()); | 205 HotwordServiceFactory::GetForProfile(profile.get()); |
| 203 EXPECT_TRUE(hotword_service != NULL); | 206 EXPECT_TRUE(hotword_service != NULL); |
| 204 | 207 |
| 205 // When the field trial is set, but not 'Disabled', it should be allowed. | 208 // When the field trial is set, but not 'Disabled', it should be allowed. |
| 206 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial( | 209 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial( |
| 207 hotword_internal::kHotwordFieldTrialName, "foo")); | 210 hotword_internal::kHotwordFieldTrialName, "foo")); |
| 208 std::string group = base::FieldTrialList::FindFullName( | 211 std::string group = base::FieldTrialList::FindFullName( |
| 209 hotword_internal::kHotwordFieldTrialName); | 212 hotword_internal::kHotwordFieldTrialName); |
| 210 EXPECT_TRUE(group == "foo"); | 213 EXPECT_TRUE(group == "foo"); |
| 211 EXPECT_TRUE(HotwordServiceFactory::IsHotwordAllowed(profile.get())); | 214 EXPECT_TRUE(HotwordServiceFactory::IsHotwordAllowed(profile.get())); |
| 212 | 215 |
| 213 // Test that incognito returns false as well. | 216 // Test that incognito returns false as well. |
| 214 EXPECT_FALSE(HotwordServiceFactory::IsHotwordAllowed( | 217 EXPECT_FALSE(HotwordServiceFactory::IsHotwordAllowed( |
| 215 profile->GetOffTheRecordProfile())); | 218 profile->GetOffTheRecordProfile())); |
| 219 #endif |
| 216 } | 220 } |
| 217 | 221 |
| 218 TEST_P(HotwordServiceTest, IsHotwordAllowedLocale) { | 222 TEST_P(HotwordServiceTest, IsHotwordAllowedLocale) { |
| 223 #if defined(ENABLE_HOTWORDING) |
| 219 TestingProfile::Builder profile_builder; | 224 TestingProfile::Builder profile_builder; |
| 220 scoped_ptr<TestingProfile> profile = profile_builder.Build(); | 225 scoped_ptr<TestingProfile> profile = profile_builder.Build(); |
| 221 | 226 |
| 222 // Check that the service exists so that a NULL service be ruled out in | 227 // Check that the service exists so that a NULL service be ruled out in |
| 223 // following tests. | 228 // following tests. |
| 224 HotwordService* hotword_service = | 229 HotwordService* hotword_service = |
| 225 HotwordServiceFactory::GetForProfile(profile.get()); | 230 HotwordServiceFactory::GetForProfile(profile.get()); |
| 226 EXPECT_TRUE(hotword_service != NULL); | 231 EXPECT_TRUE(hotword_service != NULL); |
| 227 | 232 |
| 228 // Set the language to an invalid one. | 233 // Set the language to an invalid one. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 239 SetApplicationLocale(static_cast<Profile*>(profile.get()), "de_DE"); | 244 SetApplicationLocale(static_cast<Profile*>(profile.get()), "de_DE"); |
| 240 EXPECT_TRUE(HotwordServiceFactory::IsHotwordAllowed(profile.get())); | 245 EXPECT_TRUE(HotwordServiceFactory::IsHotwordAllowed(profile.get())); |
| 241 SetApplicationLocale(static_cast<Profile*>(profile.get()), "fr_fr"); | 246 SetApplicationLocale(static_cast<Profile*>(profile.get()), "fr_fr"); |
| 242 EXPECT_TRUE(HotwordServiceFactory::IsHotwordAllowed(profile.get())); | 247 EXPECT_TRUE(HotwordServiceFactory::IsHotwordAllowed(profile.get())); |
| 243 | 248 |
| 244 // Test that incognito even with a valid locale and valid field trial | 249 // Test that incognito even with a valid locale and valid field trial |
| 245 // still returns false. | 250 // still returns false. |
| 246 Profile* otr_profile = profile->GetOffTheRecordProfile(); | 251 Profile* otr_profile = profile->GetOffTheRecordProfile(); |
| 247 SetApplicationLocale(otr_profile, "en"); | 252 SetApplicationLocale(otr_profile, "en"); |
| 248 EXPECT_FALSE(HotwordServiceFactory::IsHotwordAllowed(otr_profile)); | 253 EXPECT_FALSE(HotwordServiceFactory::IsHotwordAllowed(otr_profile)); |
| 254 #endif // defined(ENABLE_HOTWORDING) |
| 249 } | 255 } |
| 250 | 256 |
| 251 TEST_P(HotwordServiceTest, ShouldReinstallExtension) { | 257 TEST_P(HotwordServiceTest, ShouldReinstallExtension) { |
| 252 InitializeEmptyExtensionService(); | 258 InitializeEmptyExtensionService(); |
| 253 | 259 |
| 254 HotwordServiceFactory* hotword_service_factory = | 260 HotwordServiceFactory* hotword_service_factory = |
| 255 HotwordServiceFactory::GetInstance(); | 261 HotwordServiceFactory::GetInstance(); |
| 256 | 262 |
| 257 MockHotwordService* hotword_service = static_cast<MockHotwordService*>( | 263 MockHotwordService* hotword_service = static_cast<MockHotwordService*>( |
| 258 hotword_service_factory->SetTestingFactoryAndUse( | 264 hotword_service_factory->SetTestingFactoryAndUse( |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 295 SetApplicationLocale(profile(), "test_locale"); | 301 SetApplicationLocale(profile(), "test_locale"); |
| 296 | 302 |
| 297 hotword_service->InstallHotwordExtensionFromWebstore(1); | 303 hotword_service->InstallHotwordExtensionFromWebstore(1); |
| 298 base::MessageLoop::current()->RunUntilIdle(); | 304 base::MessageLoop::current()->RunUntilIdle(); |
| 299 | 305 |
| 300 EXPECT_EQ("test_locale", | 306 EXPECT_EQ("test_locale", |
| 301 profile()->GetPrefs()->GetString(prefs::kHotwordPreviousLanguage)); | 307 profile()->GetPrefs()->GetString(prefs::kHotwordPreviousLanguage)); |
| 302 } | 308 } |
| 303 | 309 |
| 304 TEST_P(HotwordServiceTest, UninstallReinstallTriggeredCorrectly) { | 310 TEST_P(HotwordServiceTest, UninstallReinstallTriggeredCorrectly) { |
| 311 #if defined(ENABLE_HOTWORDING) |
| 305 InitializeEmptyExtensionService(); | 312 InitializeEmptyExtensionService(); |
| 306 service_->Init(); | 313 service_->Init(); |
| 307 | 314 |
| 308 HotwordServiceFactory* hotword_service_factory = | 315 HotwordServiceFactory* hotword_service_factory = |
| 309 HotwordServiceFactory::GetInstance(); | 316 HotwordServiceFactory::GetInstance(); |
| 310 | 317 |
| 311 MockHotwordService* hotword_service = static_cast<MockHotwordService*>( | 318 MockHotwordService* hotword_service = static_cast<MockHotwordService*>( |
| 312 hotword_service_factory->SetTestingFactoryAndUse( | 319 hotword_service_factory->SetTestingFactoryAndUse( |
| 313 profile(), BuildMockHotwordService)); | 320 profile(), BuildMockHotwordService)); |
| 314 ASSERT_TRUE(hotword_service != NULL); | 321 ASSERT_TRUE(hotword_service != NULL); |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 365 EXPECT_FALSE(hotword_service->MaybeReinstallHotwordExtension()); | 372 EXPECT_FALSE(hotword_service->MaybeReinstallHotwordExtension()); |
| 366 EXPECT_EQ("fr_fr", | 373 EXPECT_EQ("fr_fr", |
| 367 profile()->GetPrefs()->GetString(prefs::kHotwordPreviousLanguage)); | 374 profile()->GetPrefs()->GetString(prefs::kHotwordPreviousLanguage)); |
| 368 | 375 |
| 369 // If the locale is set back to the last valid one, then an uninstall-install | 376 // If the locale is set back to the last valid one, then an uninstall-install |
| 370 // shouldn't be needed. | 377 // shouldn't be needed. |
| 371 SetApplicationLocale(profile(), "fr_fr"); | 378 SetApplicationLocale(profile(), "fr_fr"); |
| 372 EXPECT_TRUE(HotwordServiceFactory::IsHotwordAllowed(profile())); | 379 EXPECT_TRUE(HotwordServiceFactory::IsHotwordAllowed(profile())); |
| 373 EXPECT_FALSE(hotword_service->MaybeReinstallHotwordExtension()); | 380 EXPECT_FALSE(hotword_service->MaybeReinstallHotwordExtension()); |
| 374 EXPECT_EQ(1, hotword_service->uninstall_count()); // no change | 381 EXPECT_EQ(1, hotword_service->uninstall_count()); // no change |
| 382 #endif // defined(ENABLE_HOTWORDING) |
| 375 } | 383 } |
| 376 | 384 |
| 377 TEST_P(HotwordServiceTest, DisableAlwaysOnOnLanguageChange) { | 385 TEST_P(HotwordServiceTest, DisableAlwaysOnOnLanguageChange) { |
| 378 // Bypass test for old hotwording. | 386 // Bypass test for old hotwording. |
| 379 if (extension_id_ != extension_misc::kHotwordSharedModuleId) | 387 if (extension_id_ != extension_misc::kHotwordSharedModuleId) |
| 380 return; | 388 return; |
| 381 | 389 |
| 382 // Turn on Always On | 390 // Turn on Always On |
| 383 base::CommandLine::ForCurrentProcess()->AppendSwitch( | 391 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 384 switches::kEnableExperimentalHotwordHardware); | 392 switches::kEnableExperimentalHotwordHardware); |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 554 EXPECT_EQ(1, audio_history_handler->GetAudioHistoryCalls()); | 562 EXPECT_EQ(1, audio_history_handler->GetAudioHistoryCalls()); |
| 555 // We expect the next check for audio history to be in the queue. | 563 // We expect the next check for audio history to be in the queue. |
| 556 EXPECT_EQ(base::TimeDelta::FromDays(1), | 564 EXPECT_EQ(base::TimeDelta::FromDays(1), |
| 557 test_task_runner->NextPendingTaskDelay()); | 565 test_task_runner->NextPendingTaskDelay()); |
| 558 EXPECT_TRUE(test_task_runner->HasPendingTask()); | 566 EXPECT_TRUE(test_task_runner->HasPendingTask()); |
| 559 test_task_runner->RunPendingTasks(); | 567 test_task_runner->RunPendingTasks(); |
| 560 EXPECT_EQ(2, audio_history_handler->GetAudioHistoryCalls()); | 568 EXPECT_EQ(2, audio_history_handler->GetAudioHistoryCalls()); |
| 561 EXPECT_TRUE(test_task_runner->HasPendingTask()); | 569 EXPECT_TRUE(test_task_runner->HasPendingTask()); |
| 562 test_task_runner->ClearPendingTasks(); | 570 test_task_runner->ClearPendingTasks(); |
| 563 } | 571 } |
| OLD | NEW |