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