roll libyuv to r1464 for yuva support
[chromium-blink-merge.git] / components / proximity_auth / unlock_manager_unittest.cc
blobeffc52fa5862d21207da7eea920717912f5a9794
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/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;
31 using testing::_;
33 namespace proximity_auth {
34 namespace {
36 // Note that the trust agent state is currently ignored by the UnlockManager
37 // implementation.
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 {
49 public:
50 MockController() {}
51 ~MockController() override {}
53 MOCK_CONST_METHOD0(GetState, State());
54 MOCK_METHOD0(GetClient, Client*());
57 class MockClient : public Client {
58 public:
59 MockClient() {}
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());
69 private:
70 DISALLOW_COPY_AND_ASSIGN(MockClient);
73 class MockProximityMonitor : public ProximityMonitor {
74 public:
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());
90 private:
91 DISALLOW_COPY_AND_ASSIGN(MockProximityMonitor);
94 class MockProximityAuthClient : public ProximityAuthClient {
95 public:
96 MockProximityAuthClient() {}
97 ~MockProximityAuthClient() override {}
99 MOCK_CONST_METHOD0(GetAuthenticatedUsername, std::string());
100 MOCK_METHOD1(UpdateScreenlockState,
101 void(proximity_auth::ScreenlockState state));
102 MOCK_METHOD1(FinalizeUnlock, void(bool success));
103 MOCK_METHOD1(FinalizeSignin, void(const std::string& secret));
105 private:
106 DISALLOW_COPY_AND_ASSIGN(MockProximityAuthClient);
109 class FakeLockHandler : public ScreenlockBridge::LockHandler {
110 public:
111 FakeLockHandler() {}
112 ~FakeLockHandler() override {}
114 // LockHandler:
115 void ShowBannerMessage(const base::string16& message) override {}
116 void ShowUserPodCustomIcon(
117 const std::string& user_email,
118 const ScreenlockBridge::UserPodCustomIconOptions& icon) override {}
119 void HideUserPodCustomIcon(const std::string& user_email) override {}
120 void EnableInput() override {}
121 void SetAuthType(const std::string& user_email,
122 AuthType auth_type,
123 const base::string16& auth_value) override {}
124 AuthType GetAuthType(const std::string& user_email) const override {
125 return USER_CLICK;
127 ScreenType GetScreenType() const override { return LOCK_SCREEN; }
128 void Unlock(const std::string& user_email) override {}
129 void AttemptEasySignin(const std::string& user_email,
130 const std::string& secret,
131 const std::string& key_label) override {}
133 private:
134 DISALLOW_COPY_AND_ASSIGN(FakeLockHandler);
137 class TestUnlockManager : public UnlockManager {
138 public:
139 TestUnlockManager(ScreenlockType screenlock_type,
140 scoped_ptr<ProximityMonitor> proximity_monitor,
141 ProximityAuthClient* proximity_auth_client)
142 : UnlockManager(screenlock_type,
143 proximity_monitor.Pass(),
144 proximity_auth_client) {}
145 ~TestUnlockManager() override {}
147 using UnlockManager::OnAuthAttempted;
148 using ClientObserver::OnUnlockEventSent;
149 using ClientObserver::OnRemoteStatusUpdate;
150 using ClientObserver::OnDecryptResponse;
151 using ClientObserver::OnUnlockResponse;
152 using ClientObserver::OnDisconnected;
153 using ScreenlockBridge::Observer::OnScreenDidLock;
154 using ScreenlockBridge::Observer::OnScreenDidUnlock;
155 using ScreenlockBridge::Observer::OnFocusedUserChanged;
158 // Creates a mock Bluetooth adapter and sets it as the global adapter for
159 // testing.
160 scoped_refptr<device::MockBluetoothAdapter>
161 CreateAndRegisterMockBluetoothAdapter() {
162 scoped_refptr<device::MockBluetoothAdapter> adapter =
163 new NiceMock<device::MockBluetoothAdapter>();
164 device::BluetoothAdapterFactory::SetAdapterForTesting(adapter);
165 return adapter;
168 } // namespace
170 class ProximityAuthUnlockManagerTest : public testing::Test {
171 public:
172 ProximityAuthUnlockManagerTest()
173 : bluetooth_adapter_(CreateAndRegisterMockBluetoothAdapter()),
174 proximity_monitor_(nullptr),
175 task_runner_(new base::TestSimpleTaskRunner()),
176 thread_task_runner_handle_(task_runner_) {
177 ON_CALL(*bluetooth_adapter_, IsPowered()).WillByDefault(Return(true));
178 ON_CALL(controller_, GetClient()).WillByDefault(Return(&client_));
179 ON_CALL(client_, SupportsSignIn()).WillByDefault(Return(true));
181 ScreenlockBridge::Get()->SetLockHandler(&lock_handler_);
183 #if defined(OS_CHROMEOS)
184 chromeos::DBusThreadManager::Initialize();
185 #endif
188 ~ProximityAuthUnlockManagerTest() override {
189 // Make sure to verify the mock prior to the destruction of the unlock
190 // manager, as otherwise it's impossible to tell whether calls to Stop()
191 // occur as a side-effect of the destruction or from the code intended to be
192 // under test.
193 if (proximity_monitor_)
194 testing::Mock::VerifyAndClearExpectations(proximity_monitor_);
196 // The UnlockManager must be destroyed before calling
197 // chromeos::DBusThreadManager::Shutdown(), as the UnlockManager's
198 // destructor references the DBusThreadManager.
199 unlock_manager_.reset();
201 #if defined(OS_CHROMEOS)
202 chromeos::DBusThreadManager::Shutdown();
203 #endif
205 ScreenlockBridge::Get()->SetLockHandler(nullptr);
208 void CreateUnlockManager(UnlockManager::ScreenlockType screenlock_type) {
209 proximity_monitor_ = new NiceMock<MockProximityMonitor>;
210 unlock_manager_.reset(new TestUnlockManager(
211 screenlock_type, make_scoped_ptr(proximity_monitor_),
212 &proximity_auth_client_));
215 void SimulateUserPresentState() {
216 ON_CALL(controller_, GetState())
217 .WillByDefault(Return(Controller::State::STOPPED));
218 unlock_manager_->SetController(&controller_);
220 ON_CALL(controller_, GetState())
221 .WillByDefault(Return(Controller::State::SECURE_CHANNEL_ESTABLISHED));
222 unlock_manager_->OnControllerStateChanged();
224 unlock_manager_->OnRemoteStatusUpdate(kRemoteScreenUnlocked);
227 void RunPendingTasks() { task_runner_->RunPendingTasks(); }
229 protected:
230 // Mock used for verifying interactions with the Bluetooth subsystem.
231 scoped_refptr<device::MockBluetoothAdapter> bluetooth_adapter_;
233 NiceMock<MockProximityAuthClient> proximity_auth_client_;
234 NiceMock<MockController> controller_;
235 NiceMock<MockClient> client_;
236 scoped_ptr<TestUnlockManager> unlock_manager_;
237 // Owned by the |unlock_manager_|.
238 MockProximityMonitor* proximity_monitor_;
240 private:
241 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
242 base::ThreadTaskRunnerHandle thread_task_runner_handle_;
243 FakeLockHandler lock_handler_;
244 ScopedDisableLoggingForTesting disable_logging_;
247 TEST_F(ProximityAuthUnlockManagerTest, IsUnlockAllowed_InitialState) {
248 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
249 EXPECT_FALSE(unlock_manager_->IsUnlockAllowed());
252 TEST_F(ProximityAuthUnlockManagerTest, IsUnlockAllowed_SessionLock_AllGood) {
253 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
255 ON_CALL(controller_, GetState())
256 .WillByDefault(Return(Controller::State::SECURE_CHANNEL_ESTABLISHED));
257 unlock_manager_->SetController(&controller_);
258 unlock_manager_->OnRemoteStatusUpdate(kRemoteScreenUnlocked);
260 EXPECT_TRUE(unlock_manager_->IsUnlockAllowed());
263 TEST_F(ProximityAuthUnlockManagerTest, IsUnlockAllowed_SignIn_AllGood) {
264 CreateUnlockManager(UnlockManager::ScreenlockType::SIGN_IN);
266 ON_CALL(controller_, GetState())
267 .WillByDefault(Return(Controller::State::STOPPED));
268 unlock_manager_->SetController(&controller_);
270 ON_CALL(controller_, GetState())
271 .WillByDefault(Return(Controller::State::SECURE_CHANNEL_ESTABLISHED));
272 unlock_manager_->OnControllerStateChanged();
274 ON_CALL(client_, SupportsSignIn()).WillByDefault(Return(true));
275 unlock_manager_->OnRemoteStatusUpdate(kRemoteScreenUnlocked);
277 EXPECT_TRUE(unlock_manager_->IsUnlockAllowed());
280 TEST_F(ProximityAuthUnlockManagerTest,
281 IsUnlockAllowed_SignIn_ClientDoesNotSupportSignIn) {
282 CreateUnlockManager(UnlockManager::ScreenlockType::SIGN_IN);
284 ON_CALL(controller_, GetState())
285 .WillByDefault(Return(Controller::State::STOPPED));
286 unlock_manager_->SetController(&controller_);
288 ON_CALL(controller_, GetState())
289 .WillByDefault(Return(Controller::State::SECURE_CHANNEL_ESTABLISHED));
290 unlock_manager_->OnControllerStateChanged();
292 ON_CALL(client_, SupportsSignIn()).WillByDefault(Return(false));
293 unlock_manager_->OnRemoteStatusUpdate(kRemoteScreenUnlocked);
295 EXPECT_FALSE(unlock_manager_->IsUnlockAllowed());
298 TEST_F(ProximityAuthUnlockManagerTest, IsUnlockAllowed_SignIn_ClientIsNull) {
299 CreateUnlockManager(UnlockManager::ScreenlockType::SIGN_IN);
301 ON_CALL(controller_, GetState())
302 .WillByDefault(Return(Controller::State::SECURE_CHANNEL_ESTABLISHED));
303 ON_CALL(controller_, GetClient()).WillByDefault(Return(nullptr));
304 unlock_manager_->SetController(&controller_);
305 unlock_manager_->OnRemoteStatusUpdate(kRemoteScreenUnlocked);
307 EXPECT_FALSE(unlock_manager_->IsUnlockAllowed());
310 TEST_F(ProximityAuthUnlockManagerTest,
311 IsUnlockAllowed_DisallowedByProximityMonitor) {
312 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
314 ON_CALL(controller_, GetState())
315 .WillByDefault(Return(Controller::State::SECURE_CHANNEL_ESTABLISHED));
316 unlock_manager_->SetController(&controller_);
317 unlock_manager_->OnRemoteStatusUpdate(kRemoteScreenUnlocked);
319 ON_CALL(*proximity_monitor_, IsUnlockAllowed()).WillByDefault(Return(false));
320 EXPECT_FALSE(unlock_manager_->IsUnlockAllowed());
323 TEST_F(ProximityAuthUnlockManagerTest,
324 IsUnlockAllowed_SecureChannelNotEstablished) {
325 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
327 ON_CALL(controller_, GetState())
328 .WillByDefault(Return(Controller::State::AUTHENTICATING));
329 unlock_manager_->SetController(&controller_);
330 unlock_manager_->OnRemoteStatusUpdate(kRemoteScreenUnlocked);
332 EXPECT_FALSE(unlock_manager_->IsUnlockAllowed());
335 TEST_F(ProximityAuthUnlockManagerTest, IsUnlockAllowed_ControllerIsNull) {
336 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
338 unlock_manager_->SetController(nullptr);
339 unlock_manager_->OnRemoteStatusUpdate(kRemoteScreenUnlocked);
341 EXPECT_FALSE(unlock_manager_->IsUnlockAllowed());
344 TEST_F(ProximityAuthUnlockManagerTest,
345 IsUnlockAllowed_RemoteScreenlockStateLocked) {
346 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
348 ON_CALL(controller_, GetState())
349 .WillByDefault(Return(Controller::State::SECURE_CHANNEL_ESTABLISHED));
350 unlock_manager_->SetController(&controller_);
351 unlock_manager_->OnRemoteStatusUpdate(kRemoteScreenLocked);
353 EXPECT_FALSE(unlock_manager_->IsUnlockAllowed());
356 TEST_F(ProximityAuthUnlockManagerTest,
357 IsUnlockAllowed_RemoteScreenlockStateUnknown) {
358 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
360 ON_CALL(controller_, GetState())
361 .WillByDefault(Return(Controller::State::SECURE_CHANNEL_ESTABLISHED));
362 unlock_manager_->SetController(&controller_);
363 unlock_manager_->OnRemoteStatusUpdate(kRemoteScreenlockStateUnknown);
365 EXPECT_FALSE(unlock_manager_->IsUnlockAllowed());
368 TEST_F(ProximityAuthUnlockManagerTest,
369 IsUnlockAllowed_RemoteScreenlockStateDisabled) {
370 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
372 ON_CALL(controller_, GetState())
373 .WillByDefault(Return(Controller::State::SECURE_CHANNEL_ESTABLISHED));
374 unlock_manager_->SetController(&controller_);
375 unlock_manager_->OnRemoteStatusUpdate(kRemoteScreenlockDisabled);
377 EXPECT_FALSE(unlock_manager_->IsUnlockAllowed());
380 TEST_F(ProximityAuthUnlockManagerTest,
381 IsUnlockAllowed_RemoteScreenlockStateNotYetReceived) {
382 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
384 ON_CALL(controller_, GetState())
385 .WillByDefault(Return(Controller::State::SECURE_CHANNEL_ESTABLISHED));
386 unlock_manager_->SetController(&controller_);
388 EXPECT_FALSE(unlock_manager_->IsUnlockAllowed());
391 TEST_F(ProximityAuthUnlockManagerTest, SetController_SetToNull) {
392 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
393 SimulateUserPresentState();
395 EXPECT_CALL(proximity_auth_client_,
396 UpdateScreenlockState(ScreenlockState::INACTIVE));
397 unlock_manager_->SetController(nullptr);
400 TEST_F(ProximityAuthUnlockManagerTest, SetController_ExistingController) {
401 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
402 SimulateUserPresentState();
404 EXPECT_CALL(proximity_auth_client_, UpdateScreenlockState(_)).Times(0);
405 unlock_manager_->SetController(&controller_);
408 TEST_F(ProximityAuthUnlockManagerTest,
409 SetController_NullThenExistingController) {
410 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
411 SimulateUserPresentState();
413 EXPECT_CALL(proximity_auth_client_,
414 UpdateScreenlockState(ScreenlockState::INACTIVE));
415 unlock_manager_->SetController(nullptr);
417 EXPECT_CALL(proximity_auth_client_,
418 UpdateScreenlockState(ScreenlockState::AUTHENTICATED));
419 unlock_manager_->SetController(&controller_);
422 TEST_F(ProximityAuthUnlockManagerTest, SetController_AuthenticationFailed) {
423 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
424 SimulateUserPresentState();
426 unlock_manager_->SetController(nullptr);
428 ON_CALL(controller_, GetState())
429 .WillByDefault(Return(Controller::State::AUTHENTICATION_FAILED));
430 EXPECT_CALL(proximity_auth_client_,
431 UpdateScreenlockState(ScreenlockState::PHONE_NOT_AUTHENTICATED));
432 unlock_manager_->SetController(&controller_);
435 TEST_F(ProximityAuthUnlockManagerTest, SetController_WakingUp) {
436 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
437 SimulateUserPresentState();
439 unlock_manager_->SetController(nullptr);
441 ON_CALL(controller_, GetState())
442 .WillByDefault(Return(Controller::State::FINDING_CONNECTION));
443 EXPECT_CALL(proximity_auth_client_,
444 UpdateScreenlockState(ScreenlockState::BLUETOOTH_CONNECTING));
445 unlock_manager_->SetController(&controller_);
448 TEST_F(ProximityAuthUnlockManagerTest,
449 SetController_NullController_StopsProximityMonitor) {
450 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
451 SimulateUserPresentState();
453 EXPECT_CALL(*proximity_monitor_, Stop()).Times(AtLeast(1));
454 unlock_manager_->SetController(nullptr);
457 TEST_F(ProximityAuthUnlockManagerTest,
458 SetController_ConnectingController_StopsProximityMonitor) {
459 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
460 SimulateUserPresentState();
462 NiceMock<MockController> controller;
463 ON_CALL(controller, GetState())
464 .WillByDefault(Return(Controller::State::FINDING_CONNECTION));
466 EXPECT_CALL(*proximity_monitor_, Stop()).Times(AtLeast(1));
467 unlock_manager_->SetController(&controller);
470 TEST_F(ProximityAuthUnlockManagerTest,
471 SetController_ConnectedController_StartsProximityMonitor) {
472 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
473 SimulateUserPresentState();
475 NiceMock<MockController> controller;
476 ON_CALL(controller, GetState())
477 .WillByDefault(Return(Controller::State::SECURE_CHANNEL_ESTABLISHED));
479 EXPECT_CALL(*proximity_monitor_, Start()).Times(AtLeast(1));
480 unlock_manager_->SetController(&controller);
483 TEST_F(ProximityAuthUnlockManagerTest,
484 OnControllerStateChanged_SecureChannelEstablished_RegistersAsObserver) {
485 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
486 SimulateUserPresentState();
488 NiceMock<MockController> controller;
489 ON_CALL(controller, GetState())
490 .WillByDefault(Return(Controller::State::SECURE_CHANNEL_ESTABLISHED));
492 EXPECT_CALL(client_, AddObserver(unlock_manager_.get()));
493 unlock_manager_->OnControllerStateChanged();
496 TEST_F(ProximityAuthUnlockManagerTest,
497 OnControllerStateChanged_StartsProximityMonitor) {
498 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
499 SimulateUserPresentState();
501 NiceMock<MockController> controller;
502 ON_CALL(controller, GetState())
503 .WillByDefault(Return(Controller::State::SECURE_CHANNEL_ESTABLISHED));
505 EXPECT_CALL(*proximity_monitor_, Start()).Times(AtLeast(1));
506 unlock_manager_->OnControllerStateChanged();
509 TEST_F(ProximityAuthUnlockManagerTest,
510 OnControllerStateChanged_StopsProximityMonitor) {
511 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
512 SimulateUserPresentState();
514 ON_CALL(controller_, GetState())
515 .WillByDefault(Return(Controller::State::AUTHENTICATION_FAILED));
517 EXPECT_CALL(*proximity_monitor_, Stop()).Times(AtLeast(1));
518 unlock_manager_->OnControllerStateChanged();
521 TEST_F(ProximityAuthUnlockManagerTest,
522 OnControllerStateChanged_Stopped_UpdatesScreenlockState) {
523 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
524 SimulateUserPresentState();
526 ON_CALL(controller_, GetState())
527 .WillByDefault(Return(Controller::State::STOPPED));
529 EXPECT_CALL(proximity_auth_client_,
530 UpdateScreenlockState(ScreenlockState::INACTIVE));
531 unlock_manager_->OnControllerStateChanged();
534 TEST_F(ProximityAuthUnlockManagerTest,
535 OnControllerStateChanged_AuthenticationFailed_UpdatesScreenlockState) {
536 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
537 SimulateUserPresentState();
539 ON_CALL(controller_, GetState())
540 .WillByDefault(Return(Controller::State::AUTHENTICATION_FAILED));
542 EXPECT_CALL(proximity_auth_client_,
543 UpdateScreenlockState(ScreenlockState::PHONE_NOT_AUTHENTICATED));
544 unlock_manager_->OnControllerStateChanged();
547 TEST_F(ProximityAuthUnlockManagerTest,
548 OnControllerStateChanged_FindingConnection_UpdatesScreenlockState) {
549 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
551 ON_CALL(controller_, GetState())
552 .WillByDefault(Return(Controller::State::STOPPED));
553 unlock_manager_->SetController(&controller_);
555 ON_CALL(controller_, GetState())
556 .WillByDefault(Return(Controller::State::FINDING_CONNECTION));
558 EXPECT_CALL(proximity_auth_client_,
559 UpdateScreenlockState(ScreenlockState::BLUETOOTH_CONNECTING));
560 unlock_manager_->OnControllerStateChanged();
563 TEST_F(ProximityAuthUnlockManagerTest,
564 OnControllerStateChanged_Authenticating_UpdatesScreenlockState) {
565 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
567 ON_CALL(controller_, GetState())
568 .WillByDefault(Return(Controller::State::STOPPED));
569 unlock_manager_->SetController(&controller_);
571 ON_CALL(controller_, GetState())
572 .WillByDefault(Return(Controller::State::AUTHENTICATING));
574 EXPECT_CALL(proximity_auth_client_,
575 UpdateScreenlockState(ScreenlockState::BLUETOOTH_CONNECTING));
576 unlock_manager_->OnControllerStateChanged();
579 TEST_F(
580 ProximityAuthUnlockManagerTest,
581 OnControllerStateChanged_SecureChannelEstablished_UpdatesScreenlockState) {
582 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
584 ON_CALL(controller_, GetState())
585 .WillByDefault(Return(Controller::State::STOPPED));
586 unlock_manager_->SetController(&controller_);
588 ON_CALL(controller_, GetState())
589 .WillByDefault(Return(Controller::State::SECURE_CHANNEL_ESTABLISHED));
591 EXPECT_CALL(proximity_auth_client_,
592 UpdateScreenlockState(ScreenlockState::BLUETOOTH_CONNECTING));
593 unlock_manager_->OnControllerStateChanged();
596 TEST_F(ProximityAuthUnlockManagerTest, OnDisconnected_UnregistersAsObserver) {
597 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
598 SimulateUserPresentState();
600 ON_CALL(controller_, GetState())
601 .WillByDefault(Return(Controller::State::AUTHENTICATION_FAILED));
603 EXPECT_CALL(client_, RemoveObserver(unlock_manager_.get()));
604 unlock_manager_.get()->OnDisconnected();
607 TEST_F(ProximityAuthUnlockManagerTest,
608 OnScreenDidUnlock_StopsProximityMonitor) {
609 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
610 SimulateUserPresentState();
612 EXPECT_CALL(*proximity_monitor_, Stop());
613 unlock_manager_.get()->OnScreenDidUnlock(
614 ScreenlockBridge::LockHandler::LOCK_SCREEN);
617 TEST_F(ProximityAuthUnlockManagerTest, OnScreenDidLock_StartsProximityMonitor) {
618 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
620 ON_CALL(controller_, GetState())
621 .WillByDefault(Return(Controller::State::STOPPED));
622 unlock_manager_->SetController(&controller_);
624 ON_CALL(controller_, GetState())
625 .WillByDefault(Return(Controller::State::SECURE_CHANNEL_ESTABLISHED));
626 unlock_manager_->OnControllerStateChanged();
628 EXPECT_CALL(*proximity_monitor_, Start());
629 unlock_manager_.get()->OnScreenDidLock(
630 ScreenlockBridge::LockHandler::LOCK_SCREEN);
633 TEST_F(ProximityAuthUnlockManagerTest, OnScreenDidLock_SetsWakingUpState) {
634 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
635 SimulateUserPresentState();
637 unlock_manager_.get()->OnScreenDidUnlock(
638 ScreenlockBridge::LockHandler::LOCK_SCREEN);
640 ON_CALL(controller_, GetState())
641 .WillByDefault(Return(Controller::State::FINDING_CONNECTION));
642 unlock_manager_->OnControllerStateChanged();
644 EXPECT_CALL(proximity_auth_client_,
645 UpdateScreenlockState(ScreenlockState::BLUETOOTH_CONNECTING));
646 unlock_manager_.get()->OnScreenDidLock(
647 ScreenlockBridge::LockHandler::LOCK_SCREEN);
650 TEST_F(ProximityAuthUnlockManagerTest,
651 OnDecryptResponse_NoAuthAttemptInProgress) {
652 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
653 SimulateUserPresentState();
655 EXPECT_CALL(proximity_auth_client_, FinalizeUnlock(_)).Times(0);
656 unlock_manager_.get()->OnDecryptResponse(nullptr);
659 TEST_F(ProximityAuthUnlockManagerTest,
660 OnUnlockEventSent_NoAuthAttemptInProgress) {
661 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
662 SimulateUserPresentState();
664 EXPECT_CALL(proximity_auth_client_, FinalizeUnlock(_)).Times(0);
665 unlock_manager_.get()->OnUnlockEventSent(true);
668 TEST_F(ProximityAuthUnlockManagerTest,
669 OnUnlockResponse_NoAuthAttemptInProgress) {
670 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
671 SimulateUserPresentState();
673 EXPECT_CALL(proximity_auth_client_, FinalizeUnlock(_)).Times(0);
674 unlock_manager_.get()->OnUnlockResponse(true);
677 TEST_F(ProximityAuthUnlockManagerTest, OnAuthAttempted_NoController) {
678 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
679 SimulateUserPresentState();
681 unlock_manager_->SetController(nullptr);
683 EXPECT_CALL(proximity_auth_client_, FinalizeUnlock(false));
684 unlock_manager_->OnAuthAttempted(ScreenlockBridge::LockHandler::USER_CLICK);
687 TEST_F(ProximityAuthUnlockManagerTest, OnAuthAttempted_UnlockNotAllowed) {
688 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
689 SimulateUserPresentState();
691 ON_CALL(*proximity_monitor_, IsUnlockAllowed()).WillByDefault(Return(false));
693 EXPECT_CALL(proximity_auth_client_, FinalizeUnlock(false));
694 unlock_manager_->OnAuthAttempted(ScreenlockBridge::LockHandler::USER_CLICK);
697 TEST_F(ProximityAuthUnlockManagerTest, OnAuthAttempted_NotUserClick) {
698 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
699 SimulateUserPresentState();
701 EXPECT_CALL(proximity_auth_client_, FinalizeUnlock(_)).Times(0);
702 unlock_manager_->OnAuthAttempted(
703 ScreenlockBridge::LockHandler::EXPAND_THEN_USER_CLICK);
706 TEST_F(ProximityAuthUnlockManagerTest, OnAuthAttempted_DuplicateCall) {
707 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
708 SimulateUserPresentState();
710 EXPECT_CALL(client_, RequestUnlock());
711 unlock_manager_->OnAuthAttempted(ScreenlockBridge::LockHandler::USER_CLICK);
713 EXPECT_CALL(client_, RequestUnlock()).Times(0);
714 unlock_manager_->OnAuthAttempted(ScreenlockBridge::LockHandler::USER_CLICK);
717 TEST_F(ProximityAuthUnlockManagerTest, OnAuthAttempted_TimesOut) {
718 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
719 SimulateUserPresentState();
721 unlock_manager_->OnAuthAttempted(ScreenlockBridge::LockHandler::USER_CLICK);
723 // Simulate the timeout period elapsing.
724 EXPECT_CALL(proximity_auth_client_, FinalizeUnlock(false));
725 RunPendingTasks();
728 TEST_F(ProximityAuthUnlockManagerTest,
729 OnAuthAttempted_DoesntTimeOutFollowingResponse) {
730 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
731 SimulateUserPresentState();
733 unlock_manager_->OnAuthAttempted(ScreenlockBridge::LockHandler::USER_CLICK);
735 EXPECT_CALL(proximity_auth_client_, FinalizeUnlock(_));
736 unlock_manager_->OnUnlockResponse(false);
738 // Simulate the timeout period elapsing.
739 EXPECT_CALL(proximity_auth_client_, FinalizeUnlock(_)).Times(0);
740 RunPendingTasks();
743 TEST_F(ProximityAuthUnlockManagerTest,
744 OnAuthAttempted_Unlock_SupportsSignIn_UnlockRequestFails) {
745 ON_CALL(client_, SupportsSignIn()).WillByDefault(Return(true));
746 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
747 SimulateUserPresentState();
749 EXPECT_CALL(client_, RequestUnlock());
750 unlock_manager_->OnAuthAttempted(ScreenlockBridge::LockHandler::USER_CLICK);
752 EXPECT_CALL(proximity_auth_client_, FinalizeUnlock(false));
753 unlock_manager_->OnUnlockResponse(false);
756 TEST_F(ProximityAuthUnlockManagerTest,
757 OnAuthAttempted_Unlock_WithSignIn_RequestSucceeds_EventSendFails) {
758 ON_CALL(client_, SupportsSignIn()).WillByDefault(Return(true));
759 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
760 SimulateUserPresentState();
762 EXPECT_CALL(client_, RequestUnlock());
763 unlock_manager_->OnAuthAttempted(ScreenlockBridge::LockHandler::USER_CLICK);
765 EXPECT_CALL(client_, DispatchUnlockEvent());
766 unlock_manager_->OnUnlockResponse(true);
768 EXPECT_CALL(proximity_auth_client_, FinalizeUnlock(false));
769 unlock_manager_->OnUnlockEventSent(false);
772 TEST_F(ProximityAuthUnlockManagerTest,
773 OnAuthAttempted_Unlock_WithSignIn_RequestSucceeds_EventSendSucceeds) {
774 ON_CALL(client_, SupportsSignIn()).WillByDefault(Return(true));
775 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
776 SimulateUserPresentState();
778 EXPECT_CALL(client_, RequestUnlock());
779 unlock_manager_->OnAuthAttempted(ScreenlockBridge::LockHandler::USER_CLICK);
781 EXPECT_CALL(client_, DispatchUnlockEvent());
782 unlock_manager_->OnUnlockResponse(true);
784 EXPECT_CALL(proximity_auth_client_, FinalizeUnlock(true));
785 unlock_manager_->OnUnlockEventSent(true);
788 TEST_F(ProximityAuthUnlockManagerTest,
789 OnAuthAttempted_Unlock_DoesntSupportSignIn_UnlockEventSendFails) {
790 ON_CALL(client_, SupportsSignIn()).WillByDefault(Return(false));
791 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
792 SimulateUserPresentState();
794 EXPECT_CALL(client_, DispatchUnlockEvent());
795 unlock_manager_->OnAuthAttempted(ScreenlockBridge::LockHandler::USER_CLICK);
797 EXPECT_CALL(proximity_auth_client_, FinalizeUnlock(false));
798 unlock_manager_->OnUnlockEventSent(false);
801 TEST_F(ProximityAuthUnlockManagerTest,
802 OnAuthAttempted_Unlock_SupportsSignIn_UnlockEventSendSucceeds) {
803 ON_CALL(client_, SupportsSignIn()).WillByDefault(Return(false));
804 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
805 SimulateUserPresentState();
807 EXPECT_CALL(client_, DispatchUnlockEvent());
808 unlock_manager_->OnAuthAttempted(ScreenlockBridge::LockHandler::USER_CLICK);
810 EXPECT_CALL(proximity_auth_client_, FinalizeUnlock(true));
811 unlock_manager_->OnUnlockEventSent(true);
814 } // namespace proximity_auth