summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/extension_tts_apitest.cc
blob: 560235df2b9172eccb26465f8dcfb19ef02c5436 (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// Copyright (c) 2010 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 "base/command_line.h"
#include "chrome/browser/extensions/extension_apitest.h"
#include "chrome/browser/extensions/extension_tts_api.h"
#include "chrome/common/chrome_switches.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

// Needed for CreateFunctor.
#define GMOCK_MUTANT_INCLUDE_LATE_OBJECT_BINDING
#include "testing/gmock_mutant.h"

#if defined(OS_CHROMEOS)
#include "chrome/browser/chromeos/cros/cros_mock.h"
#endif

using ::testing::AnyNumber;
using ::testing::CreateFunctor;
using ::testing::DoAll;
using ::testing::InSequence;
using ::testing::InvokeWithoutArgs;
using ::testing::Return;
using ::testing::StrictMock;
using ::testing::_;

class MockExtensionTtsPlatformImpl : public ExtensionTtsPlatformImpl {
 public:
  MOCK_METHOD6(Speak,
               bool(const std::string& utterance,
                    const std::string& locale,
                    const std::string& gender,
                    double rate,
                    double pitch,
                    double volume));
  MOCK_METHOD0(StopSpeaking, bool(void));
  MOCK_METHOD0(IsSpeaking, bool(void));

  void SetErrorToEpicFail() {
    set_error("epic fail");
  }
};

class TtsApiTest : public ExtensionApiTest {
 public:
  virtual void SetUpCommandLine(CommandLine* command_line) {
    ExtensionApiTest::SetUpCommandLine(command_line);
    command_line->AppendSwitch(switches::kEnableExperimentalExtensionApis);
  }

  virtual void SetUpInProcessBrowserTestFixture() {
    ExtensionApiTest::SetUpInProcessBrowserTestFixture();
    ExtensionTtsController::GetInstance()->SetPlatformImpl(
        &mock_platform_impl_);
  }

 protected:
  StrictMock<MockExtensionTtsPlatformImpl> mock_platform_impl_;
};

IN_PROC_BROWSER_TEST_F(TtsApiTest, PlatformSpeakFinishesImmediately) {
  InSequence s;
  EXPECT_CALL(mock_platform_impl_, StopSpeaking())
      .WillOnce(Return(true));
  EXPECT_CALL(mock_platform_impl_, Speak(_, _, _, _, _, _))
      .WillOnce(Return(true));
  EXPECT_CALL(mock_platform_impl_, IsSpeaking())
      .WillOnce(Return(false));
  ASSERT_TRUE(RunExtensionTest("tts/speak_once")) << message_;
}

IN_PROC_BROWSER_TEST_F(TtsApiTest, PlatformSpeakKeepsSpeakingTwice) {
  InSequence s;
  EXPECT_CALL(mock_platform_impl_, StopSpeaking())
      .WillOnce(Return(true));
  EXPECT_CALL(mock_platform_impl_, Speak(_, _, _, _, _, _))
      .WillOnce(Return(true));
  EXPECT_CALL(mock_platform_impl_, IsSpeaking())
      .WillOnce(Return(true))
      .WillOnce(Return(true))
      .WillOnce(Return(false));
  ASSERT_TRUE(RunExtensionTest("tts/speak_once")) << message_;
}

IN_PROC_BROWSER_TEST_F(TtsApiTest, PlatformSpeakInterrupt) {
  // One utterances starts speaking, and then a second interrupts.
  InSequence s;
  EXPECT_CALL(mock_platform_impl_, StopSpeaking())
      .WillOnce(Return(true));
  EXPECT_CALL(mock_platform_impl_, Speak("text 1", _, _, _, _, _))
      .WillOnce(Return(true));

  // Ensure that the first utterance keeps going until it's interrupted.
  EXPECT_CALL(mock_platform_impl_, IsSpeaking())
      .Times(AnyNumber())
      .WillRepeatedly(Return(true));

  // Expect the second utterance and allow it to continue for two calls to
  // IsSpeaking and then finish successfully.
  EXPECT_CALL(mock_platform_impl_, StopSpeaking())
      .WillOnce(Return(true));
  EXPECT_CALL(mock_platform_impl_, Speak("text 2", _, _, _, _, _))
      .WillOnce(Return(true));
  EXPECT_CALL(mock_platform_impl_, IsSpeaking())
      .WillOnce(Return(true))
      .WillOnce(Return(true))
      .WillOnce(Return(false));
  ASSERT_TRUE(RunExtensionTest("tts/interrupt")) << message_;
}

IN_PROC_BROWSER_TEST_F(TtsApiTest, PlatformSpeakQueueInterrupt) {
  // In this test, two utterances are queued, and then a third
  // interrupts. Speak() never gets called on the second utterance.
  InSequence s;
  EXPECT_CALL(mock_platform_impl_, StopSpeaking())
      .WillOnce(Return(true));
  EXPECT_CALL(mock_platform_impl_, Speak("text 1", _, _, _, _, _))
      .WillOnce(Return(true));

  // Ensure that the first utterance keeps going until it's interrupted.
  EXPECT_CALL(mock_platform_impl_, IsSpeaking())
      .Times(AnyNumber())
      .WillRepeatedly(Return(true));

  // Expect the third utterance and allow it to continue for two calls to
  // IsSpeaking and then finish successfully.
  EXPECT_CALL(mock_platform_impl_, StopSpeaking())
      .WillOnce(Return(true));
  EXPECT_CALL(mock_platform_impl_, Speak("text 3", _, _, _, _, _))
      .WillOnce(Return(true));
  EXPECT_CALL(mock_platform_impl_, IsSpeaking())
      .WillOnce(Return(true))
      .WillOnce(Return(true))
      .WillOnce(Return(false));
  ASSERT_TRUE(RunExtensionTest("tts/queue_interrupt")) << message_;
}

IN_PROC_BROWSER_TEST_F(TtsApiTest, PlatformSpeakEnqueue) {
  InSequence s;
  EXPECT_CALL(mock_platform_impl_, StopSpeaking())
      .WillOnce(Return(true));
  EXPECT_CALL(mock_platform_impl_, Speak("text 1", _, _, _, _, _))
      .WillOnce(Return(true));
  EXPECT_CALL(mock_platform_impl_, IsSpeaking())
      .WillOnce(Return(true))
      .WillOnce(Return(true))
      .WillOnce(Return(false));
  EXPECT_CALL(mock_platform_impl_, Speak("text 2", _, _, _, _, _))
      .WillOnce(Return(true));
  EXPECT_CALL(mock_platform_impl_, IsSpeaking())
      .WillOnce(Return(true))
      .WillOnce(Return(true))
      .WillOnce(Return(false));
  ASSERT_TRUE(RunExtensionTest("tts/enqueue")) << message_;
}

IN_PROC_BROWSER_TEST_F(TtsApiTest, PlatformSpeakError) {
  InSequence s;
  EXPECT_CALL(mock_platform_impl_, StopSpeaking())
      .WillOnce(Return(true));
  EXPECT_CALL(mock_platform_impl_, Speak(_, _, _, _, _, _))
      .WillOnce(Return(true));
  EXPECT_CALL(mock_platform_impl_, IsSpeaking())
      .WillOnce(Return(false));
  EXPECT_CALL(mock_platform_impl_, StopSpeaking())
      .WillOnce(Return(true));
  EXPECT_CALL(mock_platform_impl_, Speak(_, _, _, _, _, _))
      .WillOnce(DoAll(
          InvokeWithoutArgs(
              CreateFunctor(&mock_platform_impl_,
                            &MockExtensionTtsPlatformImpl::SetErrorToEpicFail)),
          Return(false)));
  EXPECT_CALL(mock_platform_impl_, StopSpeaking())
      .WillOnce(Return(true));
  EXPECT_CALL(mock_platform_impl_, Speak(_, _, _, _, _, _))
      .WillOnce(Return(true));
  EXPECT_CALL(mock_platform_impl_, IsSpeaking())
      .WillOnce(Return(false));
  ASSERT_TRUE(RunExtensionTest("tts/speak_error")) << message_;
}

#if defined(OS_WIN)
// Flakily fails on Windows: http://crbug.com/70198
#define MAYBE_Provide FLAKY_Provide
#else
#define MAYBE_Provide Provide
#endif
IN_PROC_BROWSER_TEST_F(TtsApiTest, MAYBE_Provide) {
  EXPECT_CALL(mock_platform_impl_, StopSpeaking())
      .WillRepeatedly(Return(true));
  EXPECT_CALL(mock_platform_impl_, IsSpeaking())
      .WillRepeatedly(Return(false));

  {
    InSequence s;
    EXPECT_CALL(mock_platform_impl_, Speak("native speech", _, _, _, _, _))
        .WillOnce(Return(true));
    EXPECT_CALL(mock_platform_impl_, Speak("native speech 2", _, _, _, _, _))
        .WillOnce(Return(true));
    EXPECT_CALL(mock_platform_impl_, Speak("native speech 3", _, _, _, _, _))
        .WillOnce(Return(true));
  }

  ASSERT_TRUE(RunExtensionTest("tts/provide")) << message_;
}

#if defined(OS_CHROMEOS)
IN_PROC_BROWSER_TEST_F(ExtensionApiTest, TtsChromeOs) {
  CommandLine::ForCurrentProcess()->AppendSwitch(
      switches::kEnableExperimentalExtensionApis);

  chromeos::CrosMock crosMock;
  crosMock.InitMockSpeechSynthesisLibrary();
  crosMock.SetSpeechSynthesisLibraryExpectations();

  ASSERT_TRUE(RunExtensionTest("tts/chromeos")) << message_;
}
#endif