Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / extensions / common / permissions / usb_device_permission_unittest.cc
blob345a0003b9e5393fdc15a16553c324f63ce1607f
1 // Copyright (c) 2013 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.
5 #include <set>
6 #include <string>
8 #include "base/memory/scoped_ptr.h"
9 #include "base/pickle.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "base/values.h"
12 #include "extensions/common/permissions/api_permission.h"
13 #include "extensions/common/permissions/permissions_info.h"
14 #include "extensions/common/permissions/usb_device_permission.h"
15 #include "extensions/common/permissions/usb_device_permission_data.h"
16 #include "ipc/ipc_message.h"
17 #include "testing/gtest/include/gtest/gtest.h"
19 namespace extensions {
21 TEST(USBDevicePermissionTest, PermissionDataOrder) {
22 EXPECT_LT(UsbDevicePermissionData(0x02ad, 0x138c, -1),
23 UsbDevicePermissionData(0x02ad, 0x138d, -1));
24 ASSERT_LT(UsbDevicePermissionData(0x02ad, 0x138d, -1),
25 UsbDevicePermissionData(0x02ae, 0x138c, -1));
26 EXPECT_LT(UsbDevicePermissionData(0x02ad, 0x138c, -1),
27 UsbDevicePermissionData(0x02ad, 0x138c, 0));
30 TEST(USBDevicePermissionTest, SingleDevicePermissionMessages) {
31 const char* const kMessages[] = {
32 "Access any PVR Mass Storage from HUMAX Co., Ltd. via USB",
33 "Access USB devices from HUMAX Co., Ltd.",
34 "Access USB devices from an unknown vendor",
38 scoped_ptr<base::ListValue> permission_list(new base::ListValue());
39 permission_list->Append(
40 UsbDevicePermissionData(0x02ad, 0x138c, -1).ToValue().release());
42 UsbDevicePermission permission(
43 PermissionsInfo::GetInstance()->GetByID(APIPermission::kUsbDevice));
44 ASSERT_TRUE(permission.FromValue(permission_list.get(), NULL, NULL));
46 PermissionMessages messages = permission.GetMessages();
47 ASSERT_EQ(1U, messages.size());
48 EXPECT_EQ(base::ASCIIToUTF16(kMessages[0]), messages.at(0).message());
51 scoped_ptr<base::ListValue> permission_list(new base::ListValue());
52 permission_list->Append(
53 UsbDevicePermissionData(0x02ad, 0x138d, -1).ToValue().release());
55 UsbDevicePermission permission(
56 PermissionsInfo::GetInstance()->GetByID(APIPermission::kUsbDevice));
57 ASSERT_TRUE(permission.FromValue(permission_list.get(), NULL, NULL));
59 PermissionMessages messages = permission.GetMessages();
60 ASSERT_EQ(1U, messages.size());
61 EXPECT_EQ(base::ASCIIToUTF16(kMessages[1]), messages.at(0).message());
64 scoped_ptr<base::ListValue> permission_list(new base::ListValue());
65 permission_list->Append(
66 UsbDevicePermissionData(0x02ae, 0x138d, -1).ToValue().release());
68 UsbDevicePermission permission(
69 PermissionsInfo::GetInstance()->GetByID(APIPermission::kUsbDevice));
70 ASSERT_TRUE(permission.FromValue(permission_list.get(), NULL, NULL));
72 PermissionMessages messages = permission.GetMessages();
73 ASSERT_EQ(1U, messages.size());
74 EXPECT_EQ(base::ASCIIToUTF16(kMessages[2]), messages.at(0).message());
78 TEST(USBDevicePermissionTest, MultipleDevicePermissionMessage) {
79 const char* const kMessage = "Access any of these USB devices";
80 const char* const kDetails =
81 "PVR Mass Storage from HUMAX Co., Ltd.\n"
82 "unknown devices from HUMAX Co., Ltd.\n"
83 "devices from an unknown vendor";
85 // Prepare data set
86 scoped_ptr<base::ListValue> permission_list(new base::ListValue());
87 permission_list->Append(
88 UsbDevicePermissionData(0x02ad, 0x138c, -1).ToValue().release());
89 // This device's product ID is not in Chrome's database.
90 permission_list->Append(
91 UsbDevicePermissionData(0x02ad, 0x138d, -1).ToValue().release());
92 // This additional unknown product will be collapsed into the entry above.
93 permission_list->Append(
94 UsbDevicePermissionData(0x02ad, 0x138e, -1).ToValue().release());
95 // This device's vendor ID is not in Chrome's database.
96 permission_list->Append(
97 UsbDevicePermissionData(0x02ae, 0x138d, -1).ToValue().release());
98 // This additional unknown vendor will be collapsed into the entry above.
99 permission_list->Append(
100 UsbDevicePermissionData(0x02af, 0x138d, -1).ToValue().release());
102 UsbDevicePermission permission(
103 PermissionsInfo::GetInstance()->GetByID(APIPermission::kUsbDevice));
104 ASSERT_TRUE(permission.FromValue(permission_list.get(), NULL, NULL));
106 PermissionMessages messages = permission.GetMessages();
107 ASSERT_EQ(1U, messages.size());
108 EXPECT_EQ(base::ASCIIToUTF16(kMessage), messages.at(0).message());
109 EXPECT_EQ(base::ASCIIToUTF16(kDetails), messages.at(0).details());
112 } // namespace extensions