1 // Copyright 2013 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 "chrome/browser/extensions/external_component_loader.h"
7 #include "base/values.h"
8 #include "chrome/browser/extensions/external_provider_impl.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/speech/tts_platform.h"
11 #include "chrome/common/extensions/extension_constants.h"
12 #include "chrome/test/base/testing_profile.h"
13 #include "content/public/test/test_browser_thread.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 #if defined(OS_CHROMEOS)
17 #include "chrome/browser/chromeos/login/user_manager_impl.h"
18 #include "chrome/browser/chromeos/settings/cros_settings.h"
19 #include "chrome/browser/chromeos/settings/device_settings_service.h"
20 #endif // defined(OS_CHROMEOS)
22 using content::BrowserThread
;
23 using extensions::ExternalProviderImpl
;
24 using extensions::ExternalProviderInterface
;
25 using extensions::Manifest
;
26 using extensions::ProviderCollection
;
28 namespace extensions
{
31 class TestUtterance
: public Utterance
{
33 explicit TestUtterance(Profile
* profile
) : Utterance(profile
) {
36 virtual ~TestUtterance() {
37 set_finished_for_testing(true);
41 class FakeVisitorInterface
42 : public ExternalProviderInterface::VisitorInterface
{
44 FakeVisitorInterface() {}
45 virtual ~FakeVisitorInterface() {}
47 virtual bool OnExternalExtensionFileFound(
48 const std::string
& id
,
49 const base::Version
* version
,
50 const base::FilePath
& path
,
51 Manifest::Location location
,
53 bool mark_acknowledged
) OVERRIDE
{
57 virtual bool OnExternalExtensionUpdateUrlFound(
58 const std::string
& id
,
59 const GURL
& update_url
,
60 Manifest::Location location
,
62 bool mark_acknowledged
) OVERRIDE
{
66 virtual void OnExternalProviderReady(
67 const ExternalProviderInterface
* provider
) OVERRIDE
{}
69 } // anonymous namespace
71 #if defined(OS_CHROMEOS)
72 class ExternalComponentLoaderTest
: public testing::Test
{
74 ExternalComponentLoaderTest()
75 : ui_thread_(BrowserThread::UI
, &message_loop_
),
76 user_manager_enabler_(new chromeos::UserManagerImpl()) {
79 virtual ~ExternalComponentLoaderTest() {}
81 // testing::Test overrides:
82 virtual void SetUp() OVERRIDE
{
83 testing_profile_
.reset(new TestingProfile());
84 ExternalProviderImpl::CreateExternalProviders(
85 &service_
, testing_profile_
.get(), &providers_
);
88 virtual void TearDown() OVERRIDE
{
91 bool IsHighQualityEnglishSpeechExtensionInstalled() {
92 const std::string
& id
= extension_misc::kHighQuality_en_US_ExtensionId
;
93 for (size_t i
= 0; i
< providers_
.size(); ++i
) {
94 if (!providers_
[i
]->IsReady())
96 if (providers_
[i
]->HasExtension(id
))
103 base::MessageLoop message_loop_
;
104 content::TestBrowserThread ui_thread_
;
105 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_
;
106 chromeos::ScopedTestCrosSettings test_cros_settings_
;
107 chromeos::ScopedUserManagerEnabler user_manager_enabler_
;
108 scoped_ptr
<Profile
> testing_profile_
;
109 FakeVisitorInterface service_
;
110 ProviderCollection providers_
;
113 DISALLOW_COPY_AND_ASSIGN(ExternalComponentLoaderTest
);
116 TEST_F(ExternalComponentLoaderTest
, Speaking100TimesInstallsSpeechExtension
) {
117 ASSERT_FALSE(IsHighQualityEnglishSpeechExtensionInstalled());
119 TtsPlatformImpl
* tts_platform
= TtsPlatformImpl::GetInstance();
120 TestUtterance
utterance(testing_profile_
.get());
121 VoiceData voice_data
;
122 voice_data
.lang
= "en-US";
123 voice_data
.extension_id
= extension_misc::kSpeechSynthesisExtensionId
;
125 // 99 times should not be sufficient.
126 for (int i
= 0; i
< 99; i
++)
127 tts_platform
->WillSpeakUtteranceWithVoice(&utterance
, voice_data
);
128 ASSERT_FALSE(IsHighQualityEnglishSpeechExtensionInstalled());
130 // The 100th time should install it.
131 tts_platform
->WillSpeakUtteranceWithVoice(&utterance
, voice_data
);
132 ASSERT_TRUE(IsHighQualityEnglishSpeechExtensionInstalled());
135 TEST_F(ExternalComponentLoaderTest
,
136 UsingOtherVoiceDoesNotTriggerInstallingSpeechExtension
) {
137 ASSERT_FALSE(IsHighQualityEnglishSpeechExtensionInstalled());
139 TtsPlatformImpl
* tts_platform
= TtsPlatformImpl::GetInstance();
140 TestUtterance
utterance(testing_profile_
.get());
141 VoiceData voice_data
;
142 voice_data
.lang
= "en-US";
143 voice_data
.extension_id
= "dummy"; // Some other extension id.
145 for (int i
= 0; i
< 100; i
++)
146 tts_platform
->WillSpeakUtteranceWithVoice(&utterance
, voice_data
);
147 ASSERT_FALSE(IsHighQualityEnglishSpeechExtensionInstalled());
150 TEST_F(ExternalComponentLoaderTest
,
151 UnsupportedLangDoesNotTriggerInstallingSpeechExtension
) {
152 ASSERT_FALSE(IsHighQualityEnglishSpeechExtensionInstalled());
154 TtsPlatformImpl
* tts_platform
= TtsPlatformImpl::GetInstance();
155 TestUtterance
utterance(testing_profile_
.get());
156 VoiceData voice_data
;
157 voice_data
.lang
= "tlh"; // Klingon
158 voice_data
.extension_id
= extension_misc::kSpeechSynthesisExtensionId
;
160 for (int i
= 0; i
< 100; i
++)
161 tts_platform
->WillSpeakUtteranceWithVoice(&utterance
, voice_data
);
162 ASSERT_FALSE(IsHighQualityEnglishSpeechExtensionInstalled());
164 #endif // defined(OS_CHROMEOS)
166 } // namespace extensions