#include "chrome/browser/hid/hid_chooser_context.h"
#include <optional>
#include <string_view>
#include "base/barrier_closure.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/scoped_observation.h"
#include "base/test/bind.h"
#include "base/test/gmock_callback_support.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/test_future.h"
#include "base/test/values_test_util.h"
#include "base/uuid.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/hid/hid_chooser_context_factory.h"
#include "chrome/browser/hid/mock_hid_device_observer.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "components/content_settings/core/common/pref_names.h"
#include "components/permissions/test/object_permission_context_base_mock_permission_observer.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "content/public/test/browser_task_environment.h"
#include "extensions/common/extension_features.h"
#include "services/device/public/cpp/hid/hid_blocklist.h"
#include "services/device/public/cpp/test/fake_hid_manager.h"
#include "services/device/public/mojom/hid.mojom.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/origin.h"
#if BUILDFLAG(IS_CHROMEOS_ASH)
#include "chrome/browser/ash/login/users/fake_chrome_user_manager.h"
#include "chromeos/ash/components/browser_context_helper/browser_context_types.h"
#include "components/account_id/account_id.h"
#include "components/user_manager/scoped_user_manager.h"
#endif
ParseJson;
RunClosure;
namespace {
constexpr uint16_t kTestVendorId = …;
constexpr uint16_t kTestProductId = …;
constexpr char kTestSerialNumber[] = …;
constexpr char kTestProductName[] = …;
const char* const kTestPhysicalDeviceIds[] = …;
constexpr char kTestUserEmail[] = …;
constexpr uint16_t kTestUsagePage = …;
constexpr uint16_t kTestUsage = …;
constexpr char kTestExtensionId[] = …;
class HidChooserContextTestBase { … };
class HidChooserContextTest : public HidChooserContextTestBase,
public testing::Test { … };
}
TEST_F(HidChooserContextTest, GrantAndRevokeEphemeralDevice) { … }
TEST_F(HidChooserContextTest, GrantAndForgetEphemeralDevice) { … }
TEST_F(HidChooserContextTest, GrantTwoEphemeralDevicesForgetOne) { … }
TEST_F(HidChooserContextTest, GrantAndDisconnectEphemeralDevice) { … }
TEST_F(HidChooserContextTest, GrantDisconnectRevokeUsbPersistentDevice) { … }
TEST_F(HidChooserContextTest, GrantForgetUsbPersistentDevice) { … }
TEST_F(HidChooserContextTest, GuardPermission) { … }
TEST_F(HidChooserContextTest, ConnectionErrorWithEphemeralPermission) { … }
TEST_F(HidChooserContextTest, ConnectionErrorWithPersistentPermission) { … }
namespace {
device::mojom::HidDeviceInfoPtr CreateDeviceWithOneCollection(
const std::string& guid) { … }
device::mojom::HidDeviceInfoPtr CreateDeviceWithTwoCollections(
const std::string& guid) { … }
}
TEST_F(HidChooserContextTest, AddChangeRemoveDevice) { … }
namespace {
struct BlocklistTestData { … } kBlocklistTestData[]{ … };
class HidChooserContextBlocklistTest
: public HidChooserContextTestBase,
public testing::TestWithParam<BlocklistTestData> { … };
}
TEST_P(HidChooserContextBlocklistTest, Blocklist) { … }
INSTANTIATE_TEST_SUITE_P(…);
TEST_F(HidChooserContextTest, PolicyGuardPermission) { … }
TEST_F(HidChooserContextTest, PolicyAskForUrls) { … }
TEST_F(HidChooserContextTest, PolicyBlockedForUrls) { … }
namespace {
class HidChooserContextAffiliatedTest : public HidChooserContextTestBase,
public testing::TestWithParam<bool> { … };
}
TEST_P(HidChooserContextAffiliatedTest, PolicyAllowForUrls) { … }
TEST_P(HidChooserContextAffiliatedTest, BlocklistOverridesPolicy) { … }
TEST_F(HidChooserContextTest, FidoAllowlistOverridesBlocklistDeviceIdRule) { … }
TEST_P(HidChooserContextAffiliatedTest, FidoAllowlistAndPolicy) { … }
INSTANTIATE_TEST_SUITE_P(…);
namespace {
class HidChooserContextLoginScreenTest : public HidChooserContextTestBase,
public testing::Test { … };
}
TEST_F(HidChooserContextLoginScreenTest, ApplyPolicyOnLoginScreen) { … }
class HidChooserContextWebViewTest : public HidChooserContextTest { … };
TEST_F(HidChooserContextWebViewTest, GrantDevicePermissionToWebView) { … }
TEST_F(HidChooserContextWebViewTest, RevokeDevicePermissionFromEmbedder) { … }
TEST_F(HidChooserContextWebViewTest, RevokeDevicePermissionFromWebView) { … }
TEST_F(HidChooserContextWebViewTest, WebsitePermissionDoesNotLeakToWebView) { … }