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
3 // found in the LICENSE file.
5 #include "base/prefs/pref_service.h"
6 #include "chrome/browser/extensions/api/hotword_private/hotword_private_api.h"
7 #include "chrome/browser/extensions/extension_apitest.h"
8 #include "chrome/browser/extensions/extension_service.h"
9 #include "chrome/browser/extensions/extension_test_message_listener.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/search/hotword_service.h"
12 #include "chrome/browser/search/hotword_service_factory.h"
13 #include "chrome/common/pref_names.h"
17 class MockHotwordService : public HotwordService {
19 explicit MockHotwordService(Profile* profile)
20 : HotwordService(profile), service_available_(true) {};
21 virtual ~MockHotwordService() {}
23 virtual bool IsServiceAvailable() OVERRIDE {
24 return service_available_;
27 void setServiceAvailable(bool available) {
28 service_available_ = available;
31 static KeyedService* Build(content::BrowserContext* profile) {
32 return new MockHotwordService(static_cast<Profile*>(profile));
36 bool service_available_;
38 DISALLOW_COPY_AND_ASSIGN(MockHotwordService);
41 class HotwordPrivateApiTest : public ExtensionApiTest {
43 HotwordPrivateApiTest() {}
44 virtual ~HotwordPrivateApiTest() {}
46 virtual void SetUpOnMainThread() OVERRIDE {
47 ExtensionApiTest::SetUpOnMainThread();
49 test_data_dir_ = test_data_dir_.AppendASCII("hotword_private");
51 service_ = static_cast<MockHotwordService*>(
52 HotwordServiceFactory::GetInstance()->SetTestingFactoryAndUse(
53 profile(), MockHotwordService::Build));
56 MockHotwordService* service() {
61 MockHotwordService* service_;
63 DISALLOW_COPY_AND_ASSIGN(HotwordPrivateApiTest);
66 } // anonymous namespace
68 IN_PROC_BROWSER_TEST_F(HotwordPrivateApiTest, SetEnabled) {
69 EXPECT_FALSE(profile()->GetPrefs()->GetBoolean(prefs::kHotwordSearchEnabled));
71 ExtensionTestMessageListener listenerTrue("ready", false);
72 EXPECT_TRUE(RunComponentExtensionTest("setEnabledTrue")) << message_;
73 EXPECT_TRUE(listenerTrue.WaitUntilSatisfied());
74 EXPECT_TRUE(profile()->GetPrefs()->GetBoolean(prefs::kHotwordSearchEnabled));
76 ExtensionTestMessageListener listenerFalse("ready", false);
77 EXPECT_TRUE(RunComponentExtensionTest("setEnabledFalse")) << message_;
78 EXPECT_TRUE(listenerFalse.WaitUntilSatisfied());
79 EXPECT_FALSE(profile()->GetPrefs()->GetBoolean(prefs::kHotwordSearchEnabled));
82 IN_PROC_BROWSER_TEST_F(HotwordPrivateApiTest, SetAudioLoggingEnabled) {
83 EXPECT_FALSE(service()->IsOptedIntoAudioLogging());
84 EXPECT_TRUE(profile()->GetPrefs()->GetBoolean(
85 prefs::kHotwordAudioLoggingEnabled));
87 ExtensionTestMessageListener listenerTrue("ready", false);
89 RunComponentExtensionTest("setAudioLoggingEnableTrue")) << message_;
90 EXPECT_TRUE(listenerTrue.WaitUntilSatisfied());
91 EXPECT_TRUE(profile()->GetPrefs()->GetBoolean(
92 prefs::kHotwordAudioLoggingEnabled));
93 EXPECT_TRUE(service()->IsOptedIntoAudioLogging());
95 ExtensionTestMessageListener listenerFalse("ready", false);
97 RunComponentExtensionTest("setAudioLoggingEnableFalse")) << message_;
98 EXPECT_TRUE(listenerFalse.WaitUntilSatisfied());
99 EXPECT_FALSE(profile()->GetPrefs()->GetBoolean(
100 prefs::kHotwordAudioLoggingEnabled));
101 EXPECT_FALSE(service()->IsOptedIntoAudioLogging());
104 IN_PROC_BROWSER_TEST_F(HotwordPrivateApiTest, GetStatus) {
105 EXPECT_TRUE(RunComponentExtensionTest("getEnabled")) << message_;
108 IN_PROC_BROWSER_TEST_F(HotwordPrivateApiTest, IsAvailableTrue) {
109 service()->setServiceAvailable(true);
110 ExtensionTestMessageListener listener("available: true", false);
111 EXPECT_TRUE(RunComponentExtensionTest("isAvailable")) << message_;
112 EXPECT_TRUE(listener.WaitUntilSatisfied());
115 IN_PROC_BROWSER_TEST_F(HotwordPrivateApiTest, IsAvailableFalse) {
116 service()->setServiceAvailable(false);
117 ExtensionTestMessageListener listener("available: false", false);
118 EXPECT_TRUE(RunComponentExtensionTest("isAvailable")) << message_;
119 EXPECT_TRUE(listener.WaitUntilSatisfied());
122 IN_PROC_BROWSER_TEST_F(HotwordPrivateApiTest, OnEnabledChanged) {
123 // Trigger the pref registrar.
124 extensions::HotwordPrivateEventService::GetFactoryInstance();
125 ExtensionTestMessageListener listener("ready", false);
126 LoadExtensionAsComponent(
127 test_data_dir_.AppendASCII("onEnabledChanged"));
128 EXPECT_TRUE(listener.WaitUntilSatisfied());
130 ExtensionTestMessageListener listenerNotification("notification", false);
131 profile()->GetPrefs()->SetBoolean(prefs::kHotwordSearchEnabled, true);
132 EXPECT_TRUE(listenerNotification.WaitUntilSatisfied());