| Index: content/browser/bluetooth/bluetooth_dispatcher_host_unittest.cc
|
| diff --git a/content/browser/bluetooth/bluetooth_dispatcher_host_unittest.cc b/content/browser/bluetooth/bluetooth_dispatcher_host_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..862ebcebc5a1c64f42fcb9d178a53ea69d85ffdc
|
| --- /dev/null
|
| +++ b/content/browser/bluetooth/bluetooth_dispatcher_host_unittest.cc
|
| @@ -0,0 +1,109 @@
|
| +// Copyright 2016 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 "content/browser/bluetooth/bluetooth_dispatcher_host.h"
|
| +
|
| +#include "base/run_loop.h"
|
| +#include "content/common/bluetooth/bluetooth_messages.h"
|
| +#include "content/public/browser/browser_thread.h"
|
| +#include "content/public/browser/content_browser_client.h"
|
| +#include "content/public/test/mock_render_process_host.h"
|
| +#include "content/public/test/test_renderer_host.h"
|
| +#include "device/bluetooth/test/mock_bluetooth_adapter.h"
|
| +
|
| +namespace content {
|
| +namespace {
|
| +
|
| +class BluetoothDispatcherHostTest : public RenderViewHostTestHarness {
|
| + protected:
|
| + void TearDown() override {
|
| + if (old_browser_client_) {
|
| + SetBrowserClientForTesting(old_browser_client_);
|
| + }
|
| + RenderViewHostTestHarness::TearDown();
|
| + }
|
| +
|
| + void SetBrowserClient(ContentBrowserClient* new_browser_client) {
|
| + EXPECT_FALSE(old_browser_client_);
|
| + old_browser_client_ = SetBrowserClientForTesting(new_browser_client);
|
| + }
|
| +
|
| + ContentBrowserClient* old_browser_client_ = nullptr;
|
| +};
|
| +
|
| +class WaitForRequestDeviceMessage : public IPC::Listener {
|
| + public:
|
| + explicit WaitForRequestDeviceMessage(base::RunLoop* loop) : loop_(loop) {}
|
| +
|
| + bool OnMessageReceived(const IPC::Message& msg) override {
|
| + if (msg.type() == BluetoothMsg_RequestDeviceError::ID ||
|
| + msg.type() == BluetoothMsg_RequestDeviceSuccess::ID) {
|
| + BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
|
| + loop_->QuitClosure());
|
| + }
|
| + return false; // Store the message in the sink.
|
| + }
|
| +
|
| + private:
|
| + base::RunLoop* loop_;
|
| +};
|
| +
|
| +class DisallowBluetoothContentBrowserClient : public ContentBrowserClient {
|
| + bool AllowWebBluetooth() override { return false; }
|
| +};
|
| +
|
| +// PolicyTest.BlockWebBluetooth tests that policy affects the
|
| +// ContentBrowserClient.
|
| +TEST_F(BluetoothDispatcherHostTest, ContentClientCanDisableAPI) {
|
| + NavigateAndCommit(GURL("https://example.com/"));
|
| + ASSERT_EQ("https://example.com",
|
| + main_rfh()->GetLastCommittedOrigin().Serialize());
|
| +
|
| + scoped_refptr<content::BluetoothDispatcherHost> bluetooth =
|
| + new content::BluetoothDispatcherHost(process()->GetID());
|
| + process()->AddFilter(bluetooth.get());
|
| +
|
| + DisallowBluetoothContentBrowserClient disallow_bluetooth_client;
|
| + SetBrowserClient(&disallow_bluetooth_client);
|
| +
|
| + // Make the adapter say it's present.
|
| + scoped_refptr<device::MockBluetoothAdapter> adapter =
|
| + new testing::NiceMock<device::MockBluetoothAdapter>;
|
| + EXPECT_CALL(*adapter, IsPresent()).WillRepeatedly(testing::Return(true));
|
| + bluetooth->SetBluetoothAdapterForTesting(adapter);
|
| +
|
| + // Build a filter for the requestDevice() call.
|
| + std::vector<content::BluetoothScanFilter> scan_filters;
|
| + content::BluetoothScanFilter scan_filter;
|
| + scan_filter.name = "Hello";
|
| + scan_filters.push_back(scan_filter);
|
| +
|
| + base::RunLoop request_device_loop;
|
| + WaitForRequestDeviceMessage waiter(&request_device_loop);
|
| + process()->sink().AddFilter(&waiter);
|
| +
|
| + bluetooth->OnMessageReceived(BluetoothHostMsg_RequestDevice(
|
| + 1, 1, main_rfh()->GetRoutingID(), scan_filters,
|
| + std::vector<device::BluetoothUUID>()));
|
| +
|
| + request_device_loop.Run();
|
| +
|
| + // Check that we reject with the right message.
|
| + const IPC::Message* response_msg = process()->sink().GetUniqueMessageMatching(
|
| + BluetoothMsg_RequestDeviceError::ID);
|
| + ASSERT_TRUE(response_msg);
|
| + BluetoothMsg_RequestDeviceError::Param response;
|
| + BluetoothMsg_RequestDeviceError::Read(response_msg, &response);
|
| + EXPECT_EQ(blink::WebBluetoothError::ChooserDisabled, base::get<2>(response));
|
| +
|
| + // Avoid leaking the MockBluetoothAdapter.
|
| + bluetooth->SetBluetoothAdapterForTesting(nullptr);
|
| +
|
| + // Make sure no more messages go through the BluetoothDispatcherHost after
|
| + // it's destroyed.
|
| + DeleteContents();
|
| +}
|
| +
|
| +} // namespace
|
| +} // namespace content
|
|
|