chromium/extensions/browser/api/usb/usb_apitest.cc

// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <stddef.h>

#include <numeric>

#include "base/run_loop.h"
#include "base/test/gmock_callback_support.h"
#include "base/test/gmock_move_support.h"
#include "build/build_config.h"
#include "content/public/browser/web_contents.h"
#include "extensions/browser/api/device_permissions_prompt.h"
#include "extensions/browser/api/usb/usb_device_manager.h"
#include "extensions/shell/browser/shell_extensions_api_client.h"
#include "extensions/shell/test/shell_apitest.h"
#include "extensions/test/extension_test_message_listener.h"
#include "mojo/public/cpp/bindings/pending_remote.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/cpp/test/mock_usb_mojo_device.h"
#include "services/device/public/mojom/usb_device.mojom.h"

namespace extensions {

namespace  // namespace

IN_PROC_BROWSER_TEST_F(UsbApiTest, DeviceHandling) {}

IN_PROC_BROWSER_TEST_F(UsbApiTest, ResetDevice) {}

IN_PROC_BROWSER_TEST_F(UsbApiTest, SetConfiguration) {}

IN_PROC_BROWSER_TEST_F(UsbApiTest, ListInterfaces) {}

IN_PROC_BROWSER_TEST_F(UsbApiTest, TransferEvent) {}

IN_PROC_BROWSER_TEST_F(UsbApiTest, ZeroLengthTransfer) {}

IN_PROC_BROWSER_TEST_F(UsbApiTest, TransferFailure) {}

IN_PROC_BROWSER_TEST_F(UsbApiTest, InvalidLengthTransfer) {}

IN_PROC_BROWSER_TEST_F(UsbApiTest, InvalidTimeout) {}

IN_PROC_BROWSER_TEST_F(UsbApiTest, CallsAfterDisconnect) {}

IN_PROC_BROWSER_TEST_F(UsbApiTest, TransferFailureOnDisconnect) {}

IN_PROC_BROWSER_TEST_F(UsbApiTest, OnDeviceAdded) {}

IN_PROC_BROWSER_TEST_F(UsbApiTest, OnDeviceRemoved) {}

IN_PROC_BROWSER_TEST_F(UsbApiTest, GetUserSelectedDevices) {}

#if BUILDFLAG(IS_CHROMEOS)
IN_PROC_BROWSER_TEST_F(UsbApiTest, MassStorage) {
  ExtensionTestMessageListener ready_listener("ready");
  ready_listener.set_failure_message("failure");
  ExtensionTestMessageListener result_listener("success");
  result_listener.set_failure_message("failure");

  // Mass storage devices should be hidden unless allowed in policy.
  // The TestExtensionsAPIClient allows only vid=1, pid=2.
  TestExtensionsAPIClient test_api_client;
  std::vector<device::mojom::UsbConfigurationInfoPtr> storage_configs;
  auto storage_config = device::FakeUsbDeviceInfo::CreateConfiguration(
      /* mass storage */ 0x08, 0x06, 0x50);
  storage_configs.push_back(storage_config->Clone());
  device::mojom::UsbDeviceInfoPtr device_1 =
      fake_usb_manager_.CreateAndAddDevice(0x1, 0x2, 0x00,
                                           std::move(storage_configs));

  storage_configs.clear();
  storage_configs.push_back(storage_config->Clone());
  device::mojom::UsbDeviceInfoPtr device_2 =
      fake_usb_manager_.CreateAndAddDevice(0x5, 0x6, 0x00,
                                           std::move(storage_configs));

  ASSERT_TRUE(LoadApp("api_test/usb/mass_storage"));
  ASSERT_TRUE(ready_listener.WaitUntilSatisfied());

  fake_usb_manager_.RemoveDevice(device_2->guid);
  fake_usb_manager_.RemoveDevice(device_1->guid);

  ASSERT_TRUE(result_listener.WaitUntilSatisfied());
}
#endif  // BUILDFLAG(IS_CHROMEOS)

}  // namespace extensions