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
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CONTENT_BROWSER_USB_USB_TEST_UTILS_H_
#define CONTENT_BROWSER_USB_USB_TEST_UTILS_H_
#include <memory>
#include <vector>
#include "base/containers/span.h"
#include "base/observer_list.h"
#include "content/public/browser/content_browser_client.h"
#include "content/public/browser/usb_chooser.h"
#include "content/public/browser/usb_delegate.h"
#include "mojo/public/cpp/bindings/associated_receiver.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "services/device/public/mojom/usb_device.mojom-forward.h"
#include "services/device/public/mojom/usb_enumeration_options.mojom-forward.h"
#include "services/device/public/mojom/usb_manager.mojom-forward.h"
#include "services/device/public/mojom/usb_manager_client.mojom.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "third_party/blink/public/mojom/usb/web_usb_service.mojom.h"
#include "url/origin.h"
namespace content {
class RenderFrameHost;
// A mock UsbDeviceManagerClient implementation that can be used to listen for
// USB device connection events.
class MockDeviceManagerClient : public device::mojom::UsbDeviceManagerClient {
public:
MockDeviceManagerClient();
MockDeviceManagerClient(MockDeviceManagerClient&) = delete;
MockDeviceManagerClient& operator=(MockDeviceManagerClient&) = delete;
~MockDeviceManagerClient() override;
void Bind(
mojo::PendingAssociatedReceiver<device::mojom::UsbDeviceManagerClient>
receiver) {
receiver_.Bind(std::move(receiver));
}
mojo::PendingAssociatedRemote<device::mojom::UsbDeviceManagerClient>
CreateInterfacePtrAndBind() {
auto client = receiver_.BindNewEndpointAndPassRemote();
receiver_.set_disconnect_handler(base::BindOnce(
&MockDeviceManagerClient::OnConnectionError, base::Unretained(this)));
return client;
}
MOCK_METHOD(void, OnDeviceAdded, (device::mojom::UsbDeviceInfoPtr));
MOCK_METHOD(void, OnDeviceRemoved, (device::mojom::UsbDeviceInfoPtr));
MOCK_METHOD(void, ConnectionError, ());
void OnConnectionError() {
receiver_.reset();
ConnectionError();
}
private:
mojo::AssociatedReceiver<device::mojom::UsbDeviceManagerClient> receiver_{
this};
};
// A UsbDelegate implementation that can be mocked for tests.
class MockUsbDelegate : public UsbDelegate {
public:
MockUsbDelegate();
MockUsbDelegate(MockUsbDelegate&) = delete;
MockUsbDelegate& operator=(MockUsbDelegate&) = delete;
~MockUsbDelegate() override;
// Simulates opening the USB device chooser dialog and selecting an item. The
// chooser automatically selects the item returned by RunChooserInternal,
// which may be mocked. Returns `nullptr`. `options` is ignored.
std::unique_ptr<UsbChooser> RunChooser(
RenderFrameHost& frame,
blink::mojom::WebUsbRequestDeviceOptionsPtr options,
blink::mojom::WebUsbService::GetPermissionCallback callback) override;
void AddObserver(BrowserContext* browser_context,
Observer* observer) override;
void RemoveObserver(BrowserContext* browser_context,
Observer* observer) override;
// Simulate events from tests.
void OnDeviceAdded(const device::mojom::UsbDeviceInfo& device);
void OnDeviceRemoved(const device::mojom::UsbDeviceInfo& device);
void OnPermissionRevoked(const url::Origin& origin);
void OnDeviceManagerConnectionError();
MOCK_METHOD0(RunChooserInternal, device::mojom::UsbDeviceInfoPtr());
MOCK_METHOD4(AdjustProtectedInterfaceClasses,
void(BrowserContext*,
const url::Origin&,
RenderFrameHost*,
std::vector<uint8_t>&));
MOCK_METHOD1(PageMayUseUsb, bool(Page&));
MOCK_METHOD2(CanRequestDevicePermission,
bool(BrowserContext*, const url::Origin&));
MOCK_METHOD3(RevokeDevicePermissionWebInitiated,
void(BrowserContext*,
const url::Origin&,
const device::mojom::UsbDeviceInfo&));
MOCK_METHOD2(GetDeviceInfo,
const device::mojom::UsbDeviceInfo*(BrowserContext*,
const std::string& guid));
MOCK_METHOD4(HasDevicePermission,
bool(BrowserContext*,
RenderFrameHost*,
const url::Origin&,
const device::mojom::UsbDeviceInfo&));
MOCK_METHOD2(GetDevices,
void(BrowserContext*,
blink::mojom::WebUsbService::GetDevicesCallback));
MOCK_METHOD5(GetDevice,
void(BrowserContext*,
const std::string&,
base::span<const uint8_t>,
mojo::PendingReceiver<device::mojom::UsbDevice>,
mojo::PendingRemote<device::mojom::UsbDeviceClient>));
MOCK_METHOD2(SetDeviceManagerForTesting,
void(RenderFrameHost&,
mojo::PendingRemote<device::mojom::UsbDeviceManager>
device_manager));
MOCK_METHOD1(IsServiceWorkerAllowedForOrigin, bool(const url::Origin&));
MOCK_METHOD2(IncrementConnectionCount,
void(BrowserContext*, const url::Origin&));
MOCK_METHOD2(DecrementConnectionCount,
void(BrowserContext*, const url::Origin&));
const base::ObserverList<Observer>& observer_list() { return observer_list_; }
void SetAssertBrowserContext(bool assert_browser_context);
private:
base::ObserverList<UsbDelegate::Observer> observer_list_;
bool assert_browser_context_ = false;
};
template <typename SuperClass>
class UsbTestContentBrowserClientBase : public SuperClass {
public:
MockUsbDelegate& delegate() { return delegate_; }
// ContentBrowserClient:
UsbDelegate* GetUsbDelegate() override { return &delegate_; }
private:
testing::NiceMock<MockUsbDelegate> delegate_;
};
using UsbTestContentBrowserClient =
UsbTestContentBrowserClientBase<ContentBrowserClient>;
} // namespace content
#endif // CONTENT_BROWSER_USB_USB_TEST_UTILS_H_
|