OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/usb/usb_chooser_permission_context.h" | |
6 #include "chrome/browser/usb/usb_chooser_permission_context_factory.h" | |
7 #include "chrome/test/base/testing_profile.h" | |
8 #include "content/public/test/test_browser_thread_bundle.h" | |
9 #include "device/core/device_client.h" | |
10 #include "device/usb/mock_usb_device.h" | |
11 #include "device/usb/mock_usb_service.h" | |
12 | |
13 using device::MockUsbDevice; | |
14 using device::UsbDevice; | |
15 | |
16 namespace { | |
17 | |
18 class TestDeviceClient : public device::DeviceClient { | |
19 public: | |
20 TestDeviceClient() {} | |
21 ~TestDeviceClient() override {} | |
22 | |
23 device::MockUsbService& usb_service() { return usb_service_; } | |
24 | |
25 private: | |
26 device::UsbService* GetUsbService() override { return &usb_service_; } | |
27 | |
28 device::MockUsbService usb_service_; | |
29 }; | |
30 | |
31 } // namespace | |
32 | |
33 class UsbChooserPermissionContextTest : public testing::Test { | |
34 public: | |
35 UsbChooserPermissionContextTest() {} | |
36 ~UsbChooserPermissionContextTest() override {} | |
37 | |
38 protected: | |
39 Profile* profile() { return &profile_; } | |
40 device::MockUsbService& usb_service() { return device_client_.usb_service(); } | |
41 | |
42 private: | |
43 content::TestBrowserThreadBundle thread_bundle_; | |
44 TestDeviceClient device_client_; | |
45 TestingProfile profile_; | |
46 }; | |
47 | |
48 TEST_F(UsbChooserPermissionContextTest, CheckGrantAndRevokePermission) { | |
49 GURL origin("https://www.google.com"); | |
50 scoped_refptr<UsbDevice> device = | |
51 new MockUsbDevice(0, 0, "Google", "Gizmo", "123ABC"); | |
52 usb_service().AddDevice(device); | |
53 UsbChooserPermissionContext* store = | |
54 UsbChooserPermissionContextFactory::GetForProfile(profile()); | |
55 | |
56 EXPECT_FALSE(store->HasDevicePermission(origin, origin, device->guid())); | |
57 store->GrantDevicePermission(origin, origin, device->guid()); | |
58 EXPECT_TRUE(store->HasDevicePermission(origin, origin, device->guid())); | |
59 store->RevokeDevicePermission(origin, origin, device->guid()); | |
60 EXPECT_FALSE(store->HasDevicePermission(origin, origin, device->guid())); | |
61 } | |
62 | |
63 TEST_F(UsbChooserPermissionContextTest, | |
64 CheckGrantAndRevokeEphemeralPermission) { | |
65 GURL origin("https://www.google.com"); | |
66 scoped_refptr<UsbDevice> device = | |
67 new MockUsbDevice(0, 0, "Google", "Gizmo", ""); | |
68 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.
| |
69 new MockUsbDevice(0, 0, "Google", "Gizmo", ""); | |
70 usb_service().AddDevice(device); | |
71 UsbChooserPermissionContext* store = | |
72 UsbChooserPermissionContextFactory::GetForProfile(profile()); | |
73 | |
74 EXPECT_FALSE(store->HasDevicePermission(origin, origin, device->guid())); | |
75 store->GrantDevicePermission(origin, origin, device->guid()); | |
76 EXPECT_TRUE(store->HasDevicePermission(origin, origin, device->guid())); | |
77 store->RevokeDevicePermission(origin, origin, device->guid()); | |
78 EXPECT_FALSE(store->HasDevicePermission(origin, origin, device->guid())); | |
79 } | |
80 | |
81 TEST_F(UsbChooserPermissionContextTest, DisconnectDeviceWithPermission) { | |
82 GURL origin("https://www.google.com"); | |
83 scoped_refptr<UsbDevice> device = | |
84 new MockUsbDevice(0, 0, "Google", "Gizmo", "123ABC"); | |
85 usb_service().AddDevice(device); | |
86 UsbChooserPermissionContext* store = | |
87 UsbChooserPermissionContextFactory::GetForProfile(profile()); | |
88 | |
89 EXPECT_FALSE(store->HasDevicePermission(origin, origin, device->guid())); | |
90 store->GrantDevicePermission(origin, origin, device->guid()); | |
91 EXPECT_TRUE(store->HasDevicePermission(origin, origin, device->guid())); | |
92 usb_service().RemoveDevice(device); | |
93 EXPECT_FALSE(store->HasDevicePermission(origin, origin, device->guid())); | |
94 | |
95 scoped_refptr<UsbDevice> reconnected_device = | |
96 new MockUsbDevice(0, 0, "Google", "Gizmo", "123ABC"); | |
97 usb_service().AddDevice(reconnected_device); | |
98 EXPECT_TRUE( | |
99 store->HasDevicePermission(origin, origin, reconnected_device->guid())); | |
100 } | |
101 | |
102 TEST_F(UsbChooserPermissionContextTest, | |
103 DisconnectDeviceWithEphemeralPermission) { | |
104 GURL origin("https://www.google.com"); | |
105 scoped_refptr<UsbDevice> device = | |
106 new MockUsbDevice(0, 0, "Google", "Gizmo", ""); | |
107 usb_service().AddDevice(device); | |
108 UsbChooserPermissionContext* store = | |
109 UsbChooserPermissionContextFactory::GetForProfile(profile()); | |
110 | |
111 EXPECT_FALSE(store->HasDevicePermission(origin, origin, device->guid())); | |
112 store->GrantDevicePermission(origin, origin, device->guid()); | |
113 EXPECT_TRUE(store->HasDevicePermission(origin, origin, device->guid())); | |
114 usb_service().RemoveDevice(device); | |
115 EXPECT_FALSE(store->HasDevicePermission(origin, origin, device->guid())); | |
116 | |
117 scoped_refptr<UsbDevice> reconnected_device = | |
118 new MockUsbDevice(0, 0, "Google", "Gizmo", ""); | |
119 usb_service().AddDevice(reconnected_device); | |
120 EXPECT_FALSE( | |
121 store->HasDevicePermission(origin, origin, reconnected_device->guid())); | |
122 } | |
OLD | NEW |