Home | History | Annotate | Download | only in cros
      1 // Copyright (c) 2011 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/memory/ref_counted.h"
      8 #include "base/message_loop.h"
      9 #include "base/time.h"
     10 #include "chrome/browser/chromeos/cros/mock_cryptohome_library.h"
     11 #include "chrome/browser/chromeos/cros/mock_input_method_library.h"
     12 #include "chrome/browser/chromeos/cros/mock_library_loader.h"
     13 #include "chrome/browser/chromeos/cros/mock_network_library.h"
     14 #include "chrome/browser/chromeos/cros/mock_power_library.h"
     15 #include "chrome/browser/chromeos/cros/mock_screen_lock_library.h"
     16 #include "chrome/browser/chromeos/cros/mock_speech_synthesis_library.h"
     17 #include "chrome/browser/chromeos/cros/mock_touchpad_library.h"
     18 #include "chrome/browser/chromeos/input_method/input_method_util.h"
     19 #include "chrome/browser/chromeos/login/wizard_controller.h"
     20 #include "chrome/browser/chromeos/login/wizard_screen.h"
     21 #include "chrome/test/in_process_browser_test.h"
     22 #include "chrome/test/ui_test_utils.h"
     23 #include "testing/gmock/include/gmock/gmock.h"
     24 #include "testing/gtest/include/gtest/gtest.h"
     25 
     26 namespace chromeos {
     27 
     28 using ::testing::AnyNumber;
     29 using ::testing::AtMost;
     30 using ::testing::InSequence;
     31 using ::testing::InvokeWithoutArgs;
     32 using ::testing::Return;
     33 using ::testing::ReturnRef;
     34 using ::testing::StrictMock;
     35 using ::testing::_;
     36 
     37 CrosMock::CrosMock()
     38     : loader_(NULL),
     39       mock_cryptohome_library_(NULL),
     40       mock_input_method_library_(NULL),
     41       mock_network_library_(NULL),
     42       mock_power_library_(NULL),
     43       mock_screen_lock_library_(NULL),
     44       mock_speech_synthesis_library_(NULL),
     45       mock_touchpad_library_(NULL),
     46       current_input_method_("", "", "", ""),
     47       previous_input_method_("", "", "", "") {
     48   current_input_method_ =
     49       input_method::GetFallbackInputMethodDescriptor();
     50 }
     51 
     52 CrosMock::~CrosMock() {
     53 }
     54 
     55 chromeos::CrosLibrary::TestApi* CrosMock::test_api() {
     56   return chromeos::CrosLibrary::Get()->GetTestApi();
     57 }
     58 
     59 void CrosMock::InitStatusAreaMocks() {
     60   InitMockInputMethodLibrary();
     61   InitMockNetworkLibrary();
     62   InitMockPowerLibrary();
     63   InitMockTouchpadLibrary();
     64 }
     65 
     66 void 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 
     76 void 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 
     84 void CrosMock::InitMockInputMethodLibrary() {
     85   InitMockLibraryLoader();
     86   if (mock_input_method_library_)
     87     return;
     88   mock_input_method_library_ = new StrictMock<MockInputMethodLibrary>();
     89   test_api()->SetInputMethodLibrary(mock_input_method_library_, true);
     90 }
     91 
     92 void CrosMock::InitMockNetworkLibrary() {
     93   InitMockLibraryLoader();
     94   if (mock_network_library_)
     95     return;
     96   mock_network_library_ = new StrictMock<MockNetworkLibrary>();
     97   test_api()->SetNetworkLibrary(mock_network_library_, true);
     98 }
     99 
    100 void CrosMock::InitMockPowerLibrary() {
    101   InitMockLibraryLoader();
    102   if (mock_power_library_)
    103     return;
    104   mock_power_library_ = new StrictMock<MockPowerLibrary>();
    105   test_api()->SetPowerLibrary(mock_power_library_, true);
    106 }
    107 
    108 void CrosMock::InitMockScreenLockLibrary() {
    109   InitMockLibraryLoader();
    110   if (mock_screen_lock_library_)
    111     return;
    112   mock_screen_lock_library_ = new StrictMock<MockScreenLockLibrary>();
    113   test_api()->SetScreenLockLibrary(mock_screen_lock_library_, true);
    114 }
    115 
    116 void CrosMock::InitMockSpeechSynthesisLibrary() {
    117   InitMockLibraryLoader();
    118   if (mock_speech_synthesis_library_)
    119     return;
    120   mock_speech_synthesis_library_ =
    121       new StrictMock<MockSpeechSynthesisLibrary>();
    122   test_api()->SetSpeechSynthesisLibrary(mock_speech_synthesis_library_, true);
    123 }
    124 
    125 void CrosMock::InitMockTouchpadLibrary() {
    126   InitMockLibraryLoader();
    127   if (mock_touchpad_library_)
    128     return;
    129   mock_touchpad_library_ = new StrictMock<MockTouchpadLibrary>();
    130   test_api()->SetTouchpadLibrary(mock_touchpad_library_, true);
    131 }
    132 
    133 // Initialization of mocks.
    134 MockCryptohomeLibrary* CrosMock::mock_cryptohome_library() {
    135   return mock_cryptohome_library_;
    136 }
    137 
    138 MockInputMethodLibrary* CrosMock::mock_input_method_library() {
    139   return mock_input_method_library_;
    140 }
    141 
    142 MockNetworkLibrary* CrosMock::mock_network_library() {
    143   return mock_network_library_;
    144 }
    145 
    146 MockPowerLibrary* CrosMock::mock_power_library() {
    147   return mock_power_library_;
    148 }
    149 
    150 MockScreenLockLibrary* CrosMock::mock_screen_lock_library() {
    151   return mock_screen_lock_library_;
    152 }
    153 
    154 MockSpeechSynthesisLibrary* CrosMock::mock_speech_synthesis_library() {
    155   return mock_speech_synthesis_library_;
    156 }
    157 
    158 MockTouchpadLibrary* CrosMock::mock_touchpad_library() {
    159   return mock_touchpad_library_;
    160 }
    161 
    162 void CrosMock::SetStatusAreaMocksExpectations() {
    163   SetInputMethodLibraryStatusAreaExpectations();
    164   SetNetworkLibraryStatusAreaExpectations();
    165   SetPowerLibraryStatusAreaExpectations();
    166   SetPowerLibraryExpectations();
    167   SetTouchpadLibraryExpectations();
    168 }
    169 
    170 void CrosMock::SetInputMethodLibraryStatusAreaExpectations() {
    171   EXPECT_CALL(*mock_input_method_library_, AddObserver(_))
    172       .Times(AnyNumber())
    173       .RetiresOnSaturation();
    174   EXPECT_CALL(*mock_input_method_library_, GetActiveInputMethods())
    175       .Times(AnyNumber())
    176       .WillRepeatedly(InvokeWithoutArgs(CreateInputMethodDescriptors))
    177       .RetiresOnSaturation();
    178   EXPECT_CALL(*mock_input_method_library_, GetSupportedInputMethods())
    179       .Times(AnyNumber())
    180       .WillRepeatedly(InvokeWithoutArgs(CreateInputMethodDescriptors))
    181       .RetiresOnSaturation();
    182   EXPECT_CALL(*mock_input_method_library_, GetKeyboardOverlayId(_))
    183       .Times(AnyNumber())
    184       .WillRepeatedly((Return("en_US")))
    185       .RetiresOnSaturation();
    186   EXPECT_CALL(*mock_input_method_library_, current_input_method())
    187       .Times(AnyNumber())
    188       .WillRepeatedly((Return(current_input_method_)))
    189       .RetiresOnSaturation();
    190   EXPECT_CALL(*mock_input_method_library_, previous_input_method())
    191       .Times(AnyNumber())
    192       .WillRepeatedly((Return(previous_input_method_)))
    193       .RetiresOnSaturation();
    194   EXPECT_CALL(*mock_input_method_library_, current_ime_properties())
    195       .Times(AnyNumber())
    196       .WillRepeatedly((ReturnRef(ime_properties_)))
    197       .RetiresOnSaturation();
    198   EXPECT_CALL(*mock_input_method_library_, GetNumActiveInputMethods())
    199       .Times(AnyNumber())
    200       .WillRepeatedly((Return(1)))
    201       .RetiresOnSaturation();
    202   EXPECT_CALL(*mock_input_method_library_, SetImeConfig(_, _, _))
    203       .Times(AnyNumber())
    204       .WillRepeatedly((Return(true)))
    205       .RetiresOnSaturation();
    206   EXPECT_CALL(*mock_input_method_library_, RemoveObserver(_))
    207       .Times(AnyNumber())
    208       .RetiresOnSaturation();
    209   EXPECT_CALL(*mock_input_method_library_, SetDeferImeStartup(_))
    210       .Times(AnyNumber())
    211       .RetiresOnSaturation();
    212   EXPECT_CALL(*mock_input_method_library_, StopInputMethodDaemon())
    213       .Times(AnyNumber())
    214       .RetiresOnSaturation();
    215   EXPECT_CALL(*mock_input_method_library_, ChangeInputMethod(_))
    216       .Times(AnyNumber())
    217       .RetiresOnSaturation();
    218 }
    219 
    220 void CrosMock::SetNetworkLibraryStatusAreaExpectations() {
    221   // We don't care how often these are called, just set their return values:
    222   EXPECT_CALL(*mock_network_library_, AddNetworkManagerObserver(_))
    223       .Times(AnyNumber());
    224   EXPECT_CALL(*mock_network_library_, AddNetworkDeviceObserver(_, _))
    225       .Times(AnyNumber());
    226   EXPECT_CALL(*mock_network_library_, AddCellularDataPlanObserver(_))
    227       .Times(AnyNumber());
    228   EXPECT_CALL(*mock_network_library_, RemoveNetworkManagerObserver(_))
    229       .Times(AnyNumber());
    230   EXPECT_CALL(*mock_network_library_, RemoveNetworkDeviceObserver(_, _))
    231       .Times(AnyNumber());
    232   EXPECT_CALL(*mock_network_library_, RemoveObserverForAllNetworks(_))
    233       .Times(AnyNumber());
    234   EXPECT_CALL(*mock_network_library_, RemoveCellularDataPlanObserver(_))
    235       .Times(AnyNumber());
    236   EXPECT_CALL(*mock_network_library_, IsLocked())
    237       .Times(AnyNumber())
    238       .WillRepeatedly((Return(false)));
    239   EXPECT_CALL(*mock_network_library_, FindCellularDevice())
    240       .Times(AnyNumber())
    241       .WillRepeatedly((Return((const NetworkDevice*)(NULL))));
    242   EXPECT_CALL(*mock_network_library_, ethernet_available())
    243       .Times(AnyNumber())
    244       .WillRepeatedly((Return(true)));
    245   EXPECT_CALL(*mock_network_library_, wifi_available())
    246       .Times(AnyNumber())
    247       .WillRepeatedly((Return(false)));
    248   EXPECT_CALL(*mock_network_library_, cellular_available())
    249       .Times(AnyNumber())
    250       .WillRepeatedly((Return(false)));
    251   EXPECT_CALL(*mock_network_library_, ethernet_enabled())
    252       .Times(AnyNumber())
    253       .WillRepeatedly((Return(true)));
    254   EXPECT_CALL(*mock_network_library_, wifi_enabled())
    255       .Times(AnyNumber())
    256       .WillRepeatedly((Return(false)));
    257   EXPECT_CALL(*mock_network_library_, cellular_enabled())
    258       .Times(AnyNumber())
    259       .WillRepeatedly((Return(false)));
    260   EXPECT_CALL(*mock_network_library_, active_network())
    261       .Times(AnyNumber())
    262       .WillRepeatedly((Return((const Network*)(NULL))));
    263   EXPECT_CALL(*mock_network_library_, wifi_network())
    264       .Times(AnyNumber())
    265       .WillRepeatedly((Return((const WifiNetwork*)(NULL))));
    266   EXPECT_CALL(*mock_network_library_, cellular_network())
    267       .Times(AnyNumber())
    268       .WillRepeatedly((Return((const CellularNetwork*)(NULL))));
    269   EXPECT_CALL(*mock_network_library_, virtual_network())
    270       .Times(AnyNumber())
    271       .WillRepeatedly((Return((const VirtualNetwork*)(NULL))));
    272   EXPECT_CALL(*mock_network_library_, wifi_networks())
    273       .Times(AnyNumber())
    274       .WillRepeatedly((ReturnRef(wifi_networks_)));
    275   EXPECT_CALL(*mock_network_library_, cellular_networks())
    276       .Times(AnyNumber())
    277       .WillRepeatedly((ReturnRef(cellular_networks_)));
    278   EXPECT_CALL(*mock_network_library_, virtual_networks())
    279       .Times(AnyNumber())
    280       .WillRepeatedly((ReturnRef(virtual_networks_)));
    281 
    282   // Set specific expectations for interesting functions:
    283 
    284   // NetworkMenuButton::OnNetworkChanged() calls:
    285   EXPECT_CALL(*mock_network_library_, Connected())
    286       .Times(AnyNumber())
    287       .WillRepeatedly((Return(false)))
    288       .RetiresOnSaturation();
    289   EXPECT_CALL(*mock_network_library_, Connecting())
    290       .Times(AnyNumber())
    291       .WillRepeatedly((Return(false)))
    292       .RetiresOnSaturation();
    293   EXPECT_CALL(*mock_network_library_, cellular_connected())
    294       .Times(AnyNumber())
    295       .WillRepeatedly((Return(false)))
    296       .RetiresOnSaturation();
    297 
    298   // NetworkMenu::InitMenuItems() calls:
    299   EXPECT_CALL(*mock_network_library_, IsLocked())
    300       .Times(AnyNumber())
    301       .WillRepeatedly((Return(false)))
    302       .RetiresOnSaturation();
    303   EXPECT_CALL(*mock_network_library_, ethernet_connected())
    304       .Times(1)
    305       .WillRepeatedly((Return(false)))
    306       .RetiresOnSaturation();
    307   EXPECT_CALL(*mock_network_library_, ethernet_connecting())
    308       .Times(1)
    309       .WillRepeatedly((Return(false)))
    310       .RetiresOnSaturation();
    311 }
    312 
    313 void CrosMock::SetPowerLibraryStatusAreaExpectations() {
    314   EXPECT_CALL(*mock_power_library_, AddObserver(_))
    315       .Times(2)
    316       .RetiresOnSaturation();
    317   EXPECT_CALL(*mock_power_library_, battery_fully_charged())
    318       .Times(1)
    319       .WillRepeatedly((Return(false)))
    320       .RetiresOnSaturation();
    321   EXPECT_CALL(*mock_power_library_, battery_is_present())
    322       .Times(1)
    323       .WillOnce((Return(true)))
    324       .RetiresOnSaturation();
    325   EXPECT_CALL(*mock_power_library_, battery_percentage())
    326       .Times(1)
    327       .WillRepeatedly((Return(42.0)))
    328       .RetiresOnSaturation();
    329   EXPECT_CALL(*mock_power_library_, line_power_on())
    330       .Times(1)
    331       .WillRepeatedly((Return(false)))
    332       .RetiresOnSaturation();
    333   EXPECT_CALL(*mock_power_library_, battery_time_to_empty())
    334       .Times(1)
    335       .WillRepeatedly((Return(base::TimeDelta::FromMinutes(42))))
    336       .RetiresOnSaturation();
    337   EXPECT_CALL(*mock_power_library_, battery_time_to_full())
    338       .Times(1)
    339       .WillRepeatedly((Return(base::TimeDelta::FromMinutes(24))))
    340       .RetiresOnSaturation();
    341   EXPECT_CALL(*mock_power_library_, RemoveObserver(_))
    342       .Times(2)
    343       .RetiresOnSaturation();
    344 }
    345 
    346 void CrosMock::SetPowerLibraryExpectations() {
    347   // EnableScreenLock is currently bounded with a prefs value and thus is
    348   // always called when loading
    349   EXPECT_CALL(*mock_power_library_, EnableScreenLock(_))
    350       .Times(AnyNumber());
    351 }
    352 
    353 void CrosMock::SetSpeechSynthesisLibraryExpectations() {
    354   InSequence s;
    355   EXPECT_CALL(*mock_speech_synthesis_library_, StopSpeaking())
    356       .WillOnce(Return(true))
    357       .RetiresOnSaturation();
    358   EXPECT_CALL(*mock_speech_synthesis_library_, Speak(_))
    359       .WillOnce(Return(true))
    360       .RetiresOnSaturation();
    361   EXPECT_CALL(*mock_speech_synthesis_library_, IsSpeaking())
    362       .Times(AnyNumber())
    363       .WillRepeatedly(Return(true));
    364   EXPECT_CALL(*mock_speech_synthesis_library_, StopSpeaking())
    365       .WillOnce(Return(true))
    366       .RetiresOnSaturation();
    367   EXPECT_CALL(*mock_speech_synthesis_library_, Speak(_))
    368       .WillOnce(Return(true))
    369       .RetiresOnSaturation();
    370   EXPECT_CALL(*mock_speech_synthesis_library_, IsSpeaking())
    371       .WillOnce(Return(true))
    372       .WillOnce(Return(true))
    373       .WillOnce(Return(false))
    374       .RetiresOnSaturation();
    375 }
    376 
    377 void CrosMock::SetTouchpadLibraryExpectations() {
    378   EXPECT_CALL(*mock_touchpad_library_, SetSensitivity(_))
    379       .Times(AnyNumber());
    380   EXPECT_CALL(*mock_touchpad_library_, SetTapToClick(_))
    381       .Times(AnyNumber());
    382 }
    383 
    384 void CrosMock::TearDownMocks() {
    385   // Prevent bogus gMock leak check from firing.
    386   if (loader_)
    387     test_api()->SetLibraryLoader(NULL, false);
    388   if (mock_cryptohome_library_)
    389     test_api()->SetCryptohomeLibrary(NULL, false);
    390   if (mock_input_method_library_)
    391     test_api()->SetInputMethodLibrary(NULL, false);
    392   if (mock_network_library_)
    393     test_api()->SetNetworkLibrary(NULL, false);
    394   if (mock_power_library_)
    395     test_api()->SetPowerLibrary(NULL, false);
    396   if (mock_screen_lock_library_)
    397     test_api()->SetScreenLockLibrary(NULL, false);
    398   if (mock_speech_synthesis_library_)
    399     test_api()->SetSpeechSynthesisLibrary(NULL, false);
    400   if (mock_touchpad_library_)
    401     test_api()->SetTouchpadLibrary(NULL, false);
    402 }
    403 
    404 InputMethodDescriptors* CrosMock::CreateInputMethodDescriptors() {
    405   InputMethodDescriptors* descriptors = new InputMethodDescriptors;
    406   descriptors->push_back(
    407       input_method::GetFallbackInputMethodDescriptor());
    408   return descriptors;
    409 }
    410 
    411 }  // namespace chromeos
    412