1 // Copyright 2015 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.
5 #include "components/proximity_auth/ble/proximity_auth_ble_system.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/prefs/testing_pref_service.h"
10 #include "base/test/test_mock_time_task_runner.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "components/proximity_auth/ble/bluetooth_low_energy_device_whitelist.h"
13 #include "components/proximity_auth/connection_finder.h"
14 #include "components/proximity_auth/cryptauth/mock_cryptauth_client.h"
15 #include "components/proximity_auth/mock_proximity_auth_client.h"
16 #include "components/proximity_auth/screenlock_bridge.h"
17 #include "testing/gmock/include/gmock/gmock.h"
18 #include "testing/gtest/include/gtest/gtest.h"
21 using testing::NiceMock
;
22 using testing::Return
;
24 namespace proximity_auth
{
28 class MockConnectionFinder
: public ConnectionFinder
{
30 MockConnectionFinder() {}
31 ~MockConnectionFinder() {}
33 MOCK_METHOD1(Find
, void(const ConnectionCallback
&));
36 class MockLockHandler
: public ScreenlockBridge::LockHandler
{
41 // ScreenlockBridge::LockHandler:
42 MOCK_METHOD1(ShowBannerMessage
, void(const base::string16
& message
));
43 MOCK_METHOD2(ShowUserPodCustomIcon
,
44 void(const std::string
& user_email
,
45 const ScreenlockBridge::UserPodCustomIconOptions
& icon
));
46 MOCK_METHOD1(HideUserPodCustomIcon
, void(const std::string
& user_email
));
47 MOCK_METHOD0(EnableInput
, void());
48 MOCK_METHOD3(SetAuthType
,
49 void(const std::string
& user_email
,
51 const base::string16
& auth_value
));
52 MOCK_CONST_METHOD1(GetAuthType
, AuthType(const std::string
& user_email
));
53 MOCK_CONST_METHOD0(GetScreenType
, ScreenType());
54 MOCK_METHOD1(Unlock
, void(const std::string
& user_email
));
55 MOCK_METHOD3(AttemptEasySignin
,
56 void(const std::string
& user_email
,
57 const std::string
& secret
,
58 const std::string
& key_label
));
61 DISALLOW_COPY_AND_ASSIGN(MockLockHandler
);
66 class ProximityAuthBleSystemTestable
: public ProximityAuthBleSystem
{
68 ProximityAuthBleSystemTestable(
69 ScreenlockBridge
* screenlock_bridge
,
70 ProximityAuthClient
* proximity_auth_client
,
71 scoped_ptr
<CryptAuthClientFactory
> cryptauth_client_factory
,
72 PrefService
* pref_service
)
73 : ProximityAuthBleSystem(screenlock_bridge
,
74 proximity_auth_client
,
75 cryptauth_client_factory
.Pass(),
78 ConnectionFinder
* CreateConnectionFinder() override
{
79 return new NiceMock
<MockConnectionFinder
>();
83 class ProximityAuthBleSystemTest
: public testing::Test
{
85 ProximityAuthBleSystemTest()
86 : task_runner_(new base::TestMockTimeTaskRunner
),
87 runner_handle_(task_runner_
) {}
89 void SetUp() override
{
90 BluetoothLowEnergyDeviceWhitelist::RegisterPrefs(pref_service_
.registry());
92 scoped_ptr
<CryptAuthClientFactory
> cryptauth_client_factory(
93 new MockCryptAuthClientFactory(
94 MockCryptAuthClientFactory::MockType::MAKE_NICE_MOCKS
));
96 proximity_auth_system_
.reset(new ProximityAuthBleSystemTestable(
97 ScreenlockBridge::Get(), &proximity_auth_client_
,
98 cryptauth_client_factory
.Pass(), &pref_service_
));
101 // Injects the thread's TaskRunner for testing.
102 scoped_refptr
<base::TestMockTimeTaskRunner
> task_runner_
;
103 base::ThreadTaskRunnerHandle runner_handle_
;
105 NiceMock
<MockProximityAuthClient
> proximity_auth_client_
;
106 TestingPrefServiceSimple pref_service_
;
107 scoped_ptr
<ProximityAuthBleSystem
> proximity_auth_system_
;
109 NiceMock
<MockLockHandler
> lock_handler_
;
112 TEST_F(ProximityAuthBleSystemTest
, LockAndUnlock_LockScreen
) {
114 ON_CALL(lock_handler_
, GetScreenType())
115 .WillByDefault(Return(ScreenlockBridge::LockHandler::LOCK_SCREEN
));
116 ScreenlockBridge::Get()->SetLockHandler(&lock_handler_
);
118 // Unlock the screen.
119 ScreenlockBridge::Get()->SetLockHandler(nullptr);
122 TEST_F(ProximityAuthBleSystemTest
, LockAndUnlock_SigninScreen
) {
123 // Show the sign-in screen.
124 ON_CALL(lock_handler_
, GetScreenType())
125 .WillByDefault(Return(ScreenlockBridge::LockHandler::SIGNIN_SCREEN
));
126 ScreenlockBridge::Get()->SetLockHandler(&lock_handler_
);
129 ScreenlockBridge::Get()->SetLockHandler(nullptr);
132 TEST_F(ProximityAuthBleSystemTest
, LockAndUnlock_OtherScreen
) {
134 ON_CALL(lock_handler_
, GetScreenType())
135 .WillByDefault(Return(ScreenlockBridge::LockHandler::OTHER_SCREEN
));
136 ScreenlockBridge::Get()->SetLockHandler(&lock_handler_
);
139 ScreenlockBridge::Get()->SetLockHandler(nullptr);
142 } // namespace proximity_auth