diff options
author | chaitanyag@chromium.org <chaitanyag@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-08-02 21:44:53 +0000 |
---|---|---|
committer | chaitanyag@chromium.org <chaitanyag@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-08-02 21:44:53 +0000 |
commit | abd701d7cd8b601b5f13c6ff2d56eccf9f22fa64 (patch) | |
tree | 721b56a99ee4735b6333573a4d5f10dabb75ab43 /chrome/browser | |
parent | dc2cb99f2b5ef4614c4cfdab74c34fbc3a37939b (diff) | |
download | chromium_src-abd701d7cd8b601b5f13c6ff2d56eccf9f22fa64.zip chromium_src-abd701d7cd8b601b5f13c6ff2d56eccf9f22fa64.tar.gz chromium_src-abd701d7cd8b601b5f13c6ff2d56eccf9f22fa64.tar.bz2 |
Refactor cros_in_process_browser_test to separate the mock initialization.
Review URL: http://codereview.chromium.org/3012017
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@54612 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser')
11 files changed, 549 insertions, 387 deletions
diff --git a/chrome/browser/chromeos/cros/cros_in_process_browser_test.cc b/chrome/browser/chromeos/cros/cros_in_process_browser_test.cc index 46304d5..ad88ae9 100644 --- a/chrome/browser/chromeos/cros/cros_in_process_browser_test.cc +++ b/chrome/browser/chromeos/cros/cros_in_process_browser_test.cc @@ -3,303 +3,19 @@ // found in the LICENSE file. #include "chrome/browser/chromeos/cros/cros_in_process_browser_test.h" - -#include "base/message_loop.h" -#include "base/ref_counted.h" -#include "base/time.h" -#include "chrome/browser/browser.h" -#include "chrome/browser/browser_process.h" -#include "chrome/browser/chromeos/cros/mock_cryptohome_library.h" -#include "chrome/browser/chromeos/cros/mock_keyboard_library.h" -#include "chrome/browser/chromeos/cros/mock_input_method_library.h" -#include "chrome/browser/chromeos/cros/mock_library_loader.h" -#include "chrome/browser/chromeos/cros/mock_network_library.h" -#include "chrome/browser/chromeos/cros/mock_power_library.h" -#include "chrome/browser/chromeos/cros/mock_screen_lock_library.h" -#include "chrome/browser/chromeos/cros/mock_synaptics_library.h" -#include "chrome/browser/chromeos/cros/mock_system_library.h" -#include "chrome/browser/chromeos/login/wizard_controller.h" -#include "chrome/browser/chromeos/login/wizard_screen.h" #include "chrome/test/in_process_browser_test.h" -#include "chrome/test/ui_test_utils.h" -#include "testing/gtest/include/gtest/gtest.h" -#include "testing/gmock/include/gmock/gmock.h" namespace chromeos { -using ::testing::AnyNumber; -using ::testing::InvokeWithoutArgs; -using ::testing::Return; -using ::testing::ReturnRef; -using ::testing::StrictMock; -using ::testing::_; - -CrosInProcessBrowserTest::CrosInProcessBrowserTest() - : loader_(NULL), - mock_cryptohome_library_(NULL), - mock_keyboard_library_(NULL), - mock_input_method_library_(NULL), - mock_network_library_(NULL), - mock_power_library_(NULL), - mock_screen_lock_library_(NULL), - mock_synaptics_library_(NULL), - mock_system_library_(NULL) {} - -CrosInProcessBrowserTest::~CrosInProcessBrowserTest() { -} - -chromeos::CrosLibrary::TestApi* CrosInProcessBrowserTest::test_api() { - return chromeos::CrosLibrary::Get()->GetTestApi(); -} - -void CrosInProcessBrowserTest::InitStatusAreaMocks() { - InitMockKeyboardLibrary(); - InitMockInputMethodLibrary(); - InitMockNetworkLibrary(); - InitMockPowerLibrary(); - InitMockSynapticsLibrary(); - InitMockSystemLibrary(); -} - -void CrosInProcessBrowserTest::InitMockLibraryLoader() { - if (loader_) - return; - loader_ = new StrictMock<MockLibraryLoader>(); - EXPECT_CALL(*loader_, Load(_)) - .Times(AnyNumber()) - .WillRepeatedly(Return(true)); - test_api()->SetLibraryLoader(loader_, true); -} - -void CrosInProcessBrowserTest::InitMockCryptohomeLibrary() { - InitMockLibraryLoader(); - if (mock_cryptohome_library_) - return; - mock_cryptohome_library_ = new StrictMock<MockCryptohomeLibrary>(); - test_api()->SetCryptohomeLibrary(mock_cryptohome_library_, true); -} - -void CrosInProcessBrowserTest::InitMockKeyboardLibrary() { - InitMockLibraryLoader(); - if (mock_keyboard_library_) - return; - mock_keyboard_library_ = new StrictMock<MockKeyboardLibrary>(); - test_api()->SetKeyboardLibrary(mock_keyboard_library_, true); -} - -void CrosInProcessBrowserTest::InitMockInputMethodLibrary() { - InitMockLibraryLoader(); - if (mock_input_method_library_) - return; - mock_input_method_library_ = new StrictMock<MockInputMethodLibrary>(); - test_api()->SetInputMethodLibrary(mock_input_method_library_, true); -} - -void CrosInProcessBrowserTest::InitMockNetworkLibrary() { - InitMockLibraryLoader(); - if (mock_network_library_) - return; - mock_network_library_ = new StrictMock<MockNetworkLibrary>(); - test_api()->SetNetworkLibrary(mock_network_library_, true); -} - -void CrosInProcessBrowserTest::InitMockPowerLibrary() { - InitMockLibraryLoader(); - if (mock_power_library_) - return; - mock_power_library_ = new StrictMock<MockPowerLibrary>(); - test_api()->SetPowerLibrary(mock_power_library_, true); -} - -void CrosInProcessBrowserTest::InitMockScreenLockLibrary() { - InitMockLibraryLoader(); - if (mock_screen_lock_library_) - return; - mock_screen_lock_library_ = new StrictMock<MockScreenLockLibrary>(); - test_api()->SetScreenLockLibrary(mock_screen_lock_library_, true); -} - -void CrosInProcessBrowserTest::InitMockSynapticsLibrary() { - InitMockLibraryLoader(); - if (mock_synaptics_library_) - return; - mock_synaptics_library_ = new StrictMock<MockSynapticsLibrary>(); - test_api()->SetSynapticsLibrary(mock_synaptics_library_, true); -} - -void CrosInProcessBrowserTest::InitMockSystemLibrary() { - InitMockLibraryLoader(); - if (mock_system_library_) - return; - mock_system_library_ = new StrictMock<MockSystemLibrary>(); - test_api()->SetSystemLibrary(mock_system_library_, true); -} - -void CrosInProcessBrowserTest::SetStatusAreaMocksExpectations() { - SetKeyboardLibraryStatusAreaExpectations(); - SetInputMethodLibraryStatusAreaExpectations(); - SetNetworkLibraryStatusAreaExpectations(); - SetPowerLibraryStatusAreaExpectations(); - SetSynapticsLibraryExpectations(); - SetSystemLibraryStatusAreaExpectations(); -} - -void CrosInProcessBrowserTest::SetKeyboardLibraryStatusAreaExpectations() { - EXPECT_CALL(*mock_keyboard_library_, GetCurrentKeyboardLayoutName()) - .Times(AnyNumber()) - .WillRepeatedly((Return("us"))) - .RetiresOnSaturation(); - EXPECT_CALL(*mock_keyboard_library_, SetCurrentKeyboardLayoutByName(_)) - .Times(AnyNumber()) - .WillRepeatedly((Return(true))) - .RetiresOnSaturation(); - EXPECT_CALL(*mock_keyboard_library_, RemapModifierKeys(_)) - .Times(AnyNumber()) - .WillRepeatedly((Return(true))) - .RetiresOnSaturation(); - EXPECT_CALL(*mock_keyboard_library_, SetKeyboardLayoutPerWindow(_)) - .Times(AnyNumber()) - .WillRepeatedly((Return(true))) - .RetiresOnSaturation(); - EXPECT_CALL(*mock_keyboard_library_, GetKeyboardLayoutPerWindow(_)) - .Times(AnyNumber()) - .WillRepeatedly((Return(true))) - .RetiresOnSaturation(); -} - -void CrosInProcessBrowserTest::SetInputMethodLibraryStatusAreaExpectations() { - EXPECT_CALL(*mock_input_method_library_, AddObserver(_)) - .Times(1) - .RetiresOnSaturation(); - EXPECT_CALL(*mock_input_method_library_, GetActiveInputMethods()) - .Times(AnyNumber()) - .WillRepeatedly(InvokeWithoutArgs(CreateFallbackInputMethodDescriptors)) - .RetiresOnSaturation(); - EXPECT_CALL(*mock_input_method_library_, GetSupportedInputMethods()) - .Times(AnyNumber()) - .WillRepeatedly(InvokeWithoutArgs(CreateFallbackInputMethodDescriptors)) - .RetiresOnSaturation(); - EXPECT_CALL(*mock_input_method_library_, current_ime_properties()) - .Times(1) - .WillOnce((ReturnRef(ime_properties_))) - .RetiresOnSaturation(); - EXPECT_CALL(*mock_input_method_library_, SetImeConfig(_, _, _)) - .Times(AnyNumber()) - .WillRepeatedly((Return(true))) - .RetiresOnSaturation(); - EXPECT_CALL(*mock_input_method_library_, RemoveObserver(_)) - .Times(1) - .RetiresOnSaturation(); -} - -void CrosInProcessBrowserTest::SetNetworkLibraryStatusAreaExpectations() { - EXPECT_CALL(*mock_network_library_, AddObserver(_)) - .Times(1) - .RetiresOnSaturation(); - EXPECT_CALL(*mock_network_library_, wifi_connecting()) - .Times(1) - .WillRepeatedly((Return(false))) - .RetiresOnSaturation(); - EXPECT_CALL(*mock_network_library_, wifi_connected()) - .Times(1) - .WillRepeatedly((Return(false))) - .RetiresOnSaturation(); - EXPECT_CALL(*mock_network_library_, cellular_connecting()) - .Times(1) - .WillRepeatedly((Return(false))) - .RetiresOnSaturation(); - EXPECT_CALL(*mock_network_library_, cellular_connected()) - .Times(1) - .WillRepeatedly((Return(false))) - .RetiresOnSaturation(); - EXPECT_CALL(*mock_network_library_, ethernet_connected()) - .Times(1) - .WillRepeatedly((Return(false))) - .RetiresOnSaturation(); - EXPECT_CALL(*mock_network_library_, Connected()) - .Times(1) - .WillRepeatedly((Return(false))) - .RetiresOnSaturation(); - EXPECT_CALL(*mock_network_library_, Connecting()) - .Times(1) - .WillRepeatedly((Return(false))) - .RetiresOnSaturation(); - EXPECT_CALL(*mock_network_library_, RemoveObserver(_)) - .Times(1) - .RetiresOnSaturation(); -} -void CrosInProcessBrowserTest::SetPowerLibraryStatusAreaExpectations() { - EXPECT_CALL(*mock_power_library_, AddObserver(_)) - .Times(1) - .RetiresOnSaturation(); - EXPECT_CALL(*mock_power_library_, battery_fully_charged()) - .Times(3) - .WillRepeatedly((Return(false))) - .RetiresOnSaturation(); - EXPECT_CALL(*mock_power_library_, battery_is_present()) - .Times(1) - .WillOnce((Return(true))) - .RetiresOnSaturation(); - EXPECT_CALL(*mock_power_library_, battery_percentage()) - .Times(2) - .WillRepeatedly((Return(42.0))) - .RetiresOnSaturation(); - EXPECT_CALL(*mock_power_library_, line_power_on()) - .Times(4) - .WillRepeatedly((Return(false))) - .RetiresOnSaturation(); - EXPECT_CALL(*mock_power_library_, battery_time_to_empty()) - .Times(1) - .WillOnce((Return(base::TimeDelta::FromMinutes(42)))) - .RetiresOnSaturation(); - EXPECT_CALL(*mock_power_library_, RemoveObserver(_)) - .Times(1) - .RetiresOnSaturation(); +CrosInProcessBrowserTest::CrosInProcessBrowserTest() { + cros_mock_.reset(new CrosMock()); } -void CrosInProcessBrowserTest::SetSystemLibraryStatusAreaExpectations() { - EXPECT_CALL(*mock_system_library_, AddObserver(_)) - .Times(1) - .RetiresOnSaturation(); - EXPECT_CALL(*mock_system_library_, RemoveObserver(_)) - .Times(1) - .RetiresOnSaturation(); -} - -void CrosInProcessBrowserTest::SetSynapticsLibraryExpectations() { - EXPECT_CALL(*mock_synaptics_library_, SetBoolParameter(_, _)) - .Times(AnyNumber()); - EXPECT_CALL(*mock_synaptics_library_, SetRangeParameter(_, _)) - .Times(AnyNumber()); -} - -void CrosInProcessBrowserTest::SetSystemLibraryExpectations() { - EXPECT_CALL(*mock_system_library_, GetTimezone()) - .Times(AnyNumber()); - EXPECT_CALL(*mock_system_library_, SetTimezone(_)) - .Times(AnyNumber()); +CrosInProcessBrowserTest::~CrosInProcessBrowserTest() { } void CrosInProcessBrowserTest::TearDownInProcessBrowserTestFixture() { - // Prevent bogus gMock leak check from firing. - if (loader_) - test_api()->SetLibraryLoader(NULL, false); - if (mock_cryptohome_library_) - test_api()->SetCryptohomeLibrary(NULL, false); - if (mock_keyboard_library_) - test_api()->SetKeyboardLibrary(NULL, false); - if (mock_input_method_library_) - test_api()->SetInputMethodLibrary(NULL, false); - if (mock_network_library_) - test_api()->SetNetworkLibrary(NULL, false); - if (mock_power_library_) - test_api()->SetPowerLibrary(NULL, false); - if (mock_screen_lock_library_) - test_api()->SetScreenLockLibrary(NULL, false); - if (mock_synaptics_library_) - test_api()->SetSynapticsLibrary(NULL, false); - if (mock_system_library_) - test_api()->SetSystemLibrary(NULL, false); + cros_mock_->TearDownMocks(); } } // namespace chromeos diff --git a/chrome/browser/chromeos/cros/cros_in_process_browser_test.h b/chrome/browser/chromeos/cros/cros_in_process_browser_test.h index 7a7ea71..b4e9a2e 100644 --- a/chrome/browser/chromeos/cros/cros_in_process_browser_test.h +++ b/chrome/browser/chromeos/cros/cros_in_process_browser_test.h @@ -7,30 +7,16 @@ #pragma once #include "base/scoped_ptr.h" -#include "chrome/browser/chromeos/cros/cros_library.h" +#include "chrome/browser/chromeos/cros/cros_mock.h" #include "chrome/test/in_process_browser_test.h" -#include "cros/chromeos_input_method.h" namespace chromeos { -class MockCryptohomeLibrary; -class MockKeyboardLibrary; -class MockInputMethodLibrary; -class MockLibraryLoader; -class MockNetworkLibrary; -class MockPowerLibrary; -class MockScreenLockLibrary; -class MockScreenLockLibrary; -class MockSynapticsLibrary; -class MockSystemLibrary; - // Base class for Chromium OS tests wanting to bring up a browser in the // unit test process and mock some parts of CrosLibrary. Once you mock part of // CrosLibrary it will be considered as successfully loaded and libraries -// that compose CrosLibrary will be created. The issue here is that you do -// need to specify minimum set of mocks for you test to succeed. -// CrosInProcessBrowserTest defines minimum set of mocks that is used -// by status area elements (network, input language, power). +// that compose CrosLibrary will be created. Use CrosMock to specify minimum +// set of mocks for you test to succeed. // See comments for InProcessBrowserTest base class too. class CrosInProcessBrowserTest : public InProcessBrowserTest { public: @@ -38,62 +24,11 @@ class CrosInProcessBrowserTest : public InProcessBrowserTest { virtual ~CrosInProcessBrowserTest(); protected: - // This method setups basic mocks that are used by status area items: - // LibraryLoader, Language, Network, Power, Synaptics libraries. - // Add call to this method at the beginning of your - // SetUpInProcessBrowserTestFixture. - void InitStatusAreaMocks(); - - // Initialization of CrosLibrary mock loader. If you intend calling - // separate init methods for mocks call this one first. - void InitMockLibraryLoader(); - - // Initialization of mocks. - void InitMockCryptohomeLibrary(); - void InitMockKeyboardLibrary(); - void InitMockInputMethodLibrary(); - void InitMockNetworkLibrary(); - void InitMockPowerLibrary(); - void InitMockScreenLockLibrary(); - void InitMockSynapticsLibrary(); - void InitMockSystemLibrary(); - - // This method setups corresponding expectations for basic mocks that - // are used by status area items. - // Make sure that InitStatusAreaMocks was called before. - // Add call to this method in your SetUpInProcessBrowserTestFixture. - // They are all configured with RetiresOnSaturation(). - // Once such expectation is used it won't block expectations you've defined. - void SetStatusAreaMocksExpectations(); - - // Methods to setup minimal mocks expectations for status area. - void SetKeyboardLibraryStatusAreaExpectations(); - void SetInputMethodLibraryStatusAreaExpectations(); - void SetNetworkLibraryStatusAreaExpectations(); - void SetPowerLibraryStatusAreaExpectations(); - void SetSystemLibraryStatusAreaExpectations(); - void SetSynapticsLibraryExpectations(); - void SetSystemLibraryExpectations(); + scoped_ptr<CrosMock> cros_mock_; // Overriden for things you would normally override TearDown for. virtual void TearDownInProcessBrowserTestFixture(); - // TestApi gives access to CrosLibrary private members. - chromeos::CrosLibrary::TestApi* test_api(); - - // Mocks, destroyed by CrosLibrary class. - MockLibraryLoader* loader_; - MockCryptohomeLibrary* mock_cryptohome_library_; - MockKeyboardLibrary* mock_keyboard_library_; - MockInputMethodLibrary* mock_input_method_library_; - MockNetworkLibrary* mock_network_library_; - MockPowerLibrary* mock_power_library_; - MockScreenLockLibrary* mock_screen_lock_library_; - MockSynapticsLibrary* mock_synaptics_library_; - MockSystemLibrary* mock_system_library_; - - ImePropertyList ime_properties_; - private: DISALLOW_COPY_AND_ASSIGN(CrosInProcessBrowserTest); }; diff --git a/chrome/browser/chromeos/cros/cros_mock.cc b/chrome/browser/chromeos/cros/cros_mock.cc new file mode 100644 index 0000000..5c818ca --- /dev/null +++ b/chrome/browser/chromeos/cros/cros_mock.cc @@ -0,0 +1,374 @@ +// 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 "chrome/browser/chromeos/cros/cros_mock.h" + +#include "base/message_loop.h" +#include "base/ref_counted.h" +#include "base/time.h" +#include "chrome/browser/browser.h" +#include "chrome/browser/browser_process.h" +#include "chrome/browser/chromeos/cros/mock_cryptohome_library.h" +#include "chrome/browser/chromeos/cros/mock_keyboard_library.h" +#include "chrome/browser/chromeos/cros/mock_input_method_library.h" +#include "chrome/browser/chromeos/cros/mock_library_loader.h" +#include "chrome/browser/chromeos/cros/mock_network_library.h" +#include "chrome/browser/chromeos/cros/mock_power_library.h" +#include "chrome/browser/chromeos/cros/mock_screen_lock_library.h" +#include "chrome/browser/chromeos/cros/mock_speech_synthesis_library.h" +#include "chrome/browser/chromeos/cros/mock_synaptics_library.h" +#include "chrome/browser/chromeos/cros/mock_system_library.h" +#include "chrome/browser/chromeos/login/wizard_controller.h" +#include "chrome/browser/chromeos/login/wizard_screen.h" +#include "chrome/test/in_process_browser_test.h" +#include "chrome/test/ui_test_utils.h" +#include "testing/gtest/include/gtest/gtest.h" +#include "testing/gmock/include/gmock/gmock.h" + +namespace chromeos { + +using ::testing::AnyNumber; +using ::testing::InvokeWithoutArgs; +using ::testing::Return; +using ::testing::ReturnRef; +using ::testing::StrictMock; +using ::testing::_; + +CrosMock::CrosMock() + : loader_(NULL), + mock_cryptohome_library_(NULL), + mock_keyboard_library_(NULL), + mock_input_method_library_(NULL), + mock_network_library_(NULL), + mock_power_library_(NULL), + mock_screen_lock_library_(NULL), + mock_speech_synthesis_library_(NULL), + mock_synaptics_library_(NULL), + mock_system_library_(NULL) {} + +CrosMock::~CrosMock() { +} + +chromeos::CrosLibrary::TestApi* CrosMock::test_api() { + return chromeos::CrosLibrary::Get()->GetTestApi(); +} + +void CrosMock::InitStatusAreaMocks() { + InitMockKeyboardLibrary(); + InitMockInputMethodLibrary(); + InitMockNetworkLibrary(); + InitMockPowerLibrary(); + InitMockSynapticsLibrary(); + InitMockSystemLibrary(); +} + +void CrosMock::InitMockLibraryLoader() { + if (loader_) + return; + loader_ = new StrictMock<MockLibraryLoader>(); + EXPECT_CALL(*loader_, Load(_)) + .Times(AnyNumber()) + .WillRepeatedly(Return(true)); + test_api()->SetLibraryLoader(loader_, true); +} + +void CrosMock::InitMockCryptohomeLibrary() { + InitMockLibraryLoader(); + if (mock_cryptohome_library_) + return; + mock_cryptohome_library_ = new StrictMock<MockCryptohomeLibrary>(); + test_api()->SetCryptohomeLibrary(mock_cryptohome_library_, true); +} + +void CrosMock::InitMockKeyboardLibrary() { + InitMockLibraryLoader(); + if (mock_keyboard_library_) + return; + mock_keyboard_library_ = new StrictMock<MockKeyboardLibrary>(); + test_api()->SetKeyboardLibrary(mock_keyboard_library_, true); +} + +void CrosMock::InitMockInputMethodLibrary() { + InitMockLibraryLoader(); + if (mock_input_method_library_) + return; + mock_input_method_library_ = new StrictMock<MockInputMethodLibrary>(); + test_api()->SetInputMethodLibrary(mock_input_method_library_, true); +} + +void CrosMock::InitMockNetworkLibrary() { + InitMockLibraryLoader(); + if (mock_network_library_) + return; + mock_network_library_ = new StrictMock<MockNetworkLibrary>(); + test_api()->SetNetworkLibrary(mock_network_library_, true); +} + +void CrosMock::InitMockPowerLibrary() { + InitMockLibraryLoader(); + if (mock_power_library_) + return; + mock_power_library_ = new StrictMock<MockPowerLibrary>(); + test_api()->SetPowerLibrary(mock_power_library_, true); +} + +void CrosMock::InitMockScreenLockLibrary() { + InitMockLibraryLoader(); + if (mock_screen_lock_library_) + return; + mock_screen_lock_library_ = new StrictMock<MockScreenLockLibrary>(); + test_api()->SetScreenLockLibrary(mock_screen_lock_library_, true); +} + +void CrosMock::InitMockSpeechSynthesisLibrary() { + InitMockLibraryLoader(); + if (mock_speech_synthesis_library_) + return; + mock_speech_synthesis_library_ = + new StrictMock<MockSpeechSynthesisLibrary>(); + test_api()->SetSpeechSynthesisLibrary(mock_speech_synthesis_library_, true); +} + +void CrosMock::InitMockSynapticsLibrary() { + InitMockLibraryLoader(); + if (mock_synaptics_library_) + return; + mock_synaptics_library_ = new StrictMock<MockSynapticsLibrary>(); + test_api()->SetSynapticsLibrary(mock_synaptics_library_, true); +} + +void CrosMock::InitMockSystemLibrary() { + InitMockLibraryLoader(); + if (mock_system_library_) + return; + mock_system_library_ = new StrictMock<MockSystemLibrary>(); + test_api()->SetSystemLibrary(mock_system_library_, true); +} + +// Initialization of mocks. +MockCryptohomeLibrary* CrosMock::mock_cryptohome_library() { + return mock_cryptohome_library_; +} + +MockKeyboardLibrary* CrosMock::mock_keyboard_library() { + return mock_keyboard_library_; +} + +MockInputMethodLibrary* CrosMock::mock_input_method_library() { + return mock_input_method_library_; +} + +MockNetworkLibrary* CrosMock::mock_network_library() { + return mock_network_library_; +} + +MockPowerLibrary* CrosMock::mock_power_library() { + return mock_power_library_; +} + +MockScreenLockLibrary* CrosMock::mock_screen_lock_library() { + return mock_screen_lock_library_; +} + +MockSpeechSynthesisLibrary* CrosMock::mock_speech_synthesis_library() { + return mock_speech_synthesis_library_; +} + +MockSynapticsLibrary* CrosMock::mock_synaptics_library() { + return mock_synaptics_library_; +} + +MockSystemLibrary* CrosMock::mock_system_library() { + return mock_system_library_; +} + +void CrosMock::SetStatusAreaMocksExpectations() { + SetKeyboardLibraryStatusAreaExpectations(); + SetInputMethodLibraryStatusAreaExpectations(); + SetNetworkLibraryStatusAreaExpectations(); + SetPowerLibraryStatusAreaExpectations(); + SetSynapticsLibraryExpectations(); + SetSystemLibraryStatusAreaExpectations(); +} + +void CrosMock::SetKeyboardLibraryStatusAreaExpectations() { + EXPECT_CALL(*mock_keyboard_library_, GetCurrentKeyboardLayoutName()) + .Times(AnyNumber()) + .WillRepeatedly((Return("us"))) + .RetiresOnSaturation(); + EXPECT_CALL(*mock_keyboard_library_, SetCurrentKeyboardLayoutByName(_)) + .Times(AnyNumber()) + .WillRepeatedly((Return(true))) + .RetiresOnSaturation(); + EXPECT_CALL(*mock_keyboard_library_, RemapModifierKeys(_)) + .Times(AnyNumber()) + .WillRepeatedly((Return(true))) + .RetiresOnSaturation(); + EXPECT_CALL(*mock_keyboard_library_, SetKeyboardLayoutPerWindow(_)) + .Times(AnyNumber()) + .WillRepeatedly((Return(true))) + .RetiresOnSaturation(); + EXPECT_CALL(*mock_keyboard_library_, GetKeyboardLayoutPerWindow(_)) + .Times(AnyNumber()) + .WillRepeatedly((Return(true))) + .RetiresOnSaturation(); +} + +void CrosMock::SetInputMethodLibraryStatusAreaExpectations() { + EXPECT_CALL(*mock_input_method_library_, AddObserver(_)) + .Times(1) + .RetiresOnSaturation(); + EXPECT_CALL(*mock_input_method_library_, GetActiveInputMethods()) + .Times(AnyNumber()) + .WillRepeatedly(InvokeWithoutArgs(CreateFallbackInputMethodDescriptors)) + .RetiresOnSaturation(); + EXPECT_CALL(*mock_input_method_library_, GetSupportedInputMethods()) + .Times(AnyNumber()) + .WillRepeatedly(InvokeWithoutArgs(CreateFallbackInputMethodDescriptors)) + .RetiresOnSaturation(); + EXPECT_CALL(*mock_input_method_library_, current_ime_properties()) + .Times(1) + .WillOnce((ReturnRef(ime_properties_))) + .RetiresOnSaturation(); + EXPECT_CALL(*mock_input_method_library_, SetImeConfig(_, _, _)) + .Times(AnyNumber()) + .WillRepeatedly((Return(true))) + .RetiresOnSaturation(); + EXPECT_CALL(*mock_input_method_library_, RemoveObserver(_)) + .Times(1) + .RetiresOnSaturation(); +} + +void CrosMock::SetNetworkLibraryStatusAreaExpectations() { + EXPECT_CALL(*mock_network_library_, AddObserver(_)) + .Times(1) + .RetiresOnSaturation(); + EXPECT_CALL(*mock_network_library_, wifi_connecting()) + .Times(1) + .WillRepeatedly((Return(false))) + .RetiresOnSaturation(); + EXPECT_CALL(*mock_network_library_, wifi_connected()) + .Times(1) + .WillRepeatedly((Return(false))) + .RetiresOnSaturation(); + EXPECT_CALL(*mock_network_library_, cellular_connecting()) + .Times(1) + .WillRepeatedly((Return(false))) + .RetiresOnSaturation(); + EXPECT_CALL(*mock_network_library_, cellular_connected()) + .Times(1) + .WillRepeatedly((Return(false))) + .RetiresOnSaturation(); + EXPECT_CALL(*mock_network_library_, ethernet_connected()) + .Times(1) + .WillRepeatedly((Return(false))) + .RetiresOnSaturation(); + EXPECT_CALL(*mock_network_library_, Connected()) + .Times(1) + .WillRepeatedly((Return(false))) + .RetiresOnSaturation(); + EXPECT_CALL(*mock_network_library_, Connecting()) + .Times(1) + .WillRepeatedly((Return(false))) + .RetiresOnSaturation(); + EXPECT_CALL(*mock_network_library_, RemoveObserver(_)) + .Times(1) + .RetiresOnSaturation(); +} + +void CrosMock::SetPowerLibraryStatusAreaExpectations() { + EXPECT_CALL(*mock_power_library_, AddObserver(_)) + .Times(1) + .RetiresOnSaturation(); + EXPECT_CALL(*mock_power_library_, battery_fully_charged()) + .Times(3) + .WillRepeatedly((Return(false))) + .RetiresOnSaturation(); + EXPECT_CALL(*mock_power_library_, battery_is_present()) + .Times(1) + .WillOnce((Return(true))) + .RetiresOnSaturation(); + EXPECT_CALL(*mock_power_library_, battery_percentage()) + .Times(2) + .WillRepeatedly((Return(42.0))) + .RetiresOnSaturation(); + EXPECT_CALL(*mock_power_library_, line_power_on()) + .Times(4) + .WillRepeatedly((Return(false))) + .RetiresOnSaturation(); + EXPECT_CALL(*mock_power_library_, battery_time_to_empty()) + .Times(1) + .WillOnce((Return(base::TimeDelta::FromMinutes(42)))) + .RetiresOnSaturation(); + EXPECT_CALL(*mock_power_library_, RemoveObserver(_)) + .Times(1) + .RetiresOnSaturation(); +} + +void CrosMock::SetSpeechSynthesisLibraryExpectations() { + EXPECT_CALL(*mock_speech_synthesis_library_, Speak(_)) + .Times(1) + .WillOnce(Return(true)) + .RetiresOnSaturation(); + EXPECT_CALL(*mock_speech_synthesis_library_, StopSpeaking()) + .Times(1) + .WillOnce(Return(true)) + .RetiresOnSaturation(); + EXPECT_CALL(*mock_speech_synthesis_library_, IsSpeaking()) + .Times(4) + .WillOnce(Return(true)) + .WillOnce(Return(true)) + .WillOnce(Return(true)) + .WillOnce(Return(false)) + .RetiresOnSaturation(); +} + +void CrosMock::SetSystemLibraryStatusAreaExpectations() { + EXPECT_CALL(*mock_system_library_, AddObserver(_)) + .Times(1) + .RetiresOnSaturation(); + EXPECT_CALL(*mock_system_library_, RemoveObserver(_)) + .Times(1) + .RetiresOnSaturation(); +} + +void CrosMock::SetSynapticsLibraryExpectations() { + EXPECT_CALL(*mock_synaptics_library_, SetBoolParameter(_, _)) + .Times(AnyNumber()); + EXPECT_CALL(*mock_synaptics_library_, SetRangeParameter(_, _)) + .Times(AnyNumber()); +} + +void CrosMock::SetSystemLibraryExpectations() { + EXPECT_CALL(*mock_system_library_, GetTimezone()) + .Times(AnyNumber()); + EXPECT_CALL(*mock_system_library_, SetTimezone(_)) + .Times(AnyNumber()); +} + +void CrosMock::TearDownMocks() { + // Prevent bogus gMock leak check from firing. + if (loader_) + test_api()->SetLibraryLoader(NULL, false); + if (mock_cryptohome_library_) + test_api()->SetCryptohomeLibrary(NULL, false); + if (mock_keyboard_library_) + test_api()->SetKeyboardLibrary(NULL, false); + if (mock_input_method_library_) + test_api()->SetInputMethodLibrary(NULL, false); + if (mock_network_library_) + test_api()->SetNetworkLibrary(NULL, false); + if (mock_power_library_) + test_api()->SetPowerLibrary(NULL, false); + if (mock_screen_lock_library_) + test_api()->SetScreenLockLibrary(NULL, false); + if (mock_speech_synthesis_library_) + test_api()->SetSpeechSynthesisLibrary(NULL, false); + if (mock_synaptics_library_) + test_api()->SetSynapticsLibrary(NULL, false); + if (mock_system_library_) + test_api()->SetSystemLibrary(NULL, false); +} + +} // namespace chromeos diff --git a/chrome/browser/chromeos/cros/cros_mock.h b/chrome/browser/chromeos/cros/cros_mock.h new file mode 100644 index 0000000..329d10c --- /dev/null +++ b/chrome/browser/chromeos/cros/cros_mock.h @@ -0,0 +1,110 @@ +// 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. + +#ifndef CHROME_BROWSER_CHROMEOS_CROS_CROS_MOCK_H_ +#define CHROME_BROWSER_CHROMEOS_CROS_CROS_MOCK_H_ + +#include "base/scoped_ptr.h" +#include "chrome/browser/chromeos/cros/cros_library.h" +#include "chrome/test/in_process_browser_test.h" +#include "cros/chromeos_input_method.h" + +namespace chromeos { + +class MockCryptohomeLibrary; +class MockKeyboardLibrary; +class MockInputMethodLibrary; +class MockLibraryLoader; +class MockNetworkLibrary; +class MockPowerLibrary; +class MockScreenLockLibrary; +class MockSpeechSynthesisLibrary; +class MockSynapticsLibrary; +class MockSystemLibrary; + +// Class for initializing mocks for some parts of CrosLibrary. Once you mock +// part of CrosLibrary it will be considered as successfully loaded and +// libraries that compose CrosLibrary will be created. CrosMock also defines a +// minimum set of mocks that is used by status area elements (network, +// input language, power). +class CrosMock { + public: + CrosMock(); + virtual ~CrosMock(); + + // This method sets up basic mocks that are used by status area items: + // LibraryLoader, Language, Network, Power, Synaptics libraries. + // Add a call to this method at the beginning of your + // SetUpInProcessBrowserTestFixture. + void InitStatusAreaMocks(); + + // Initialization of CrosLibrary mock loader. If you intend calling + // separate init methods for mocks call this one first. + void InitMockLibraryLoader(); + + // Initialization of mocks. + void InitMockCryptohomeLibrary(); + void InitMockKeyboardLibrary(); + void InitMockInputMethodLibrary(); + void InitMockNetworkLibrary(); + void InitMockPowerLibrary(); + void InitMockScreenLockLibrary(); + void InitMockSpeechSynthesisLibrary(); + void InitMockSynapticsLibrary(); + void InitMockSystemLibrary(); + + // Get mocks. + MockCryptohomeLibrary* mock_cryptohome_library(); + MockKeyboardLibrary* mock_keyboard_library(); + MockInputMethodLibrary* mock_input_method_library(); + MockNetworkLibrary* mock_network_library(); + MockPowerLibrary* mock_power_library(); + MockScreenLockLibrary* mock_screen_lock_library(); + MockSpeechSynthesisLibrary* mock_speech_synthesis_library(); + MockSynapticsLibrary* mock_synaptics_library(); + MockSystemLibrary* mock_system_library(); + + // This method sets up corresponding expectations for basic mocks that + // are used by status area items. + // Make sure that InitStatusAreaMocks was called before. + // Add a call to this method in your SetUpInProcessBrowserTestFixture. + // They are all configured with RetiresOnSaturation(). + // Once such expectation is used it won't block expectations you've defined. + void SetStatusAreaMocksExpectations(); + + // Methods to setup minimal mocks expectations for status area. + void SetKeyboardLibraryStatusAreaExpectations(); + void SetInputMethodLibraryStatusAreaExpectations(); + void SetNetworkLibraryStatusAreaExpectations(); + void SetPowerLibraryStatusAreaExpectations(); + void SetSpeechSynthesisLibraryExpectations(); + void SetSystemLibraryStatusAreaExpectations(); + void SetSynapticsLibraryExpectations(); + void SetSystemLibraryExpectations(); + + void TearDownMocks(); + + // TestApi gives access to CrosLibrary private members. + chromeos::CrosLibrary::TestApi* test_api(); + + private: + // Mocks, destroyed by CrosLibrary class. + MockLibraryLoader* loader_; + MockCryptohomeLibrary* mock_cryptohome_library_; + MockKeyboardLibrary* mock_keyboard_library_; + MockInputMethodLibrary* mock_input_method_library_; + MockNetworkLibrary* mock_network_library_; + MockPowerLibrary* mock_power_library_; + MockScreenLockLibrary* mock_screen_lock_library_; + MockSpeechSynthesisLibrary* mock_speech_synthesis_library_; + MockSynapticsLibrary* mock_synaptics_library_; + MockSystemLibrary* mock_system_library_; + + ImePropertyList ime_properties_; + DISALLOW_COPY_AND_ASSIGN(CrosMock); +}; + +} // namespace chromeos + +#endif // CHROME_BROWSER_CHROMEOS_CROS_CROS_MOCK_H_ diff --git a/chrome/browser/chromeos/login/login_screen_browsertest.cc b/chrome/browser/chromeos/login/login_screen_browsertest.cc index 3184426..8353268 100644 --- a/chrome/browser/chromeos/login/login_screen_browsertest.cc +++ b/chrome/browser/chromeos/login/login_screen_browsertest.cc @@ -28,21 +28,31 @@ const char kPassword[] = "test_password"; class LoginScreenTest : public WizardInProcessBrowserTest { public: - LoginScreenTest(): WizardInProcessBrowserTest("login") { + LoginScreenTest(): WizardInProcessBrowserTest("login"), + mock_cryptohome_library_(NULL), + mock_login_library_(NULL), + mock_network_library_(NULL) { } protected: + MockCryptohomeLibrary *mock_cryptohome_library_; + MockLoginLibrary *mock_login_library_; + MockNetworkLibrary *mock_network_library_; + virtual void SetUpInProcessBrowserTestFixture() { WizardInProcessBrowserTest::SetUpInProcessBrowserTestFixture(); - InitStatusAreaMocks(); - SetStatusAreaMocksExpectations(); + cros_mock_->InitStatusAreaMocks(); + cros_mock_->SetStatusAreaMocksExpectations(); + + mock_network_library_ = cros_mock_->mock_network_library(); mock_login_library_ = new MockLoginLibrary(); EXPECT_CALL(*mock_login_library_, EmitLoginPromptReady()) .Times(1); - test_api()->SetLoginLibrary(mock_login_library_, true); + cros_mock_->test_api()->SetLoginLibrary(mock_login_library_, true); - InitMockCryptohomeLibrary(); + cros_mock_->InitMockCryptohomeLibrary(); + mock_cryptohome_library_ = cros_mock_->mock_cryptohome_library(); EXPECT_CALL(*mock_cryptohome_library_, IsMounted()) .Times(AnyNumber()) .WillRepeatedly((Return(true))); @@ -51,12 +61,10 @@ class LoginScreenTest : public WizardInProcessBrowserTest { virtual void TearDownInProcessBrowserTestFixture() { WizardInProcessBrowserTest::TearDownInProcessBrowserTestFixture(); - test_api()->SetLoginLibrary(NULL, false); + cros_mock_->test_api()->SetLoginLibrary(NULL, false); } private: - MockLoginLibrary* mock_login_library_; - DISALLOW_COPY_AND_ASSIGN(LoginScreenTest); }; diff --git a/chrome/browser/chromeos/login/network_screen_browsertest.cc b/chrome/browser/chromeos/login/network_screen_browsertest.cc index 26ea327..5bcbedc 100644 --- a/chrome/browser/chromeos/login/network_screen_browsertest.cc +++ b/chrome/browser/chromeos/login/network_screen_browsertest.cc @@ -54,17 +54,19 @@ class DummyComboboxModel : public ComboboxModel { class NetworkScreenTest : public WizardInProcessBrowserTest { public: - NetworkScreenTest(): WizardInProcessBrowserTest("network") { + NetworkScreenTest(): WizardInProcessBrowserTest("network"), + mock_login_library_(NULL), + mock_network_library_(NULL) { cellular_.set_name("Cellular network"); wifi_.set_name("WiFi network"); } protected: virtual void SetUpInProcessBrowserTestFixture() { - InitStatusAreaMocks(); - + cros_mock_->InitStatusAreaMocks(); + mock_network_library_ = cros_mock_->mock_network_library(); mock_login_library_ = new MockLoginLibrary(); - test_api()->SetLoginLibrary(mock_login_library_, true); + cros_mock_->test_api()->SetLoginLibrary(mock_login_library_, true); EXPECT_CALL(*mock_login_library_, EmitLoginPromptReady()) .Times(1); @@ -91,12 +93,12 @@ class NetworkScreenTest : public WizardInProcessBrowserTest { EXPECT_CALL(*mock_network_library_, RemoveObserver(_)) .Times(1); - SetStatusAreaMocksExpectations(); + cros_mock_->SetStatusAreaMocksExpectations(); } virtual void TearDownInProcessBrowserTestFixture() { CrosInProcessBrowserTest::TearDownInProcessBrowserTestFixture(); - test_api()->SetLoginLibrary(NULL, false); + cros_mock_->test_api()->SetLoginLibrary(NULL, false); } void NetworkChangedExpectations(bool wifi_enabled) { @@ -185,6 +187,7 @@ class NetworkScreenTest : public WizardInProcessBrowserTest { } MockLoginLibrary* mock_login_library_; + MockNetworkLibrary* mock_network_library_; CellularNetworkVector cellular_networks_; WifiNetworkVector wifi_networks_; diff --git a/chrome/browser/chromeos/login/screen_locker_browsertest.cc b/chrome/browser/chromeos/login/screen_locker_browsertest.cc index f3354a6..b8e1985 100644 --- a/chrome/browser/chromeos/login/screen_locker_browsertest.cc +++ b/chrome/browser/chromeos/login/screen_locker_browsertest.cc @@ -31,7 +31,7 @@ namespace { // An object that wait for lock state and fullscreen state. class Waiter : public NotificationObserver { public: - Waiter(Browser* browser) + explicit Waiter(Browser* browser) : browser_(browser) { registrar_.Add(this, NotificationType::SCREEN_LOCK_STATE_CHANGED, @@ -91,9 +91,13 @@ namespace chromeos { class ScreenLockerTest : public CrosInProcessBrowserTest { public: - ScreenLockerTest() {} + ScreenLockerTest() : mock_screen_lock_library_(NULL), + mock_input_method_library_(NULL) { + } protected: + MockScreenLockLibrary *mock_screen_lock_library_; + MockInputMethodLibrary *mock_input_method_library_; // Test the no password mode with different unlock scheme given by // |unlock| function. @@ -126,8 +130,10 @@ class ScreenLockerTest : public CrosInProcessBrowserTest { private: virtual void SetUpInProcessBrowserTestFixture() { - InitStatusAreaMocks(); - InitMockScreenLockLibrary(); + cros_mock_->InitStatusAreaMocks(); + cros_mock_->InitMockScreenLockLibrary(); + mock_screen_lock_library_ = cros_mock_->mock_screen_lock_library(); + mock_input_method_library_ = cros_mock_->mock_input_method_library(); EXPECT_CALL(*mock_screen_lock_library_, AddObserver(testing::_)) .Times(1) .RetiresOnSaturation(); @@ -135,9 +141,9 @@ class ScreenLockerTest : public CrosInProcessBrowserTest { .Times(1) .RetiresOnSaturation(); // Expectations for the status are on the screen lock window. - SetStatusAreaMocksExpectations(); + cros_mock_->SetStatusAreaMocksExpectations(); // Expectations for the status area on the browser window. - SetStatusAreaMocksExpectations(); + cros_mock_->SetStatusAreaMocksExpectations(); } virtual void SetUpCommandLine(CommandLine* command_line) { diff --git a/chrome/browser/chromeos/network_state_notifier_browsertest.cc b/chrome/browser/chromeos/network_state_notifier_browsertest.cc index 520dbcf..6d4e2ec 100644 --- a/chrome/browser/chromeos/network_state_notifier_browsertest.cc +++ b/chrome/browser/chromeos/network_state_notifier_browsertest.cc @@ -21,15 +21,16 @@ using ::testing::_; class NetworkStateNotifierTest : public CrosInProcessBrowserTest, public NotificationObserver { public: - NetworkStateNotifierTest() { + NetworkStateNotifierTest() : mock_network_library_(NULL) { } protected: virtual void SetUpInProcessBrowserTestFixture() { - InitStatusAreaMocks(); - SetStatusAreaMocksExpectations(); + cros_mock_->InitStatusAreaMocks(); + cros_mock_->SetStatusAreaMocksExpectations(); // Initialize network state notifier. ASSERT_TRUE(CrosLibrary::Get()->EnsureLoaded()); + mock_network_library_ = cros_mock_->mock_network_library(); ASSERT_TRUE(mock_network_library_); EXPECT_CALL(*mock_network_library_, Connected()) .Times(1) @@ -55,6 +56,7 @@ class NetworkStateNotifierTest : public CrosInProcessBrowserTest, } protected: + MockNetworkLibrary *mock_network_library_; NetworkStateDetails::State state_; }; diff --git a/chrome/browser/chromeos/options/wifi_config_view_browsertest.cc b/chrome/browser/chromeos/options/wifi_config_view_browsertest.cc index 468328a..3d292fd 100644 --- a/chrome/browser/chromeos/options/wifi_config_view_browsertest.cc +++ b/chrome/browser/chromeos/options/wifi_config_view_browsertest.cc @@ -18,11 +18,14 @@ using ::testing::_; class WifiConfigViewTest : public CrosInProcessBrowserTest { protected: + MockNetworkLibrary *mock_network_library_; + WifiConfigViewTest() : CrosInProcessBrowserTest() {} virtual void SetUpInProcessBrowserTestFixture() { - InitStatusAreaMocks(); - SetStatusAreaMocksExpectations(); + cros_mock_->InitStatusAreaMocks(); + cros_mock_->SetStatusAreaMocksExpectations(); + mock_network_library_ = cros_mock_->mock_network_library(); } }; diff --git a/chrome/browser/chromeos/status/language_menu_button_browsertest.cc b/chrome/browser/chromeos/status/language_menu_button_browsertest.cc index 452e598..2176997 100644 --- a/chrome/browser/chromeos/status/language_menu_button_browsertest.cc +++ b/chrome/browser/chromeos/status/language_menu_button_browsertest.cc @@ -23,8 +23,8 @@ class LanguageMenuButtonTest : public CrosInProcessBrowserTest { } virtual void SetUpInProcessBrowserTestFixture() { - InitStatusAreaMocks(); - SetStatusAreaMocksExpectations(); + cros_mock_->InitStatusAreaMocks(); + cros_mock_->SetStatusAreaMocksExpectations(); } LanguageMenuButton* GetLanguageMenuButton() { diff --git a/chrome/browser/chromeos/status/power_menu_button_browsertest.cc b/chrome/browser/chromeos/status/power_menu_button_browsertest.cc index 62ffab6..d9d34c2 100644 --- a/chrome/browser/chromeos/status/power_menu_button_browsertest.cc +++ b/chrome/browser/chromeos/status/power_menu_button_browsertest.cc @@ -22,11 +22,16 @@ using ::testing::_; class PowerMenuButtonTest : public CrosInProcessBrowserTest { protected: - PowerMenuButtonTest() : CrosInProcessBrowserTest() {} + MockPowerLibrary *mock_power_library_; + + PowerMenuButtonTest() : CrosInProcessBrowserTest(), + mock_power_library_(NULL) { + } virtual void SetUpInProcessBrowserTestFixture() { - InitStatusAreaMocks(); - SetStatusAreaMocksExpectations(); + cros_mock_->InitStatusAreaMocks(); + cros_mock_->SetStatusAreaMocksExpectations(); + mock_power_library_ = cros_mock_->mock_power_library(); } PowerMenuButton* GetPowerMenuButton() { |