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.
4
5#include "chrome/browser/ui/webui/options/pepper_flash_content_settings_utils.h"
6
7#include "base/basictypes.h"
8#include "testing/gtest/include/gtest/gtest.h"
9
10using options::MediaException;
11using options::MediaExceptions;
12using options::PepperFlashContentSettingsUtils;
13
14namespace {
15
16MediaExceptions ConvertAndSort(const MediaException* items, size_t count) {
17  MediaExceptions result(items, items + count);
18  PepperFlashContentSettingsUtils::SortMediaExceptions(&result);
19  return result;
20}
21
22}  // namespace
23
24TEST(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);
33
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);
39
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);
45
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);
51
52  EXPECT_NE(list_1, list_2);
53  EXPECT_NE(list_2, list_3);
54  EXPECT_NE(list_3, list_1);
55
56  PepperFlashContentSettingsUtils::SortMediaExceptions(&list_1);
57  PepperFlashContentSettingsUtils::SortMediaExceptions(&list_2);
58  PepperFlashContentSettingsUtils::SortMediaExceptions(&list_3);
59
60  EXPECT_EQ(list_1, list_2);
61  EXPECT_EQ(list_2, list_3);
62}
63
64TEST(PepperFlashContentSettingsUtilsTest, AreMediaExceptionsEqual) {
65  {
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,
71        MediaExceptions(),
72        CONTENT_SETTING_ASK,
73        MediaExceptions(),
74        false,
75        false));
76  }
77
78  {
79    MediaException exceptions_1[] = {
80      MediaException(ContentSettingsPattern::FromString("www.google.com"),
81                     CONTENT_SETTING_ALLOW, CONTENT_SETTING_ALLOW),
82      MediaException(ContentSettingsPattern::FromString("www.youtube.com"),
83                     CONTENT_SETTING_ASK, CONTENT_SETTING_ASK)
84    };
85
86    MediaException exceptions_2[] = {
87      MediaException(ContentSettingsPattern::FromString("www.google.com"),
88                     CONTENT_SETTING_ALLOW, CONTENT_SETTING_ALLOW)
89    };
90
91    // The exception of "www.youtube.com" in |exceptions_1| should not affect
92    // the result, because it has the same settings as |default_setting_2|.
93    EXPECT_TRUE(PepperFlashContentSettingsUtils::AreMediaExceptionsEqual(
94        CONTENT_SETTING_ALLOW,
95        ConvertAndSort(exceptions_1, arraysize(exceptions_1)),
96        CONTENT_SETTING_ASK,
97        ConvertAndSort(exceptions_2, arraysize(exceptions_2)),
98        false,
99        false));
100    EXPECT_TRUE(PepperFlashContentSettingsUtils::AreMediaExceptionsEqual(
101        CONTENT_SETTING_ASK,
102        ConvertAndSort(exceptions_2, arraysize(exceptions_2)),
103        CONTENT_SETTING_ALLOW,
104        ConvertAndSort(exceptions_1, arraysize(exceptions_1)),
105        false,
106        false));
107    // Changing |default_setting_2| should change the result.
108    EXPECT_FALSE(PepperFlashContentSettingsUtils::AreMediaExceptionsEqual(
109        CONTENT_SETTING_ALLOW,
110        ConvertAndSort(exceptions_1, arraysize(exceptions_1)),
111        CONTENT_SETTING_ALLOW,
112        ConvertAndSort(exceptions_2, arraysize(exceptions_2)),
113        false,
114        false));
115  }
116
117  {
118    // Similar to the previous block, but reoder the exceptions. The outcome
119    // should be the same.
120    MediaException exceptions_1[] = {
121      MediaException(ContentSettingsPattern::FromString("www.youtube.com"),
122                     CONTENT_SETTING_ASK, CONTENT_SETTING_ASK),
123      MediaException(ContentSettingsPattern::FromString("www.google.com"),
124                     CONTENT_SETTING_ALLOW, CONTENT_SETTING_ALLOW)
125    };
126
127    MediaException exceptions_2[] = {
128      MediaException(ContentSettingsPattern::FromString("www.google.com"),
129                     CONTENT_SETTING_ALLOW, CONTENT_SETTING_ALLOW)
130    };
131
132    EXPECT_TRUE(PepperFlashContentSettingsUtils::AreMediaExceptionsEqual(
133        CONTENT_SETTING_ALLOW,
134        ConvertAndSort(exceptions_1, arraysize(exceptions_1)),
135        CONTENT_SETTING_ASK,
136        ConvertAndSort(exceptions_2, arraysize(exceptions_2)),
137        false,
138        false));
139    EXPECT_FALSE(PepperFlashContentSettingsUtils::AreMediaExceptionsEqual(
140        CONTENT_SETTING_ALLOW,
141        ConvertAndSort(exceptions_1, arraysize(exceptions_1)),
142        CONTENT_SETTING_ALLOW,
143        ConvertAndSort(exceptions_2, arraysize(exceptions_2)),
144        false,
145        false));
146  }
147
148  {
149    MediaException exceptions_1[] = {
150      MediaException(ContentSettingsPattern::FromString("www.google.com"),
151                     CONTENT_SETTING_ALLOW, CONTENT_SETTING_BLOCK)
152    };
153
154    MediaException exceptions_2[] = {
155      MediaException(ContentSettingsPattern::FromString("www.google.com"),
156                     CONTENT_SETTING_ALLOW, CONTENT_SETTING_ALLOW)
157    };
158
159    // Test that |ignore_video_setting| works.
160    EXPECT_TRUE(PepperFlashContentSettingsUtils::AreMediaExceptionsEqual(
161        CONTENT_SETTING_ASK,
162        ConvertAndSort(exceptions_1, arraysize(exceptions_1)),
163        CONTENT_SETTING_ASK,
164        ConvertAndSort(exceptions_2, arraysize(exceptions_2)),
165        false,
166        true));
167    EXPECT_FALSE(PepperFlashContentSettingsUtils::AreMediaExceptionsEqual(
168        CONTENT_SETTING_ASK,
169        ConvertAndSort(exceptions_1, arraysize(exceptions_1)),
170        CONTENT_SETTING_ASK,
171        ConvertAndSort(exceptions_2, arraysize(exceptions_2)),
172        false,
173        false));
174  }
175
176  {
177    MediaException exceptions_1[] = {
178      MediaException(ContentSettingsPattern::FromString("www.google.com"),
179                     CONTENT_SETTING_BLOCK, CONTENT_SETTING_ALLOW)
180    };
181
182    MediaException exceptions_2[] = {
183      MediaException(ContentSettingsPattern::FromString("www.google.com"),
184                     CONTENT_SETTING_ALLOW, CONTENT_SETTING_ALLOW)
185    };
186
187    // Test that |ignore_audio_setting| works.
188    EXPECT_TRUE(PepperFlashContentSettingsUtils::AreMediaExceptionsEqual(
189        CONTENT_SETTING_ASK,
190        ConvertAndSort(exceptions_1, arraysize(exceptions_1)),
191        CONTENT_SETTING_ASK,
192        ConvertAndSort(exceptions_2, arraysize(exceptions_2)),
193        true,
194        false));
195    EXPECT_FALSE(PepperFlashContentSettingsUtils::AreMediaExceptionsEqual(
196        CONTENT_SETTING_ASK,
197        ConvertAndSort(exceptions_1, arraysize(exceptions_1)),
198        CONTENT_SETTING_ASK,
199        ConvertAndSort(exceptions_2, arraysize(exceptions_2)),
200        false,
201        false));
202  }
203}
204