Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(228)

Unified Diff: chrome/browser/usb/usb_chooser_permission_context_unittest.cc

Issue 1382783002: Store USB device permissions in website settings. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove UsbPermissionContext and add ChooserPermissionContext helper functions. Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/usb/usb_chooser_permission_context_unittest.cc
diff --git a/chrome/browser/usb/usb_chooser_permission_context_unittest.cc b/chrome/browser/usb/usb_chooser_permission_context_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..723bb02a0107c1a26c977411137a3b10bd5fada2
--- /dev/null
+++ b/chrome/browser/usb/usb_chooser_permission_context_unittest.cc
@@ -0,0 +1,122 @@
+// Copyright 2015 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 "chrome/browser/usb/usb_chooser_permission_context.h"
+#include "chrome/browser/usb/usb_chooser_permission_context_factory.h"
+#include "chrome/test/base/testing_profile.h"
+#include "content/public/test/test_browser_thread_bundle.h"
+#include "device/core/device_client.h"
+#include "device/usb/mock_usb_device.h"
+#include "device/usb/mock_usb_service.h"
+
+using device::MockUsbDevice;
+using device::UsbDevice;
+
+namespace {
+
+class TestDeviceClient : public device::DeviceClient {
+ public:
+ TestDeviceClient() {}
+ ~TestDeviceClient() override {}
+
+ device::MockUsbService& usb_service() { return usb_service_; }
+
+ private:
+ device::UsbService* GetUsbService() override { return &usb_service_; }
+
+ device::MockUsbService usb_service_;
+};
+
+} // namespace
+
+class UsbChooserPermissionContextTest : public testing::Test {
+ public:
+ UsbChooserPermissionContextTest() {}
+ ~UsbChooserPermissionContextTest() override {}
+
+ protected:
+ Profile* profile() { return &profile_; }
+ device::MockUsbService& usb_service() { return device_client_.usb_service(); }
+
+ private:
+ content::TestBrowserThreadBundle thread_bundle_;
+ TestDeviceClient device_client_;
+ TestingProfile profile_;
+};
+
+TEST_F(UsbChooserPermissionContextTest, CheckGrantAndRevokePermission) {
+ GURL origin("https://www.google.com");
+ scoped_refptr<UsbDevice> device =
+ new MockUsbDevice(0, 0, "Google", "Gizmo", "123ABC");
+ usb_service().AddDevice(device);
+ UsbChooserPermissionContext* store =
+ UsbChooserPermissionContextFactory::GetForProfile(profile());
+
+ EXPECT_FALSE(store->HasDevicePermission(origin, origin, device->guid()));
+ store->GrantDevicePermission(origin, origin, device->guid());
+ EXPECT_TRUE(store->HasDevicePermission(origin, origin, device->guid()));
+ store->RevokeDevicePermission(origin, origin, device->guid());
+ EXPECT_FALSE(store->HasDevicePermission(origin, origin, device->guid()));
+}
+
+TEST_F(UsbChooserPermissionContextTest,
+ CheckGrantAndRevokeEphemeralPermission) {
+ GURL origin("https://www.google.com");
+ scoped_refptr<UsbDevice> device =
+ new MockUsbDevice(0, 0, "Google", "Gizmo", "");
+ scoped_refptr<UsbDevice> other_device =
raymes 2015/10/14 02:07:04 nit: this is unused Though possible add a case whe
Reilly Grant (use Gerrit) 2015/10/22 00:57:03 Fixed.
+ new MockUsbDevice(0, 0, "Google", "Gizmo", "");
+ usb_service().AddDevice(device);
+ UsbChooserPermissionContext* store =
+ UsbChooserPermissionContextFactory::GetForProfile(profile());
+
+ EXPECT_FALSE(store->HasDevicePermission(origin, origin, device->guid()));
+ store->GrantDevicePermission(origin, origin, device->guid());
+ EXPECT_TRUE(store->HasDevicePermission(origin, origin, device->guid()));
+ store->RevokeDevicePermission(origin, origin, device->guid());
+ EXPECT_FALSE(store->HasDevicePermission(origin, origin, device->guid()));
+}
+
+TEST_F(UsbChooserPermissionContextTest, DisconnectDeviceWithPermission) {
+ GURL origin("https://www.google.com");
+ scoped_refptr<UsbDevice> device =
+ new MockUsbDevice(0, 0, "Google", "Gizmo", "123ABC");
+ usb_service().AddDevice(device);
+ UsbChooserPermissionContext* store =
+ UsbChooserPermissionContextFactory::GetForProfile(profile());
+
+ EXPECT_FALSE(store->HasDevicePermission(origin, origin, device->guid()));
+ store->GrantDevicePermission(origin, origin, device->guid());
+ EXPECT_TRUE(store->HasDevicePermission(origin, origin, device->guid()));
+ usb_service().RemoveDevice(device);
+ EXPECT_FALSE(store->HasDevicePermission(origin, origin, device->guid()));
+
+ scoped_refptr<UsbDevice> reconnected_device =
+ new MockUsbDevice(0, 0, "Google", "Gizmo", "123ABC");
+ usb_service().AddDevice(reconnected_device);
+ EXPECT_TRUE(
+ store->HasDevicePermission(origin, origin, reconnected_device->guid()));
+}
+
+TEST_F(UsbChooserPermissionContextTest,
+ DisconnectDeviceWithEphemeralPermission) {
+ GURL origin("https://www.google.com");
+ scoped_refptr<UsbDevice> device =
+ new MockUsbDevice(0, 0, "Google", "Gizmo", "");
+ usb_service().AddDevice(device);
+ UsbChooserPermissionContext* store =
+ UsbChooserPermissionContextFactory::GetForProfile(profile());
+
+ EXPECT_FALSE(store->HasDevicePermission(origin, origin, device->guid()));
+ store->GrantDevicePermission(origin, origin, device->guid());
+ EXPECT_TRUE(store->HasDevicePermission(origin, origin, device->guid()));
+ usb_service().RemoveDevice(device);
+ EXPECT_FALSE(store->HasDevicePermission(origin, origin, device->guid()));
+
+ scoped_refptr<UsbDevice> reconnected_device =
+ new MockUsbDevice(0, 0, "Google", "Gizmo", "");
+ usb_service().AddDevice(reconnected_device);
+ EXPECT_FALSE(
+ store->HasDevicePermission(origin, origin, reconnected_device->guid()));
+}

Powered by Google App Engine
This is Rietveld 408576698