chromium/chrome/browser/usb/chrome_usb_delegate_unittest.cc

// 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.

#include "chrome/browser/usb/chrome_usb_delegate.h"

#include <optional>
#include <string_view>

#include "base/barrier_closure.h"
#include "base/test/gmock_callback_support.h"
#include "base/test/test_future.h"
#include "build/build_config.h"
#include "chrome/browser/usb/usb_chooser_context.h"
#include "chrome/browser/usb/usb_chooser_context_factory.h"
#include "chrome/browser/usb/usb_connection_tracker.h"
#include "chrome/browser/usb/usb_connection_tracker_factory.h"
#include "chrome/common/chrome_features.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/embedded_worker_instance_test_harness.h"
#include "content/public/test/test_renderer_host.h"
#include "services/device/public/cpp/test/fake_usb_device_info.h"
#include "services/device/public/cpp/test/fake_usb_device_manager.h"
#include "services/device/public/mojom/usb_device.mojom.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/mojom/usb/web_usb_service.mojom.h"
#include "url/gurl.h"

#if BUILDFLAG(ENABLE_EXTENSIONS)
#include "base/command_line.h"
#include "base/values.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/test_extension_system.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/buildflags/buildflags.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_builder.h"
#endif  // BUILDFLAG(ENABLE_EXTENSIONS)

namespace {

RunClosure;
RunOnceCallback;
RunOnceClosure;
TestFuture;
_;
NiceMock;

constexpr std::string_view kDefaultTestUrl{};
constexpr std::string_view kCrossOriginTestUrl{};

#if BUILDFLAG(ENABLE_EXTENSIONS)
constexpr std::string_view kExtensionId{};
constexpr char kAllowlistedImprivataExtensionId[] =;
constexpr char kAllowlistedSmartCardExtensionId[] =;
#endif  // BUILDFLAG(ENABLE_EXTENSIONS)

ACTION_P2(ExpectGuidAndThen, expected_guid, callback) {}

// Calls GetDevices on `service` and blocks until the GetDevicesCallback is
// invoked. Fails the test if the guids of received UsbDeviceInfos do not
// exactly match `expected_guids`.
void GetDevicesBlocking(blink::mojom::WebUsbService* service,
                        const std::set<std::string>& expected_guids) {}

device::mojom::UsbOpenDeviceResultPtr NewUsbOpenDeviceSuccess() {}

#if BUILDFLAG(ENABLE_EXTENSIONS)
// Creates a FakeUsbDeviceInfo with USB class code.
scoped_refptr<device::FakeUsbDeviceInfo> CreateFakeUsbDeviceInfo() {}

// Creates a FakeUsbDeviceInfo with Smart Card class code.
scoped_refptr<device::FakeUsbDeviceInfo> CreateFakeSmartCardDeviceInfo() {}
#endif  // BUILDFLAG(ENABLE_EXTENSIONS)

// A mock UsbDeviceManagerClient implementation that can be used to listen for
// USB device connection events.
class MockDeviceManagerClient
    : public UsbChooserContext::UsbDeviceManagerClient {};

class MockUsbConnectionTracker : public UsbConnectionTracker {};

// Tests for embedder-specific behaviors of Chrome's blink::mojom::WebUsbService
// implementation.
class ChromeUsbTestHelper {};

class ChromeUsbDelegateRenderFrameTestBase
    : public ChromeRenderViewHostTestHarness,
      public ChromeUsbTestHelper {};

class ChromeUsbDelegateServiceWorkerTestBase
    : public content::EmbeddedWorkerInstanceTestHarness,
      public ChromeUsbTestHelper {};

class ChromeUsbDelegateRenderFrameTest
    : public ChromeUsbDelegateRenderFrameTestBase {};

class ChromeUsbDelegateServiceWorkerTest
    : public ChromeUsbDelegateServiceWorkerTestBase {};

#if BUILDFLAG(ENABLE_EXTENSIONS)
class DisableWebUsbOnExtensionServiceWorkerHelper {};

class ChromeUsbDelegateExtensionRenderFrameTest
    : public ChromeUsbDelegateRenderFrameTestBase {};

class ChromeUsbDelegateImprivataExtensionRenderFrameTest
    : public ChromeUsbDelegateRenderFrameTestBase {};

class ChromeUsbDelegateSmartCardExtensionRenderFrameTest
    : public ChromeUsbDelegateRenderFrameTestBase {};

class ChromeUsbDelegateExtensionRenderFrameFeatureDisabledTest
    : public ChromeUsbDelegateRenderFrameTestBase,
      public DisableWebUsbOnExtensionServiceWorkerHelper {};

class ChromeUsbDelegateExtensionServiceWorkerTest
    : public ChromeUsbDelegateServiceWorkerTestBase {};

class ChromeUsbDelegateExtensionServiceWorkerFeatureDisabledTest
    : public ChromeUsbDelegateServiceWorkerTestBase,
      public DisableWebUsbOnExtensionServiceWorkerHelper {};

class ChromeUsbDelegateImprivataExtensionServiceWorkerTest
    : public ChromeUsbDelegateServiceWorkerTestBase {};

class ChromeUsbDelegateSmartCardExtensionServiceWorkerTest
    : public ChromeUsbDelegateServiceWorkerTestBase {};

#endif  // BUILDFLAG(ENABLE_EXTENSIONS)

}  // namespace

TEST_F(ChromeUsbDelegateRenderFrameTest, NoPermissionDevice) {}

TEST_F(ChromeUsbDelegateRenderFrameTest, ReconnectDeviceManager) {}

TEST_F(ChromeUsbDelegateRenderFrameTest, RevokeDevicePermission) {}

TEST_F(ChromeUsbDelegateRenderFrameTest, OpenAndCloseDevice) {}

TEST_F(ChromeUsbDelegateRenderFrameTest, OpenAndDisconnectDevice) {}

TEST_F(ChromeUsbDelegateRenderFrameTest, OpenAndNavigateCrossOrigin) {}

TEST_F(ChromeUsbDelegateServiceWorkerTest, WebUsbServiceNotConnected) {}

#if BUILDFLAG(ENABLE_EXTENSIONS)
TEST_F(ChromeUsbDelegateExtensionRenderFrameTest, NoPermissionDevice) {}

TEST_F(ChromeUsbDelegateExtensionRenderFrameTest, ReconnectDeviceManager) {}

TEST_F(ChromeUsbDelegateExtensionRenderFrameTest, RevokeDevicePermission) {}

TEST_F(ChromeUsbDelegateExtensionRenderFrameTest, OpenAndCloseDevice) {}

TEST_F(ChromeUsbDelegateExtensionRenderFrameTest, OpenAndDisconnectDevice) {}

TEST_F(ChromeUsbDelegateImprivataExtensionRenderFrameTest,
       AllowlistedImprivataExtension) {}

TEST_F(ChromeUsbDelegateSmartCardExtensionRenderFrameTest,
       AllowlistedSmartCardConnectorExtension) {}

TEST_F(ChromeUsbDelegateExtensionRenderFrameFeatureDisabledTest,
       OpenAndCloseDevice) {}

TEST_F(ChromeUsbDelegateExtensionRenderFrameFeatureDisabledTest,
       OpenAndDisconnectDevice) {}

TEST_F(ChromeUsbDelegateExtensionServiceWorkerFeatureDisabledTest,
       WebUsbServiceNotConnected) {}

TEST_F(ChromeUsbDelegateImprivataExtensionServiceWorkerTest,
       AllowlistedImprivataExtension) {}

TEST_F(ChromeUsbDelegateSmartCardExtensionServiceWorkerTest,
       AllowlistedSmartCardConnectorExtension) {}

TEST_F(ChromeUsbDelegateExtensionServiceWorkerTest, NoPermissionDevice) {}

TEST_F(ChromeUsbDelegateExtensionServiceWorkerTest, ReconnectDeviceManager) {}

TEST_F(ChromeUsbDelegateExtensionServiceWorkerTest, RevokeDevicePermission) {}

TEST_F(ChromeUsbDelegateExtensionServiceWorkerTest, OpenAndCloseDevice) {}

TEST_F(ChromeUsbDelegateExtensionServiceWorkerTest, OpenAndDisconnectDevice) {}

#endif  // BUILDFLAG(ENABLE_EXTENSIONS)

TEST(ChromeUsbDelegateBrowserContextTest, BrowserContextIsNull) {}