summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/external_component_loader_unittest.cc
blob: 6fb03eedc118ea59f21a0bb8ba2a24f8da3291c1 (plain)
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
// Copyright 2013 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/extensions/external_component_loader.h"

#include "base/values.h"
#include "chrome/browser/extensions/external_provider_impl.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/speech/tts_platform.h"
#include "chrome/common/extensions/extension_constants.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/test/test_browser_thread.h"
#include "testing/gtest/include/gtest/gtest.h"

#if defined(OS_CHROMEOS)
#include "chrome/browser/chromeos/login/user_manager_impl.h"
#include "chrome/browser/chromeos/settings/cros_settings.h"
#include "chrome/browser/chromeos/settings/device_settings_service.h"
#endif  // defined(OS_CHROMEOS)

using content::BrowserThread;
using extensions::ExternalProviderImpl;
using extensions::ExternalProviderInterface;
using extensions::Manifest;
using extensions::ProviderCollection;

namespace extensions {

namespace {
class TestUtterance : public Utterance {
 public:
  explicit TestUtterance(Profile* profile) : Utterance(profile) {
  }

  virtual ~TestUtterance() {
    set_finished_for_testing(true);
  }
};

class FakeVisitorInterface
    : public ExternalProviderInterface::VisitorInterface {
 public:
  FakeVisitorInterface() {}
  virtual ~FakeVisitorInterface() {}

  virtual bool OnExternalExtensionFileFound(
      const std::string& id,
      const base::Version* version,
      const base::FilePath& path,
      Manifest::Location location,
      int creation_flags,
      bool mark_acknowledged) OVERRIDE {
    return true;
  }

  virtual bool OnExternalExtensionUpdateUrlFound(
      const std::string& id,
      const GURL& update_url,
      Manifest::Location location,
      int creation_flags,
      bool mark_acknowledged) OVERRIDE {
    return true;
  }

  virtual void OnExternalProviderReady(
      const ExternalProviderInterface* provider) OVERRIDE {}
};
}  // anonymous namespace

#if defined(OS_CHROMEOS)
class ExternalComponentLoaderTest : public testing::Test {
 public:
  ExternalComponentLoaderTest()
      : ui_thread_(BrowserThread::UI, &message_loop_),
        user_manager_enabler_(new chromeos::UserManagerImpl()) {
  }

  virtual ~ExternalComponentLoaderTest() {}

  // testing::Test overrides:
  virtual void SetUp() OVERRIDE {
    testing_profile_.reset(new TestingProfile());
    ExternalProviderImpl::CreateExternalProviders(
        &service_, testing_profile_.get(), &providers_);
  }

  virtual void TearDown() OVERRIDE {
  }

  bool IsHighQualityEnglishSpeechExtensionInstalled() {
    const std::string& id = extension_misc::kHighQuality_en_US_ExtensionId;
    for (size_t i = 0; i < providers_.size(); ++i) {
      if (!providers_[i]->IsReady())
        continue;
      if (providers_[i]->HasExtension(id))
        return true;
    }
    return false;
  }

 protected:
  base::MessageLoop message_loop_;
  content::TestBrowserThread ui_thread_;
  chromeos::ScopedTestDeviceSettingsService test_device_settings_service_;
  chromeos::ScopedTestCrosSettings test_cros_settings_;
  chromeos::ScopedUserManagerEnabler user_manager_enabler_;
  scoped_ptr<Profile> testing_profile_;
  FakeVisitorInterface service_;
  ProviderCollection providers_;

 private:
  DISALLOW_COPY_AND_ASSIGN(ExternalComponentLoaderTest);
};

TEST_F(ExternalComponentLoaderTest, Speaking100TimesInstallsSpeechExtension) {
  ASSERT_FALSE(IsHighQualityEnglishSpeechExtensionInstalled());

  TtsPlatformImpl* tts_platform = TtsPlatformImpl::GetInstance();
  TestUtterance utterance(testing_profile_.get());
  VoiceData voice_data;
  voice_data.lang = "en-US";
  voice_data.extension_id = extension_misc::kSpeechSynthesisExtensionId;

  // 99 times should not be sufficient.
  for (int i = 0; i < 99; i++)
    tts_platform->WillSpeakUtteranceWithVoice(&utterance, voice_data);
  ASSERT_FALSE(IsHighQualityEnglishSpeechExtensionInstalled());

  // The 100th time should install it.
  tts_platform->WillSpeakUtteranceWithVoice(&utterance, voice_data);
  ASSERT_TRUE(IsHighQualityEnglishSpeechExtensionInstalled());
}

TEST_F(ExternalComponentLoaderTest,
       UsingOtherVoiceDoesNotTriggerInstallingSpeechExtension) {
  ASSERT_FALSE(IsHighQualityEnglishSpeechExtensionInstalled());

  TtsPlatformImpl* tts_platform = TtsPlatformImpl::GetInstance();
  TestUtterance utterance(testing_profile_.get());
  VoiceData voice_data;
  voice_data.lang = "en-US";
  voice_data.extension_id = "dummy";  // Some other extension id.

  for (int i = 0; i < 100; i++)
    tts_platform->WillSpeakUtteranceWithVoice(&utterance, voice_data);
  ASSERT_FALSE(IsHighQualityEnglishSpeechExtensionInstalled());
}

TEST_F(ExternalComponentLoaderTest,
       UnsupportedLangDoesNotTriggerInstallingSpeechExtension) {
  ASSERT_FALSE(IsHighQualityEnglishSpeechExtensionInstalled());

  TtsPlatformImpl* tts_platform = TtsPlatformImpl::GetInstance();
  TestUtterance utterance(testing_profile_.get());
  VoiceData voice_data;
  voice_data.lang = "tlh";  // Klingon
  voice_data.extension_id = extension_misc::kSpeechSynthesisExtensionId;

  for (int i = 0; i < 100; i++)
    tts_platform->WillSpeakUtteranceWithVoice(&utterance, voice_data);
  ASSERT_FALSE(IsHighQualityEnglishSpeechExtensionInstalled());
}
#endif  // defined(OS_CHROMEOS)

}  // namespace extensions