-
Notifications
You must be signed in to change notification settings - Fork 0
/
0001-hotword-disable-at-build-time-by-default.patch
624 lines (616 loc) · 22.8 KB
/
0001-hotword-disable-at-build-time-by-default.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
From 707983af82ee417ee081a57ff06c7a82b33599c5 Mon Sep 17 00:00:00 2001
From: amistry <[email protected]>
Date: Tue, 9 Jun 2015 12:18:39 -0700
Subject: [PATCH] hotword: disable at build time by default
Commit f269d3b548203e217e8c0080c2e22e7ae3efb51e upstream:
"""
Hotwording downloads a shared module from the web store containing a
NaCl module. There is a desire to build and distribute Chromium
without this happening. This change adds an "enable_hotwording" build
flag that is enabled by default, but can be disabled at compile time.
BUG=491435
Review URL: https://codereview.chromium.org/1160243004
Cr-Commit-Position: refs/heads/master@{#333548}
"""
Hard-disable hotwording by default even on a build level.
---
chrome/browser/BUILD.gn | 11 +
chrome/browser/extensions/component_loader.cc | 18 +
chrome/browser/search/hotword_service_unittest.cc | 538 ++++++++++++++++++++++
3 files changed, 567 insertions(+)
create mode 100644 chrome/browser/search/hotword_service_unittest.cc
diff --git a/chrome/browser/BUILD.gn b/chrome/browser/BUILD.gn
index c61e8b60b4c4..a6ba057ddfc2 100644
--- a/chrome/browser/BUILD.gn
+++ b/chrome/browser/BUILD.gn
@@ -3614,6 +3614,17 @@ split_static_library("browser") {
]
}
+ if (false) {
+ defines += [ "ENABLE_HOTWORDING" ]
+ }
+
+ if (enable_library_cdms) {
+ sources += [
+ "component_updater/widevine_cdm_component_installer.cc",
+ "component_updater/widevine_cdm_component_installer.h",
+ ]
+ }
+
if (enable_mdns) {
sources += [
"local_discovery/service_discovery_client_impl.cc",
diff --git a/chrome/browser/extensions/component_loader.cc b/chrome/browser/extensions/component_loader.cc
index aa175db53c4a..9a54366843ab 100644
--- a/chrome/browser/extensions/component_loader.cc
+++ b/chrome/browser/extensions/component_loader.cc
@@ -353,6 +353,24 @@ void ComponentLoader::AddHangoutServicesExtension() {
#endif
}
+void ComponentLoader::AddHotwordAudioVerificationApp() {
+#if 0
+ if (HotwordServiceFactory::IsAlwaysOnAvailable()) {
+ Add(IDR_HOTWORD_AUDIO_VERIFICATION_MANIFEST,
+ base::FilePath(FILE_PATH_LITERAL("hotword_audio_verification")));
+ }
+#endif
+}
+
+void ComponentLoader::AddHotwordHelperExtension() {
+#if 0
+ if (HotwordServiceFactory::IsHotwordAllowed(profile_)) {
+ Add(IDR_HOTWORD_MANIFEST,
+ base::FilePath(FILE_PATH_LITERAL("hotword")));
+ }
+#endif
+}
+
void ComponentLoader::AddImageLoaderExtension() {
#if defined(OS_CHROMEOS)
Add(IDR_IMAGE_LOADER_MANIFEST,
diff --git a/chrome/browser/search/hotword_service_unittest.cc b/chrome/browser/search/hotword_service_unittest.cc
new file mode 100644
index 000000000000..2f1b5a97d1d2
--- /dev/null
+++ b/chrome/browser/search/hotword_service_unittest.cc
@@ -0,0 +1,538 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/search/hotword_service.h"
+
+#include <memory>
+#include <utility>
+
+#include "base/command_line.h"
+#include "base/memory/ptr_util.h"
+#include "base/metrics/field_trial.h"
+#include "base/run_loop.h"
+#include "base/test/test_simple_task_runner.h"
+#include "build/build_config.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_audio_history_handler.h"
+#include "chrome/browser/search/hotword_service_factory.h"
+#include "chrome/common/chrome_switches.h"
+#include "chrome/common/extensions/extension_constants.h"
+#include "chrome/common/pref_names.h"
+#include "chrome/test/base/testing_profile.h"
+#include "components/history/core/browser/web_history_service.h"
+#include "components/prefs/pref_service.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"
+
+#if defined(OS_CHROMEOS)
+#include "chromeos/audio/cras_audio_handler.h"
+#endif
+
+namespace {
+
+class MockAudioHistoryHandler : public HotwordAudioHistoryHandler {
+ public:
+ MockAudioHistoryHandler(
+ content::BrowserContext* context,
+ const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
+ history::WebHistoryService* web_history)
+ : HotwordAudioHistoryHandler(context, task_runner),
+ get_audio_history_calls_(0),
+ web_history_(web_history) {
+ }
+ ~MockAudioHistoryHandler() override {}
+
+ void GetAudioHistoryEnabled(
+ const HotwordAudioHistoryCallback& callback) override {
+ get_audio_history_calls_++;
+ callback.Run(true, true);
+ }
+
+ history::WebHistoryService* GetWebHistory() override {
+ return web_history_.get();
+ }
+
+ int GetAudioHistoryCalls() {
+ return get_audio_history_calls_;
+ }
+
+ private:
+ int get_audio_history_calls_;
+ std::unique_ptr<history::WebHistoryService> web_history_;
+};
+
+class MockHotwordService : public HotwordService {
+ public:
+ explicit MockHotwordService(Profile* profile)
+ : HotwordService(profile),
+ uninstall_count_(0) {
+ }
+
+ bool UninstallHotwordExtension(ExtensionService* extension_service) override {
+ uninstall_count_++;
+ return HotwordService::UninstallHotwordExtension(extension_service);
+ }
+
+ void InstallHotwordExtensionFromWebstore(int num_tries) override {
+ std::unique_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(std::move(manifest))
+ .AddFlags(extensions::Extension::FROM_WEBSTORE |
+ extensions::Extension::WAS_INSTALLED_BY_DEFAULT)
+ .SetID(extension_id_)
+ .SetLocation(extensions::Manifest::EXTERNAL_COMPONENT)
+ .Build();
+ ASSERT_TRUE(extension.get());
+ service_->OnExtensionInstalled(extension.get(), syncer::StringOrdinal());
+ }
+
+
+ int uninstall_count() { return uninstall_count_; }
+
+ void SetExtensionService(ExtensionService* service) { service_ = service; }
+ void SetExtensionId(const std::string& extension_id) {
+ extension_id_ = extension_id;
+ }
+
+ ExtensionService* extension_service() { return service_; }
+
+ private:
+ ExtensionService* service_;
+ int uninstall_count_;
+ std::string extension_id_;
+};
+
+std::unique_ptr<KeyedService> BuildMockHotwordService(
+ content::BrowserContext* context) {
+ return base::MakeUnique<MockHotwordService>(static_cast<Profile*>(context));
+}
+
+} // namespace
+
+class HotwordServiceTest :
+ public extensions::ExtensionServiceTestBase,
+ public ::testing::WithParamInterface<const char*> {
+ protected:
+ HotwordServiceTest() {}
+ virtual ~HotwordServiceTest() {}
+
+ void SetApplicationLocale(Profile* profile, const std::string& new_locale) {
+#if defined(OS_CHROMEOS)
+ // On ChromeOS locale is per-profile.
+ profile->GetPrefs()->SetString(prefs::kApplicationLocale, new_locale);
+#else
+ g_browser_process->SetApplicationLocale(new_locale);
+#endif
+ }
+
+ void SetUp() override {
+ extension_id_ = GetParam();
+#if defined(OS_CHROMEOS)
+ // Tests on chromeos need to have the handler initialized.
+ chromeos::CrasAudioHandler::InitializeForTesting();
+#endif
+
+ extensions::ExtensionServiceTestBase::SetUp();
+ }
+
+ void TearDown() override {
+#if defined(OS_CHROMEOS)
+ DCHECK(chromeos::CrasAudioHandler::IsInitialized());
+ chromeos::CrasAudioHandler::Shutdown();
+#endif
+ }
+
+ std::string extension_id_;
+};
+
+INSTANTIATE_TEST_CASE_P(HotwordServiceTests,
+ HotwordServiceTest,
+ ::testing::Values(
+ extension_misc::kHotwordSharedModuleId));
+
+// Disabled due to http://crbug.com/503963.
+TEST_P(HotwordServiceTest, DISABLED_IsHotwordAllowedLocale) {
+#if defined(ENABLE_HOTWORDING)
+ TestingProfile::Builder profile_builder;
+ std::unique_ptr<TestingProfile> profile = profile_builder.Build();
+
+#if defined(ENABLE_HOTWORDING)
+ bool hotwording_enabled = true;
+#else
+ bool hotwording_enabled = false;
+#endif
+
+ // Check that the service exists so that a NULL service be ruled out in
+ // following tests.
+ HotwordService* hotword_service =
+ HotwordServiceFactory::GetForProfile(profile.get());
+ EXPECT_TRUE(hotword_service != NULL);
+
+ // Set the language to an invalid one.
+ SetApplicationLocale(static_cast<Profile*>(profile.get()), "non-valid");
+ EXPECT_FALSE(HotwordServiceFactory::IsHotwordAllowed(profile.get()));
+
+ // Now with valid locales it should be fine.
+ SetApplicationLocale(static_cast<Profile*>(profile.get()), "en");
+ EXPECT_EQ(hotwording_enabled,
+ HotwordServiceFactory::IsHotwordAllowed(profile.get()));
+ SetApplicationLocale(static_cast<Profile*>(profile.get()), "en-US");
+ EXPECT_EQ(hotwording_enabled,
+ HotwordServiceFactory::IsHotwordAllowed(profile.get()));
+ SetApplicationLocale(static_cast<Profile*>(profile.get()), "en_us");
+ EXPECT_EQ(hotwording_enabled,
+ HotwordServiceFactory::IsHotwordAllowed(profile.get()));
+ SetApplicationLocale(static_cast<Profile*>(profile.get()), "de_DE");
+ EXPECT_EQ(hotwording_enabled,
+ HotwordServiceFactory::IsHotwordAllowed(profile.get()));
+ SetApplicationLocale(static_cast<Profile*>(profile.get()), "fr_fr");
+ EXPECT_EQ(hotwording_enabled,
+ HotwordServiceFactory::IsHotwordAllowed(profile.get()));
+
+ // Test that incognito even with a valid locale and valid field trial
+ // still returns false.
+ Profile* otr_profile = profile->GetOffTheRecordProfile();
+ SetApplicationLocale(otr_profile, "en");
+ EXPECT_FALSE(HotwordServiceFactory::IsHotwordAllowed(otr_profile));
+#endif // defined(ENABLE_HOTWORDING)
+}
+
+TEST_P(HotwordServiceTest, ShouldReinstallExtension) {
+ InitializeEmptyExtensionService();
+
+ HotwordServiceFactory* hotword_service_factory =
+ HotwordServiceFactory::GetInstance();
+
+ MockHotwordService* hotword_service = static_cast<MockHotwordService*>(
+ hotword_service_factory->SetTestingFactoryAndUse(
+ profile(), BuildMockHotwordService));
+ ASSERT_TRUE(hotword_service != NULL);
+ hotword_service->SetExtensionId(extension_id_);
+
+ // If no locale has been set, no reason to uninstall.
+ EXPECT_FALSE(hotword_service->ShouldReinstallHotwordExtension());
+
+ SetApplicationLocale(profile(), "en");
+ hotword_service->SetPreviousLanguagePref();
+
+ // Now a locale is set, but it hasn't changed.
+ EXPECT_FALSE(hotword_service->ShouldReinstallHotwordExtension());
+
+ SetApplicationLocale(profile(), "fr_fr");
+
+ // Now it's a different locale so it should uninstall.
+ EXPECT_TRUE(hotword_service->ShouldReinstallHotwordExtension());
+}
+
+TEST_P(HotwordServiceTest, PreviousLanguageSetOnInstall) {
+ InitializeEmptyExtensionService();
+ service_->Init();
+
+ HotwordServiceFactory* hotword_service_factory =
+ HotwordServiceFactory::GetInstance();
+
+ MockHotwordService* hotword_service = static_cast<MockHotwordService*>(
+ hotword_service_factory->SetTestingFactoryAndUse(
+ profile(), BuildMockHotwordService));
+ ASSERT_TRUE(hotword_service != NULL);
+ hotword_service->SetExtensionService(service());
+ hotword_service->SetExtensionId(extension_id_);
+
+ // If no locale has been set, no reason to uninstall.
+ EXPECT_FALSE(hotword_service->ShouldReinstallHotwordExtension());
+
+ SetApplicationLocale(profile(), "test_locale");
+
+ hotword_service->InstallHotwordExtensionFromWebstore(1);
+ base::RunLoop().RunUntilIdle();
+
+ EXPECT_EQ("test_locale",
+ profile()->GetPrefs()->GetString(prefs::kHotwordPreviousLanguage));
+}
+
+TEST_P(HotwordServiceTest, UninstallReinstallTriggeredCorrectly) {
+#if defined(ENABLE_HOTWORDING)
+ InitializeEmptyExtensionService();
+ service_->Init();
+
+ HotwordServiceFactory* hotword_service_factory =
+ HotwordServiceFactory::GetInstance();
+
+ MockHotwordService* hotword_service = static_cast<MockHotwordService*>(
+ hotword_service_factory->SetTestingFactoryAndUse(
+ profile(), BuildMockHotwordService));
+ ASSERT_TRUE(hotword_service != NULL);
+ hotword_service->SetExtensionService(service());
+ hotword_service->SetExtensionId(extension_id_);
+
+ // Initialize the locale to "en".
+ SetApplicationLocale(profile(), "en");
+
+ // The previous locale should not be set. No reason to uninstall.
+ EXPECT_FALSE(hotword_service->MaybeReinstallHotwordExtension());
+
+ // Do an initial installation.
+ hotword_service->InstallHotwordExtensionFromWebstore(1);
+ base::RunLoop().RunUntilIdle();
+ EXPECT_EQ("en",
+ profile()->GetPrefs()->GetString(prefs::kHotwordPreviousLanguage));
+
+ if (extension_id_ == extension_misc::kHotwordSharedModuleId) {
+ // Shared module is installed and enabled.
+ EXPECT_EQ(0U, registry()->disabled_extensions().size());
+ EXPECT_TRUE(registry()->enabled_extensions().Contains(extension_id_));
+ } else {
+ // Verify the extension is installed but disabled.
+ EXPECT_EQ(1U, registry()->disabled_extensions().size());
+ EXPECT_TRUE(registry()->disabled_extensions().Contains(extension_id_));
+ }
+
+ // The previous locale should be set but should match the current
+ // locale. No reason to uninstall.
+ EXPECT_FALSE(hotword_service->MaybeReinstallHotwordExtension());
+
+ // Switch the locale to a valid but different one.
+ SetApplicationLocale(profile(), "fr_fr");
+#if defined(ENABLE_HOTWORDING)
+ EXPECT_TRUE(HotwordServiceFactory::IsHotwordAllowed(profile()));
+#else
+ // Disabled due to http://crbug.com/503963.
+ // EXPECT_FALSE(HotwordServiceFactory::IsHotwordAllowed(profile()));
+#endif
+
+ // Different but valid locale so expect uninstall.
+ EXPECT_TRUE(hotword_service->MaybeReinstallHotwordExtension());
+ EXPECT_EQ(1, hotword_service->uninstall_count());
+ EXPECT_EQ("fr_fr",
+ profile()->GetPrefs()->GetString(prefs::kHotwordPreviousLanguage));
+
+ if (extension_id_ == extension_misc::kHotwordSharedModuleId) {
+ // Shared module is installed and enabled.
+ EXPECT_TRUE(registry()->enabled_extensions().Contains(extension_id_));
+ } else {
+ // Verify the extension is installed. It's still disabled.
+ EXPECT_TRUE(registry()->disabled_extensions().Contains(extension_id_));
+ }
+
+ // Switch the locale to an invalid one.
+ SetApplicationLocale(profile(), "invalid");
+ EXPECT_FALSE(HotwordServiceFactory::IsHotwordAllowed(profile()));
+ EXPECT_FALSE(hotword_service->MaybeReinstallHotwordExtension());
+ 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");
+#if defined(ENABLE_HOTWORDING)
+ EXPECT_TRUE(HotwordServiceFactory::IsHotwordAllowed(profile()));
+#else
+ // Disabled due to http://crbug.com/503963.
+ // EXPECT_FALSE(HotwordServiceFactory::IsHotwordAllowed(profile()));
+#endif
+ EXPECT_FALSE(hotword_service->MaybeReinstallHotwordExtension());
+ EXPECT_EQ(1, hotword_service->uninstall_count()); // no change
+#endif // defined(ENABLE_HOTWORDING)
+}
+
+TEST_P(HotwordServiceTest, DisableAlwaysOnOnLanguageChange) {
+ // Bypass test for old hotwording.
+ if (extension_id_ != extension_misc::kHotwordSharedModuleId)
+ return;
+
+ // Turn on Always On
+ base::CommandLine::ForCurrentProcess()->AppendSwitch(
+ switches::kEnableExperimentalHotwordHardware);
+
+ InitializeEmptyExtensionService();
+ service_->Init();
+
+ // Enable always-on.
+ profile()->GetPrefs()->SetBoolean(prefs::kHotwordAlwaysOnSearchEnabled, true);
+
+ HotwordServiceFactory* hotword_service_factory =
+ HotwordServiceFactory::GetInstance();
+
+ MockHotwordService* hotword_service = static_cast<MockHotwordService*>(
+ hotword_service_factory->SetTestingFactoryAndUse(
+ profile(), BuildMockHotwordService));
+ ASSERT_TRUE(hotword_service != NULL);
+ hotword_service->SetExtensionService(service());
+ hotword_service->SetExtensionId(extension_id_);
+
+ // Initialize the locale to "en_us".
+ SetApplicationLocale(profile(), "en_us");
+
+ // The previous locale should not be set. No reason to uninstall.
+ EXPECT_FALSE(hotword_service->MaybeReinstallHotwordExtension());
+ EXPECT_TRUE(hotword_service->IsAlwaysOnEnabled());
+
+ // Do an initial installation.
+ hotword_service->InstallHotwordExtensionFromWebstore(1);
+ base::RunLoop().RunUntilIdle();
+
+ // The previous locale should be set but should match the current
+ // locale. No reason to uninstall.
+ EXPECT_FALSE(hotword_service->MaybeReinstallHotwordExtension());
+ EXPECT_TRUE(hotword_service->IsAlwaysOnEnabled());
+
+ // TODO(kcarattini): Uncomment this sectione once we launch always-on
+ // in more languages.
+ // // 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->MaybeReinstallHotwordExtension());
+ // EXPECT_FALSE(hotword_service->IsAlwaysOnEnabled());
+
+ // Re-enable always-on.
+ profile()->GetPrefs()->SetBoolean(prefs::kHotwordAlwaysOnSearchEnabled, true);
+
+ // Switch the locale to an invalid one.
+ SetApplicationLocale(profile(), "invalid");
+ EXPECT_FALSE(HotwordServiceFactory::IsHotwordAllowed(profile()));
+ EXPECT_FALSE(hotword_service->MaybeReinstallHotwordExtension());
+ EXPECT_TRUE(hotword_service->IsAlwaysOnEnabled());
+
+ // TODO(kcarattini): Uncomment this sectione once we launch always-on
+ // in more languages.
+ // // 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->MaybeReinstallHotwordExtension());
+ // EXPECT_TRUE(hotword_service->IsAlwaysOnEnabled());
+}
+
+TEST_P(HotwordServiceTest, IsAlwaysOnEnabled) {
+ // Bypass test for old hotwording.
+ if (extension_id_ != extension_misc::kHotwordSharedModuleId)
+ return;
+
+ InitializeEmptyExtensionService();
+ service_->Init();
+ HotwordServiceFactory* hotword_service_factory =
+ HotwordServiceFactory::GetInstance();
+
+ MockHotwordService* hotword_service = static_cast<MockHotwordService*>(
+ hotword_service_factory->SetTestingFactoryAndUse(
+ profile(), BuildMockHotwordService));
+ ASSERT_TRUE(hotword_service != NULL);
+ hotword_service->SetExtensionService(service());
+ hotword_service->SetExtensionId(extension_id_);
+
+ // No hardware available. Should never be true.
+ EXPECT_FALSE(hotword_service->IsAlwaysOnEnabled());
+
+ // Enable always-on, still not available.
+ profile()->GetPrefs()->SetBoolean(prefs::kHotwordAlwaysOnSearchEnabled, true);
+ EXPECT_FALSE(hotword_service->IsAlwaysOnEnabled());
+
+ // Enable regular hotwording, still not available.
+ profile()->GetPrefs()->SetBoolean(prefs::kHotwordSearchEnabled, true);
+ EXPECT_FALSE(hotword_service->IsAlwaysOnEnabled());
+
+ // Bypass hardware check.
+ base::CommandLine::ForCurrentProcess()->AppendSwitch(
+ switches::kEnableExperimentalHotwordHardware);
+ EXPECT_TRUE(hotword_service->IsAlwaysOnEnabled());
+
+ // Disable.
+ profile()->GetPrefs()->SetBoolean(prefs::kHotwordAlwaysOnSearchEnabled,
+ false);
+ EXPECT_FALSE(hotword_service->IsAlwaysOnEnabled());
+}
+
+TEST_P(HotwordServiceTest, IsSometimesOnEnabled) {
+ InitializeEmptyExtensionService();
+ service_->Init();
+ HotwordServiceFactory* hotword_service_factory =
+ HotwordServiceFactory::GetInstance();
+
+ MockHotwordService* hotword_service = static_cast<MockHotwordService*>(
+ hotword_service_factory->SetTestingFactoryAndUse(
+ profile(), BuildMockHotwordService));
+ ASSERT_TRUE(hotword_service != NULL);
+ hotword_service->SetExtensionService(service());
+ hotword_service->SetExtensionId(extension_id_);
+
+ // No pref set.
+ EXPECT_FALSE(hotword_service->IsSometimesOnEnabled());
+
+ // Set pref.
+ profile()->GetPrefs()->SetBoolean(prefs::kHotwordSearchEnabled, true);
+ EXPECT_TRUE(hotword_service->IsSometimesOnEnabled());
+
+ // Turn on always-on pref. Should have no effect.
+ profile()->GetPrefs()->SetBoolean(prefs::kHotwordAlwaysOnSearchEnabled, true);
+ EXPECT_TRUE(hotword_service->IsSometimesOnEnabled());
+
+ // Disable.
+ profile()->GetPrefs()->SetBoolean(prefs::kHotwordSearchEnabled, false);
+ EXPECT_FALSE(hotword_service->IsSometimesOnEnabled());
+
+ // Bypass rest of test for old hotwording.
+ if (extension_id_ != extension_misc::kHotwordSharedModuleId)
+ return;
+
+ // Bypass hardware check.
+ base::CommandLine::ForCurrentProcess()->AppendSwitch(
+ switches::kEnableExperimentalHotwordHardware);
+
+ // With hardware, only always-on is allowed.
+ EXPECT_FALSE(hotword_service->IsSometimesOnEnabled());
+
+ // Set pref.
+ profile()->GetPrefs()->SetBoolean(prefs::kHotwordSearchEnabled, true);
+ EXPECT_FALSE(hotword_service->IsSometimesOnEnabled());
+
+ // Disable always-on.
+ profile()->GetPrefs()->SetBoolean(prefs::kHotwordAlwaysOnSearchEnabled,
+ false);
+ EXPECT_FALSE(hotword_service->IsSometimesOnEnabled());
+}
+
+TEST_P(HotwordServiceTest, AudioHistorySyncOccurs) {
+ InitializeEmptyExtensionService();
+ service_->Init();
+
+ HotwordServiceFactory* hotword_service_factory =
+ HotwordServiceFactory::GetInstance();
+
+ MockHotwordService* hotword_service = static_cast<MockHotwordService*>(
+ hotword_service_factory->SetTestingFactoryAndUse(
+ profile(), BuildMockHotwordService));
+ ASSERT_TRUE(hotword_service != NULL);
+ hotword_service->SetExtensionService(service());
+ hotword_service->SetExtensionId(extension_id_);
+
+ scoped_refptr<base::TestSimpleTaskRunner> test_task_runner(
+ new base::TestSimpleTaskRunner());
+ MockAudioHistoryHandler* audio_history_handler =
+ new MockAudioHistoryHandler(profile(), test_task_runner, nullptr);
+ hotword_service->SetAudioHistoryHandler(audio_history_handler);
+ EXPECT_EQ(1, audio_history_handler->GetAudioHistoryCalls());
+ // We expect the next check for audio history to be in the queue.
+ EXPECT_EQ(base::TimeDelta::FromDays(1),
+ test_task_runner->NextPendingTaskDelay());
+ EXPECT_TRUE(test_task_runner->HasPendingTask());
+ test_task_runner->RunPendingTasks();
+ EXPECT_EQ(2, audio_history_handler->GetAudioHistoryCalls());
+ EXPECT_TRUE(test_task_runner->HasPendingTask());
+ test_task_runner->ClearPendingTasks();
+}
--
2.14.3