Cleanup: Pass std::string as const reference from pdf/
[chromium-blink-merge.git] / components / proximity_auth / unlock_manager_unittest.cc
blob95d21e0fee5a9e02c3da98a90053288be24b8fff
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/logging/logging.h"
14 #include "components/proximity_auth/mock_proximity_auth_client.h"
15 #include "components/proximity_auth/proximity_monitor.h"
16 #include "components/proximity_auth/remote_device_life_cycle.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 MockRemoteDeviceLifeCycle : public RemoteDeviceLifeCycle {
49 public:
50 MockRemoteDeviceLifeCycle() {}
51 ~MockRemoteDeviceLifeCycle() override {}
53 MOCK_METHOD0(Start, void());
54 MOCK_CONST_METHOD0(GetState, State());
55 MOCK_METHOD0(GetClient, Client*());
56 MOCK_METHOD1(AddObserver, void(Observer*));
57 MOCK_METHOD1(RemoveObserver, void(Observer*));
60 class MockClient : public Client {
61 public:
62 MockClient() {}
63 ~MockClient() override {}
65 MOCK_METHOD1(AddObserver, void(ClientObserver* observer));
66 MOCK_METHOD1(RemoveObserver, void(ClientObserver* observer));
67 MOCK_CONST_METHOD0(SupportsSignIn, bool());
68 MOCK_METHOD0(DispatchUnlockEvent, void());
69 MOCK_METHOD1(RequestDecryption, void(const std::string& challenge));
70 MOCK_METHOD0(RequestUnlock, void());
72 private:
73 DISALLOW_COPY_AND_ASSIGN(MockClient);
76 class MockProximityMonitor : public ProximityMonitor {
77 public:
78 MockProximityMonitor() {
79 ON_CALL(*this, GetStrategy())
80 .WillByDefault(Return(ProximityMonitor::Strategy::NONE));
81 ON_CALL(*this, IsUnlockAllowed()).WillByDefault(Return(true));
82 ON_CALL(*this, IsInRssiRange()).WillByDefault(Return(false));
84 ~MockProximityMonitor() override {}
86 MOCK_METHOD0(Start, void());
87 MOCK_METHOD0(Stop, void());
88 MOCK_CONST_METHOD0(GetStrategy, Strategy());
89 MOCK_CONST_METHOD0(IsUnlockAllowed, bool());
90 MOCK_CONST_METHOD0(IsInRssiRange, bool());
91 MOCK_METHOD0(RecordProximityMetricsOnAuthSuccess, void());
93 private:
94 DISALLOW_COPY_AND_ASSIGN(MockProximityMonitor);
97 class FakeLockHandler : public ScreenlockBridge::LockHandler {
98 public:
99 FakeLockHandler() {}
100 ~FakeLockHandler() override {}
102 // LockHandler:
103 void ShowBannerMessage(const base::string16& message) override {}
104 void ShowUserPodCustomIcon(
105 const std::string& user_email,
106 const ScreenlockBridge::UserPodCustomIconOptions& icon) override {}
107 void HideUserPodCustomIcon(const std::string& user_email) override {}
108 void EnableInput() override {}
109 void SetAuthType(const std::string& user_email,
110 AuthType auth_type,
111 const base::string16& auth_value) override {}
112 AuthType GetAuthType(const std::string& user_email) const override {
113 return USER_CLICK;
115 ScreenType GetScreenType() const override { return LOCK_SCREEN; }
116 void Unlock(const std::string& user_email) override {}
117 void AttemptEasySignin(const std::string& user_email,
118 const std::string& secret,
119 const std::string& key_label) override {}
121 private:
122 DISALLOW_COPY_AND_ASSIGN(FakeLockHandler);
125 class TestUnlockManager : public UnlockManager {
126 public:
127 TestUnlockManager(ScreenlockType screenlock_type,
128 scoped_ptr<ProximityMonitor> proximity_monitor,
129 ProximityAuthClient* proximity_auth_client)
130 : UnlockManager(screenlock_type,
131 proximity_monitor.Pass(),
132 proximity_auth_client) {}
133 ~TestUnlockManager() override {}
135 using UnlockManager::OnAuthAttempted;
136 using ClientObserver::OnUnlockEventSent;
137 using ClientObserver::OnRemoteStatusUpdate;
138 using ClientObserver::OnDecryptResponse;
139 using ClientObserver::OnUnlockResponse;
140 using ClientObserver::OnDisconnected;
141 using ScreenlockBridge::Observer::OnScreenDidLock;
142 using ScreenlockBridge::Observer::OnScreenDidUnlock;
143 using ScreenlockBridge::Observer::OnFocusedUserChanged;
146 // Creates a mock Bluetooth adapter and sets it as the global adapter for
147 // testing.
148 scoped_refptr<device::MockBluetoothAdapter>
149 CreateAndRegisterMockBluetoothAdapter() {
150 scoped_refptr<device::MockBluetoothAdapter> adapter =
151 new NiceMock<device::MockBluetoothAdapter>();
152 device::BluetoothAdapterFactory::SetAdapterForTesting(adapter);
153 return adapter;
156 } // namespace
158 class ProximityAuthUnlockManagerTest : public testing::Test {
159 public:
160 ProximityAuthUnlockManagerTest()
161 : bluetooth_adapter_(CreateAndRegisterMockBluetoothAdapter()),
162 proximity_monitor_(nullptr),
163 task_runner_(new base::TestSimpleTaskRunner()),
164 thread_task_runner_handle_(task_runner_) {
165 ON_CALL(*bluetooth_adapter_, IsPowered()).WillByDefault(Return(true));
166 ON_CALL(life_cycle_, GetClient()).WillByDefault(Return(&client_));
167 ON_CALL(client_, SupportsSignIn()).WillByDefault(Return(true));
169 ScreenlockBridge::Get()->SetLockHandler(&lock_handler_);
171 #if defined(OS_CHROMEOS)
172 chromeos::DBusThreadManager::Initialize();
173 #endif
176 ~ProximityAuthUnlockManagerTest() override {
177 // Make sure to verify the mock prior to the destruction of the unlock
178 // manager, as otherwise it's impossible to tell whether calls to Stop()
179 // occur as a side-effect of the destruction or from the code intended to be
180 // under test.
181 if (proximity_monitor_)
182 testing::Mock::VerifyAndClearExpectations(proximity_monitor_);
184 // The UnlockManager must be destroyed before calling
185 // chromeos::DBusThreadManager::Shutdown(), as the UnlockManager's
186 // destructor references the DBusThreadManager.
187 unlock_manager_.reset();
189 #if defined(OS_CHROMEOS)
190 chromeos::DBusThreadManager::Shutdown();
191 #endif
193 ScreenlockBridge::Get()->SetLockHandler(nullptr);
196 void CreateUnlockManager(UnlockManager::ScreenlockType screenlock_type) {
197 proximity_monitor_ = new NiceMock<MockProximityMonitor>;
198 unlock_manager_.reset(new TestUnlockManager(
199 screenlock_type, make_scoped_ptr(proximity_monitor_),
200 &proximity_auth_client_));
203 void SimulateUserPresentState() {
204 ON_CALL(life_cycle_, GetState())
205 .WillByDefault(Return(RemoteDeviceLifeCycle::State::STOPPED));
206 unlock_manager_->SetRemoteDeviceLifeCycle(&life_cycle_);
208 ON_CALL(life_cycle_, GetState())
209 .WillByDefault(
210 Return(RemoteDeviceLifeCycle::State::SECURE_CHANNEL_ESTABLISHED));
211 unlock_manager_->OnLifeCycleStateChanged();
213 unlock_manager_->OnRemoteStatusUpdate(kRemoteScreenUnlocked);
216 void RunPendingTasks() { task_runner_->RunPendingTasks(); }
218 protected:
219 // Mock used for verifying interactions with the Bluetooth subsystem.
220 scoped_refptr<device::MockBluetoothAdapter> bluetooth_adapter_;
222 NiceMock<MockProximityAuthClient> proximity_auth_client_;
223 NiceMock<MockRemoteDeviceLifeCycle> life_cycle_;
224 NiceMock<MockClient> client_;
225 scoped_ptr<TestUnlockManager> unlock_manager_;
226 // Owned by the |unlock_manager_|.
227 MockProximityMonitor* proximity_monitor_;
229 private:
230 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
231 base::ThreadTaskRunnerHandle thread_task_runner_handle_;
232 FakeLockHandler lock_handler_;
233 ScopedDisableLoggingForTesting disable_logging_;
236 TEST_F(ProximityAuthUnlockManagerTest, IsUnlockAllowed_InitialState) {
237 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
238 EXPECT_FALSE(unlock_manager_->IsUnlockAllowed());
241 TEST_F(ProximityAuthUnlockManagerTest, IsUnlockAllowed_SessionLock_AllGood) {
242 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
244 ON_CALL(life_cycle_, GetState())
245 .WillByDefault(
246 Return(RemoteDeviceLifeCycle::State::SECURE_CHANNEL_ESTABLISHED));
247 unlock_manager_->SetRemoteDeviceLifeCycle(&life_cycle_);
248 unlock_manager_->OnRemoteStatusUpdate(kRemoteScreenUnlocked);
250 EXPECT_TRUE(unlock_manager_->IsUnlockAllowed());
253 TEST_F(ProximityAuthUnlockManagerTest, IsUnlockAllowed_SignIn_AllGood) {
254 CreateUnlockManager(UnlockManager::ScreenlockType::SIGN_IN);
256 ON_CALL(life_cycle_, GetState())
257 .WillByDefault(Return(RemoteDeviceLifeCycle::State::STOPPED));
258 unlock_manager_->SetRemoteDeviceLifeCycle(&life_cycle_);
260 ON_CALL(life_cycle_, GetState())
261 .WillByDefault(
262 Return(RemoteDeviceLifeCycle::State::SECURE_CHANNEL_ESTABLISHED));
263 unlock_manager_->OnLifeCycleStateChanged();
265 ON_CALL(client_, SupportsSignIn()).WillByDefault(Return(true));
266 unlock_manager_->OnRemoteStatusUpdate(kRemoteScreenUnlocked);
268 EXPECT_TRUE(unlock_manager_->IsUnlockAllowed());
271 TEST_F(ProximityAuthUnlockManagerTest,
272 IsUnlockAllowed_SignIn_ClientDoesNotSupportSignIn) {
273 CreateUnlockManager(UnlockManager::ScreenlockType::SIGN_IN);
275 ON_CALL(life_cycle_, GetState())
276 .WillByDefault(Return(RemoteDeviceLifeCycle::State::STOPPED));
277 unlock_manager_->SetRemoteDeviceLifeCycle(&life_cycle_);
279 ON_CALL(life_cycle_, GetState())
280 .WillByDefault(
281 Return(RemoteDeviceLifeCycle::State::SECURE_CHANNEL_ESTABLISHED));
282 unlock_manager_->OnLifeCycleStateChanged();
284 ON_CALL(client_, SupportsSignIn()).WillByDefault(Return(false));
285 unlock_manager_->OnRemoteStatusUpdate(kRemoteScreenUnlocked);
287 EXPECT_FALSE(unlock_manager_->IsUnlockAllowed());
290 TEST_F(ProximityAuthUnlockManagerTest, IsUnlockAllowed_SignIn_ClientIsNull) {
291 CreateUnlockManager(UnlockManager::ScreenlockType::SIGN_IN);
293 ON_CALL(life_cycle_, GetState())
294 .WillByDefault(
295 Return(RemoteDeviceLifeCycle::State::SECURE_CHANNEL_ESTABLISHED));
296 ON_CALL(life_cycle_, GetClient()).WillByDefault(Return(nullptr));
297 unlock_manager_->SetRemoteDeviceLifeCycle(&life_cycle_);
298 unlock_manager_->OnRemoteStatusUpdate(kRemoteScreenUnlocked);
300 EXPECT_FALSE(unlock_manager_->IsUnlockAllowed());
303 TEST_F(ProximityAuthUnlockManagerTest,
304 IsUnlockAllowed_DisallowedByProximityMonitor) {
305 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
307 ON_CALL(life_cycle_, GetState())
308 .WillByDefault(
309 Return(RemoteDeviceLifeCycle::State::SECURE_CHANNEL_ESTABLISHED));
310 unlock_manager_->SetRemoteDeviceLifeCycle(&life_cycle_);
311 unlock_manager_->OnRemoteStatusUpdate(kRemoteScreenUnlocked);
313 ON_CALL(*proximity_monitor_, IsUnlockAllowed()).WillByDefault(Return(false));
314 EXPECT_FALSE(unlock_manager_->IsUnlockAllowed());
317 TEST_F(ProximityAuthUnlockManagerTest,
318 IsUnlockAllowed_SecureChannelNotEstablished) {
319 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
321 ON_CALL(life_cycle_, GetState())
322 .WillByDefault(Return(RemoteDeviceLifeCycle::State::AUTHENTICATING));
323 unlock_manager_->SetRemoteDeviceLifeCycle(&life_cycle_);
324 unlock_manager_->OnRemoteStatusUpdate(kRemoteScreenUnlocked);
326 EXPECT_FALSE(unlock_manager_->IsUnlockAllowed());
329 TEST_F(ProximityAuthUnlockManagerTest,
330 IsUnlockAllowed_RemoteDeviceLifeCycleIsNull) {
331 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
333 unlock_manager_->SetRemoteDeviceLifeCycle(nullptr);
334 unlock_manager_->OnRemoteStatusUpdate(kRemoteScreenUnlocked);
336 EXPECT_FALSE(unlock_manager_->IsUnlockAllowed());
339 TEST_F(ProximityAuthUnlockManagerTest,
340 IsUnlockAllowed_RemoteScreenlockStateLocked) {
341 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
343 ON_CALL(life_cycle_, GetState())
344 .WillByDefault(
345 Return(RemoteDeviceLifeCycle::State::SECURE_CHANNEL_ESTABLISHED));
346 unlock_manager_->SetRemoteDeviceLifeCycle(&life_cycle_);
347 unlock_manager_->OnRemoteStatusUpdate(kRemoteScreenLocked);
349 EXPECT_FALSE(unlock_manager_->IsUnlockAllowed());
352 TEST_F(ProximityAuthUnlockManagerTest,
353 IsUnlockAllowed_RemoteScreenlockStateUnknown) {
354 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
356 ON_CALL(life_cycle_, GetState())
357 .WillByDefault(
358 Return(RemoteDeviceLifeCycle::State::SECURE_CHANNEL_ESTABLISHED));
359 unlock_manager_->SetRemoteDeviceLifeCycle(&life_cycle_);
360 unlock_manager_->OnRemoteStatusUpdate(kRemoteScreenlockStateUnknown);
362 EXPECT_FALSE(unlock_manager_->IsUnlockAllowed());
365 TEST_F(ProximityAuthUnlockManagerTest,
366 IsUnlockAllowed_RemoteScreenlockStateDisabled) {
367 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
369 ON_CALL(life_cycle_, GetState())
370 .WillByDefault(
371 Return(RemoteDeviceLifeCycle::State::SECURE_CHANNEL_ESTABLISHED));
372 unlock_manager_->SetRemoteDeviceLifeCycle(&life_cycle_);
373 unlock_manager_->OnRemoteStatusUpdate(kRemoteScreenlockDisabled);
375 EXPECT_FALSE(unlock_manager_->IsUnlockAllowed());
378 TEST_F(ProximityAuthUnlockManagerTest,
379 IsUnlockAllowed_RemoteScreenlockStateNotYetReceived) {
380 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
382 ON_CALL(life_cycle_, GetState())
383 .WillByDefault(
384 Return(RemoteDeviceLifeCycle::State::SECURE_CHANNEL_ESTABLISHED));
385 unlock_manager_->SetRemoteDeviceLifeCycle(&life_cycle_);
387 EXPECT_FALSE(unlock_manager_->IsUnlockAllowed());
390 TEST_F(ProximityAuthUnlockManagerTest, SetRemoteDeviceLifeCycle_SetToNull) {
391 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
392 SimulateUserPresentState();
394 EXPECT_CALL(proximity_auth_client_,
395 UpdateScreenlockState(ScreenlockState::INACTIVE));
396 unlock_manager_->SetRemoteDeviceLifeCycle(nullptr);
399 TEST_F(ProximityAuthUnlockManagerTest,
400 SetRemoteDeviceLifeCycle_ExistingRemoteDeviceLifeCycle) {
401 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
402 SimulateUserPresentState();
404 EXPECT_CALL(proximity_auth_client_, UpdateScreenlockState(_)).Times(0);
405 unlock_manager_->SetRemoteDeviceLifeCycle(&life_cycle_);
408 TEST_F(ProximityAuthUnlockManagerTest,
409 SetRemoteDeviceLifeCycle_NullThenExistingRemoteDeviceLifeCycle) {
410 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
411 SimulateUserPresentState();
413 EXPECT_CALL(proximity_auth_client_,
414 UpdateScreenlockState(ScreenlockState::INACTIVE));
415 unlock_manager_->SetRemoteDeviceLifeCycle(nullptr);
417 EXPECT_CALL(proximity_auth_client_,
418 UpdateScreenlockState(ScreenlockState::AUTHENTICATED));
419 unlock_manager_->SetRemoteDeviceLifeCycle(&life_cycle_);
422 TEST_F(ProximityAuthUnlockManagerTest,
423 SetRemoteDeviceLifeCycle_AuthenticationFailed) {
424 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
425 SimulateUserPresentState();
427 unlock_manager_->SetRemoteDeviceLifeCycle(nullptr);
429 ON_CALL(life_cycle_, GetState())
430 .WillByDefault(
431 Return(RemoteDeviceLifeCycle::State::AUTHENTICATION_FAILED));
432 EXPECT_CALL(proximity_auth_client_,
433 UpdateScreenlockState(ScreenlockState::PHONE_NOT_AUTHENTICATED));
434 unlock_manager_->SetRemoteDeviceLifeCycle(&life_cycle_);
437 TEST_F(ProximityAuthUnlockManagerTest, SetRemoteDeviceLifeCycle_WakingUp) {
438 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
439 SimulateUserPresentState();
441 unlock_manager_->SetRemoteDeviceLifeCycle(nullptr);
443 ON_CALL(life_cycle_, GetState())
444 .WillByDefault(Return(RemoteDeviceLifeCycle::State::FINDING_CONNECTION));
445 EXPECT_CALL(proximity_auth_client_,
446 UpdateScreenlockState(ScreenlockState::BLUETOOTH_CONNECTING));
447 unlock_manager_->SetRemoteDeviceLifeCycle(&life_cycle_);
450 TEST_F(
451 ProximityAuthUnlockManagerTest,
452 SetRemoteDeviceLifeCycle_NullRemoteDeviceLifeCycle_StopsProximityMonitor) {
453 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
454 SimulateUserPresentState();
456 EXPECT_CALL(*proximity_monitor_, Stop()).Times(AtLeast(1));
457 unlock_manager_->SetRemoteDeviceLifeCycle(nullptr);
460 TEST_F(
461 ProximityAuthUnlockManagerTest,
462 SetRemoteDeviceLifeCycle_ConnectingRemoteDeviceLifeCycle_StopsProximityMonitor) {
463 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
464 SimulateUserPresentState();
466 NiceMock<MockRemoteDeviceLifeCycle> life_cycle;
467 ON_CALL(life_cycle, GetState())
468 .WillByDefault(Return(RemoteDeviceLifeCycle::State::FINDING_CONNECTION));
470 EXPECT_CALL(*proximity_monitor_, Stop()).Times(AtLeast(1));
471 unlock_manager_->SetRemoteDeviceLifeCycle(&life_cycle);
474 TEST_F(
475 ProximityAuthUnlockManagerTest,
476 SetRemoteDeviceLifeCycle_ConnectedRemoteDeviceLifeCycle_StartsProximityMonitor) {
477 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
478 SimulateUserPresentState();
480 NiceMock<MockRemoteDeviceLifeCycle> life_cycle;
481 ON_CALL(life_cycle, GetState())
482 .WillByDefault(
483 Return(RemoteDeviceLifeCycle::State::SECURE_CHANNEL_ESTABLISHED));
485 EXPECT_CALL(*proximity_monitor_, Start()).Times(AtLeast(1));
486 unlock_manager_->SetRemoteDeviceLifeCycle(&life_cycle);
489 TEST_F(ProximityAuthUnlockManagerTest,
490 OnLifeCycleStateChanged_SecureChannelEstablished_RegistersAsObserver) {
491 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
492 SimulateUserPresentState();
494 NiceMock<MockRemoteDeviceLifeCycle> life_cycle;
495 ON_CALL(life_cycle, GetState())
496 .WillByDefault(
497 Return(RemoteDeviceLifeCycle::State::SECURE_CHANNEL_ESTABLISHED));
499 EXPECT_CALL(client_, AddObserver(unlock_manager_.get()));
500 unlock_manager_->OnLifeCycleStateChanged();
503 TEST_F(ProximityAuthUnlockManagerTest,
504 OnLifeCycleStateChanged_StartsProximityMonitor) {
505 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
506 SimulateUserPresentState();
508 NiceMock<MockRemoteDeviceLifeCycle> life_cycle;
509 ON_CALL(life_cycle, GetState())
510 .WillByDefault(
511 Return(RemoteDeviceLifeCycle::State::SECURE_CHANNEL_ESTABLISHED));
513 EXPECT_CALL(*proximity_monitor_, Start()).Times(AtLeast(1));
514 unlock_manager_->OnLifeCycleStateChanged();
517 TEST_F(ProximityAuthUnlockManagerTest,
518 OnLifeCycleStateChanged_StopsProximityMonitor) {
519 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
520 SimulateUserPresentState();
522 ON_CALL(life_cycle_, GetState())
523 .WillByDefault(
524 Return(RemoteDeviceLifeCycle::State::AUTHENTICATION_FAILED));
526 EXPECT_CALL(*proximity_monitor_, Stop()).Times(AtLeast(1));
527 unlock_manager_->OnLifeCycleStateChanged();
530 TEST_F(ProximityAuthUnlockManagerTest,
531 OnLifeCycleStateChanged_Stopped_UpdatesScreenlockState) {
532 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
533 SimulateUserPresentState();
535 ON_CALL(life_cycle_, GetState())
536 .WillByDefault(Return(RemoteDeviceLifeCycle::State::STOPPED));
538 EXPECT_CALL(proximity_auth_client_,
539 UpdateScreenlockState(ScreenlockState::INACTIVE));
540 unlock_manager_->OnLifeCycleStateChanged();
543 TEST_F(ProximityAuthUnlockManagerTest,
544 OnLifeCycleStateChanged_AuthenticationFailed_UpdatesScreenlockState) {
545 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
546 SimulateUserPresentState();
548 ON_CALL(life_cycle_, GetState())
549 .WillByDefault(
550 Return(RemoteDeviceLifeCycle::State::AUTHENTICATION_FAILED));
552 EXPECT_CALL(proximity_auth_client_,
553 UpdateScreenlockState(ScreenlockState::PHONE_NOT_AUTHENTICATED));
554 unlock_manager_->OnLifeCycleStateChanged();
557 TEST_F(ProximityAuthUnlockManagerTest,
558 OnLifeCycleStateChanged_FindingConnection_UpdatesScreenlockState) {
559 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
561 ON_CALL(life_cycle_, GetState())
562 .WillByDefault(Return(RemoteDeviceLifeCycle::State::STOPPED));
563 unlock_manager_->SetRemoteDeviceLifeCycle(&life_cycle_);
565 ON_CALL(life_cycle_, GetState())
566 .WillByDefault(Return(RemoteDeviceLifeCycle::State::FINDING_CONNECTION));
568 EXPECT_CALL(proximity_auth_client_,
569 UpdateScreenlockState(ScreenlockState::BLUETOOTH_CONNECTING));
570 unlock_manager_->OnLifeCycleStateChanged();
573 TEST_F(ProximityAuthUnlockManagerTest,
574 OnLifeCycleStateChanged_Authenticating_UpdatesScreenlockState) {
575 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
577 ON_CALL(life_cycle_, GetState())
578 .WillByDefault(Return(RemoteDeviceLifeCycle::State::STOPPED));
579 unlock_manager_->SetRemoteDeviceLifeCycle(&life_cycle_);
581 ON_CALL(life_cycle_, GetState())
582 .WillByDefault(Return(RemoteDeviceLifeCycle::State::AUTHENTICATING));
584 EXPECT_CALL(proximity_auth_client_,
585 UpdateScreenlockState(ScreenlockState::BLUETOOTH_CONNECTING));
586 unlock_manager_->OnLifeCycleStateChanged();
589 TEST_F(
590 ProximityAuthUnlockManagerTest,
591 OnLifeCycleStateChanged_SecureChannelEstablished_UpdatesScreenlockState) {
592 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
594 ON_CALL(life_cycle_, GetState())
595 .WillByDefault(Return(RemoteDeviceLifeCycle::State::STOPPED));
596 unlock_manager_->SetRemoteDeviceLifeCycle(&life_cycle_);
598 ON_CALL(life_cycle_, GetState())
599 .WillByDefault(
600 Return(RemoteDeviceLifeCycle::State::SECURE_CHANNEL_ESTABLISHED));
602 EXPECT_CALL(proximity_auth_client_,
603 UpdateScreenlockState(ScreenlockState::BLUETOOTH_CONNECTING));
604 unlock_manager_->OnLifeCycleStateChanged();
607 TEST_F(ProximityAuthUnlockManagerTest, OnDisconnected_UnregistersAsObserver) {
608 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
609 SimulateUserPresentState();
611 ON_CALL(life_cycle_, GetState())
612 .WillByDefault(
613 Return(RemoteDeviceLifeCycle::State::AUTHENTICATION_FAILED));
615 EXPECT_CALL(client_, RemoveObserver(unlock_manager_.get()));
616 unlock_manager_.get()->OnDisconnected();
619 TEST_F(ProximityAuthUnlockManagerTest,
620 OnScreenDidUnlock_StopsProximityMonitor) {
621 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
622 SimulateUserPresentState();
624 EXPECT_CALL(*proximity_monitor_, Stop());
625 unlock_manager_.get()->OnScreenDidUnlock(
626 ScreenlockBridge::LockHandler::LOCK_SCREEN);
629 TEST_F(ProximityAuthUnlockManagerTest, OnScreenDidLock_StartsProximityMonitor) {
630 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
632 ON_CALL(life_cycle_, GetState())
633 .WillByDefault(Return(RemoteDeviceLifeCycle::State::STOPPED));
634 unlock_manager_->SetRemoteDeviceLifeCycle(&life_cycle_);
636 ON_CALL(life_cycle_, GetState())
637 .WillByDefault(
638 Return(RemoteDeviceLifeCycle::State::SECURE_CHANNEL_ESTABLISHED));
639 unlock_manager_->OnLifeCycleStateChanged();
641 EXPECT_CALL(*proximity_monitor_, Start());
642 unlock_manager_.get()->OnScreenDidLock(
643 ScreenlockBridge::LockHandler::LOCK_SCREEN);
646 TEST_F(ProximityAuthUnlockManagerTest, OnScreenDidLock_SetsWakingUpState) {
647 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
648 SimulateUserPresentState();
650 unlock_manager_.get()->OnScreenDidUnlock(
651 ScreenlockBridge::LockHandler::LOCK_SCREEN);
653 ON_CALL(life_cycle_, GetState())
654 .WillByDefault(Return(RemoteDeviceLifeCycle::State::FINDING_CONNECTION));
655 unlock_manager_->OnLifeCycleStateChanged();
657 EXPECT_CALL(proximity_auth_client_,
658 UpdateScreenlockState(ScreenlockState::BLUETOOTH_CONNECTING));
659 unlock_manager_.get()->OnScreenDidLock(
660 ScreenlockBridge::LockHandler::LOCK_SCREEN);
663 TEST_F(ProximityAuthUnlockManagerTest,
664 OnDecryptResponse_NoAuthAttemptInProgress) {
665 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
666 SimulateUserPresentState();
668 EXPECT_CALL(proximity_auth_client_, FinalizeUnlock(_)).Times(0);
669 unlock_manager_.get()->OnDecryptResponse(nullptr);
672 TEST_F(ProximityAuthUnlockManagerTest,
673 OnUnlockEventSent_NoAuthAttemptInProgress) {
674 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
675 SimulateUserPresentState();
677 EXPECT_CALL(proximity_auth_client_, FinalizeUnlock(_)).Times(0);
678 unlock_manager_.get()->OnUnlockEventSent(true);
681 TEST_F(ProximityAuthUnlockManagerTest,
682 OnUnlockResponse_NoAuthAttemptInProgress) {
683 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
684 SimulateUserPresentState();
686 EXPECT_CALL(proximity_auth_client_, FinalizeUnlock(_)).Times(0);
687 unlock_manager_.get()->OnUnlockResponse(true);
690 TEST_F(ProximityAuthUnlockManagerTest,
691 OnAuthAttempted_NoRemoteDeviceLifeCycle) {
692 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
693 SimulateUserPresentState();
695 unlock_manager_->SetRemoteDeviceLifeCycle(nullptr);
697 EXPECT_CALL(proximity_auth_client_, FinalizeUnlock(false));
698 unlock_manager_->OnAuthAttempted(ScreenlockBridge::LockHandler::USER_CLICK);
701 TEST_F(ProximityAuthUnlockManagerTest, OnAuthAttempted_UnlockNotAllowed) {
702 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
703 SimulateUserPresentState();
705 ON_CALL(*proximity_monitor_, IsUnlockAllowed()).WillByDefault(Return(false));
707 EXPECT_CALL(proximity_auth_client_, FinalizeUnlock(false));
708 unlock_manager_->OnAuthAttempted(ScreenlockBridge::LockHandler::USER_CLICK);
711 TEST_F(ProximityAuthUnlockManagerTest, OnAuthAttempted_NotUserClick) {
712 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
713 SimulateUserPresentState();
715 EXPECT_CALL(proximity_auth_client_, FinalizeUnlock(_)).Times(0);
716 unlock_manager_->OnAuthAttempted(
717 ScreenlockBridge::LockHandler::EXPAND_THEN_USER_CLICK);
720 TEST_F(ProximityAuthUnlockManagerTest, OnAuthAttempted_DuplicateCall) {
721 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
722 SimulateUserPresentState();
724 EXPECT_CALL(client_, RequestUnlock());
725 unlock_manager_->OnAuthAttempted(ScreenlockBridge::LockHandler::USER_CLICK);
727 EXPECT_CALL(client_, RequestUnlock()).Times(0);
728 unlock_manager_->OnAuthAttempted(ScreenlockBridge::LockHandler::USER_CLICK);
731 TEST_F(ProximityAuthUnlockManagerTest, OnAuthAttempted_TimesOut) {
732 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
733 SimulateUserPresentState();
735 unlock_manager_->OnAuthAttempted(ScreenlockBridge::LockHandler::USER_CLICK);
737 // Simulate the timeout period elapsing.
738 EXPECT_CALL(proximity_auth_client_, FinalizeUnlock(false));
739 RunPendingTasks();
742 TEST_F(ProximityAuthUnlockManagerTest,
743 OnAuthAttempted_DoesntTimeOutFollowingResponse) {
744 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
745 SimulateUserPresentState();
747 unlock_manager_->OnAuthAttempted(ScreenlockBridge::LockHandler::USER_CLICK);
749 EXPECT_CALL(proximity_auth_client_, FinalizeUnlock(_));
750 unlock_manager_->OnUnlockResponse(false);
752 // Simulate the timeout period elapsing.
753 EXPECT_CALL(proximity_auth_client_, FinalizeUnlock(_)).Times(0);
754 RunPendingTasks();
757 TEST_F(ProximityAuthUnlockManagerTest,
758 OnAuthAttempted_Unlock_SupportsSignIn_UnlockRequestFails) {
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(proximity_auth_client_, FinalizeUnlock(false));
767 unlock_manager_->OnUnlockResponse(false);
770 TEST_F(ProximityAuthUnlockManagerTest,
771 OnAuthAttempted_Unlock_WithSignIn_RequestSucceeds_EventSendFails) {
772 ON_CALL(client_, SupportsSignIn()).WillByDefault(Return(true));
773 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
774 SimulateUserPresentState();
776 EXPECT_CALL(client_, RequestUnlock());
777 unlock_manager_->OnAuthAttempted(ScreenlockBridge::LockHandler::USER_CLICK);
779 EXPECT_CALL(client_, DispatchUnlockEvent());
780 unlock_manager_->OnUnlockResponse(true);
782 EXPECT_CALL(proximity_auth_client_, FinalizeUnlock(false));
783 unlock_manager_->OnUnlockEventSent(false);
786 TEST_F(ProximityAuthUnlockManagerTest,
787 OnAuthAttempted_Unlock_WithSignIn_RequestSucceeds_EventSendSucceeds) {
788 ON_CALL(client_, SupportsSignIn()).WillByDefault(Return(true));
789 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
790 SimulateUserPresentState();
792 EXPECT_CALL(client_, RequestUnlock());
793 unlock_manager_->OnAuthAttempted(ScreenlockBridge::LockHandler::USER_CLICK);
795 EXPECT_CALL(client_, DispatchUnlockEvent());
796 unlock_manager_->OnUnlockResponse(true);
798 EXPECT_CALL(proximity_auth_client_, FinalizeUnlock(true));
799 unlock_manager_->OnUnlockEventSent(true);
802 TEST_F(ProximityAuthUnlockManagerTest,
803 OnAuthAttempted_Unlock_DoesntSupportSignIn_UnlockEventSendFails) {
804 ON_CALL(client_, SupportsSignIn()).WillByDefault(Return(false));
805 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
806 SimulateUserPresentState();
808 EXPECT_CALL(client_, DispatchUnlockEvent());
809 unlock_manager_->OnAuthAttempted(ScreenlockBridge::LockHandler::USER_CLICK);
811 EXPECT_CALL(proximity_auth_client_, FinalizeUnlock(false));
812 unlock_manager_->OnUnlockEventSent(false);
815 TEST_F(ProximityAuthUnlockManagerTest,
816 OnAuthAttempted_Unlock_SupportsSignIn_UnlockEventSendSucceeds) {
817 ON_CALL(client_, SupportsSignIn()).WillByDefault(Return(false));
818 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK);
819 SimulateUserPresentState();
821 EXPECT_CALL(client_, DispatchUnlockEvent());
822 unlock_manager_->OnAuthAttempted(ScreenlockBridge::LockHandler::USER_CLICK);
824 EXPECT_CALL(proximity_auth_client_, FinalizeUnlock(true));
825 unlock_manager_->OnUnlockEventSent(true);
828 } // namespace proximity_auth