File: mock_bluetooth_device.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 (95 lines) | stat: -rw-r--r-- 3,705 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
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "device/bluetooth/test/mock_bluetooth_device.h"

#include <memory>
#include <utility>

#include "base/strings/utf_string_conversions.h"
#include "device/bluetooth/bluetooth_remote_gatt_service.h"

namespace device {

using ::testing::Return;
using ::testing::ReturnPointee;

MockBluetoothDevice::MockBluetoothDevice(BluetoothAdapter* adapter,
                                         uint32_t bluetooth_class,
                                         const char* name,
                                         const std::string& address,
                                         bool initially_paired,
                                         bool connected)
    : BluetoothDevice(adapter),
      bluetooth_class_(bluetooth_class),
      name_(name ? std::optional<std::string>(name) : std::nullopt),
      address_(address),
      connected_(connected),
      paired_(initially_paired) {
  ON_CALL(*this, GetBluetoothClass()).WillByDefault(Return(bluetooth_class_));
  ON_CALL(*this, GetIdentifier())
      .WillByDefault(Return(address_ + "-Identifier"));
  ON_CALL(*this, GetAddress()).WillByDefault(Return(address_));
  ON_CALL(*this, GetVendorIDSource()).WillByDefault(Return(VENDOR_ID_UNKNOWN));
  ON_CALL(*this, GetVendorID()).WillByDefault(Return(0));
  ON_CALL(*this, GetProductID()).WillByDefault(Return(0));
  ON_CALL(*this, GetDeviceID()).WillByDefault(Return(0));
  ON_CALL(*this, GetName()).WillByDefault(Return(name_));
  ON_CALL(*this, GetNameForDisplay())
      .WillByDefault(
          Return(base::UTF8ToUTF16(name_ ? name_.value() : "Unnamed Device")));
  ON_CALL(*this, GetType()).WillByDefault(ReturnPointee(&transport_));
  ON_CALL(*this, GetDeviceType())
      .WillByDefault(Return(BluetoothDeviceType::UNKNOWN));
  ON_CALL(*this, IsPaired()).WillByDefault(ReturnPointee(&paired_));
#if BUILDFLAG(IS_CHROMEOS)
  ON_CALL(*this, IsBonded()).WillByDefault(ReturnPointee(&paired_));
#endif  // BUILDFLAG(IS_CHROMEOS)
  ON_CALL(*this, IsConnected()).WillByDefault(ReturnPointee(&connected_));
  ON_CALL(*this, IsGattConnected()).WillByDefault(ReturnPointee(&connected_));
  ON_CALL(*this, IsConnectable()).WillByDefault(Return(false));
  ON_CALL(*this, IsConnecting()).WillByDefault(Return(false));
  ON_CALL(*this, GetUUIDs()).WillByDefault(ReturnPointee(&uuids_));
  ON_CALL(*this, ExpectingPinCode()).WillByDefault(Return(false));
  ON_CALL(*this, ExpectingPasskey()).WillByDefault(Return(false));
  ON_CALL(*this, ExpectingConfirmation()).WillByDefault(Return(false));
}

MockBluetoothDevice::~MockBluetoothDevice() = default;

void MockBluetoothDevice::AddMockService(
    std::unique_ptr<MockBluetoothGattService> mock_service) {
  mock_services_.push_back(std::move(mock_service));
}

std::vector<BluetoothRemoteGattService*> MockBluetoothDevice::GetMockServices()
    const {
  std::vector<BluetoothRemoteGattService*> services;
  for (const auto& service : mock_services_) {
    services.push_back(service.get());
  }
  return services;
}

BluetoothRemoteGattService* MockBluetoothDevice::GetMockService(
    const std::string& identifier) const {
  for (const auto& service : mock_services_) {
    if (service->GetIdentifier() == identifier)
      return service.get();
  }
  return nullptr;
}

void MockBluetoothDevice::PushPendingCallback(base::OnceClosure callback) {
  pending_callbacks_.push(std::move(callback));
}

void MockBluetoothDevice::RunPendingCallbacks() {
  while (!pending_callbacks_.empty()) {
    std::move(pending_callbacks_.front()).Run();
    pending_callbacks_.pop();
  }
}

}  // namespace device