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 const char kTestUser
[] = "example@gmail.com";
30 class MockConnectionFinder
: public ConnectionFinder
{
32 MockConnectionFinder() {}
33 ~MockConnectionFinder() {}
35 MOCK_METHOD1(Find
, void(const ConnectionCallback
&));
38 class MockLockHandler
: public ScreenlockBridge::LockHandler
{
43 // ScreenlockBridge::LockHandler:
44 MOCK_METHOD1(ShowBannerMessage
, void(const base::string16
& message
));
45 MOCK_METHOD2(ShowUserPodCustomIcon
,
46 void(const std::string
& user_email
,
47 const ScreenlockBridge::UserPodCustomIconOptions
& icon
));
48 MOCK_METHOD1(HideUserPodCustomIcon
, void(const std::string
& user_email
));
49 MOCK_METHOD0(EnableInput
, void());
50 MOCK_METHOD3(SetAuthType
,
51 void(const std::string
& user_email
,
53 const base::string16
& auth_value
));
54 MOCK_CONST_METHOD1(GetAuthType
, AuthType(const std::string
& user_email
));
55 MOCK_CONST_METHOD0(GetScreenType
, ScreenType());
56 MOCK_METHOD1(Unlock
, void(const std::string
& user_email
));
57 MOCK_METHOD3(AttemptEasySignin
,
58 void(const std::string
& user_email
,
59 const std::string
& secret
,
60 const std::string
& key_label
));
63 DISALLOW_COPY_AND_ASSIGN(MockLockHandler
);
68 class ProximityAuthBleSystemTestable
: public ProximityAuthBleSystem
{
70 ProximityAuthBleSystemTestable(
71 ScreenlockBridge
* screenlock_bridge
,
72 ProximityAuthClient
* proximity_auth_client
,
73 PrefService
* pref_service
)
74 : ProximityAuthBleSystem(screenlock_bridge
,
75 proximity_auth_client
,
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 proximity_auth_system_
.reset(new ProximityAuthBleSystemTestable(
93 ScreenlockBridge::Get(), &proximity_auth_client_
, &pref_service_
));
95 ON_CALL(proximity_auth_client_
, GetAuthenticatedUsername())
96 .WillByDefault(Return(kTestUser
));
99 // Injects the thread's TaskRunner for testing.
100 scoped_refptr
<base::TestMockTimeTaskRunner
> task_runner_
;
101 base::ThreadTaskRunnerHandle runner_handle_
;
103 NiceMock
<MockProximityAuthClient
> proximity_auth_client_
;
104 TestingPrefServiceSimple pref_service_
;
105 scoped_ptr
<ProximityAuthBleSystem
> proximity_auth_system_
;
107 NiceMock
<MockLockHandler
> lock_handler_
;
110 TEST_F(ProximityAuthBleSystemTest
, LockAndUnlock_LockScreen
) {
112 ON_CALL(lock_handler_
, GetScreenType())
113 .WillByDefault(Return(ScreenlockBridge::LockHandler::LOCK_SCREEN
));
114 ScreenlockBridge::Get()->SetLockHandler(&lock_handler_
);
116 // Unlock the screen.
117 ScreenlockBridge::Get()->SetLockHandler(nullptr);
120 TEST_F(ProximityAuthBleSystemTest
, LockAndUnlock_SigninScreen
) {
121 // Show the sign-in screen.
122 ON_CALL(lock_handler_
, GetScreenType())
123 .WillByDefault(Return(ScreenlockBridge::LockHandler::SIGNIN_SCREEN
));
124 ScreenlockBridge::Get()->SetLockHandler(&lock_handler_
);
127 ScreenlockBridge::Get()->SetLockHandler(nullptr);
130 TEST_F(ProximityAuthBleSystemTest
, LockAndUnlock_OtherScreen
) {
132 ON_CALL(lock_handler_
, GetScreenType())
133 .WillByDefault(Return(ScreenlockBridge::LockHandler::OTHER_SCREEN
));
134 ScreenlockBridge::Get()->SetLockHandler(&lock_handler_
);
137 ScreenlockBridge::Get()->SetLockHandler(nullptr);
140 } // namespace proximity_auth