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 "chrome/common/extensions/permissions/media_galleries_permission.h"
9 #include "chrome/common/extensions/permissions/media_galleries_permission_data.h"
10 #include "extensions/common/permissions/api_permission.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 TEST(MediaGalleriesPermissionTest
, GoodValues
) {
22 const APIPermissionInfo
* permission_info
=
23 PermissionsInfo::GetInstance()->GetByID(APIPermission::kMediaGalleries
);
25 scoped_ptr
<APIPermission
> permission(
26 permission_info
->CreateAPIPermission());
28 // access_type + all_detected
29 scoped_ptr
<base::ListValue
> value(new base::ListValue());
30 value
->AppendString(MediaGalleriesPermission::kAllAutoDetectedPermission
);
31 value
->AppendString(MediaGalleriesPermission::kReadPermission
);
32 EXPECT_TRUE(permission
->FromValue(value
.get()));
34 value
.reset(new base::ListValue());
35 value
->AppendString(MediaGalleriesPermission::kAllAutoDetectedPermission
);
36 value
->AppendString(MediaGalleriesPermission::kCopyToPermission
);
37 value
->AppendString(MediaGalleriesPermission::kReadPermission
);
38 value
->AppendString(MediaGalleriesPermission::kDeletePermission
);
39 EXPECT_TRUE(permission
->FromValue(value
.get()));
42 value
.reset(new base::ListValue());
43 value
->AppendString(MediaGalleriesPermission::kAllAutoDetectedPermission
);
44 EXPECT_TRUE(permission
->FromValue(value
.get()));
47 value
.reset(new base::ListValue());
48 value
->AppendString(MediaGalleriesPermission::kReadPermission
);
49 EXPECT_TRUE(permission
->FromValue(value
.get()));
51 value
.reset(new base::ListValue());
52 value
->AppendString(MediaGalleriesPermission::kDeletePermission
);
53 value
->AppendString(MediaGalleriesPermission::kReadPermission
);
54 EXPECT_TRUE(permission
->FromValue(value
.get()));
56 value
.reset(new base::ListValue());
57 value
->AppendString(MediaGalleriesPermission::kCopyToPermission
);
58 value
->AppendString(MediaGalleriesPermission::kDeletePermission
);
59 value
->AppendString(MediaGalleriesPermission::kReadPermission
);
60 EXPECT_TRUE(permission
->FromValue(value
.get()));
62 // Repeats do not make a difference.
63 value
.reset(new base::ListValue());
64 value
->AppendString(MediaGalleriesPermission::kAllAutoDetectedPermission
);
65 value
->AppendString(MediaGalleriesPermission::kAllAutoDetectedPermission
);
66 EXPECT_TRUE(permission
->FromValue(value
.get()));
68 value
.reset(new base::ListValue());
69 value
->AppendString(MediaGalleriesPermission::kAllAutoDetectedPermission
);
70 value
->AppendString(MediaGalleriesPermission::kReadPermission
);
71 value
->AppendString(MediaGalleriesPermission::kReadPermission
);
72 value
->AppendString(MediaGalleriesPermission::kDeletePermission
);
73 value
->AppendString(MediaGalleriesPermission::kDeletePermission
);
74 value
->AppendString(MediaGalleriesPermission::kDeletePermission
);
75 EXPECT_TRUE(permission
->FromValue(value
.get()));
78 TEST(MediaGalleriesPermissionTest
, BadValues
) {
79 const APIPermissionInfo
* permission_info
=
80 PermissionsInfo::GetInstance()->GetByID(APIPermission::kMediaGalleries
);
82 scoped_ptr
<APIPermission
> permission(permission_info
->CreateAPIPermission());
85 scoped_ptr
<base::ListValue
> value(new base::ListValue());
86 EXPECT_FALSE(permission
->FromValue(value
.get()));
88 // copyTo and delete without read
89 value
.reset(new base::ListValue());
90 value
->AppendString(MediaGalleriesPermission::kCopyToPermission
);
91 EXPECT_FALSE(permission
->FromValue(value
.get()));
93 value
.reset(new base::ListValue());
94 value
->AppendString(MediaGalleriesPermission::kDeletePermission
);
95 EXPECT_FALSE(permission
->FromValue(value
.get()));
97 value
.reset(new base::ListValue());
98 value
->AppendString(MediaGalleriesPermission::kAllAutoDetectedPermission
);
99 value
->AppendString(MediaGalleriesPermission::kCopyToPermission
);
100 value
->AppendString(MediaGalleriesPermission::kDeletePermission
);
101 EXPECT_FALSE(permission
->FromValue(value
.get()));
103 // copyTo without delete
104 value
.reset(new base::ListValue());
105 value
->AppendString(MediaGalleriesPermission::kAllAutoDetectedPermission
);
106 value
->AppendString(MediaGalleriesPermission::kCopyToPermission
);
107 value
->AppendString(MediaGalleriesPermission::kReadPermission
);
108 EXPECT_FALSE(permission
->FromValue(value
.get()));
110 // Repeats do not make a difference.
111 value
.reset(new base::ListValue());
112 value
->AppendString(MediaGalleriesPermission::kCopyToPermission
);
113 value
->AppendString(MediaGalleriesPermission::kCopyToPermission
);
114 EXPECT_FALSE(permission
->FromValue(value
.get()));
116 value
.reset(new base::ListValue());
117 value
->AppendString(MediaGalleriesPermission::kAllAutoDetectedPermission
);
118 value
->AppendString(MediaGalleriesPermission::kAllAutoDetectedPermission
);
119 value
->AppendString(MediaGalleriesPermission::kCopyToPermission
);
120 value
->AppendString(MediaGalleriesPermission::kDeletePermission
);
121 value
->AppendString(MediaGalleriesPermission::kDeletePermission
);
122 EXPECT_FALSE(permission
->FromValue(value
.get()));
125 TEST(MediaGalleriesPermissionTest
, Equal
) {
126 const APIPermissionInfo
* permission_info
=
127 PermissionsInfo::GetInstance()->GetByID(APIPermission::kMediaGalleries
);
129 scoped_ptr
<APIPermission
> permission1(
130 permission_info
->CreateAPIPermission());
131 scoped_ptr
<APIPermission
> permission2(
132 permission_info
->CreateAPIPermission());
134 scoped_ptr
<base::ListValue
> value(new base::ListValue());
135 value
->AppendString(MediaGalleriesPermission::kAllAutoDetectedPermission
);
136 value
->AppendString(MediaGalleriesPermission::kReadPermission
);
137 ASSERT_TRUE(permission1
->FromValue(value
.get()));
139 value
.reset(new base::ListValue());
140 value
->AppendString(MediaGalleriesPermission::kReadPermission
);
141 value
->AppendString(MediaGalleriesPermission::kAllAutoDetectedPermission
);
142 ASSERT_TRUE(permission2
->FromValue(value
.get()));
143 EXPECT_TRUE(permission1
->Equal(permission2
.get()));
145 value
.reset(new base::ListValue());
146 value
->AppendString(MediaGalleriesPermission::kReadPermission
);
147 value
->AppendString(MediaGalleriesPermission::kReadPermission
);
148 value
->AppendString(MediaGalleriesPermission::kAllAutoDetectedPermission
);
149 ASSERT_TRUE(permission2
->FromValue(value
.get()));
150 EXPECT_TRUE(permission1
->Equal(permission2
.get()));
152 value
.reset(new base::ListValue());
153 value
->AppendString(MediaGalleriesPermission::kReadPermission
);
154 value
->AppendString(MediaGalleriesPermission::kDeletePermission
);
155 ASSERT_TRUE(permission1
->FromValue(value
.get()));
157 value
.reset(new base::ListValue());
158 value
->AppendString(MediaGalleriesPermission::kDeletePermission
);
159 value
->AppendString(MediaGalleriesPermission::kReadPermission
);
160 ASSERT_TRUE(permission2
->FromValue(value
.get()));
161 EXPECT_TRUE(permission1
->Equal(permission2
.get()));
163 value
.reset(new base::ListValue());
164 value
->AppendString(MediaGalleriesPermission::kReadPermission
);
165 value
->AppendString(MediaGalleriesPermission::kCopyToPermission
);
166 value
->AppendString(MediaGalleriesPermission::kDeletePermission
);
167 ASSERT_TRUE(permission1
->FromValue(value
.get()));
169 value
.reset(new base::ListValue());
170 value
->AppendString(MediaGalleriesPermission::kDeletePermission
);
171 value
->AppendString(MediaGalleriesPermission::kCopyToPermission
);
172 value
->AppendString(MediaGalleriesPermission::kReadPermission
);
173 ASSERT_TRUE(permission2
->FromValue(value
.get()));
174 EXPECT_TRUE(permission1
->Equal(permission2
.get()));
177 TEST(MediaGalleriesPermissionTest
, NotEqual
) {
178 const APIPermissionInfo
* permission_info
=
179 PermissionsInfo::GetInstance()->GetByID(APIPermission::kMediaGalleries
);
181 scoped_ptr
<APIPermission
> permission1(
182 permission_info
->CreateAPIPermission());
183 scoped_ptr
<APIPermission
> permission2(
184 permission_info
->CreateAPIPermission());
186 scoped_ptr
<base::ListValue
> value(new base::ListValue());
187 value
->AppendString(MediaGalleriesPermission::kAllAutoDetectedPermission
);
188 value
->AppendString(MediaGalleriesPermission::kReadPermission
);
189 ASSERT_TRUE(permission1
->FromValue(value
.get()));
191 value
.reset(new base::ListValue());
192 value
->AppendString(MediaGalleriesPermission::kAllAutoDetectedPermission
);
193 value
->AppendString(MediaGalleriesPermission::kReadPermission
);
194 value
->AppendString(MediaGalleriesPermission::kDeletePermission
);
195 value
->AppendString(MediaGalleriesPermission::kCopyToPermission
);
196 ASSERT_TRUE(permission2
->FromValue(value
.get()));
197 EXPECT_FALSE(permission1
->Equal(permission2
.get()));
200 TEST(MediaGalleriesPermissionTest
, ToFromValue
) {
201 const APIPermissionInfo
* permission_info
=
202 PermissionsInfo::GetInstance()->GetByID(APIPermission::kMediaGalleries
);
204 scoped_ptr
<APIPermission
> permission1(
205 permission_info
->CreateAPIPermission());
206 scoped_ptr
<APIPermission
> permission2(
207 permission_info
->CreateAPIPermission());
209 scoped_ptr
<base::ListValue
> value(new base::ListValue());
210 value
->AppendString(MediaGalleriesPermission::kAllAutoDetectedPermission
);
211 value
->AppendString(MediaGalleriesPermission::kReadPermission
);
212 ASSERT_TRUE(permission1
->FromValue(value
.get()));
214 scoped_ptr
<base::Value
> vtmp(permission1
->ToValue());
216 ASSERT_TRUE(permission2
->FromValue(vtmp
.get()));
217 EXPECT_TRUE(permission1
->Equal(permission2
.get()));
219 value
.reset(new base::ListValue());
220 value
->AppendString(MediaGalleriesPermission::kReadPermission
);
221 value
->AppendString(MediaGalleriesPermission::kDeletePermission
);
222 value
->AppendString(MediaGalleriesPermission::kCopyToPermission
);
223 ASSERT_TRUE(permission1
->FromValue(value
.get()));
225 vtmp
= permission1
->ToValue();
227 ASSERT_TRUE(permission2
->FromValue(vtmp
.get()));
228 EXPECT_TRUE(permission1
->Equal(permission2
.get()));
230 value
.reset(new base::ListValue());
231 value
->AppendString(MediaGalleriesPermission::kReadPermission
);
232 value
->AppendString(MediaGalleriesPermission::kDeletePermission
);
233 ASSERT_TRUE(permission1
->FromValue(value
.get()));
235 vtmp
= permission1
->ToValue();
237 ASSERT_TRUE(permission2
->FromValue(vtmp
.get()));
238 EXPECT_TRUE(permission1
->Equal(permission2
.get()));
243 } // namespace extensions