| 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/prefs/pref_service.h" | 6 #include "base/prefs/pref_service.h" |
| 7 #include "chrome/browser/extensions/api/hotword_private/hotword_private_api.h" | 7 #include "chrome/browser/extensions/api/hotword_private/hotword_private_api.h" |
| 8 #include "chrome/browser/extensions/extension_apitest.h" | 8 #include "chrome/browser/extensions/extension_apitest.h" |
| 9 #include "chrome/browser/extensions/extension_service.h" | 9 #include "chrome/browser/extensions/extension_service.h" |
| 10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
| 11 #include "chrome/browser/search/hotword_client.h" | 11 #include "chrome/browser/search/hotword_client.h" |
| 12 #include "chrome/browser/search/hotword_service.h" | 12 #include "chrome/browser/search/hotword_service.h" |
| 13 #include "chrome/browser/search/hotword_service_factory.h" | 13 #include "chrome/browser/search/hotword_service_factory.h" |
| 14 #include "chrome/common/chrome_switches.h" | 14 #include "chrome/common/chrome_switches.h" |
| 15 #include "chrome/common/pref_names.h" | 15 #include "chrome/common/pref_names.h" |
| 16 #include "extensions/common/switches.h" | 16 #include "extensions/common/switches.h" |
| 17 #include "extensions/test/extension_test_message_listener.h" | 17 #include "extensions/test/extension_test_message_listener.h" |
| 18 | 18 |
| 19 namespace { | 19 namespace { |
| 20 | 20 |
| 21 const char kHotwordTestExtensionId[] = "cpfhkdbjfdgdebcjlifoldbijinjfifp"; | 21 const char kHotwordTestExtensionId[] = "cpfhkdbjfdgdebcjlifoldbijinjfifp"; |
| 22 | 22 |
| 23 class MockHotwordService : public HotwordService { | 23 class MockHotwordService : public HotwordService { |
| 24 public: | 24 public: |
| 25 explicit MockHotwordService(Profile* profile) | 25 explicit MockHotwordService(Profile* profile) |
| 26 : HotwordService(profile), service_available_(true) {}; | 26 : HotwordService(profile), service_available_(true) {}; |
| 27 virtual ~MockHotwordService() {} | 27 ~MockHotwordService() override {} |
| 28 | 28 |
| 29 virtual bool IsServiceAvailable() override { | 29 bool IsServiceAvailable() override { return service_available_; } |
| 30 return service_available_; | |
| 31 } | |
| 32 | 30 |
| 33 void setServiceAvailable(bool available) { | 31 void setServiceAvailable(bool available) { |
| 34 service_available_ = available; | 32 service_available_ = available; |
| 35 } | 33 } |
| 36 | 34 |
| 37 static KeyedService* Build(content::BrowserContext* profile) { | 35 static KeyedService* Build(content::BrowserContext* profile) { |
| 38 return new MockHotwordService(static_cast<Profile*>(profile)); | 36 return new MockHotwordService(static_cast<Profile*>(profile)); |
| 39 } | 37 } |
| 40 | 38 |
| 41 virtual LaunchMode GetHotwordAudioVerificationLaunchMode() override { | 39 LaunchMode GetHotwordAudioVerificationLaunchMode() override { |
| 42 return launch_mode_; | 40 return launch_mode_; |
| 43 } | 41 } |
| 44 | 42 |
| 45 void SetHotwordAudioVerificationLaunchMode(const LaunchMode& launch_mode) { | 43 void SetHotwordAudioVerificationLaunchMode(const LaunchMode& launch_mode) { |
| 46 launch_mode_ = launch_mode; | 44 launch_mode_ = launch_mode; |
| 47 } | 45 } |
| 48 | 46 |
| 49 private: | 47 private: |
| 50 bool service_available_; | 48 bool service_available_; |
| 51 LaunchMode launch_mode_; | 49 LaunchMode launch_mode_; |
| 52 | 50 |
| 53 DISALLOW_COPY_AND_ASSIGN(MockHotwordService); | 51 DISALLOW_COPY_AND_ASSIGN(MockHotwordService); |
| 54 }; | 52 }; |
| 55 | 53 |
| 56 class MockHotwordClient : public HotwordClient { | 54 class MockHotwordClient : public HotwordClient { |
| 57 public: | 55 public: |
| 58 MockHotwordClient() | 56 MockHotwordClient() |
| 59 : last_enabled_(false), | 57 : last_enabled_(false), |
| 60 state_changed_count_(0), | 58 state_changed_count_(0), |
| 61 recognized_count_(0) { | 59 recognized_count_(0) { |
| 62 } | 60 } |
| 63 | 61 |
| 64 virtual ~MockHotwordClient() {} | 62 ~MockHotwordClient() override {} |
| 65 | 63 |
| 66 virtual void OnHotwordStateChanged(bool enabled) override { | 64 void OnHotwordStateChanged(bool enabled) override { |
| 67 last_enabled_ = enabled; | 65 last_enabled_ = enabled; |
| 68 state_changed_count_++; | 66 state_changed_count_++; |
| 69 } | 67 } |
| 70 | 68 |
| 71 virtual void OnHotwordRecognized() override { | 69 void OnHotwordRecognized() override { recognized_count_++; } |
| 72 recognized_count_++; | |
| 73 } | |
| 74 | 70 |
| 75 bool last_enabled() const { return last_enabled_; } | 71 bool last_enabled() const { return last_enabled_; } |
| 76 int state_changed_count() const { return state_changed_count_; } | 72 int state_changed_count() const { return state_changed_count_; } |
| 77 int recognized_count() const { return recognized_count_; } | 73 int recognized_count() const { return recognized_count_; } |
| 78 | 74 |
| 79 private: | 75 private: |
| 80 bool last_enabled_; | 76 bool last_enabled_; |
| 81 int state_changed_count_; | 77 int state_changed_count_; |
| 82 int recognized_count_; | 78 int recognized_count_; |
| 83 | 79 |
| 84 DISALLOW_COPY_AND_ASSIGN(MockHotwordClient); | 80 DISALLOW_COPY_AND_ASSIGN(MockHotwordClient); |
| 85 }; | 81 }; |
| 86 | 82 |
| 87 class HotwordPrivateApiTest : public ExtensionApiTest { | 83 class HotwordPrivateApiTest : public ExtensionApiTest { |
| 88 public: | 84 public: |
| 89 HotwordPrivateApiTest() {} | 85 HotwordPrivateApiTest() {} |
| 90 virtual ~HotwordPrivateApiTest() {} | 86 virtual ~HotwordPrivateApiTest() {} |
| 91 | 87 |
| 92 virtual void SetUpCommandLine(base::CommandLine* command_line) override { | 88 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 93 ExtensionApiTest::SetUpCommandLine(command_line); | 89 ExtensionApiTest::SetUpCommandLine(command_line); |
| 94 | 90 |
| 95 // Whitelist the test extensions (which all share a common ID) to use | 91 // Whitelist the test extensions (which all share a common ID) to use |
| 96 // private APIs. | 92 // private APIs. |
| 97 command_line->AppendSwitchASCII( | 93 command_line->AppendSwitchASCII( |
| 98 extensions::switches::kWhitelistedExtensionID, kHotwordTestExtensionId); | 94 extensions::switches::kWhitelistedExtensionID, kHotwordTestExtensionId); |
| 99 } | 95 } |
| 100 | 96 |
| 101 virtual void SetUpOnMainThread() override { | 97 void SetUpOnMainThread() override { |
| 102 ExtensionApiTest::SetUpOnMainThread(); | 98 ExtensionApiTest::SetUpOnMainThread(); |
| 103 | 99 |
| 104 test_data_dir_ = test_data_dir_.AppendASCII("hotword_private"); | 100 test_data_dir_ = test_data_dir_.AppendASCII("hotword_private"); |
| 105 | 101 |
| 106 service_ = static_cast<MockHotwordService*>( | 102 service_ = static_cast<MockHotwordService*>( |
| 107 HotwordServiceFactory::GetInstance()->SetTestingFactoryAndUse( | 103 HotwordServiceFactory::GetInstance()->SetTestingFactoryAndUse( |
| 108 profile(), MockHotwordService::Build)); | 104 profile(), MockHotwordService::Build)); |
| 109 } | 105 } |
| 110 | 106 |
| 111 MockHotwordService* service() { | 107 MockHotwordService* service() { |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 279 } | 275 } |
| 280 | 276 |
| 281 IN_PROC_BROWSER_TEST_F(HotwordPrivateApiTest, | 277 IN_PROC_BROWSER_TEST_F(HotwordPrivateApiTest, |
| 282 GetLaunchStateHotwordAudioHistory) { | 278 GetLaunchStateHotwordAudioHistory) { |
| 283 service()->SetHotwordAudioVerificationLaunchMode( | 279 service()->SetHotwordAudioVerificationLaunchMode( |
| 284 HotwordService::HOTWORD_AND_AUDIO_HISTORY); | 280 HotwordService::HOTWORD_AND_AUDIO_HISTORY); |
| 285 ExtensionTestMessageListener listener("launchMode: 2", false); | 281 ExtensionTestMessageListener listener("launchMode: 2", false); |
| 286 ASSERT_TRUE(RunComponentExtensionTest("getLaunchState")) << message_; | 282 ASSERT_TRUE(RunComponentExtensionTest("getLaunchState")) << message_; |
| 287 EXPECT_TRUE(listener.WaitUntilSatisfied()); | 283 EXPECT_TRUE(listener.WaitUntilSatisfied()); |
| 288 } | 284 } |
| OLD | NEW |