Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(178)

Side by Side Diff: chrome/browser/search/hotword_service_unittest.cc

Issue 330193005: [Hotword] Uninstall and reinstall the extension upon language change. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: more cleanup Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/memory/scoped_ptr.h"
5 #include "base/metrics/field_trial.h" 6 #include "base/metrics/field_trial.h"
6 #include "base/prefs/pref_service.h" 7 #include "base/prefs/pref_service.h"
7 #include "chrome/browser/browser_process.h" 8 #include "chrome/browser/browser_process.h"
9 #include "chrome/browser/extensions/extension_service_test_base.h"
10 #include "chrome/browser/extensions/test_extension_service.h"
8 #include "chrome/browser/search/hotword_service.h" 11 #include "chrome/browser/search/hotword_service.h"
9 #include "chrome/browser/search/hotword_service_factory.h" 12 #include "chrome/browser/search/hotword_service_factory.h"
13 #include "chrome/common/extensions/extension_constants.h"
10 #include "chrome/common/pref_names.h" 14 #include "chrome/common/pref_names.h"
11 #include "chrome/test/base/testing_profile.h" 15 #include "chrome/test/base/testing_profile.h"
12 #include "content/public/test/test_browser_thread_bundle.h" 16 #include "content/public/test/test_browser_thread_bundle.h"
17 #include "extensions/browser/extension_system.h"
18 #include "extensions/common/extension.h"
19 #include "extensions/common/extension_builder.h"
20 #include "extensions/common/manifest.h"
21 #include "extensions/common/one_shot_event.h"
13 #include "testing/gtest/include/gtest/gtest.h" 22 #include "testing/gtest/include/gtest/gtest.h"
14 23
15 class HotwordServiceTest : public testing::Test { 24 namespace {
25
26 class MockHotwordService : public HotwordService {
27 public:
28 explicit MockHotwordService(Profile* profile)
29 : HotwordService(profile),
30 installed_(false),
31 uninstall_count_(0) {
32 }
33
34 bool UninstallHotwordExtension(ExtensionService* extension_service) OVERRIDE {
35 uninstall_count_++;
36 return HotwordService::UninstallHotwordExtension(extension_service);
37 }
38
39 void InstallHotwordExtensionFromWebstore() {
40 scoped_ptr<base::DictionaryValue> manifest =
41 extensions::DictionaryBuilder()
42 .Set("name", "Hotword Test Extension")
43 .Set("version", "1.0")
44 .Set("manifest_version", 2)
45 .Build();
46 scoped_refptr<extensions::Extension> extension =
47 extensions::ExtensionBuilder().SetManifest(manifest.Pass())
48 .AddFlags(extensions::Extension::FROM_WEBSTORE
49 | extensions::Extension::WAS_INSTALLED_BY_DEFAULT)
50 .SetID(extension_misc::kHotwordExtensionId)
51 .SetLocation(extensions::Manifest::EXTERNAL_COMPONENT)
52 .Build();
53 ASSERT_TRUE(extension.get());
54 service_->OnExtensionInstalled(extension, syncer::StringOrdinal());
55
56 }
57
58
59 int uninstall_count() { return uninstall_count_; }
60
61 void SetExtensionService(ExtensionService* service) { service_ = service; }
62
63 ExtensionService* extension_service() { return service_; }
64
65 private:
66 ExtensionService* service_;
67 bool installed_;
68 int uninstall_count_;
69 };
70
71 KeyedService* BuildMockHotwordService(content::BrowserContext* context) {
72 return new MockHotwordService(static_cast<Profile*>(context));
73 }
74
75 } // namespace
76
77 class HotwordServiceTest : public extensions::ExtensionServiceTestBase {
16 protected: 78 protected:
17 HotwordServiceTest() : field_trial_list_(NULL) {} 79 HotwordServiceTest() : field_trial_list_(NULL) {}
18 virtual ~HotwordServiceTest() {} 80 virtual ~HotwordServiceTest() {}
19 81
20 void SetApplicationLocale(Profile* profile, const std::string& new_locale) { 82 void SetApplicationLocale(Profile* profile, const std::string& new_locale) {
21 #if defined(OS_CHROMEOS) 83 #if defined(OS_CHROMEOS)
22 // On ChromeOS locale is per-profile. 84 // On ChromeOS locale is per-profile.
23 profile->GetPrefs()->SetString(prefs::kApplicationLocale, new_locale); 85 profile->GetPrefs()->SetString(prefs::kApplicationLocale, new_locale);
24 #else 86 #else
25 g_browser_process->SetApplicationLocale(new_locale); 87 g_browser_process->SetApplicationLocale(new_locale);
26 #endif 88 #endif
27 } 89 }
28 90
29 private: 91 private:
30 base::FieldTrialList field_trial_list_; 92 base::FieldTrialList field_trial_list_;
31 content::TestBrowserThreadBundle thread_bundle_;
32 }; 93 };
33 94
34 TEST_F(HotwordServiceTest, IsHotwordAllowedBadFieldTrial) { 95 TEST_F(HotwordServiceTest, IsHotwordAllowedBadFieldTrial) {
35 TestingProfile::Builder profile_builder; 96 TestingProfile::Builder profile_builder;
36 TestingProfile::Builder otr_profile_builder; 97 TestingProfile::Builder otr_profile_builder;
37 otr_profile_builder.SetIncognito(); 98 otr_profile_builder.SetIncognito();
38 scoped_ptr<TestingProfile> profile = profile_builder.Build(); 99 scoped_ptr<TestingProfile> profile = profile_builder.Build();
39 scoped_ptr<TestingProfile> otr_profile = otr_profile_builder.Build(); 100 scoped_ptr<TestingProfile> otr_profile = otr_profile_builder.Build();
40 101
41 HotwordServiceFactory* hotword_service_factory = 102 HotwordServiceFactory* hotword_service_factory =
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
119 HotwordServiceFactory* hotword_service_factory = 180 HotwordServiceFactory* hotword_service_factory =
120 HotwordServiceFactory::GetInstance(); 181 HotwordServiceFactory::GetInstance();
121 HotwordService* hotword_service = 182 HotwordService* hotword_service =
122 hotword_service_factory->GetForProfile(profile.get()); 183 hotword_service_factory->GetForProfile(profile.get());
123 EXPECT_TRUE(hotword_service != NULL); 184 EXPECT_TRUE(hotword_service != NULL);
124 185
125 // If it's a fresh profile, although the default value is true, 186 // If it's a fresh profile, although the default value is true,
126 // it should return false if the preference has never been set. 187 // it should return false if the preference has never been set.
127 EXPECT_FALSE(hotword_service->IsOptedIntoAudioLogging()); 188 EXPECT_FALSE(hotword_service->IsOptedIntoAudioLogging());
128 } 189 }
190
191 TEST_F(HotwordServiceTest, ShouldUninstallExtension) {
192 // Set the field trial to a valid one.
193 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
194 hotword_internal::kHotwordFieldTrialName, "Install"));
195
196 InitializeEmptyExtensionService();
197
198 HotwordServiceFactory* hotword_service_factory =
199 HotwordServiceFactory::GetInstance();
200
201 MockHotwordService* hotword_service = static_cast<MockHotwordService*>(
202 hotword_service_factory->SetTestingFactoryAndUse(
203 profile(), BuildMockHotwordService));
204 EXPECT_TRUE(hotword_service != NULL);
205
206 // If no locale has been set, no reason to uninstall.
207 EXPECT_FALSE(hotword_service->ShouldUninstallHotwordExtension());
208
209 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.
210 hotword_service->SetPreviousLocalePref();
211
212 // Now a locale is set, but it hasn't changed.
213 EXPECT_FALSE(hotword_service->ShouldUninstallHotwordExtension());
214
215 SetApplicationLocale(static_cast<Profile*>(profile()), "fr_fr");
216
217 // Now it's a different locale so it should uninstall.
218 EXPECT_TRUE(hotword_service->ShouldUninstallHotwordExtension());
219 }
220
221 TEST_F(HotwordServiceTest, PreviousLanguageSetOnInstall) {
222 // Set the field trial to a valid one.
223 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
224 hotword_internal::kHotwordFieldTrialName, "Install"));
225
226 InitializeEmptyExtensionService();
227 service_->Init();
228
229 HotwordServiceFactory* hotword_service_factory =
230 HotwordServiceFactory::GetInstance();
231
232 MockHotwordService* hotword_service = static_cast<MockHotwordService*>(
233 hotword_service_factory->SetTestingFactoryAndUse(
234 profile(), BuildMockHotwordService));
235 EXPECT_TRUE(hotword_service != NULL);
236 hotword_service->SetExtensionService(service());
237
238 // If no locale has been set, no reason to uninstall.
239 EXPECT_FALSE(hotword_service->ShouldUninstallHotwordExtension());
240
241 SetApplicationLocale(static_cast<Profile*>(profile()), "test_locale");
242
243 hotword_service->InstallHotwordExtensionFromWebstore();
244 base::MessageLoop::current()->RunUntilIdle();
245
246 EXPECT_EQ("test_locale",
247 profile()->GetPrefs()->GetString(prefs::kHotwordPreviousLanguage));
248 }
249
250 TEST_F(HotwordServiceTest, UninstallReinstallTriggeredCorrectly) {
251 // Set the field trial to a valid one.
252 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
253 hotword_internal::kHotwordFieldTrialName, "Install"));
254
255 InitializeEmptyExtensionService();
256 service_->Init();
257
258 HotwordServiceFactory* hotword_service_factory =
259 HotwordServiceFactory::GetInstance();
260
261 MockHotwordService* hotword_service = static_cast<MockHotwordService*>(
262 hotword_service_factory->SetTestingFactoryAndUse(
263 profile(), BuildMockHotwordService));
264 EXPECT_TRUE(hotword_service != NULL);
265 hotword_service->SetExtensionService(service());
266
267 // Initialize the locale to "en".
268 SetApplicationLocale(static_cast<Profile*>(profile()), "en");
269
270 // 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.
271 EXPECT_FALSE(hotword_service->MaybeUninstallHotwordExtension());
272
273 // Do an initial installation.
274 hotword_service->InstallHotwordExtensionFromWebstore();
275 base::MessageLoop::current()->RunUntilIdle();
276 EXPECT_EQ("en",
277 profile()->GetPrefs()->GetString(prefs::kHotwordPreviousLanguage));
278
279 // Hotwording extension is disabled on install.
280 EXPECT_EQ(1U, registry()->disabled_extensions().size());
281
282 // Verify the extension is installed.
283 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.
284 extension_misc::kHotwordExtensionId, true) != NULL);
285
286 // The previous locale should be set but should match the current
287 // locale.. No reason to uininstall.
Yoyo Zhou 2014/06/18 00:33:22 (same typo)
rpetterson 2014/06/18 02:08:44 Done.
288 EXPECT_FALSE(hotword_service->MaybeUninstallHotwordExtension());
289
290 // Switch the locale to a valid but different one.
291 SetApplicationLocale(profile(), "fr_fr");
292 EXPECT_TRUE(HotwordServiceFactory::IsHotwordAllowed(profile()));
293
294 // Different but valid locale so expect uninstall.
295 EXPECT_TRUE(hotword_service->MaybeUninstallHotwordExtension());
296 EXPECT_EQ(1, hotword_service->uninstall_count());
297 EXPECT_EQ("fr_fr",
298 profile()->GetPrefs()->GetString(prefs::kHotwordPreviousLanguage));
299
300 // Verify the extension is installed.
301 EXPECT_TRUE(service()->GetExtensionById(
302 extension_misc::kHotwordExtensionId, true) != NULL);
303
304 // Switch the locale to an invalid one.
305 SetApplicationLocale(profile(), "invalid");
306 EXPECT_FALSE(HotwordServiceFactory::IsHotwordAllowed(profile()));
307 EXPECT_FALSE(hotword_service->MaybeUninstallHotwordExtension());
308 EXPECT_EQ("fr_fr",
309 profile()->GetPrefs()->GetString(prefs::kHotwordPreviousLanguage));
310
311 // If the locale is set back to the last valid one, then an uninstall-install
312 // shouldn't be needed.
313 SetApplicationLocale(profile(), "fr_fr");
314 EXPECT_TRUE(HotwordServiceFactory::IsHotwordAllowed(profile()));
315 EXPECT_FALSE(hotword_service->MaybeUninstallHotwordExtension());
316 EXPECT_EQ(1, hotword_service->uninstall_count()); // no change
317 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698