Upstream version 5.34.98.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / external_component_loader_unittest.cc
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.
4
5 #include "chrome/browser/extensions/external_component_loader.h"
6
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"
15
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)
21
22 using content::BrowserThread;
23 using extensions::ExternalProviderImpl;
24 using extensions::ExternalProviderInterface;
25 using extensions::Manifest;
26 using extensions::ProviderCollection;
27
28 namespace extensions {
29
30 namespace {
31 class TestUtterance : public Utterance {
32  public:
33   explicit TestUtterance(Profile* profile) : Utterance(profile) {
34   }
35
36   virtual ~TestUtterance() {
37     set_finished_for_testing(true);
38   }
39 };
40
41 class FakeVisitorInterface
42     : public ExternalProviderInterface::VisitorInterface {
43  public:
44   FakeVisitorInterface() {}
45   virtual ~FakeVisitorInterface() {}
46
47   virtual bool OnExternalExtensionFileFound(
48       const std::string& id,
49       const base::Version* version,
50       const base::FilePath& path,
51       Manifest::Location location,
52       int creation_flags,
53       bool mark_acknowledged) OVERRIDE {
54     return true;
55   }
56
57   virtual bool OnExternalExtensionUpdateUrlFound(
58       const std::string& id,
59       const GURL& update_url,
60       Manifest::Location location,
61       int creation_flags,
62       bool mark_acknowledged) OVERRIDE {
63     return true;
64   }
65
66   virtual void OnExternalProviderReady(
67       const ExternalProviderInterface* provider) OVERRIDE {}
68 };
69 }  // anonymous namespace
70
71 #if defined(OS_CHROMEOS)
72 class ExternalComponentLoaderTest : public testing::Test {
73  public:
74   ExternalComponentLoaderTest()
75       : ui_thread_(BrowserThread::UI, &message_loop_),
76         user_manager_enabler_(new chromeos::UserManagerImpl()) {
77   }
78
79   virtual ~ExternalComponentLoaderTest() {}
80
81   // testing::Test overrides:
82   virtual void SetUp() OVERRIDE {
83     testing_profile_.reset(new TestingProfile());
84     ExternalProviderImpl::CreateExternalProviders(
85         &service_, testing_profile_.get(), &providers_);
86   }
87
88   virtual void TearDown() OVERRIDE {
89   }
90
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())
95         continue;
96       if (providers_[i]->HasExtension(id))
97         return true;
98     }
99     return false;
100   }
101
102  protected:
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_;
111
112  private:
113   DISALLOW_COPY_AND_ASSIGN(ExternalComponentLoaderTest);
114 };
115
116 TEST_F(ExternalComponentLoaderTest, Speaking100TimesInstallsSpeechExtension) {
117   ASSERT_FALSE(IsHighQualityEnglishSpeechExtensionInstalled());
118
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;
124
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());
129
130   // The 100th time should install it.
131   tts_platform->WillSpeakUtteranceWithVoice(&utterance, voice_data);
132   ASSERT_TRUE(IsHighQualityEnglishSpeechExtensionInstalled());
133 }
134
135 TEST_F(ExternalComponentLoaderTest,
136        UsingOtherVoiceDoesNotTriggerInstallingSpeechExtension) {
137   ASSERT_FALSE(IsHighQualityEnglishSpeechExtensionInstalled());
138
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.
144
145   for (int i = 0; i < 100; i++)
146     tts_platform->WillSpeakUtteranceWithVoice(&utterance, voice_data);
147   ASSERT_FALSE(IsHighQualityEnglishSpeechExtensionInstalled());
148 }
149
150 TEST_F(ExternalComponentLoaderTest,
151        UnsupportedLangDoesNotTriggerInstallingSpeechExtension) {
152   ASSERT_FALSE(IsHighQualityEnglishSpeechExtensionInstalled());
153
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;
159
160   for (int i = 0; i < 100; i++)
161     tts_platform->WillSpeakUtteranceWithVoice(&utterance, voice_data);
162   ASSERT_FALSE(IsHighQualityEnglishSpeechExtensionInstalled());
163 }
164 #endif  // defined(OS_CHROMEOS)
165
166 }  // namespace extensions