cros_mock.cc revision bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293
1// Copyright (c) 2010 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/chromeos/cros/cros_mock.h"
6
7#include "base/message_loop.h"
8#include "base/ref_counted.h"
9#include "base/time.h"
10#include "chrome/browser/browser.h"
11#include "chrome/browser/browser_process.h"
12#include "chrome/browser/chromeos/cros/mock_cryptohome_library.h"
13#include "chrome/browser/chromeos/cros/mock_input_method_library.h"
14#include "chrome/browser/chromeos/cros/mock_keyboard_library.h"
15#include "chrome/browser/chromeos/cros/mock_library_loader.h"
16#include "chrome/browser/chromeos/cros/mock_network_library.h"
17#include "chrome/browser/chromeos/cros/mock_power_library.h"
18#include "chrome/browser/chromeos/cros/mock_screen_lock_library.h"
19#include "chrome/browser/chromeos/cros/mock_speech_synthesis_library.h"
20#include "chrome/browser/chromeos/cros/mock_system_library.h"
21#include "chrome/browser/chromeos/cros/mock_touchpad_library.h"
22#include "chrome/browser/chromeos/login/wizard_controller.h"
23#include "chrome/browser/chromeos/login/wizard_screen.h"
24#include "chrome/test/in_process_browser_test.h"
25#include "chrome/test/ui_test_utils.h"
26#include "testing/gmock/include/gmock/gmock.h"
27#include "testing/gtest/include/gtest/gtest.h"
28
29namespace chromeos {
30
31using ::testing::AnyNumber;
32using ::testing::InvokeWithoutArgs;
33using ::testing::Return;
34using ::testing::ReturnRef;
35using ::testing::StrictMock;
36using ::testing::_;
37
38CrosMock::CrosMock()
39    : loader_(NULL),
40      mock_cryptohome_library_(NULL),
41      mock_keyboard_library_(NULL),
42      mock_input_method_library_(NULL),
43      mock_network_library_(NULL),
44      mock_power_library_(NULL),
45      mock_screen_lock_library_(NULL),
46      mock_speech_synthesis_library_(NULL),
47      mock_system_library_(NULL),
48      mock_touchpad_library_(NULL) {}
49
50CrosMock::~CrosMock() {
51}
52
53chromeos::CrosLibrary::TestApi* CrosMock::test_api() {
54  return chromeos::CrosLibrary::Get()->GetTestApi();
55}
56
57void CrosMock::InitStatusAreaMocks() {
58  InitMockKeyboardLibrary();
59  InitMockInputMethodLibrary();
60  InitMockNetworkLibrary();
61  InitMockPowerLibrary();
62  InitMockTouchpadLibrary();
63  InitMockSystemLibrary();
64}
65
66void CrosMock::InitMockLibraryLoader() {
67  if (loader_)
68    return;
69  loader_ = new StrictMock<MockLibraryLoader>();
70  EXPECT_CALL(*loader_, Load(_))
71      .Times(AnyNumber())
72      .WillRepeatedly(Return(true));
73  test_api()->SetLibraryLoader(loader_, true);
74}
75
76void CrosMock::InitMockCryptohomeLibrary() {
77  InitMockLibraryLoader();
78  if (mock_cryptohome_library_)
79    return;
80  mock_cryptohome_library_ = new StrictMock<MockCryptohomeLibrary>();
81  test_api()->SetCryptohomeLibrary(mock_cryptohome_library_, true);
82}
83
84void CrosMock::InitMockKeyboardLibrary() {
85  InitMockLibraryLoader();
86  if (mock_keyboard_library_)
87    return;
88  mock_keyboard_library_ = new StrictMock<MockKeyboardLibrary>();
89  test_api()->SetKeyboardLibrary(mock_keyboard_library_, true);
90}
91
92void CrosMock::InitMockInputMethodLibrary() {
93  InitMockLibraryLoader();
94  if (mock_input_method_library_)
95    return;
96  mock_input_method_library_ = new StrictMock<MockInputMethodLibrary>();
97  test_api()->SetInputMethodLibrary(mock_input_method_library_, true);
98}
99
100void CrosMock::InitMockNetworkLibrary() {
101  InitMockLibraryLoader();
102  if (mock_network_library_)
103    return;
104  mock_network_library_ = new StrictMock<MockNetworkLibrary>();
105  test_api()->SetNetworkLibrary(mock_network_library_, true);
106}
107
108void CrosMock::InitMockPowerLibrary() {
109  InitMockLibraryLoader();
110  if (mock_power_library_)
111    return;
112  mock_power_library_ = new StrictMock<MockPowerLibrary>();
113  test_api()->SetPowerLibrary(mock_power_library_, true);
114}
115
116void CrosMock::InitMockScreenLockLibrary() {
117  InitMockLibraryLoader();
118  if (mock_screen_lock_library_)
119    return;
120  mock_screen_lock_library_ = new StrictMock<MockScreenLockLibrary>();
121  test_api()->SetScreenLockLibrary(mock_screen_lock_library_, true);
122}
123
124void CrosMock::InitMockSpeechSynthesisLibrary() {
125  InitMockLibraryLoader();
126  if (mock_speech_synthesis_library_)
127    return;
128  mock_speech_synthesis_library_ =
129      new StrictMock<MockSpeechSynthesisLibrary>();
130  test_api()->SetSpeechSynthesisLibrary(mock_speech_synthesis_library_, true);
131}
132
133void CrosMock::InitMockTouchpadLibrary() {
134  InitMockLibraryLoader();
135  if (mock_touchpad_library_)
136    return;
137  mock_touchpad_library_ = new StrictMock<MockTouchpadLibrary>();
138  test_api()->SetTouchpadLibrary(mock_touchpad_library_, true);
139}
140
141void CrosMock::InitMockSystemLibrary() {
142  InitMockLibraryLoader();
143  if (mock_system_library_)
144    return;
145  mock_system_library_ = new StrictMock<MockSystemLibrary>();
146  test_api()->SetSystemLibrary(mock_system_library_, true);
147}
148
149// Initialization of mocks.
150MockCryptohomeLibrary* CrosMock::mock_cryptohome_library() {
151  return mock_cryptohome_library_;
152}
153
154MockKeyboardLibrary* CrosMock::mock_keyboard_library() {
155  return mock_keyboard_library_;
156}
157
158MockInputMethodLibrary* CrosMock::mock_input_method_library() {
159  return mock_input_method_library_;
160}
161
162MockNetworkLibrary* CrosMock::mock_network_library() {
163  return mock_network_library_;
164}
165
166MockPowerLibrary* CrosMock::mock_power_library() {
167  return mock_power_library_;
168}
169
170MockScreenLockLibrary* CrosMock::mock_screen_lock_library() {
171  return mock_screen_lock_library_;
172}
173
174MockSpeechSynthesisLibrary* CrosMock::mock_speech_synthesis_library() {
175  return mock_speech_synthesis_library_;
176}
177
178MockSystemLibrary* CrosMock::mock_system_library() {
179  return mock_system_library_;
180}
181
182MockTouchpadLibrary* CrosMock::mock_touchpad_library() {
183  return mock_touchpad_library_;
184}
185
186void CrosMock::SetStatusAreaMocksExpectations() {
187  SetKeyboardLibraryStatusAreaExpectations();
188  SetInputMethodLibraryStatusAreaExpectations();
189  SetNetworkLibraryStatusAreaExpectations();
190  SetPowerLibraryStatusAreaExpectations();
191  SetTouchpadLibraryExpectations();
192  SetSystemLibraryStatusAreaExpectations();
193}
194
195void CrosMock::SetKeyboardLibraryStatusAreaExpectations() {
196  EXPECT_CALL(*mock_keyboard_library_, GetHardwareKeyboardLayoutName())
197      .Times(AnyNumber())
198      .WillRepeatedly((Return("xkb:us::eng")))
199      .RetiresOnSaturation();
200  EXPECT_CALL(*mock_keyboard_library_, GetCurrentKeyboardLayoutName())
201      .Times(AnyNumber())
202      .WillRepeatedly((Return("us")))
203      .RetiresOnSaturation();
204  EXPECT_CALL(*mock_keyboard_library_, SetCurrentKeyboardLayoutByName(_))
205      .Times(AnyNumber())
206      .WillRepeatedly((Return(true)))
207      .RetiresOnSaturation();
208  EXPECT_CALL(*mock_keyboard_library_, RemapModifierKeys(_))
209      .Times(AnyNumber())
210      .WillRepeatedly((Return(true)))
211      .RetiresOnSaturation();
212  EXPECT_CALL(*mock_keyboard_library_, SetKeyboardLayoutPerWindow(_))
213      .Times(AnyNumber())
214      .WillRepeatedly((Return(true)))
215      .RetiresOnSaturation();
216  EXPECT_CALL(*mock_keyboard_library_, GetKeyboardLayoutPerWindow(_))
217      .Times(AnyNumber())
218      .WillRepeatedly((Return(true)))
219      .RetiresOnSaturation();
220  EXPECT_CALL(*mock_keyboard_library_, GetAutoRepeatEnabled(_))
221      .Times(AnyNumber())
222      .WillRepeatedly((Return(true)))
223      .RetiresOnSaturation();
224  EXPECT_CALL(*mock_keyboard_library_, SetAutoRepeatEnabled(_))
225      .Times(AnyNumber())
226      .WillRepeatedly((Return(true)))
227      .RetiresOnSaturation();
228  EXPECT_CALL(*mock_keyboard_library_, GetAutoRepeatRate(_))
229      .Times(AnyNumber())
230      .WillRepeatedly((Return(true)))
231      .RetiresOnSaturation();
232  EXPECT_CALL(*mock_keyboard_library_, SetAutoRepeatRate(_))
233      .Times(AnyNumber())
234      .WillRepeatedly((Return(true)))
235      .RetiresOnSaturation();
236}
237
238void CrosMock::SetInputMethodLibraryStatusAreaExpectations() {
239  EXPECT_CALL(*mock_input_method_library_, AddObserver(_))
240      .Times(1)
241      .RetiresOnSaturation();
242  EXPECT_CALL(*mock_input_method_library_, GetActiveInputMethods())
243      .Times(AnyNumber())
244      .WillRepeatedly(InvokeWithoutArgs(CreateFallbackInputMethodDescriptors))
245      .RetiresOnSaturation();
246  EXPECT_CALL(*mock_input_method_library_, GetSupportedInputMethods())
247      .Times(AnyNumber())
248      .WillRepeatedly(InvokeWithoutArgs(CreateFallbackInputMethodDescriptors))
249      .RetiresOnSaturation();
250  EXPECT_CALL(*mock_input_method_library_, current_ime_properties())
251      .Times(1)
252      .WillOnce((ReturnRef(ime_properties_)))
253      .RetiresOnSaturation();
254  EXPECT_CALL(*mock_input_method_library_, SetImeConfig(_, _, _))
255      .Times(AnyNumber())
256      .WillRepeatedly((Return(true)))
257      .RetiresOnSaturation();
258  EXPECT_CALL(*mock_input_method_library_, RemoveObserver(_))
259      .Times(1)
260      .RetiresOnSaturation();
261  EXPECT_CALL(*mock_input_method_library_, SetDeferImeStartup(_))
262      .Times(AnyNumber())
263      .RetiresOnSaturation();
264  EXPECT_CALL(*mock_input_method_library_, StopInputMethodProcesses())
265      .Times(AnyNumber())
266      .RetiresOnSaturation();
267}
268
269void CrosMock::SetNetworkLibraryStatusAreaExpectations() {
270  EXPECT_CALL(*mock_network_library_, AddObserver(_))
271      .Times(1)
272      .RetiresOnSaturation();
273  EXPECT_CALL(*mock_network_library_, wifi_connecting())
274      .Times(1)
275      .WillRepeatedly((Return(false)))
276      .RetiresOnSaturation();
277  EXPECT_CALL(*mock_network_library_, wifi_connected())
278      .Times(1)
279      .WillRepeatedly((Return(false)))
280      .RetiresOnSaturation();
281  EXPECT_CALL(*mock_network_library_, cellular_connecting())
282      .Times(1)
283      .WillRepeatedly((Return(false)))
284      .RetiresOnSaturation();
285  EXPECT_CALL(*mock_network_library_, cellular_connected())
286      .Times(1)
287      .WillRepeatedly((Return(false)))
288      .RetiresOnSaturation();
289  EXPECT_CALL(*mock_network_library_, ethernet_connected())
290      .Times(1)
291      .WillRepeatedly((Return(false)))
292      .RetiresOnSaturation();
293  EXPECT_CALL(*mock_network_library_, Connected())
294      .Times(1)
295      .WillRepeatedly((Return(false)))
296      .RetiresOnSaturation();
297  EXPECT_CALL(*mock_network_library_, Connecting())
298      .Times(1)
299      .WillRepeatedly((Return(false)))
300      .RetiresOnSaturation();
301  EXPECT_CALL(*mock_network_library_, RemoveObserver(_))
302      .Times(1)
303      .RetiresOnSaturation();
304}
305
306void CrosMock::SetPowerLibraryStatusAreaExpectations() {
307  EXPECT_CALL(*mock_power_library_, AddObserver(_))
308      .Times(1)
309      .RetiresOnSaturation();
310  EXPECT_CALL(*mock_power_library_, battery_fully_charged())
311      .Times(1)
312      .WillRepeatedly((Return(false)))
313      .RetiresOnSaturation();
314  EXPECT_CALL(*mock_power_library_, battery_is_present())
315      .Times(1)
316      .WillOnce((Return(true)))
317      .RetiresOnSaturation();
318  EXPECT_CALL(*mock_power_library_, battery_percentage())
319      .Times(1)
320      .WillRepeatedly((Return(42.0)))
321      .RetiresOnSaturation();
322  EXPECT_CALL(*mock_power_library_, line_power_on())
323      .Times(1)
324      .WillRepeatedly((Return(false)))
325      .RetiresOnSaturation();
326  EXPECT_CALL(*mock_power_library_, battery_time_to_empty())
327      .Times(1)
328      .WillRepeatedly((Return(base::TimeDelta::FromMinutes(42))))
329      .RetiresOnSaturation();
330  EXPECT_CALL(*mock_power_library_, battery_time_to_full())
331      .Times(1)
332      .WillRepeatedly((Return(base::TimeDelta::FromMinutes(24))))
333      .RetiresOnSaturation();
334  EXPECT_CALL(*mock_power_library_, RemoveObserver(_))
335      .Times(1)
336      .RetiresOnSaturation();
337}
338
339void CrosMock::SetSpeechSynthesisLibraryExpectations() {
340  EXPECT_CALL(*mock_speech_synthesis_library_, Speak(_))
341      .Times(1)
342      .WillOnce(Return(true))
343      .RetiresOnSaturation();
344  EXPECT_CALL(*mock_speech_synthesis_library_, StopSpeaking())
345      .Times(1)
346      .WillOnce(Return(true))
347      .RetiresOnSaturation();
348  EXPECT_CALL(*mock_speech_synthesis_library_, IsSpeaking())
349      .Times(4)
350      .WillOnce(Return(true))
351      .WillOnce(Return(true))
352      .WillOnce(Return(true))
353      .WillOnce(Return(false))
354      .RetiresOnSaturation();
355}
356
357void CrosMock::SetSystemLibraryStatusAreaExpectations() {
358  EXPECT_CALL(*mock_system_library_, AddObserver(_))
359      .Times(1)
360      .RetiresOnSaturation();
361  EXPECT_CALL(*mock_system_library_, RemoveObserver(_))
362      .Times(1)
363      .RetiresOnSaturation();
364}
365
366void CrosMock::SetTouchpadLibraryExpectations() {
367  EXPECT_CALL(*mock_touchpad_library_, SetSensitivity(_))
368      .Times(AnyNumber());
369  EXPECT_CALL(*mock_touchpad_library_, SetTapToClick(_))
370      .Times(AnyNumber());
371}
372
373void CrosMock::SetSystemLibraryExpectations() {
374  EXPECT_CALL(*mock_system_library_, GetTimezone())
375      .Times(AnyNumber());
376  EXPECT_CALL(*mock_system_library_, SetTimezone(_))
377      .Times(AnyNumber());
378}
379
380void CrosMock::TearDownMocks() {
381  // Prevent bogus gMock leak check from firing.
382  if (loader_)
383    test_api()->SetLibraryLoader(NULL, false);
384  if (mock_cryptohome_library_)
385    test_api()->SetCryptohomeLibrary(NULL, false);
386  if (mock_keyboard_library_)
387    test_api()->SetKeyboardLibrary(NULL, false);
388  if (mock_input_method_library_)
389    test_api()->SetInputMethodLibrary(NULL, false);
390  if (mock_network_library_)
391    test_api()->SetNetworkLibrary(NULL, false);
392  if (mock_power_library_)
393    test_api()->SetPowerLibrary(NULL, false);
394  if (mock_screen_lock_library_)
395    test_api()->SetScreenLockLibrary(NULL, false);
396  if (mock_speech_synthesis_library_)
397    test_api()->SetSpeechSynthesisLibrary(NULL, false);
398  if (mock_system_library_)
399    test_api()->SetSystemLibrary(NULL, false);
400  if (mock_touchpad_library_)
401    test_api()->SetTouchpadLibrary(NULL, false);
402}
403
404}  // namespace chromeos
405