1 // Copyright (c) 2012 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 "chrome/browser/ui/webui/options/pepper_flash_content_settings_utils.h"
7 #include "base/basictypes.h"
8 #include "testing/gtest/include/gtest/gtest.h"
10 using options::MediaException
;
11 using options::MediaExceptions
;
12 using options::PepperFlashContentSettingsUtils
;
16 MediaExceptions
ConvertAndSort(const MediaException
* items
, size_t count
) {
17 MediaExceptions
result(items
, items
+ count
);
18 PepperFlashContentSettingsUtils::SortMediaExceptions(&result
);
24 TEST(PepperFlashContentSettingsUtilsTest
, SortMediaExceptions
) {
25 MediaException
entry_1(ContentSettingsPattern::FromString("www.google.com"),
26 CONTENT_SETTING_ALLOW
, CONTENT_SETTING_ASK
);
27 MediaException
entry_2(ContentSettingsPattern::FromString("www.youtube.com"),
28 CONTENT_SETTING_BLOCK
, CONTENT_SETTING_DEFAULT
);
29 MediaException
entry_3(ContentSettingsPattern::Wildcard(),
30 CONTENT_SETTING_ASK
, CONTENT_SETTING_BLOCK
);
31 MediaException
entry_4(ContentSettingsPattern(),
32 CONTENT_SETTING_SESSION_ONLY
, CONTENT_SETTING_ALLOW
);
34 MediaExceptions list_1
;
35 list_1
.push_back(entry_1
);
36 list_1
.push_back(entry_2
);
37 list_1
.push_back(entry_3
);
38 list_1
.push_back(entry_4
);
40 MediaExceptions list_2
;
41 list_2
.push_back(entry_1
);
42 list_2
.push_back(entry_3
);
43 list_2
.push_back(entry_2
);
44 list_2
.push_back(entry_4
);
46 MediaExceptions list_3
;
47 list_3
.push_back(entry_4
);
48 list_3
.push_back(entry_1
);
49 list_3
.push_back(entry_2
);
50 list_3
.push_back(entry_3
);
52 EXPECT_NE(list_1
, list_2
);
53 EXPECT_NE(list_2
, list_3
);
54 EXPECT_NE(list_3
, list_1
);
56 PepperFlashContentSettingsUtils::SortMediaExceptions(&list_1
);
57 PepperFlashContentSettingsUtils::SortMediaExceptions(&list_2
);
58 PepperFlashContentSettingsUtils::SortMediaExceptions(&list_3
);
60 EXPECT_EQ(list_1
, list_2
);
61 EXPECT_EQ(list_2
, list_3
);
64 TEST(PepperFlashContentSettingsUtilsTest
, AreMediaExceptionsEqual
) {
66 // Empty lists are equal.
67 // Default settings are not compared directly, so it is possible to return
68 // true when they are different.
69 EXPECT_TRUE(PepperFlashContentSettingsUtils::AreMediaExceptionsEqual(
70 CONTENT_SETTING_BLOCK
,
73 CONTENT_SETTING_BLOCK
,
81 MediaException exceptions_1
[] = {
82 MediaException(ContentSettingsPattern::FromString("www.google.com"),
83 CONTENT_SETTING_ALLOW
, CONTENT_SETTING_ALLOW
),
84 MediaException(ContentSettingsPattern::FromString("www.youtube.com"),
85 CONTENT_SETTING_ASK
, CONTENT_SETTING_ASK
)
88 MediaException exceptions_2
[] = {
89 MediaException(ContentSettingsPattern::FromString("www.google.com"),
90 CONTENT_SETTING_ALLOW
, CONTENT_SETTING_ALLOW
)
93 // The exception of "www.youtube.com" in |exceptions_1| should not affect
94 // the result, because it has the same settings as |default_setting_2|.
95 EXPECT_TRUE(PepperFlashContentSettingsUtils::AreMediaExceptionsEqual(
96 CONTENT_SETTING_ALLOW
,
97 CONTENT_SETTING_ALLOW
,
98 ConvertAndSort(exceptions_1
, arraysize(exceptions_1
)),
101 ConvertAndSort(exceptions_2
, arraysize(exceptions_2
)),
104 EXPECT_TRUE(PepperFlashContentSettingsUtils::AreMediaExceptionsEqual(
107 ConvertAndSort(exceptions_2
, arraysize(exceptions_2
)),
108 CONTENT_SETTING_ALLOW
,
109 CONTENT_SETTING_ALLOW
,
110 ConvertAndSort(exceptions_1
, arraysize(exceptions_1
)),
113 // Changing |default_setting_2| should change the result.
114 EXPECT_FALSE(PepperFlashContentSettingsUtils::AreMediaExceptionsEqual(
115 CONTENT_SETTING_ALLOW
,
116 CONTENT_SETTING_ALLOW
,
117 ConvertAndSort(exceptions_1
, arraysize(exceptions_1
)),
118 CONTENT_SETTING_ALLOW
,
119 CONTENT_SETTING_ALLOW
,
120 ConvertAndSort(exceptions_2
, arraysize(exceptions_2
)),
126 // Similar to the previous block, but reoder the exceptions. The outcome
127 // should be the same.
128 MediaException exceptions_1
[] = {
129 MediaException(ContentSettingsPattern::FromString("www.youtube.com"),
130 CONTENT_SETTING_ASK
, CONTENT_SETTING_ASK
),
131 MediaException(ContentSettingsPattern::FromString("www.google.com"),
132 CONTENT_SETTING_ALLOW
, CONTENT_SETTING_ALLOW
)
135 MediaException exceptions_2
[] = {
136 MediaException(ContentSettingsPattern::FromString("www.google.com"),
137 CONTENT_SETTING_ALLOW
, CONTENT_SETTING_ALLOW
)
140 EXPECT_TRUE(PepperFlashContentSettingsUtils::AreMediaExceptionsEqual(
141 CONTENT_SETTING_ALLOW
,
142 CONTENT_SETTING_ALLOW
,
143 ConvertAndSort(exceptions_1
, arraysize(exceptions_1
)),
146 ConvertAndSort(exceptions_2
, arraysize(exceptions_2
)),
149 EXPECT_FALSE(PepperFlashContentSettingsUtils::AreMediaExceptionsEqual(
150 CONTENT_SETTING_ALLOW
,
151 CONTENT_SETTING_ALLOW
,
152 ConvertAndSort(exceptions_1
, arraysize(exceptions_1
)),
153 CONTENT_SETTING_ALLOW
,
154 CONTENT_SETTING_ALLOW
,
155 ConvertAndSort(exceptions_2
, arraysize(exceptions_2
)),
161 MediaException exceptions_1
[] = {
162 MediaException(ContentSettingsPattern::FromString("www.google.com"),
163 CONTENT_SETTING_ALLOW
, CONTENT_SETTING_BLOCK
)
166 MediaException exceptions_2
[] = {
167 MediaException(ContentSettingsPattern::FromString("www.google.com"),
168 CONTENT_SETTING_ALLOW
, CONTENT_SETTING_ALLOW
)
171 // Test that |ignore_video_setting| works.
172 EXPECT_TRUE(PepperFlashContentSettingsUtils::AreMediaExceptionsEqual(
175 ConvertAndSort(exceptions_1
, arraysize(exceptions_1
)),
177 CONTENT_SETTING_BLOCK
,
178 ConvertAndSort(exceptions_2
, arraysize(exceptions_2
)),
181 EXPECT_FALSE(PepperFlashContentSettingsUtils::AreMediaExceptionsEqual(
184 ConvertAndSort(exceptions_1
, arraysize(exceptions_1
)),
187 ConvertAndSort(exceptions_2
, arraysize(exceptions_2
)),
193 MediaException exceptions_1
[] = {
194 MediaException(ContentSettingsPattern::FromString("www.google.com"),
195 CONTENT_SETTING_BLOCK
, CONTENT_SETTING_ALLOW
)
198 MediaException exceptions_2
[] = {
199 MediaException(ContentSettingsPattern::FromString("www.google.com"),
200 CONTENT_SETTING_ALLOW
, CONTENT_SETTING_ALLOW
)
203 // Test that |ignore_audio_setting| works.
204 EXPECT_TRUE(PepperFlashContentSettingsUtils::AreMediaExceptionsEqual(
205 CONTENT_SETTING_BLOCK
,
207 ConvertAndSort(exceptions_1
, arraysize(exceptions_1
)),
210 ConvertAndSort(exceptions_2
, arraysize(exceptions_2
)),
213 EXPECT_FALSE(PepperFlashContentSettingsUtils::AreMediaExceptionsEqual(
216 ConvertAndSort(exceptions_1
, arraysize(exceptions_1
)),
219 ConvertAndSort(exceptions_2
, arraysize(exceptions_2
)),