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

Side by Side Diff: chrome/browser/extensions/api/device_permissions_manager_unittest.cc

Issue 1144493003: Extract //device/usb mocks so that they can be shared. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix Android and GN builds for real. Created 5 years, 7 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 unified diff | Download patch
« no previous file with comments | « no previous file | chrome/chrome_tests_unit.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/run_loop.h" 5 #include "base/run_loop.h"
6 #include "base/strings/utf_string_conversions.h" 6 #include "base/strings/utf_string_conversions.h"
7 #include "base/test/values_test_util.h" 7 #include "base/test/values_test_util.h"
8 #include "chrome/browser/extensions/test_extension_environment.h" 8 #include "chrome/browser/extensions/test_extension_environment.h"
9 #include "chrome/test/base/testing_profile.h" 9 #include "chrome/test/base/testing_profile.h"
10 #include "device/core/device_client.h" 10 #include "device/core/device_client.h"
11 #include "device/hid/hid_device_info.h" 11 #include "device/hid/hid_device_info.h"
12 #include "device/usb/usb_device.h" 12 #include "device/usb/mock_usb_device.h"
13 #include "device/usb/usb_device_handle.h" 13 #include "device/usb/mock_usb_service.h"
14 #include "extensions/browser/api/device_permissions_manager.h" 14 #include "extensions/browser/api/device_permissions_manager.h"
15 #include "extensions/browser/extension_prefs.h" 15 #include "extensions/browser/extension_prefs.h"
16 #include "extensions/common/extension.h" 16 #include "extensions/common/extension.h"
17 #include "testing/gmock/include/gmock/gmock.h" 17 #include "testing/gmock/include/gmock/gmock.h"
18 #include "testing/gtest/include/gtest/gtest.h" 18 #include "testing/gtest/include/gtest/gtest.h"
19 19
20 namespace extensions { 20 namespace extensions {
21 21
22 namespace { 22 namespace {
23 23
24 using device::HidDeviceId; 24 using device::HidDeviceId;
25 using device::HidDeviceInfo; 25 using device::HidDeviceInfo;
26 using device::HidService; 26 using device::HidService;
27 using device::MockUsbDevice;
28 using device::MockUsbService;
27 using device::UsbDevice; 29 using device::UsbDevice;
28 using device::UsbDeviceHandle; 30 using device::UsbDeviceHandle;
29 using device::UsbService; 31 using device::UsbService;
30 using testing::_; 32 using testing::_;
31 using testing::DoAll; 33 using testing::DoAll;
32 using testing::Return; 34 using testing::Return;
33 using testing::SetArgPointee; 35 using testing::SetArgPointee;
34 36
35 #if defined(OS_MACOSX) 37 #if defined(OS_MACOSX)
36 const uint64_t kTestDeviceIds[] = {1, 2, 3, 4}; 38 const uint64_t kTestDeviceIds[] = {1, 2, 3, 4};
37 #else 39 #else
38 const char* kTestDeviceIds[] = {"A", "B", "C", "D"}; 40 const char* kTestDeviceIds[] = {"A", "B", "C", "D"};
39 #endif 41 #endif
40 42
41 int next_id;
42
43 class MockDeviceClient : device::DeviceClient { 43 class MockDeviceClient : device::DeviceClient {
44 public: 44 public:
45 MockDeviceClient() {} 45 MockDeviceClient() {}
46 46
47 // device::DeviceClient implementation: 47 // device::DeviceClient implementation:
48 UsbService* GetUsbService() override { 48 UsbService* GetUsbService() override {
49 DCHECK(usb_service_); 49 DCHECK(usb_service_);
50 return usb_service_; 50 return usb_service_;
51 } 51 }
52 52
53 HidService* GetHidService() override { 53 HidService* GetHidService() override {
54 DCHECK(hid_service_); 54 DCHECK(hid_service_);
55 return hid_service_; 55 return hid_service_;
56 } 56 }
57 57
58 void set_usb_service(UsbService* service) { usb_service_ = service; } 58 void set_usb_service(UsbService* service) { usb_service_ = service; }
59 void set_hid_service(HidService* service) { hid_service_ = service; } 59 void set_hid_service(HidService* service) { hid_service_ = service; }
60 60
61 private: 61 private:
62 UsbService* usb_service_ = nullptr; 62 UsbService* usb_service_ = nullptr;
63 HidService* hid_service_ = nullptr; 63 HidService* hid_service_ = nullptr;
64 }; 64 };
65 65
66 class MockUsbService : public UsbService {
67 public:
68 MockUsbService() {}
69
70 MOCK_METHOD1(GetDeviceById, scoped_refptr<UsbDevice>(uint32));
71 MOCK_METHOD1(GetDevices, void(const GetDevicesCallback& callback));
72
73 // Public wrapper for the protected NotifyDeviceRemove function.
74 void NotifyDeviceRemoved(scoped_refptr<UsbDevice> device) {
75 UsbService::NotifyDeviceRemoved(device);
76 }
77 };
78
79 class MockUsbDevice : public UsbDevice {
80 public:
81 explicit MockUsbDevice(const std::string& serial_number)
82 : UsbDevice(0,
83 0,
84 next_id++,
85 base::ASCIIToUTF16("Test Manufacturer"),
86 base::ASCIIToUTF16("Test Product"),
87 base::ASCIIToUTF16(serial_number)) {}
88
89 MOCK_METHOD1(Open, void(const OpenCallback&));
90 MOCK_METHOD1(Close, bool(scoped_refptr<UsbDeviceHandle>));
91 MOCK_METHOD0(GetConfiguration, const device::UsbConfigDescriptor*());
92
93 private:
94 virtual ~MockUsbDevice() {}
95 };
96
97 class MockHidService : public HidService { 66 class MockHidService : public HidService {
98 public: 67 public:
99 MockHidService() {} 68 MockHidService() {}
100 ~MockHidService() override {} 69 ~MockHidService() override {}
101 70
102 // Public wrappers around protected functions needed for tests. 71 // Public wrappers around protected functions needed for tests.
103 void AddDevice(scoped_refptr<HidDeviceInfo> info) { 72 void AddDevice(scoped_refptr<HidDeviceInfo> info) {
104 HidService::AddDevice(info); 73 HidService::AddDevice(info);
105 } 74 }
106 75
(...skipping 20 matching lines...) Expand all
127 extension_ = 96 extension_ =
128 env_->MakeExtension(*base::test::ParseJson( 97 env_->MakeExtension(*base::test::ParseJson(
129 "{" 98 "{"
130 " \"app\": {" 99 " \"app\": {"
131 " \"background\": {" 100 " \"background\": {"
132 " \"scripts\": [\"background.js\"]" 101 " \"scripts\": [\"background.js\"]"
133 " }" 102 " }"
134 " }," 103 " },"
135 " \"permissions\": [ \"hid\", \"usb\" ]" 104 " \"permissions\": [ \"hid\", \"usb\" ]"
136 "}")); 105 "}"));
137 device0_ = new MockUsbDevice("ABCDE"); 106 device0_ =
138 device1_ = new MockUsbDevice(""); 107 new MockUsbDevice(0, 0, "Test Manufacturer", "Test Product", "ABCDE");
139 device2_ = new MockUsbDevice("12345"); 108 device1_ = new MockUsbDevice(0, 0, "Test Manufacturer", "Test Product", "");
140 device3_ = new MockUsbDevice(""); 109 device2_ =
110 new MockUsbDevice(0, 0, "Test Manufacturer", "Test Product", "12345");
111 device3_ = new MockUsbDevice(0, 0, "Test Manufacturer", "Test Product", "");
141 device4_ = 112 device4_ =
142 new HidDeviceInfo(kTestDeviceIds[0], 0, 0, "Test HID Device", "abcde", 113 new HidDeviceInfo(kTestDeviceIds[0], 0, 0, "Test HID Device", "abcde",
143 device::kHIDBusTypeUSB, std::vector<uint8>()); 114 device::kHIDBusTypeUSB, std::vector<uint8>());
144 hid_service_.AddDevice(device4_); 115 hid_service_.AddDevice(device4_);
145 device5_ = new HidDeviceInfo(kTestDeviceIds[1], 0, 0, "Test HID Device", "", 116 device5_ = new HidDeviceInfo(kTestDeviceIds[1], 0, 0, "Test HID Device", "",
146 device::kHIDBusTypeUSB, std::vector<uint8>()); 117 device::kHIDBusTypeUSB, std::vector<uint8>());
147 hid_service_.AddDevice(device5_); 118 hid_service_.AddDevice(device5_);
148 device6_ = 119 device6_ =
149 new HidDeviceInfo(kTestDeviceIds[2], 0, 0, "Test HID Device", "67890", 120 new HidDeviceInfo(kTestDeviceIds[2], 0, 0, "Test HID Device", "67890",
150 device::kHIDBusTypeUSB, std::vector<uint8>()); 121 device::kHIDBusTypeUSB, std::vector<uint8>());
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
292 manager->GetForExtension(extension_->id()); 263 manager->GetForExtension(extension_->id());
293 EXPECT_TRUE(device_permissions->FindUsbDeviceEntry(device0_).get()); 264 EXPECT_TRUE(device_permissions->FindUsbDeviceEntry(device0_).get());
294 EXPECT_TRUE(device_permissions->FindUsbDeviceEntry(device1_).get()); 265 EXPECT_TRUE(device_permissions->FindUsbDeviceEntry(device1_).get());
295 EXPECT_FALSE(device_permissions->FindUsbDeviceEntry(device2_).get()); 266 EXPECT_FALSE(device_permissions->FindUsbDeviceEntry(device2_).get());
296 EXPECT_FALSE(device_permissions->FindUsbDeviceEntry(device3_).get()); 267 EXPECT_FALSE(device_permissions->FindUsbDeviceEntry(device3_).get());
297 EXPECT_TRUE(device_permissions->FindHidDeviceEntry(device4_).get()); 268 EXPECT_TRUE(device_permissions->FindHidDeviceEntry(device4_).get());
298 EXPECT_TRUE(device_permissions->FindHidDeviceEntry(device5_).get()); 269 EXPECT_TRUE(device_permissions->FindHidDeviceEntry(device5_).get());
299 EXPECT_FALSE(device_permissions->FindHidDeviceEntry(device6_).get()); 270 EXPECT_FALSE(device_permissions->FindHidDeviceEntry(device6_).get());
300 EXPECT_FALSE(device_permissions->FindHidDeviceEntry(device7_).get()); 271 EXPECT_FALSE(device_permissions->FindHidDeviceEntry(device7_).get());
301 272
302 usb_service_.NotifyDeviceRemoved(device0_); 273 usb_service_.RemoveDevice(device0_);
303 usb_service_.NotifyDeviceRemoved(device1_); 274 usb_service_.RemoveDevice(device1_);
304 hid_service_.RemoveDevice(device4_->device_id()); 275 hid_service_.RemoveDevice(device4_->device_id());
305 hid_service_.RemoveDevice(device5_->device_id()); 276 hid_service_.RemoveDevice(device5_->device_id());
306 277
307 // Device 0 will be accessible when it is reconnected because it can be 278 // Device 0 will be accessible when it is reconnected because it can be
308 // recognized by its serial number. 279 // recognized by its serial number.
309 EXPECT_TRUE(device_permissions->FindUsbDeviceEntry(device0_).get()); 280 EXPECT_TRUE(device_permissions->FindUsbDeviceEntry(device0_).get());
310 // Device 1 does not have a serial number and cannot be distinguished from 281 // Device 1 does not have a serial number and cannot be distinguished from
311 // any other device of the same model so the app must request permission again 282 // any other device of the same model so the app must request permission again
312 // when it is reconnected. 283 // when it is reconnected.
313 EXPECT_FALSE(device_permissions->FindUsbDeviceEntry(device1_).get()); 284 EXPECT_FALSE(device_permissions->FindUsbDeviceEntry(device1_).get());
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after
504 EXPECT_EQ(base::ASCIIToUTF16("Widget"), 475 EXPECT_EQ(base::ASCIIToUTF16("Widget"),
505 DevicePermissionsManager::GetPermissionMessage( 476 DevicePermissionsManager::GetPermissionMessage(
506 0x0001, 0x0000, manufacturer, product, empty, false)); 477 0x0001, 0x0000, manufacturer, product, empty, false));
507 478
508 EXPECT_EQ(base::ASCIIToUTF16("Widget (serial number A)"), 479 EXPECT_EQ(base::ASCIIToUTF16("Widget (serial number A)"),
509 DevicePermissionsManager::GetPermissionMessage( 480 DevicePermissionsManager::GetPermissionMessage(
510 0x0001, 0x0000, manufacturer, product, serial_number, false)); 481 0x0001, 0x0000, manufacturer, product, serial_number, false));
511 } 482 }
512 483
513 } // namespace extensions 484 } // namespace extensions
OLDNEW
« no previous file with comments | « no previous file | chrome/chrome_tests_unit.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698