diff options
Diffstat (limited to 'components/proximity_auth/ble/proximity_auth_ble_system_unittest.cc')
-rw-r--r-- | components/proximity_auth/ble/proximity_auth_ble_system_unittest.cc | 150 |
1 files changed, 101 insertions, 49 deletions
diff --git a/components/proximity_auth/ble/proximity_auth_ble_system_unittest.cc b/components/proximity_auth/ble/proximity_auth_ble_system_unittest.cc index b223e4a..e178a0b 100644 --- a/components/proximity_auth/ble/proximity_auth_ble_system_unittest.cc +++ b/components/proximity_auth/ble/proximity_auth_ble_system_unittest.cc @@ -6,7 +6,12 @@ #include "base/bind.h" #include "base/memory/scoped_ptr.h" +#include "base/prefs/testing_pref_service.h" +#include "base/test/test_mock_time_task_runner.h" +#include "base/thread_task_runner_handle.h" +#include "components/proximity_auth/ble/bluetooth_low_energy_device_whitelist.h" #include "components/proximity_auth/connection_finder.h" +#include "components/proximity_auth/cryptauth/mock_cryptauth_client.h" #include "components/proximity_auth/proximity_auth_client.h" #include "components/proximity_auth/screenlock_bridge.h" #include "testing/gmock/include/gmock/gmock.h" @@ -17,7 +22,9 @@ using testing::NiceMock; using testing::Return; namespace proximity_auth { + namespace { + class MockConnectionFinder : public ConnectionFinder { public: MockConnectionFinder() {} @@ -26,23 +33,63 @@ class MockConnectionFinder : public ConnectionFinder { MOCK_METHOD1(Find, void(const ConnectionCallback&)); }; -} // namespace +class MockLockHandler : public ScreenlockBridge::LockHandler { + public: + MockLockHandler() {} + ~MockLockHandler() {} + + // ScreenlockBridge::LockHandler: + MOCK_METHOD1(ShowBannerMessage, void(const base::string16& message)); + MOCK_METHOD2(ShowUserPodCustomIcon, + void(const std::string& user_email, + const ScreenlockBridge::UserPodCustomIconOptions& icon)); + MOCK_METHOD1(HideUserPodCustomIcon, void(const std::string& user_email)); + MOCK_METHOD0(EnableInput, void()); + MOCK_METHOD3(SetAuthType, + void(const std::string& user_email, + AuthType auth_type, + const base::string16& auth_value)); + MOCK_CONST_METHOD1(GetAuthType, AuthType(const std::string& user_email)); + MOCK_CONST_METHOD0(GetScreenType, ScreenType()); + MOCK_METHOD1(Unlock, void(const std::string& user_email)); + MOCK_METHOD3(AttemptEasySignin, + void(const std::string& user_email, + const std::string& secret, + const std::string& key_label)); + + private: + DISALLOW_COPY_AND_ASSIGN(MockLockHandler); +}; -class ProximityAuthBleSystemTestable : public ProximityAuthBleSystem { +const char kTestUser[] = "example@gmail.com"; + +class MockProximityAuthClient : public ProximityAuthClient { public: - class MockScreenlockBridgeAdapter - : public ProximityAuthBleSystem::ScreenlockBridgeAdapter { - public: - MockScreenlockBridgeAdapter() {} - ~MockScreenlockBridgeAdapter() {} + MockProximityAuthClient() {} + ~MockProximityAuthClient() override {} - MOCK_METHOD1(AddObserver, void(ScreenlockBridge::Observer*)); - MOCK_METHOD1(RemoveObserver, void(ScreenlockBridge::Observer*)); - MOCK_METHOD1(Unlock, void(ProximityAuthClient*)); - }; + // ProximityAuthClient: + std::string GetAuthenticatedUsername() const override { return kTestUser; } - ProximityAuthBleSystemTestable(ScreenlockBridgeAdapter* screenlock_bridge) - : ProximityAuthBleSystem(make_scoped_ptr(screenlock_bridge), nullptr) {} + MOCK_METHOD1(FinalizeUnlock, void(bool)); + + private: + DISALLOW_COPY_AND_ASSIGN(MockProximityAuthClient); +}; + +} // namespace + +class ProximityAuthBleSystemTestable : public ProximityAuthBleSystem { + public: + ProximityAuthBleSystemTestable( + ScreenlockBridge* screenlock_bridge, + ProximityAuthClient* proximity_auth_client, + scoped_ptr<CryptAuthClientFactory> cryptauth_client_factory, + PrefService* pref_service) + : ProximityAuthBleSystem(screenlock_bridge, + proximity_auth_client, + cryptauth_client_factory.Pass(), + pref_service) {} ConnectionFinder* CreateConnectionFinder() override { return new NiceMock<MockConnectionFinder>(); @@ -52,55 +99,60 @@ class ProximityAuthBleSystemTestable : public ProximityAuthBleSystem { class ProximityAuthBleSystemTest : public testing::Test { protected: ProximityAuthBleSystemTest() - : screenlock_bridge_(new NiceMock< - ProximityAuthBleSystemTestable::MockScreenlockBridgeAdapter>) {} + : task_runner_(new base::TestMockTimeTaskRunner), + runner_handle_(task_runner_) {} + + void SetUp() override { + BluetoothLowEnergyDeviceWhitelist::RegisterPrefs(pref_service_.registry()); - void SetExpectations() { - EXPECT_CALL(*screenlock_bridge_, AddObserver(_)); - EXPECT_CALL(*screenlock_bridge_, RemoveObserver(_)); + scoped_ptr<CryptAuthClientFactory> cryptauth_client_factory( + new MockCryptAuthClientFactory( + MockCryptAuthClientFactory::MockType::MAKE_NICE_MOCKS)); + + proximity_auth_system_.reset(new ProximityAuthBleSystemTestable( + ScreenlockBridge::Get(), &proximity_auth_client_, + cryptauth_client_factory.Pass(), &pref_service_)); } - ProximityAuthBleSystemTestable::MockScreenlockBridgeAdapter* - screenlock_bridge_; -}; + // Injects the thread's TaskRunner for testing. + scoped_refptr<base::TestMockTimeTaskRunner> task_runner_; + base::ThreadTaskRunnerHandle runner_handle_; -TEST_F(ProximityAuthBleSystemTest, ConstructAndDestroyShouldNotCrash) { - SetExpectations(); + NiceMock<MockProximityAuthClient> proximity_auth_client_; + TestingPrefServiceSimple pref_service_; + scoped_ptr<ProximityAuthBleSystem> proximity_auth_system_; - ProximityAuthBleSystemTestable proximity_auth_system(screenlock_bridge_); -} + NiceMock<MockLockHandler> lock_handler_; +}; -TEST_F(ProximityAuthBleSystemTest, - OnScreenDidLock_OnLockScreenEvent_OnScreenDidUnlock) { - SetExpectations(); +TEST_F(ProximityAuthBleSystemTest, LockAndUnlock_LockScreen) { + // Lock the screen. + ON_CALL(lock_handler_, GetScreenType()) + .WillByDefault(Return(ScreenlockBridge::LockHandler::LOCK_SCREEN)); + ScreenlockBridge::Get()->SetLockHandler(&lock_handler_); - ProximityAuthBleSystemTestable proximity_auth_system(screenlock_bridge_); - proximity_auth_system.OnScreenDidLock( - ScreenlockBridge::LockHandler::LOCK_SCREEN); - proximity_auth_system.OnScreenDidUnlock( - ScreenlockBridge::LockHandler::LOCK_SCREEN); + // Unlock the screen. + ScreenlockBridge::Get()->SetLockHandler(nullptr); } -TEST_F(ProximityAuthBleSystemTest, - OnScreenDidLock_OnSigninScreenEvent_OnScreenDidUnlock) { - SetExpectations(); +TEST_F(ProximityAuthBleSystemTest, LockAndUnlock_SigninScreen) { + // Show the sign-in screen. + ON_CALL(lock_handler_, GetScreenType()) + .WillByDefault(Return(ScreenlockBridge::LockHandler::SIGNIN_SCREEN)); + ScreenlockBridge::Get()->SetLockHandler(&lock_handler_); - ProximityAuthBleSystemTestable proximity_auth_system(screenlock_bridge_); - proximity_auth_system.OnScreenDidLock( - ScreenlockBridge::LockHandler::SIGNIN_SCREEN); - proximity_auth_system.OnScreenDidUnlock( - ScreenlockBridge::LockHandler::SIGNIN_SCREEN); + // Sign-in. + ScreenlockBridge::Get()->SetLockHandler(nullptr); } -TEST_F(ProximityAuthBleSystemTest, - OnScreenDidLock_OnOtherScreenEvent_OnScreenDidUnlock) { - SetExpectations(); +TEST_F(ProximityAuthBleSystemTest, LockAndUnlock_OtherScreen) { + // Show the screen. + ON_CALL(lock_handler_, GetScreenType()) + .WillByDefault(Return(ScreenlockBridge::LockHandler::OTHER_SCREEN)); + ScreenlockBridge::Get()->SetLockHandler(&lock_handler_); - ProximityAuthBleSystemTestable proximity_auth_system(screenlock_bridge_); - proximity_auth_system.OnScreenDidLock( - ScreenlockBridge::LockHandler::OTHER_SCREEN); - proximity_auth_system.OnScreenDidUnlock( - ScreenlockBridge::LockHandler::OTHER_SCREEN); + // Hide the screen. + ScreenlockBridge::Get()->SetLockHandler(nullptr); } } // namespace proximity_auth |