Index: chrome/browser/search/hotword_service_unittest.cc |
diff --git a/chrome/browser/search/hotword_service_unittest.cc b/chrome/browser/search/hotword_service_unittest.cc |
index 91707410748686ef3346c4088a270057a23121a0..c5599519223182d09528f8b727ee86c3350e3553 100644 |
--- a/chrome/browser/search/hotword_service_unittest.cc |
+++ b/chrome/browser/search/hotword_service_unittest.cc |
@@ -2,17 +2,79 @@ |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
+#include "base/memory/scoped_ptr.h" |
#include "base/metrics/field_trial.h" |
#include "base/prefs/pref_service.h" |
#include "chrome/browser/browser_process.h" |
+#include "chrome/browser/extensions/extension_service_test_base.h" |
+#include "chrome/browser/extensions/test_extension_service.h" |
#include "chrome/browser/search/hotword_service.h" |
#include "chrome/browser/search/hotword_service_factory.h" |
+#include "chrome/common/extensions/extension_constants.h" |
#include "chrome/common/pref_names.h" |
#include "chrome/test/base/testing_profile.h" |
#include "content/public/test/test_browser_thread_bundle.h" |
+#include "extensions/browser/extension_system.h" |
+#include "extensions/common/extension.h" |
+#include "extensions/common/extension_builder.h" |
+#include "extensions/common/manifest.h" |
+#include "extensions/common/one_shot_event.h" |
#include "testing/gtest/include/gtest/gtest.h" |
-class HotwordServiceTest : public testing::Test { |
+namespace { |
+ |
+class MockHotwordService : public HotwordService { |
+ public: |
+ explicit MockHotwordService(Profile* profile) |
+ : HotwordService(profile), |
+ installed_(false), |
+ uninstall_count_(0) { |
+ } |
+ |
+ bool UninstallHotwordExtension(ExtensionService* extension_service) OVERRIDE { |
+ uninstall_count_++; |
+ return HotwordService::UninstallHotwordExtension(extension_service); |
+ } |
+ |
+ void InstallHotwordExtensionFromWebstore() { |
+ scoped_ptr<base::DictionaryValue> manifest = |
+ extensions::DictionaryBuilder() |
+ .Set("name", "Hotword Test Extension") |
+ .Set("version", "1.0") |
+ .Set("manifest_version", 2) |
+ .Build(); |
+ scoped_refptr<extensions::Extension> extension = |
+ extensions::ExtensionBuilder().SetManifest(manifest.Pass()) |
+ .AddFlags(extensions::Extension::FROM_WEBSTORE |
+ | extensions::Extension::WAS_INSTALLED_BY_DEFAULT) |
+ .SetID(extension_misc::kHotwordExtensionId) |
+ .SetLocation(extensions::Manifest::EXTERNAL_COMPONENT) |
+ .Build(); |
+ ASSERT_TRUE(extension.get()); |
+ service_->OnExtensionInstalled(extension, syncer::StringOrdinal()); |
+ |
+ } |
+ |
+ |
+ int uninstall_count() { return uninstall_count_; } |
+ |
+ void SetExtensionService(ExtensionService* service) { service_ = service; } |
+ |
+ ExtensionService* extension_service() { return service_; } |
+ |
+ private: |
+ ExtensionService* service_; |
+ bool installed_; |
+ int uninstall_count_; |
+}; |
+ |
+KeyedService* BuildMockHotwordService(content::BrowserContext* context) { |
+ return new MockHotwordService(static_cast<Profile*>(context)); |
+} |
+ |
+} // namespace |
+ |
+class HotwordServiceTest : public extensions::ExtensionServiceTestBase { |
protected: |
HotwordServiceTest() : field_trial_list_(NULL) {} |
virtual ~HotwordServiceTest() {} |
@@ -28,7 +90,6 @@ class HotwordServiceTest : public testing::Test { |
private: |
base::FieldTrialList field_trial_list_; |
- content::TestBrowserThreadBundle thread_bundle_; |
}; |
TEST_F(HotwordServiceTest, IsHotwordAllowedBadFieldTrial) { |
@@ -126,3 +187,131 @@ TEST_F(HotwordServiceTest, AudioLoggingPrefSetCorrectly) { |
// it should return false if the preference has never been set. |
EXPECT_FALSE(hotword_service->IsOptedIntoAudioLogging()); |
} |
+ |
+TEST_F(HotwordServiceTest, ShouldUninstallExtension) { |
+ // Set the field trial to a valid one. |
+ ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial( |
+ hotword_internal::kHotwordFieldTrialName, "Install")); |
+ |
+ InitializeEmptyExtensionService(); |
+ |
+ HotwordServiceFactory* hotword_service_factory = |
+ HotwordServiceFactory::GetInstance(); |
+ |
+ MockHotwordService* hotword_service = static_cast<MockHotwordService*>( |
+ hotword_service_factory->SetTestingFactoryAndUse( |
+ profile(), BuildMockHotwordService)); |
+ EXPECT_TRUE(hotword_service != NULL); |
+ |
+ // If no locale has been set, no reason to uninstall. |
+ EXPECT_FALSE(hotword_service->ShouldUninstallHotwordExtension()); |
+ |
+ SetApplicationLocale(static_cast<Profile*>(profile()), "en"); |
Yoyo Zhou
2014/06/18 00:33:22
This static cast shouldn't be needed.
rpetterson
2014/06/18 02:08:44
Ah, you're right. Copy/paste . . .
Fixed.
|
+ hotword_service->SetPreviousLocalePref(); |
+ |
+ // Now a locale is set, but it hasn't changed. |
+ EXPECT_FALSE(hotword_service->ShouldUninstallHotwordExtension()); |
+ |
+ SetApplicationLocale(static_cast<Profile*>(profile()), "fr_fr"); |
+ |
+ // Now it's a different locale so it should uninstall. |
+ EXPECT_TRUE(hotword_service->ShouldUninstallHotwordExtension()); |
+} |
+ |
+TEST_F(HotwordServiceTest, PreviousLanguageSetOnInstall) { |
+ // Set the field trial to a valid one. |
+ ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial( |
+ hotword_internal::kHotwordFieldTrialName, "Install")); |
+ |
+ InitializeEmptyExtensionService(); |
+ service_->Init(); |
+ |
+ HotwordServiceFactory* hotword_service_factory = |
+ HotwordServiceFactory::GetInstance(); |
+ |
+ MockHotwordService* hotword_service = static_cast<MockHotwordService*>( |
+ hotword_service_factory->SetTestingFactoryAndUse( |
+ profile(), BuildMockHotwordService)); |
+ EXPECT_TRUE(hotword_service != NULL); |
+ hotword_service->SetExtensionService(service()); |
+ |
+ // If no locale has been set, no reason to uninstall. |
+ EXPECT_FALSE(hotword_service->ShouldUninstallHotwordExtension()); |
+ |
+ SetApplicationLocale(static_cast<Profile*>(profile()), "test_locale"); |
+ |
+ hotword_service->InstallHotwordExtensionFromWebstore(); |
+ base::MessageLoop::current()->RunUntilIdle(); |
+ |
+ EXPECT_EQ("test_locale", |
+ profile()->GetPrefs()->GetString(prefs::kHotwordPreviousLanguage)); |
+} |
+ |
+TEST_F(HotwordServiceTest, UninstallReinstallTriggeredCorrectly) { |
+ // Set the field trial to a valid one. |
+ ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial( |
+ hotword_internal::kHotwordFieldTrialName, "Install")); |
+ |
+ InitializeEmptyExtensionService(); |
+ service_->Init(); |
+ |
+ HotwordServiceFactory* hotword_service_factory = |
+ HotwordServiceFactory::GetInstance(); |
+ |
+ MockHotwordService* hotword_service = static_cast<MockHotwordService*>( |
+ hotword_service_factory->SetTestingFactoryAndUse( |
+ profile(), BuildMockHotwordService)); |
+ EXPECT_TRUE(hotword_service != NULL); |
+ hotword_service->SetExtensionService(service()); |
+ |
+ // Initialize the locale to "en". |
+ SetApplicationLocale(static_cast<Profile*>(profile()), "en"); |
+ |
+ // The previous locale should not be set. No reason to uininstall. |
Yoyo Zhou
2014/06/18 00:33:22
typo: uninstall
rpetterson
2014/06/18 02:08:44
Done.
|
+ EXPECT_FALSE(hotword_service->MaybeUninstallHotwordExtension()); |
+ |
+ // Do an initial installation. |
+ hotword_service->InstallHotwordExtensionFromWebstore(); |
+ base::MessageLoop::current()->RunUntilIdle(); |
+ EXPECT_EQ("en", |
+ profile()->GetPrefs()->GetString(prefs::kHotwordPreviousLanguage)); |
+ |
+ // Hotwording extension is disabled on install. |
+ EXPECT_EQ(1U, registry()->disabled_extensions().size()); |
+ |
+ // Verify the extension is installed. |
+ EXPECT_TRUE(service()->GetExtensionById( |
Yoyo Zhou
2014/06/18 00:33:22
Prefer registry()->enabled_extensions()->Contains(
rpetterson
2014/06/18 02:08:44
Done.
Yoyo Zhou
2014/06/18 02:15:48
Oops, yes, disabled.
|
+ extension_misc::kHotwordExtensionId, true) != NULL); |
+ |
+ // The previous locale should be set but should match the current |
+ // locale.. No reason to uininstall. |
Yoyo Zhou
2014/06/18 00:33:22
(same typo)
rpetterson
2014/06/18 02:08:44
Done.
|
+ EXPECT_FALSE(hotword_service->MaybeUninstallHotwordExtension()); |
+ |
+ // Switch the locale to a valid but different one. |
+ SetApplicationLocale(profile(), "fr_fr"); |
+ EXPECT_TRUE(HotwordServiceFactory::IsHotwordAllowed(profile())); |
+ |
+ // Different but valid locale so expect uninstall. |
+ EXPECT_TRUE(hotword_service->MaybeUninstallHotwordExtension()); |
+ EXPECT_EQ(1, hotword_service->uninstall_count()); |
+ EXPECT_EQ("fr_fr", |
+ profile()->GetPrefs()->GetString(prefs::kHotwordPreviousLanguage)); |
+ |
+ // Verify the extension is installed. |
+ EXPECT_TRUE(service()->GetExtensionById( |
+ extension_misc::kHotwordExtensionId, true) != NULL); |
+ |
+ // Switch the locale to an invalid one. |
+ SetApplicationLocale(profile(), "invalid"); |
+ EXPECT_FALSE(HotwordServiceFactory::IsHotwordAllowed(profile())); |
+ EXPECT_FALSE(hotword_service->MaybeUninstallHotwordExtension()); |
+ EXPECT_EQ("fr_fr", |
+ profile()->GetPrefs()->GetString(prefs::kHotwordPreviousLanguage)); |
+ |
+ // If the locale is set back to the last valid one, then an uninstall-install |
+ // shouldn't be needed. |
+ SetApplicationLocale(profile(), "fr_fr"); |
+ EXPECT_TRUE(HotwordServiceFactory::IsHotwordAllowed(profile())); |
+ EXPECT_FALSE(hotword_service->MaybeUninstallHotwordExtension()); |
+ EXPECT_EQ(1, hotword_service->uninstall_count()); // no change |
+} |