summaryrefslogtreecommitdiffstats
path: root/components/proximity_auth/ble/proximity_auth_ble_system_unittest.cc
blob: 61a563879b05a5bebd9aa36b95e2679b75619b72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/proximity_auth/ble/proximity_auth_ble_system.h"

#include "base/bind.h"
#include "base/memory/scoped_ptr.h"
#include "components/proximity_auth/connection_finder.h"
#include "components/proximity_auth/proximity_auth_client.h"
#include "components/proximity_auth/screenlock_bridge.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using testing::_;
using testing::NiceMock;
using testing::Return;

namespace proximity_auth {
namespace {
class MockConnectionFinder : public ConnectionFinder {
 public:
  MockConnectionFinder() {}
  ~MockConnectionFinder() {}

  MOCK_METHOD1(Find, void(const ConnectionCallback&));
};

}  // namespace

class ProximityAuthBleSystemTestable : public ProximityAuthBleSystem {
 public:
  class MockScreenlockBridgeAdapter
      : public ProximityAuthBleSystem::ScreenlockBridgeAdapter {
   public:
    MockScreenlockBridgeAdapter() {}
    ~MockScreenlockBridgeAdapter() {}

    MOCK_METHOD1(AddObserver, void(ScreenlockBridge::Observer*));
    MOCK_METHOD1(RemoveObserver, void(ScreenlockBridge::Observer*));
    MOCK_METHOD1(Unlock, void(content::BrowserContext*));
  };

  ProximityAuthBleSystemTestable(ScreenlockBridgeAdapter* screenlock_bridge)
      : ProximityAuthBleSystem(screenlock_bridge, nullptr) {}

  ConnectionFinder* CreateConnectionFinder() override {
    return new NiceMock<MockConnectionFinder>();
  }
};

class ProximityAuthBleSystemTest : public testing::Test {
 protected:
  ProximityAuthBleSystemTest()
      : screenlock_bridge_(new NiceMock<
            ProximityAuthBleSystemTestable::MockScreenlockBridgeAdapter>) {}

  void SetExpectations() {
    EXPECT_CALL(*screenlock_bridge_, AddObserver(_));
    EXPECT_CALL(*screenlock_bridge_, RemoveObserver(_));
  }

  ProximityAuthBleSystemTestable::MockScreenlockBridgeAdapter*
      screenlock_bridge_;
};

TEST_F(ProximityAuthBleSystemTest, ConstructAndDestroyShouldNotCrash) {
  SetExpectations();

  ProximityAuthBleSystemTestable proximity_auth_system(screenlock_bridge_);
}

TEST_F(ProximityAuthBleSystemTest,
       OnScreenDidLock_OnLockScreenEvent_OnScreenDidUnlock) {
  SetExpectations();

  ProximityAuthBleSystemTestable proximity_auth_system(screenlock_bridge_);
  proximity_auth_system.OnScreenDidLock(
      ScreenlockBridge::LockHandler::LOCK_SCREEN);
  proximity_auth_system.OnScreenDidUnlock(
      ScreenlockBridge::LockHandler::LOCK_SCREEN);
}

TEST_F(ProximityAuthBleSystemTest,
       OnScreenDidLock_OnSigninScreenEvent_OnScreenDidUnlock) {
  SetExpectations();

  ProximityAuthBleSystemTestable proximity_auth_system(screenlock_bridge_);
  proximity_auth_system.OnScreenDidLock(
      ScreenlockBridge::LockHandler::SIGNIN_SCREEN);
  proximity_auth_system.OnScreenDidUnlock(
      ScreenlockBridge::LockHandler::SIGNIN_SCREEN);
}

TEST_F(ProximityAuthBleSystemTest,
       OnScreenDidLock_OnOtherScreenEvent_OnScreenDidUnlock) {
  SetExpectations();

  ProximityAuthBleSystemTestable proximity_auth_system(screenlock_bridge_);
  proximity_auth_system.OnScreenDidLock(
      ScreenlockBridge::LockHandler::OTHER_SCREEN);
  proximity_auth_system.OnScreenDidUnlock(
      ScreenlockBridge::LockHandler::OTHER_SCREEN);
}

}  // namespace proximity_auth