File: fast_initiation_scanner_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (205 lines) | stat: -rw-r--r-- 8,386 bytes parent folder | download | duplicates (6)
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/nearby_sharing/fast_initiation/fast_initiation_scanner.h"

#include <memory>
#include <optional>

#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/unguessable_token.h"
#include "device/bluetooth/bluetooth_adapter.h"
#include "device/bluetooth/bluetooth_adapter_factory.h"
#include "device/bluetooth/test/mock_bluetooth_adapter.h"
#include "device/bluetooth/test/mock_bluetooth_device.h"
#include "device/bluetooth/test/mock_bluetooth_low_energy_scan_session.h"
#include "testing/gtest/include/gtest/gtest.h"

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

namespace {

// Used to verify the filter pattern value provided by the scanner.
constexpr uint8_t kPatternValue[] = {0x2c, 0xfe, 0xfc, 0x12, 0x8e};

// Used to construct MockBluetoothDevice.
constexpr char kTestDeviceName[] = "Test Device Name";

}  // namespace

class NearbySharingFastInitiationScannerTest : public testing::Test {
 public:
  NearbySharingFastInitiationScannerTest(
      const NearbySharingFastInitiationScannerTest&) = delete;
  NearbySharingFastInitiationScannerTest& operator=(
      const NearbySharingFastInitiationScannerTest&) = delete;

 protected:
  NearbySharingFastInitiationScannerTest() = default;

  void SetUp() override {
    mock_bluetooth_adapter_ =
        base::MakeRefCounted<NiceMock<device::MockBluetoothAdapter>>();
    ON_CALL(*mock_bluetooth_adapter_, IsPresent()).WillByDefault(Return(true));
    ON_CALL(*mock_bluetooth_adapter_, IsPowered()).WillByDefault(Return(true));
    ON_CALL(*mock_bluetooth_adapter_, StartLowEnergyScanSession(_, _))
        .WillByDefault(Invoke(this, &NearbySharingFastInitiationScannerTest::
                                        StartLowEnergyScanSession));
    device::BluetoothAdapterFactory::SetAdapterForTesting(
        mock_bluetooth_adapter_);

    scanner_ = FastInitiationScanner::Factory::Create(mock_bluetooth_adapter_);
    scanner_->StartScanning(
        base::BindRepeating(
            &NearbySharingFastInitiationScannerTest::OnDevicesDetected,
            weak_ptr_factory_.GetWeakPtr()),
        base::BindRepeating(
            &NearbySharingFastInitiationScannerTest::OnDevicesNotDetected,
            weak_ptr_factory_.GetWeakPtr()),
        base::BindOnce(
            &NearbySharingFastInitiationScannerTest::OnScannerInvalidated,
            weak_ptr_factory_.GetWeakPtr()));

    ASSERT_TRUE(mock_scan_session_);
    ASSERT_TRUE(scan_session_delegate_);
    ASSERT_TRUE(scan_session_filter_);
  }

  void OnDevicesDetected() { devices_detected_call_count_++; }

  void OnDevicesNotDetected() { devices_not_detected_call_count_++; }

  void OnScannerInvalidated() { scanner_invalidated_call_count_++; }

  std::unique_ptr<device::BluetoothLowEnergyScanSession>
  StartLowEnergyScanSession(
      std::unique_ptr<device::BluetoothLowEnergyScanFilter> filter,
      base::WeakPtr<device::BluetoothLowEnergyScanSession::Delegate> delegate) {
    scan_session_filter_ = std::move(filter);
    scan_session_delegate_ = delegate;
    auto mock_scan_session =
        std::make_unique<device::MockBluetoothLowEnergyScanSession>(
            base::BindOnce(
                &NearbySharingFastInitiationScannerTest::OnScanSessionDestroyed,
                weak_ptr_factory_.GetWeakPtr()));
    mock_scan_session_ = mock_scan_session.get();
    return mock_scan_session;
  }

  void OnScanSessionDestroyed() { mock_scan_session_ = nullptr; }

  std::unique_ptr<NiceMock<device::MockBluetoothDevice>> CreateMockDevice() {
    base::UnguessableToken device_address_token =
        base::UnguessableToken::Create();
    return std::make_unique<NiceMock<device::MockBluetoothDevice>>(
        /*adapter=*/mock_bluetooth_adapter_.get(), /*bluetooth_class=*/0,
        kTestDeviceName, device_address_token.ToString(), /*paired=*/false,
        /*connected=*/false);
  }

  scoped_refptr<NiceMock<device::MockBluetoothAdapter>> mock_bluetooth_adapter_;
  std::unique_ptr<FastInitiationScanner> scanner_;
  raw_ptr<device::MockBluetoothLowEnergyScanSession> mock_scan_session_ =
      nullptr;
  std::unique_ptr<device::BluetoothLowEnergyScanFilter> scan_session_filter_;
  base::WeakPtr<device::BluetoothLowEnergyScanSession::Delegate>
      scan_session_delegate_;
  size_t devices_detected_call_count_ = 0u;
  size_t devices_not_detected_call_count_ = 0u;
  size_t scanner_invalidated_call_count_ = 0u;

  base::WeakPtrFactory<NearbySharingFastInitiationScannerTest>
      weak_ptr_factory_{this};
};

TEST_F(NearbySharingFastInitiationScannerTest, CreationAndDestruction) {
  EXPECT_EQ(0u, devices_detected_call_count_);
  EXPECT_EQ(0u, devices_not_detected_call_count_);
  EXPECT_EQ(0u, scanner_invalidated_call_count_);

  scanner_.reset();
  EXPECT_FALSE(mock_scan_session_);
  EXPECT_EQ(0u, devices_detected_call_count_);
  EXPECT_EQ(0u, devices_not_detected_call_count_);
  EXPECT_EQ(0u, scanner_invalidated_call_count_);
}

TEST_F(NearbySharingFastInitiationScannerTest, SessionStartedSuccess) {
  scan_session_delegate_->OnSessionStarted(mock_scan_session_,
                                           /*error_code=*/std::nullopt);
  EXPECT_EQ(0u, scanner_invalidated_call_count_);
}

TEST_F(NearbySharingFastInitiationScannerTest, SessionStartedError) {
  scan_session_delegate_->OnSessionStarted(
      mock_scan_session_,
      device::BluetoothLowEnergyScanSession::ErrorCode::kFailed);
  EXPECT_EQ(1u, scanner_invalidated_call_count_);
}

TEST_F(NearbySharingFastInitiationScannerTest, SessionInvalidatedBeforeStart) {
  scan_session_delegate_->OnSessionInvalidated(mock_scan_session_);
  EXPECT_EQ(1u, scanner_invalidated_call_count_);
}

TEST_F(NearbySharingFastInitiationScannerTest, SessionInvalidatedAfterStart) {
  scan_session_delegate_->OnSessionStarted(mock_scan_session_,
                                           /*error_code=*/std::nullopt);
  scan_session_delegate_->OnSessionInvalidated(mock_scan_session_);
  EXPECT_EQ(1u, scanner_invalidated_call_count_);
}

TEST_F(NearbySharingFastInitiationScannerTest, AddAndRemoveDevices) {
  scan_session_delegate_->OnSessionStarted(mock_scan_session_,
                                           /*error_code=*/std::nullopt);

  auto device_a = CreateMockDevice();
  scan_session_delegate_->OnDeviceFound(mock_scan_session_, device_a.get());
  EXPECT_EQ(1u, devices_detected_call_count_);
  EXPECT_EQ(0u, devices_not_detected_call_count_);

  auto device_b = CreateMockDevice();
  scan_session_delegate_->OnDeviceFound(mock_scan_session_, device_b.get());
  EXPECT_EQ(1u, devices_detected_call_count_);
  EXPECT_EQ(0u, devices_not_detected_call_count_);

  scan_session_delegate_->OnDeviceLost(mock_scan_session_, device_b.get());
  EXPECT_EQ(1u, devices_detected_call_count_);
  EXPECT_EQ(0u, devices_not_detected_call_count_);

  scan_session_delegate_->OnDeviceLost(mock_scan_session_, device_a.get());
  EXPECT_EQ(1u, devices_detected_call_count_);
  EXPECT_EQ(1u, devices_not_detected_call_count_);

  scan_session_delegate_->OnDeviceFound(mock_scan_session_, device_b.get());
  EXPECT_EQ(2u, devices_detected_call_count_);
  EXPECT_EQ(1u, devices_not_detected_call_count_);
}

TEST_F(NearbySharingFastInitiationScannerTest, RemoveUnknownDevice) {
  scan_session_delegate_->OnSessionStarted(mock_scan_session_,
                                           /*error_code=*/std::nullopt);

  // Ensure removing an unknown device is a successful no-op.
  auto device_a = CreateMockDevice();
  scan_session_delegate_->OnDeviceLost(mock_scan_session_, device_a.get());
  EXPECT_EQ(0u, devices_detected_call_count_);
  EXPECT_EQ(0u, devices_not_detected_call_count_);
}

TEST_F(NearbySharingFastInitiationScannerTest, FilterPattern) {
  const std::vector<device::BluetoothLowEnergyScanFilter::Pattern>& patterns =
      scan_session_filter_->patterns();
  EXPECT_EQ(1u, patterns.size());
  EXPECT_EQ(0, patterns.back().start_position());
  EXPECT_EQ(
      device::BluetoothLowEnergyScanFilter::AdvertisementDataType::kServiceData,
      patterns.back().data_type());
  std::vector<uint8_t> pattern_value(std::begin(kPatternValue),
                                     std::end(kPatternValue));
  EXPECT_EQ(pattern_value, patterns.back().value());
}