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/unlock_manager.h"
7 #include "base/macros.h"
8 #include "base/memory/ref_counted.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/test/test_simple_task_runner.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "components/proximity_auth/client.h"
13 #include "components/proximity_auth/controller.h"
14 #include "components/proximity_auth/logging/logging.h"
15 #include "components/proximity_auth/mock_proximity_auth_client.h"
16 #include "components/proximity_auth/proximity_monitor.h"
17 #include "components/proximity_auth/remote_status_update.h"
18 #include "components/proximity_auth/screenlock_bridge.h"
19 #include "device/bluetooth/bluetooth_adapter_factory.h"
20 #include "device/bluetooth/test/mock_bluetooth_adapter.h"
21 #include "testing/gmock/include/gmock/gmock.h"
22 #include "testing/gtest/include/gtest/gtest.h"
24 #if defined(OS_CHROMEOS)
25 #include "chromeos/dbus/dbus_thread_manager.h"
26 #endif // defined(OS_CHROMEOS)
28 using testing::AtLeast
;
29 using testing::NiceMock
;
30 using testing::Return
;
33 namespace proximity_auth
{
36 // Note that the trust agent state is currently ignored by the UnlockManager
38 RemoteStatusUpdate kRemoteScreenUnlocked
= {
39 USER_PRESENT
, SECURE_SCREEN_LOCK_ENABLED
, TRUST_AGENT_UNSUPPORTED
};
40 RemoteStatusUpdate kRemoteScreenLocked
= {
41 USER_ABSENT
, SECURE_SCREEN_LOCK_ENABLED
, TRUST_AGENT_UNSUPPORTED
};
42 RemoteStatusUpdate kRemoteScreenlockDisabled
= {
43 USER_PRESENT
, SECURE_SCREEN_LOCK_DISABLED
, TRUST_AGENT_UNSUPPORTED
};
44 RemoteStatusUpdate kRemoteScreenlockStateUnknown
= {
45 USER_PRESENCE_UNKNOWN
, SECURE_SCREEN_LOCK_STATE_UNKNOWN
,
46 TRUST_AGENT_UNSUPPORTED
};
48 class MockController
: public Controller
{
51 ~MockController() override
{}
53 MOCK_CONST_METHOD0(GetState
, State());
54 MOCK_METHOD0(GetClient
, Client
*());
57 class MockClient
: public Client
{
60 ~MockClient() override
{}
62 MOCK_METHOD1(AddObserver
, void(ClientObserver
* observer
));
63 MOCK_METHOD1(RemoveObserver
, void(ClientObserver
* observer
));
64 MOCK_CONST_METHOD0(SupportsSignIn
, bool());
65 MOCK_METHOD0(DispatchUnlockEvent
, void());
66 MOCK_METHOD1(RequestDecryption
, void(const std::string
& challenge
));
67 MOCK_METHOD0(RequestUnlock
, void());
70 DISALLOW_COPY_AND_ASSIGN(MockClient
);
73 class MockProximityMonitor
: public ProximityMonitor
{
75 MockProximityMonitor() {
76 ON_CALL(*this, GetStrategy())
77 .WillByDefault(Return(ProximityMonitor::Strategy::NONE
));
78 ON_CALL(*this, IsUnlockAllowed()).WillByDefault(Return(true));
79 ON_CALL(*this, IsInRssiRange()).WillByDefault(Return(false));
81 ~MockProximityMonitor() override
{}
83 MOCK_METHOD0(Start
, void());
84 MOCK_METHOD0(Stop
, void());
85 MOCK_CONST_METHOD0(GetStrategy
, Strategy());
86 MOCK_CONST_METHOD0(IsUnlockAllowed
, bool());
87 MOCK_CONST_METHOD0(IsInRssiRange
, bool());
88 MOCK_METHOD0(RecordProximityMetricsOnAuthSuccess
, void());
91 DISALLOW_COPY_AND_ASSIGN(MockProximityMonitor
);
94 class FakeLockHandler
: public ScreenlockBridge::LockHandler
{
97 ~FakeLockHandler() override
{}
100 void ShowBannerMessage(const base::string16
& message
) override
{}
101 void ShowUserPodCustomIcon(
102 const std::string
& user_email
,
103 const ScreenlockBridge::UserPodCustomIconOptions
& icon
) override
{}
104 void HideUserPodCustomIcon(const std::string
& user_email
) override
{}
105 void EnableInput() override
{}
106 void SetAuthType(const std::string
& user_email
,
108 const base::string16
& auth_value
) override
{}
109 AuthType
GetAuthType(const std::string
& user_email
) const override
{
112 ScreenType
GetScreenType() const override
{ return LOCK_SCREEN
; }
113 void Unlock(const std::string
& user_email
) override
{}
114 void AttemptEasySignin(const std::string
& user_email
,
115 const std::string
& secret
,
116 const std::string
& key_label
) override
{}
119 DISALLOW_COPY_AND_ASSIGN(FakeLockHandler
);
122 class TestUnlockManager
: public UnlockManager
{
124 TestUnlockManager(ScreenlockType screenlock_type
,
125 scoped_ptr
<ProximityMonitor
> proximity_monitor
,
126 ProximityAuthClient
* proximity_auth_client
)
127 : UnlockManager(screenlock_type
,
128 proximity_monitor
.Pass(),
129 proximity_auth_client
) {}
130 ~TestUnlockManager() override
{}
132 using UnlockManager::OnAuthAttempted
;
133 using ClientObserver::OnUnlockEventSent
;
134 using ClientObserver::OnRemoteStatusUpdate
;
135 using ClientObserver::OnDecryptResponse
;
136 using ClientObserver::OnUnlockResponse
;
137 using ClientObserver::OnDisconnected
;
138 using ScreenlockBridge::Observer::OnScreenDidLock
;
139 using ScreenlockBridge::Observer::OnScreenDidUnlock
;
140 using ScreenlockBridge::Observer::OnFocusedUserChanged
;
143 // Creates a mock Bluetooth adapter and sets it as the global adapter for
145 scoped_refptr
<device::MockBluetoothAdapter
>
146 CreateAndRegisterMockBluetoothAdapter() {
147 scoped_refptr
<device::MockBluetoothAdapter
> adapter
=
148 new NiceMock
<device::MockBluetoothAdapter
>();
149 device::BluetoothAdapterFactory::SetAdapterForTesting(adapter
);
155 class ProximityAuthUnlockManagerTest
: public testing::Test
{
157 ProximityAuthUnlockManagerTest()
158 : bluetooth_adapter_(CreateAndRegisterMockBluetoothAdapter()),
159 proximity_monitor_(nullptr),
160 task_runner_(new base::TestSimpleTaskRunner()),
161 thread_task_runner_handle_(task_runner_
) {
162 ON_CALL(*bluetooth_adapter_
, IsPowered()).WillByDefault(Return(true));
163 ON_CALL(controller_
, GetClient()).WillByDefault(Return(&client_
));
164 ON_CALL(client_
, SupportsSignIn()).WillByDefault(Return(true));
166 ScreenlockBridge::Get()->SetLockHandler(&lock_handler_
);
168 #if defined(OS_CHROMEOS)
169 chromeos::DBusThreadManager::Initialize();
173 ~ProximityAuthUnlockManagerTest() override
{
174 // Make sure to verify the mock prior to the destruction of the unlock
175 // manager, as otherwise it's impossible to tell whether calls to Stop()
176 // occur as a side-effect of the destruction or from the code intended to be
178 if (proximity_monitor_
)
179 testing::Mock::VerifyAndClearExpectations(proximity_monitor_
);
181 // The UnlockManager must be destroyed before calling
182 // chromeos::DBusThreadManager::Shutdown(), as the UnlockManager's
183 // destructor references the DBusThreadManager.
184 unlock_manager_
.reset();
186 #if defined(OS_CHROMEOS)
187 chromeos::DBusThreadManager::Shutdown();
190 ScreenlockBridge::Get()->SetLockHandler(nullptr);
193 void CreateUnlockManager(UnlockManager::ScreenlockType screenlock_type
) {
194 proximity_monitor_
= new NiceMock
<MockProximityMonitor
>;
195 unlock_manager_
.reset(new TestUnlockManager(
196 screenlock_type
, make_scoped_ptr(proximity_monitor_
),
197 &proximity_auth_client_
));
200 void SimulateUserPresentState() {
201 ON_CALL(controller_
, GetState())
202 .WillByDefault(Return(Controller::State::STOPPED
));
203 unlock_manager_
->SetController(&controller_
);
205 ON_CALL(controller_
, GetState())
206 .WillByDefault(Return(Controller::State::SECURE_CHANNEL_ESTABLISHED
));
207 unlock_manager_
->OnControllerStateChanged();
209 unlock_manager_
->OnRemoteStatusUpdate(kRemoteScreenUnlocked
);
212 void RunPendingTasks() { task_runner_
->RunPendingTasks(); }
215 // Mock used for verifying interactions with the Bluetooth subsystem.
216 scoped_refptr
<device::MockBluetoothAdapter
> bluetooth_adapter_
;
218 NiceMock
<MockProximityAuthClient
> proximity_auth_client_
;
219 NiceMock
<MockController
> controller_
;
220 NiceMock
<MockClient
> client_
;
221 scoped_ptr
<TestUnlockManager
> unlock_manager_
;
222 // Owned by the |unlock_manager_|.
223 MockProximityMonitor
* proximity_monitor_
;
226 scoped_refptr
<base::TestSimpleTaskRunner
> task_runner_
;
227 base::ThreadTaskRunnerHandle thread_task_runner_handle_
;
228 FakeLockHandler lock_handler_
;
229 ScopedDisableLoggingForTesting disable_logging_
;
232 TEST_F(ProximityAuthUnlockManagerTest
, IsUnlockAllowed_InitialState
) {
233 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK
);
234 EXPECT_FALSE(unlock_manager_
->IsUnlockAllowed());
237 TEST_F(ProximityAuthUnlockManagerTest
, IsUnlockAllowed_SessionLock_AllGood
) {
238 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK
);
240 ON_CALL(controller_
, GetState())
241 .WillByDefault(Return(Controller::State::SECURE_CHANNEL_ESTABLISHED
));
242 unlock_manager_
->SetController(&controller_
);
243 unlock_manager_
->OnRemoteStatusUpdate(kRemoteScreenUnlocked
);
245 EXPECT_TRUE(unlock_manager_
->IsUnlockAllowed());
248 TEST_F(ProximityAuthUnlockManagerTest
, IsUnlockAllowed_SignIn_AllGood
) {
249 CreateUnlockManager(UnlockManager::ScreenlockType::SIGN_IN
);
251 ON_CALL(controller_
, GetState())
252 .WillByDefault(Return(Controller::State::STOPPED
));
253 unlock_manager_
->SetController(&controller_
);
255 ON_CALL(controller_
, GetState())
256 .WillByDefault(Return(Controller::State::SECURE_CHANNEL_ESTABLISHED
));
257 unlock_manager_
->OnControllerStateChanged();
259 ON_CALL(client_
, SupportsSignIn()).WillByDefault(Return(true));
260 unlock_manager_
->OnRemoteStatusUpdate(kRemoteScreenUnlocked
);
262 EXPECT_TRUE(unlock_manager_
->IsUnlockAllowed());
265 TEST_F(ProximityAuthUnlockManagerTest
,
266 IsUnlockAllowed_SignIn_ClientDoesNotSupportSignIn
) {
267 CreateUnlockManager(UnlockManager::ScreenlockType::SIGN_IN
);
269 ON_CALL(controller_
, GetState())
270 .WillByDefault(Return(Controller::State::STOPPED
));
271 unlock_manager_
->SetController(&controller_
);
273 ON_CALL(controller_
, GetState())
274 .WillByDefault(Return(Controller::State::SECURE_CHANNEL_ESTABLISHED
));
275 unlock_manager_
->OnControllerStateChanged();
277 ON_CALL(client_
, SupportsSignIn()).WillByDefault(Return(false));
278 unlock_manager_
->OnRemoteStatusUpdate(kRemoteScreenUnlocked
);
280 EXPECT_FALSE(unlock_manager_
->IsUnlockAllowed());
283 TEST_F(ProximityAuthUnlockManagerTest
, IsUnlockAllowed_SignIn_ClientIsNull
) {
284 CreateUnlockManager(UnlockManager::ScreenlockType::SIGN_IN
);
286 ON_CALL(controller_
, GetState())
287 .WillByDefault(Return(Controller::State::SECURE_CHANNEL_ESTABLISHED
));
288 ON_CALL(controller_
, GetClient()).WillByDefault(Return(nullptr));
289 unlock_manager_
->SetController(&controller_
);
290 unlock_manager_
->OnRemoteStatusUpdate(kRemoteScreenUnlocked
);
292 EXPECT_FALSE(unlock_manager_
->IsUnlockAllowed());
295 TEST_F(ProximityAuthUnlockManagerTest
,
296 IsUnlockAllowed_DisallowedByProximityMonitor
) {
297 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK
);
299 ON_CALL(controller_
, GetState())
300 .WillByDefault(Return(Controller::State::SECURE_CHANNEL_ESTABLISHED
));
301 unlock_manager_
->SetController(&controller_
);
302 unlock_manager_
->OnRemoteStatusUpdate(kRemoteScreenUnlocked
);
304 ON_CALL(*proximity_monitor_
, IsUnlockAllowed()).WillByDefault(Return(false));
305 EXPECT_FALSE(unlock_manager_
->IsUnlockAllowed());
308 TEST_F(ProximityAuthUnlockManagerTest
,
309 IsUnlockAllowed_SecureChannelNotEstablished
) {
310 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK
);
312 ON_CALL(controller_
, GetState())
313 .WillByDefault(Return(Controller::State::AUTHENTICATING
));
314 unlock_manager_
->SetController(&controller_
);
315 unlock_manager_
->OnRemoteStatusUpdate(kRemoteScreenUnlocked
);
317 EXPECT_FALSE(unlock_manager_
->IsUnlockAllowed());
320 TEST_F(ProximityAuthUnlockManagerTest
, IsUnlockAllowed_ControllerIsNull
) {
321 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK
);
323 unlock_manager_
->SetController(nullptr);
324 unlock_manager_
->OnRemoteStatusUpdate(kRemoteScreenUnlocked
);
326 EXPECT_FALSE(unlock_manager_
->IsUnlockAllowed());
329 TEST_F(ProximityAuthUnlockManagerTest
,
330 IsUnlockAllowed_RemoteScreenlockStateLocked
) {
331 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK
);
333 ON_CALL(controller_
, GetState())
334 .WillByDefault(Return(Controller::State::SECURE_CHANNEL_ESTABLISHED
));
335 unlock_manager_
->SetController(&controller_
);
336 unlock_manager_
->OnRemoteStatusUpdate(kRemoteScreenLocked
);
338 EXPECT_FALSE(unlock_manager_
->IsUnlockAllowed());
341 TEST_F(ProximityAuthUnlockManagerTest
,
342 IsUnlockAllowed_RemoteScreenlockStateUnknown
) {
343 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK
);
345 ON_CALL(controller_
, GetState())
346 .WillByDefault(Return(Controller::State::SECURE_CHANNEL_ESTABLISHED
));
347 unlock_manager_
->SetController(&controller_
);
348 unlock_manager_
->OnRemoteStatusUpdate(kRemoteScreenlockStateUnknown
);
350 EXPECT_FALSE(unlock_manager_
->IsUnlockAllowed());
353 TEST_F(ProximityAuthUnlockManagerTest
,
354 IsUnlockAllowed_RemoteScreenlockStateDisabled
) {
355 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK
);
357 ON_CALL(controller_
, GetState())
358 .WillByDefault(Return(Controller::State::SECURE_CHANNEL_ESTABLISHED
));
359 unlock_manager_
->SetController(&controller_
);
360 unlock_manager_
->OnRemoteStatusUpdate(kRemoteScreenlockDisabled
);
362 EXPECT_FALSE(unlock_manager_
->IsUnlockAllowed());
365 TEST_F(ProximityAuthUnlockManagerTest
,
366 IsUnlockAllowed_RemoteScreenlockStateNotYetReceived
) {
367 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK
);
369 ON_CALL(controller_
, GetState())
370 .WillByDefault(Return(Controller::State::SECURE_CHANNEL_ESTABLISHED
));
371 unlock_manager_
->SetController(&controller_
);
373 EXPECT_FALSE(unlock_manager_
->IsUnlockAllowed());
376 TEST_F(ProximityAuthUnlockManagerTest
, SetController_SetToNull
) {
377 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK
);
378 SimulateUserPresentState();
380 EXPECT_CALL(proximity_auth_client_
,
381 UpdateScreenlockState(ScreenlockState::INACTIVE
));
382 unlock_manager_
->SetController(nullptr);
385 TEST_F(ProximityAuthUnlockManagerTest
, SetController_ExistingController
) {
386 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK
);
387 SimulateUserPresentState();
389 EXPECT_CALL(proximity_auth_client_
, UpdateScreenlockState(_
)).Times(0);
390 unlock_manager_
->SetController(&controller_
);
393 TEST_F(ProximityAuthUnlockManagerTest
,
394 SetController_NullThenExistingController
) {
395 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK
);
396 SimulateUserPresentState();
398 EXPECT_CALL(proximity_auth_client_
,
399 UpdateScreenlockState(ScreenlockState::INACTIVE
));
400 unlock_manager_
->SetController(nullptr);
402 EXPECT_CALL(proximity_auth_client_
,
403 UpdateScreenlockState(ScreenlockState::AUTHENTICATED
));
404 unlock_manager_
->SetController(&controller_
);
407 TEST_F(ProximityAuthUnlockManagerTest
, SetController_AuthenticationFailed
) {
408 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK
);
409 SimulateUserPresentState();
411 unlock_manager_
->SetController(nullptr);
413 ON_CALL(controller_
, GetState())
414 .WillByDefault(Return(Controller::State::AUTHENTICATION_FAILED
));
415 EXPECT_CALL(proximity_auth_client_
,
416 UpdateScreenlockState(ScreenlockState::PHONE_NOT_AUTHENTICATED
));
417 unlock_manager_
->SetController(&controller_
);
420 TEST_F(ProximityAuthUnlockManagerTest
, SetController_WakingUp
) {
421 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK
);
422 SimulateUserPresentState();
424 unlock_manager_
->SetController(nullptr);
426 ON_CALL(controller_
, GetState())
427 .WillByDefault(Return(Controller::State::FINDING_CONNECTION
));
428 EXPECT_CALL(proximity_auth_client_
,
429 UpdateScreenlockState(ScreenlockState::BLUETOOTH_CONNECTING
));
430 unlock_manager_
->SetController(&controller_
);
433 TEST_F(ProximityAuthUnlockManagerTest
,
434 SetController_NullController_StopsProximityMonitor
) {
435 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK
);
436 SimulateUserPresentState();
438 EXPECT_CALL(*proximity_monitor_
, Stop()).Times(AtLeast(1));
439 unlock_manager_
->SetController(nullptr);
442 TEST_F(ProximityAuthUnlockManagerTest
,
443 SetController_ConnectingController_StopsProximityMonitor
) {
444 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK
);
445 SimulateUserPresentState();
447 NiceMock
<MockController
> controller
;
448 ON_CALL(controller
, GetState())
449 .WillByDefault(Return(Controller::State::FINDING_CONNECTION
));
451 EXPECT_CALL(*proximity_monitor_
, Stop()).Times(AtLeast(1));
452 unlock_manager_
->SetController(&controller
);
455 TEST_F(ProximityAuthUnlockManagerTest
,
456 SetController_ConnectedController_StartsProximityMonitor
) {
457 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK
);
458 SimulateUserPresentState();
460 NiceMock
<MockController
> controller
;
461 ON_CALL(controller
, GetState())
462 .WillByDefault(Return(Controller::State::SECURE_CHANNEL_ESTABLISHED
));
464 EXPECT_CALL(*proximity_monitor_
, Start()).Times(AtLeast(1));
465 unlock_manager_
->SetController(&controller
);
468 TEST_F(ProximityAuthUnlockManagerTest
,
469 OnControllerStateChanged_SecureChannelEstablished_RegistersAsObserver
) {
470 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK
);
471 SimulateUserPresentState();
473 NiceMock
<MockController
> controller
;
474 ON_CALL(controller
, GetState())
475 .WillByDefault(Return(Controller::State::SECURE_CHANNEL_ESTABLISHED
));
477 EXPECT_CALL(client_
, AddObserver(unlock_manager_
.get()));
478 unlock_manager_
->OnControllerStateChanged();
481 TEST_F(ProximityAuthUnlockManagerTest
,
482 OnControllerStateChanged_StartsProximityMonitor
) {
483 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK
);
484 SimulateUserPresentState();
486 NiceMock
<MockController
> controller
;
487 ON_CALL(controller
, GetState())
488 .WillByDefault(Return(Controller::State::SECURE_CHANNEL_ESTABLISHED
));
490 EXPECT_CALL(*proximity_monitor_
, Start()).Times(AtLeast(1));
491 unlock_manager_
->OnControllerStateChanged();
494 TEST_F(ProximityAuthUnlockManagerTest
,
495 OnControllerStateChanged_StopsProximityMonitor
) {
496 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK
);
497 SimulateUserPresentState();
499 ON_CALL(controller_
, GetState())
500 .WillByDefault(Return(Controller::State::AUTHENTICATION_FAILED
));
502 EXPECT_CALL(*proximity_monitor_
, Stop()).Times(AtLeast(1));
503 unlock_manager_
->OnControllerStateChanged();
506 TEST_F(ProximityAuthUnlockManagerTest
,
507 OnControllerStateChanged_Stopped_UpdatesScreenlockState
) {
508 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK
);
509 SimulateUserPresentState();
511 ON_CALL(controller_
, GetState())
512 .WillByDefault(Return(Controller::State::STOPPED
));
514 EXPECT_CALL(proximity_auth_client_
,
515 UpdateScreenlockState(ScreenlockState::INACTIVE
));
516 unlock_manager_
->OnControllerStateChanged();
519 TEST_F(ProximityAuthUnlockManagerTest
,
520 OnControllerStateChanged_AuthenticationFailed_UpdatesScreenlockState
) {
521 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK
);
522 SimulateUserPresentState();
524 ON_CALL(controller_
, GetState())
525 .WillByDefault(Return(Controller::State::AUTHENTICATION_FAILED
));
527 EXPECT_CALL(proximity_auth_client_
,
528 UpdateScreenlockState(ScreenlockState::PHONE_NOT_AUTHENTICATED
));
529 unlock_manager_
->OnControllerStateChanged();
532 TEST_F(ProximityAuthUnlockManagerTest
,
533 OnControllerStateChanged_FindingConnection_UpdatesScreenlockState
) {
534 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK
);
536 ON_CALL(controller_
, GetState())
537 .WillByDefault(Return(Controller::State::STOPPED
));
538 unlock_manager_
->SetController(&controller_
);
540 ON_CALL(controller_
, GetState())
541 .WillByDefault(Return(Controller::State::FINDING_CONNECTION
));
543 EXPECT_CALL(proximity_auth_client_
,
544 UpdateScreenlockState(ScreenlockState::BLUETOOTH_CONNECTING
));
545 unlock_manager_
->OnControllerStateChanged();
548 TEST_F(ProximityAuthUnlockManagerTest
,
549 OnControllerStateChanged_Authenticating_UpdatesScreenlockState
) {
550 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK
);
552 ON_CALL(controller_
, GetState())
553 .WillByDefault(Return(Controller::State::STOPPED
));
554 unlock_manager_
->SetController(&controller_
);
556 ON_CALL(controller_
, GetState())
557 .WillByDefault(Return(Controller::State::AUTHENTICATING
));
559 EXPECT_CALL(proximity_auth_client_
,
560 UpdateScreenlockState(ScreenlockState::BLUETOOTH_CONNECTING
));
561 unlock_manager_
->OnControllerStateChanged();
565 ProximityAuthUnlockManagerTest
,
566 OnControllerStateChanged_SecureChannelEstablished_UpdatesScreenlockState
) {
567 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK
);
569 ON_CALL(controller_
, GetState())
570 .WillByDefault(Return(Controller::State::STOPPED
));
571 unlock_manager_
->SetController(&controller_
);
573 ON_CALL(controller_
, GetState())
574 .WillByDefault(Return(Controller::State::SECURE_CHANNEL_ESTABLISHED
));
576 EXPECT_CALL(proximity_auth_client_
,
577 UpdateScreenlockState(ScreenlockState::BLUETOOTH_CONNECTING
));
578 unlock_manager_
->OnControllerStateChanged();
581 TEST_F(ProximityAuthUnlockManagerTest
, OnDisconnected_UnregistersAsObserver
) {
582 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK
);
583 SimulateUserPresentState();
585 ON_CALL(controller_
, GetState())
586 .WillByDefault(Return(Controller::State::AUTHENTICATION_FAILED
));
588 EXPECT_CALL(client_
, RemoveObserver(unlock_manager_
.get()));
589 unlock_manager_
.get()->OnDisconnected();
592 TEST_F(ProximityAuthUnlockManagerTest
,
593 OnScreenDidUnlock_StopsProximityMonitor
) {
594 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK
);
595 SimulateUserPresentState();
597 EXPECT_CALL(*proximity_monitor_
, Stop());
598 unlock_manager_
.get()->OnScreenDidUnlock(
599 ScreenlockBridge::LockHandler::LOCK_SCREEN
);
602 TEST_F(ProximityAuthUnlockManagerTest
, OnScreenDidLock_StartsProximityMonitor
) {
603 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK
);
605 ON_CALL(controller_
, GetState())
606 .WillByDefault(Return(Controller::State::STOPPED
));
607 unlock_manager_
->SetController(&controller_
);
609 ON_CALL(controller_
, GetState())
610 .WillByDefault(Return(Controller::State::SECURE_CHANNEL_ESTABLISHED
));
611 unlock_manager_
->OnControllerStateChanged();
613 EXPECT_CALL(*proximity_monitor_
, Start());
614 unlock_manager_
.get()->OnScreenDidLock(
615 ScreenlockBridge::LockHandler::LOCK_SCREEN
);
618 TEST_F(ProximityAuthUnlockManagerTest
, OnScreenDidLock_SetsWakingUpState
) {
619 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK
);
620 SimulateUserPresentState();
622 unlock_manager_
.get()->OnScreenDidUnlock(
623 ScreenlockBridge::LockHandler::LOCK_SCREEN
);
625 ON_CALL(controller_
, GetState())
626 .WillByDefault(Return(Controller::State::FINDING_CONNECTION
));
627 unlock_manager_
->OnControllerStateChanged();
629 EXPECT_CALL(proximity_auth_client_
,
630 UpdateScreenlockState(ScreenlockState::BLUETOOTH_CONNECTING
));
631 unlock_manager_
.get()->OnScreenDidLock(
632 ScreenlockBridge::LockHandler::LOCK_SCREEN
);
635 TEST_F(ProximityAuthUnlockManagerTest
,
636 OnDecryptResponse_NoAuthAttemptInProgress
) {
637 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK
);
638 SimulateUserPresentState();
640 EXPECT_CALL(proximity_auth_client_
, FinalizeUnlock(_
)).Times(0);
641 unlock_manager_
.get()->OnDecryptResponse(nullptr);
644 TEST_F(ProximityAuthUnlockManagerTest
,
645 OnUnlockEventSent_NoAuthAttemptInProgress
) {
646 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK
);
647 SimulateUserPresentState();
649 EXPECT_CALL(proximity_auth_client_
, FinalizeUnlock(_
)).Times(0);
650 unlock_manager_
.get()->OnUnlockEventSent(true);
653 TEST_F(ProximityAuthUnlockManagerTest
,
654 OnUnlockResponse_NoAuthAttemptInProgress
) {
655 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK
);
656 SimulateUserPresentState();
658 EXPECT_CALL(proximity_auth_client_
, FinalizeUnlock(_
)).Times(0);
659 unlock_manager_
.get()->OnUnlockResponse(true);
662 TEST_F(ProximityAuthUnlockManagerTest
, OnAuthAttempted_NoController
) {
663 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK
);
664 SimulateUserPresentState();
666 unlock_manager_
->SetController(nullptr);
668 EXPECT_CALL(proximity_auth_client_
, FinalizeUnlock(false));
669 unlock_manager_
->OnAuthAttempted(ScreenlockBridge::LockHandler::USER_CLICK
);
672 TEST_F(ProximityAuthUnlockManagerTest
, OnAuthAttempted_UnlockNotAllowed
) {
673 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK
);
674 SimulateUserPresentState();
676 ON_CALL(*proximity_monitor_
, IsUnlockAllowed()).WillByDefault(Return(false));
678 EXPECT_CALL(proximity_auth_client_
, FinalizeUnlock(false));
679 unlock_manager_
->OnAuthAttempted(ScreenlockBridge::LockHandler::USER_CLICK
);
682 TEST_F(ProximityAuthUnlockManagerTest
, OnAuthAttempted_NotUserClick
) {
683 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK
);
684 SimulateUserPresentState();
686 EXPECT_CALL(proximity_auth_client_
, FinalizeUnlock(_
)).Times(0);
687 unlock_manager_
->OnAuthAttempted(
688 ScreenlockBridge::LockHandler::EXPAND_THEN_USER_CLICK
);
691 TEST_F(ProximityAuthUnlockManagerTest
, OnAuthAttempted_DuplicateCall
) {
692 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK
);
693 SimulateUserPresentState();
695 EXPECT_CALL(client_
, RequestUnlock());
696 unlock_manager_
->OnAuthAttempted(ScreenlockBridge::LockHandler::USER_CLICK
);
698 EXPECT_CALL(client_
, RequestUnlock()).Times(0);
699 unlock_manager_
->OnAuthAttempted(ScreenlockBridge::LockHandler::USER_CLICK
);
702 TEST_F(ProximityAuthUnlockManagerTest
, OnAuthAttempted_TimesOut
) {
703 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK
);
704 SimulateUserPresentState();
706 unlock_manager_
->OnAuthAttempted(ScreenlockBridge::LockHandler::USER_CLICK
);
708 // Simulate the timeout period elapsing.
709 EXPECT_CALL(proximity_auth_client_
, FinalizeUnlock(false));
713 TEST_F(ProximityAuthUnlockManagerTest
,
714 OnAuthAttempted_DoesntTimeOutFollowingResponse
) {
715 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK
);
716 SimulateUserPresentState();
718 unlock_manager_
->OnAuthAttempted(ScreenlockBridge::LockHandler::USER_CLICK
);
720 EXPECT_CALL(proximity_auth_client_
, FinalizeUnlock(_
));
721 unlock_manager_
->OnUnlockResponse(false);
723 // Simulate the timeout period elapsing.
724 EXPECT_CALL(proximity_auth_client_
, FinalizeUnlock(_
)).Times(0);
728 TEST_F(ProximityAuthUnlockManagerTest
,
729 OnAuthAttempted_Unlock_SupportsSignIn_UnlockRequestFails
) {
730 ON_CALL(client_
, SupportsSignIn()).WillByDefault(Return(true));
731 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK
);
732 SimulateUserPresentState();
734 EXPECT_CALL(client_
, RequestUnlock());
735 unlock_manager_
->OnAuthAttempted(ScreenlockBridge::LockHandler::USER_CLICK
);
737 EXPECT_CALL(proximity_auth_client_
, FinalizeUnlock(false));
738 unlock_manager_
->OnUnlockResponse(false);
741 TEST_F(ProximityAuthUnlockManagerTest
,
742 OnAuthAttempted_Unlock_WithSignIn_RequestSucceeds_EventSendFails
) {
743 ON_CALL(client_
, SupportsSignIn()).WillByDefault(Return(true));
744 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK
);
745 SimulateUserPresentState();
747 EXPECT_CALL(client_
, RequestUnlock());
748 unlock_manager_
->OnAuthAttempted(ScreenlockBridge::LockHandler::USER_CLICK
);
750 EXPECT_CALL(client_
, DispatchUnlockEvent());
751 unlock_manager_
->OnUnlockResponse(true);
753 EXPECT_CALL(proximity_auth_client_
, FinalizeUnlock(false));
754 unlock_manager_
->OnUnlockEventSent(false);
757 TEST_F(ProximityAuthUnlockManagerTest
,
758 OnAuthAttempted_Unlock_WithSignIn_RequestSucceeds_EventSendSucceeds
) {
759 ON_CALL(client_
, SupportsSignIn()).WillByDefault(Return(true));
760 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK
);
761 SimulateUserPresentState();
763 EXPECT_CALL(client_
, RequestUnlock());
764 unlock_manager_
->OnAuthAttempted(ScreenlockBridge::LockHandler::USER_CLICK
);
766 EXPECT_CALL(client_
, DispatchUnlockEvent());
767 unlock_manager_
->OnUnlockResponse(true);
769 EXPECT_CALL(proximity_auth_client_
, FinalizeUnlock(true));
770 unlock_manager_
->OnUnlockEventSent(true);
773 TEST_F(ProximityAuthUnlockManagerTest
,
774 OnAuthAttempted_Unlock_DoesntSupportSignIn_UnlockEventSendFails
) {
775 ON_CALL(client_
, SupportsSignIn()).WillByDefault(Return(false));
776 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK
);
777 SimulateUserPresentState();
779 EXPECT_CALL(client_
, DispatchUnlockEvent());
780 unlock_manager_
->OnAuthAttempted(ScreenlockBridge::LockHandler::USER_CLICK
);
782 EXPECT_CALL(proximity_auth_client_
, FinalizeUnlock(false));
783 unlock_manager_
->OnUnlockEventSent(false);
786 TEST_F(ProximityAuthUnlockManagerTest
,
787 OnAuthAttempted_Unlock_SupportsSignIn_UnlockEventSendSucceeds
) {
788 ON_CALL(client_
, SupportsSignIn()).WillByDefault(Return(false));
789 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK
);
790 SimulateUserPresentState();
792 EXPECT_CALL(client_
, DispatchUnlockEvent());
793 unlock_manager_
->OnAuthAttempted(ScreenlockBridge::LockHandler::USER_CLICK
);
795 EXPECT_CALL(proximity_auth_client_
, FinalizeUnlock(true));
796 unlock_manager_
->OnUnlockEventSent(true);
799 } // namespace proximity_auth