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 // These tests make sure MediaGalleriesPermission values are parsed correctly.
7 #include "base/values.h"
8 #include "extensions/common/permissions/api_permission.h"
9 #include "extensions/common/permissions/media_galleries_permission.h"
10 #include "extensions/common/permissions/media_galleries_permission_data.h"
11 #include "extensions/common/permissions/permissions_info.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 using content::SocketPermissionRequest
;
15 using extensions::SocketPermissionData
;
17 namespace extensions
{
21 void CheckFromValue(APIPermission
* permission
, base::ListValue
* value
,
22 bool success_expected
) {
24 std::vector
<std::string
> unhandled
;
25 EXPECT_EQ(success_expected
, permission
->FromValue(value
, &error
, &unhandled
));
26 EXPECT_EQ(success_expected
, error
.empty());
27 EXPECT_TRUE(unhandled
.empty());
30 TEST(MediaGalleriesPermissionTest
, GoodValues
) {
31 const APIPermissionInfo
* permission_info
=
32 PermissionsInfo::GetInstance()->GetByID(APIPermission::kMediaGalleries
);
34 scoped_ptr
<APIPermission
> permission(
35 permission_info
->CreateAPIPermission());
37 // access_type + all_detected
38 scoped_ptr
<base::ListValue
> value(new base::ListValue());
39 value
->AppendString(MediaGalleriesPermission::kAllAutoDetectedPermission
);
40 value
->AppendString(MediaGalleriesPermission::kReadPermission
);
41 CheckFromValue(permission
.get(), value
.get(), true);
43 value
.reset(new base::ListValue());
44 value
->AppendString(MediaGalleriesPermission::kAllAutoDetectedPermission
);
45 value
->AppendString(MediaGalleriesPermission::kCopyToPermission
);
46 value
->AppendString(MediaGalleriesPermission::kReadPermission
);
47 value
->AppendString(MediaGalleriesPermission::kDeletePermission
);
48 CheckFromValue(permission
.get(), value
.get(), true);
51 value
.reset(new base::ListValue());
52 value
->AppendString(MediaGalleriesPermission::kAllAutoDetectedPermission
);
53 CheckFromValue(permission
.get(), value
.get(), true);
56 value
.reset(new base::ListValue());
57 value
->AppendString(MediaGalleriesPermission::kReadPermission
);
58 CheckFromValue(permission
.get(), value
.get(), true);
60 value
.reset(new base::ListValue());
61 value
->AppendString(MediaGalleriesPermission::kDeletePermission
);
62 value
->AppendString(MediaGalleriesPermission::kReadPermission
);
63 CheckFromValue(permission
.get(), value
.get(), true);
65 value
.reset(new base::ListValue());
66 value
->AppendString(MediaGalleriesPermission::kCopyToPermission
);
67 value
->AppendString(MediaGalleriesPermission::kDeletePermission
);
68 value
->AppendString(MediaGalleriesPermission::kReadPermission
);
69 CheckFromValue(permission
.get(), value
.get(), true);
71 // Repeats do not make a difference.
72 value
.reset(new base::ListValue());
73 value
->AppendString(MediaGalleriesPermission::kAllAutoDetectedPermission
);
74 value
->AppendString(MediaGalleriesPermission::kAllAutoDetectedPermission
);
75 CheckFromValue(permission
.get(), value
.get(), true);
77 value
.reset(new base::ListValue());
78 value
->AppendString(MediaGalleriesPermission::kAllAutoDetectedPermission
);
79 value
->AppendString(MediaGalleriesPermission::kReadPermission
);
80 value
->AppendString(MediaGalleriesPermission::kReadPermission
);
81 value
->AppendString(MediaGalleriesPermission::kDeletePermission
);
82 value
->AppendString(MediaGalleriesPermission::kDeletePermission
);
83 value
->AppendString(MediaGalleriesPermission::kDeletePermission
);
84 CheckFromValue(permission
.get(), value
.get(), true);
87 TEST(MediaGalleriesPermissionTest
, BadValues
) {
88 const APIPermissionInfo
* permission_info
=
89 PermissionsInfo::GetInstance()->GetByID(APIPermission::kMediaGalleries
);
91 scoped_ptr
<APIPermission
> permission(permission_info
->CreateAPIPermission());
94 scoped_ptr
<base::ListValue
> value(new base::ListValue());
95 CheckFromValue(permission
.get(), value
.get(), false);
97 // copyTo and delete without read
98 value
.reset(new base::ListValue());
99 value
->AppendString(MediaGalleriesPermission::kCopyToPermission
);
100 CheckFromValue(permission
.get(), value
.get(), false);
102 value
.reset(new base::ListValue());
103 value
->AppendString(MediaGalleriesPermission::kDeletePermission
);
104 CheckFromValue(permission
.get(), value
.get(), false);
106 value
.reset(new base::ListValue());
107 value
->AppendString(MediaGalleriesPermission::kAllAutoDetectedPermission
);
108 value
->AppendString(MediaGalleriesPermission::kCopyToPermission
);
109 value
->AppendString(MediaGalleriesPermission::kDeletePermission
);
110 CheckFromValue(permission
.get(), value
.get(), false);
112 // copyTo without delete
113 value
.reset(new base::ListValue());
114 value
->AppendString(MediaGalleriesPermission::kAllAutoDetectedPermission
);
115 value
->AppendString(MediaGalleriesPermission::kCopyToPermission
);
116 value
->AppendString(MediaGalleriesPermission::kReadPermission
);
117 CheckFromValue(permission
.get(), value
.get(), false);
119 // Repeats do not make a difference.
120 value
.reset(new base::ListValue());
121 value
->AppendString(MediaGalleriesPermission::kCopyToPermission
);
122 value
->AppendString(MediaGalleriesPermission::kCopyToPermission
);
123 CheckFromValue(permission
.get(), value
.get(), false);
125 value
.reset(new base::ListValue());
126 value
->AppendString(MediaGalleriesPermission::kAllAutoDetectedPermission
);
127 value
->AppendString(MediaGalleriesPermission::kAllAutoDetectedPermission
);
128 value
->AppendString(MediaGalleriesPermission::kCopyToPermission
);
129 value
->AppendString(MediaGalleriesPermission::kDeletePermission
);
130 value
->AppendString(MediaGalleriesPermission::kDeletePermission
);
131 CheckFromValue(permission
.get(), value
.get(), false);
134 TEST(MediaGalleriesPermissionTest
, UnknownValues
) {
136 std::vector
<std::string
> unhandled
;
137 const APIPermissionInfo
* permission_info
=
138 PermissionsInfo::GetInstance()->GetByID(APIPermission::kMediaGalleries
);
140 scoped_ptr
<APIPermission
> permission(permission_info
->CreateAPIPermission());
142 // A good one and an unknown one.
143 scoped_ptr
<base::ListValue
> value(new base::ListValue());
144 value
->AppendString(MediaGalleriesPermission::kReadPermission
);
145 value
->AppendString("Unknown");
146 EXPECT_TRUE(permission
->FromValue(value
.get(), &error
, &unhandled
));
147 EXPECT_TRUE(error
.empty());
148 EXPECT_EQ(1U, unhandled
.size());
152 // Multiple unknown permissions.
153 value
.reset(new base::ListValue());
154 value
->AppendString("Unknown1");
155 value
->AppendString("Unknown2");
156 EXPECT_TRUE(permission
->FromValue(value
.get(), &error
, &unhandled
));
157 EXPECT_TRUE(error
.empty());
158 EXPECT_EQ(2U, unhandled
.size());
162 // Unnknown with a NULL argument.
163 value
.reset(new base::ListValue());
164 value
->AppendString("Unknown1");
165 EXPECT_FALSE(permission
->FromValue(value
.get(), &error
, NULL
));
166 EXPECT_FALSE(error
.empty());
170 TEST(MediaGalleriesPermissionTest
, Equal
) {
171 const APIPermissionInfo
* permission_info
=
172 PermissionsInfo::GetInstance()->GetByID(APIPermission::kMediaGalleries
);
174 scoped_ptr
<APIPermission
> permission1(
175 permission_info
->CreateAPIPermission());
176 scoped_ptr
<APIPermission
> permission2(
177 permission_info
->CreateAPIPermission());
179 scoped_ptr
<base::ListValue
> value(new base::ListValue());
180 value
->AppendString(MediaGalleriesPermission::kAllAutoDetectedPermission
);
181 value
->AppendString(MediaGalleriesPermission::kReadPermission
);
182 ASSERT_TRUE(permission1
->FromValue(value
.get(), NULL
, NULL
));
184 value
.reset(new base::ListValue());
185 value
->AppendString(MediaGalleriesPermission::kReadPermission
);
186 value
->AppendString(MediaGalleriesPermission::kAllAutoDetectedPermission
);
187 ASSERT_TRUE(permission2
->FromValue(value
.get(), NULL
, NULL
));
188 EXPECT_TRUE(permission1
->Equal(permission2
.get()));
190 value
.reset(new base::ListValue());
191 value
->AppendString(MediaGalleriesPermission::kReadPermission
);
192 value
->AppendString(MediaGalleriesPermission::kReadPermission
);
193 value
->AppendString(MediaGalleriesPermission::kAllAutoDetectedPermission
);
194 ASSERT_TRUE(permission2
->FromValue(value
.get(), NULL
, NULL
));
195 EXPECT_TRUE(permission1
->Equal(permission2
.get()));
197 value
.reset(new base::ListValue());
198 value
->AppendString(MediaGalleriesPermission::kReadPermission
);
199 value
->AppendString(MediaGalleriesPermission::kDeletePermission
);
200 ASSERT_TRUE(permission1
->FromValue(value
.get(), NULL
, NULL
));
202 value
.reset(new base::ListValue());
203 value
->AppendString(MediaGalleriesPermission::kDeletePermission
);
204 value
->AppendString(MediaGalleriesPermission::kReadPermission
);
205 ASSERT_TRUE(permission2
->FromValue(value
.get(), NULL
, NULL
));
206 EXPECT_TRUE(permission1
->Equal(permission2
.get()));
208 value
.reset(new base::ListValue());
209 value
->AppendString(MediaGalleriesPermission::kReadPermission
);
210 value
->AppendString(MediaGalleriesPermission::kCopyToPermission
);
211 value
->AppendString(MediaGalleriesPermission::kDeletePermission
);
212 ASSERT_TRUE(permission1
->FromValue(value
.get(), NULL
, NULL
));
214 value
.reset(new base::ListValue());
215 value
->AppendString(MediaGalleriesPermission::kDeletePermission
);
216 value
->AppendString(MediaGalleriesPermission::kCopyToPermission
);
217 value
->AppendString(MediaGalleriesPermission::kReadPermission
);
218 ASSERT_TRUE(permission2
->FromValue(value
.get(), NULL
, NULL
));
219 EXPECT_TRUE(permission1
->Equal(permission2
.get()));
222 TEST(MediaGalleriesPermissionTest
, NotEqual
) {
223 const APIPermissionInfo
* permission_info
=
224 PermissionsInfo::GetInstance()->GetByID(APIPermission::kMediaGalleries
);
226 scoped_ptr
<APIPermission
> permission1(
227 permission_info
->CreateAPIPermission());
228 scoped_ptr
<APIPermission
> permission2(
229 permission_info
->CreateAPIPermission());
231 scoped_ptr
<base::ListValue
> value(new base::ListValue());
232 value
->AppendString(MediaGalleriesPermission::kAllAutoDetectedPermission
);
233 value
->AppendString(MediaGalleriesPermission::kReadPermission
);
234 ASSERT_TRUE(permission1
->FromValue(value
.get(), NULL
, NULL
));
236 value
.reset(new base::ListValue());
237 value
->AppendString(MediaGalleriesPermission::kAllAutoDetectedPermission
);
238 value
->AppendString(MediaGalleriesPermission::kReadPermission
);
239 value
->AppendString(MediaGalleriesPermission::kDeletePermission
);
240 value
->AppendString(MediaGalleriesPermission::kCopyToPermission
);
241 ASSERT_TRUE(permission2
->FromValue(value
.get(), NULL
, NULL
));
242 EXPECT_FALSE(permission1
->Equal(permission2
.get()));
245 TEST(MediaGalleriesPermissionTest
, ToFromValue
) {
246 const APIPermissionInfo
* permission_info
=
247 PermissionsInfo::GetInstance()->GetByID(APIPermission::kMediaGalleries
);
249 scoped_ptr
<APIPermission
> permission1(
250 permission_info
->CreateAPIPermission());
251 scoped_ptr
<APIPermission
> permission2(
252 permission_info
->CreateAPIPermission());
254 scoped_ptr
<base::ListValue
> value(new base::ListValue());
255 value
->AppendString(MediaGalleriesPermission::kAllAutoDetectedPermission
);
256 value
->AppendString(MediaGalleriesPermission::kReadPermission
);
257 ASSERT_TRUE(permission1
->FromValue(value
.get(), NULL
, NULL
));
259 scoped_ptr
<base::Value
> vtmp(permission1
->ToValue());
261 ASSERT_TRUE(permission2
->FromValue(vtmp
.get(), NULL
, NULL
));
262 EXPECT_TRUE(permission1
->Equal(permission2
.get()));
264 value
.reset(new base::ListValue());
265 value
->AppendString(MediaGalleriesPermission::kReadPermission
);
266 value
->AppendString(MediaGalleriesPermission::kDeletePermission
);
267 value
->AppendString(MediaGalleriesPermission::kCopyToPermission
);
268 ASSERT_TRUE(permission1
->FromValue(value
.get(), NULL
, NULL
));
270 vtmp
= permission1
->ToValue();
272 ASSERT_TRUE(permission2
->FromValue(vtmp
.get(), NULL
, NULL
));
273 EXPECT_TRUE(permission1
->Equal(permission2
.get()));
275 value
.reset(new base::ListValue());
276 value
->AppendString(MediaGalleriesPermission::kReadPermission
);
277 value
->AppendString(MediaGalleriesPermission::kDeletePermission
);
278 ASSERT_TRUE(permission1
->FromValue(value
.get(), NULL
, NULL
));
280 vtmp
= permission1
->ToValue();
282 ASSERT_TRUE(permission2
->FromValue(vtmp
.get(), NULL
, NULL
));
283 EXPECT_TRUE(permission1
->Equal(permission2
.get()));
288 } // namespace extensions