OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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 "base/auto_reset.h" | |
6 #include "base/command_line.h" | |
7 #include "base/utf_string_conversions.h" | |
8 #include "chrome/browser/mock_plugin_exceptions_table_model.h" | |
9 #include "chrome/common/chrome_switches.h" | |
10 #include "chrome/test/base/testing_pref_service.h" | |
11 #include "chrome/test/base/testing_profile.h" | |
12 #include "testing/gmock/include/gmock/gmock.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | |
14 #include "ui/base/models/table_model_observer.h" | |
15 #include "webkit/plugins/npapi/plugin_group.h" | |
16 #include "webkit/plugins/webplugininfo.h" | |
17 | |
18 // Can't be an internal namespace because PluginExceptionsTableModel declares | |
19 // as a friend. | |
20 namespace plugin_test_internal { | |
21 | |
22 using ::testing::_; | |
23 using ::testing::Invoke; | |
24 | |
25 class MockTableModelObserver : public ui::TableModelObserver { | |
26 public: | |
27 explicit MockTableModelObserver(ui::TableModel* model) | |
28 : model_(model) { | |
29 ON_CALL(*this, OnItemsRemoved(_, _)) | |
30 .WillByDefault( | |
31 Invoke(this, &MockTableModelObserver::CheckOnItemsRemoved)); | |
32 } | |
33 | |
34 MOCK_METHOD0(OnModelChanged, void()); | |
35 MOCK_METHOD2(OnItemsChanged, void(int start, int length)); | |
36 MOCK_METHOD2(OnItemsAdded, void(int start, int length)); | |
37 MOCK_METHOD2(OnItemsRemoved, void(int start, int length)); | |
38 | |
39 private: | |
40 void CheckOnItemsRemoved(int start, int length) { | |
41 if (!model_) | |
42 return; | |
43 // This method is called *after* the items have been removed, so we check if | |
44 // the first removed item was still inside the correct range. | |
45 EXPECT_LT(start, model_->RowCount() + 1); | |
46 } | |
47 | |
48 ui::TableModel* model_; | |
49 }; | |
50 | |
51 } // namespace plugin_test_internal | |
52 | |
53 using ::testing::InSequence; | |
54 | |
55 class PluginExceptionsTableModelTest : public testing::Test { | |
56 public: | |
57 PluginExceptionsTableModelTest() | |
58 : ui_thread_(BrowserThread::UI, &message_loop_), | |
59 command_line_(CommandLine::ForCurrentProcess(), | |
60 *CommandLine::ForCurrentProcess()) {} | |
61 | |
62 virtual void SetUp() { | |
63 CommandLine::ForCurrentProcess()->AppendSwitch( | |
64 switches::kEnableResourceContentSettings); | |
65 | |
66 profile_.reset(new TestingProfile()); | |
67 | |
68 HostContentSettingsMap* map = profile_->GetHostContentSettingsMap(); | |
69 | |
70 ContentSettingsPattern example_com = | |
71 ContentSettingsPattern::FromString("[*.]example.com"); | |
72 ContentSettingsPattern moose_org = | |
73 ContentSettingsPattern::FromString("[*.]moose.org"); | |
74 map->SetContentSetting(example_com, | |
75 ContentSettingsPattern::Wildcard(), | |
76 CONTENT_SETTINGS_TYPE_PLUGINS, | |
77 "a-foo", | |
78 CONTENT_SETTING_ALLOW); | |
79 map->SetContentSetting(moose_org, | |
80 ContentSettingsPattern::Wildcard(), | |
81 CONTENT_SETTINGS_TYPE_PLUGINS, | |
82 "b-bar", | |
83 CONTENT_SETTING_BLOCK); | |
84 map->SetContentSetting(example_com, | |
85 ContentSettingsPattern::Wildcard(), | |
86 CONTENT_SETTINGS_TYPE_PLUGINS, | |
87 "b-bar", | |
88 CONTENT_SETTING_ALLOW); | |
89 | |
90 table_model_.reset(new MockPluginExceptionsTableModel(map, NULL)); | |
91 | |
92 std::vector<webkit::npapi::PluginGroup> plugins; | |
93 webkit::WebPluginInfo foo_plugin; | |
94 foo_plugin.path = FilePath(FILE_PATH_LITERAL("a-foo")); | |
95 foo_plugin.name = ASCIIToUTF16("FooPlugin"); | |
96 scoped_ptr<webkit::npapi::PluginGroup> foo_group( | |
97 webkit::npapi::PluginGroup::FromWebPluginInfo(foo_plugin)); | |
98 plugins.push_back(*foo_group); | |
99 | |
100 webkit::WebPluginInfo bar_plugin; | |
101 bar_plugin.path = FilePath(FILE_PATH_LITERAL("b-bar")); | |
102 bar_plugin.name = ASCIIToUTF16("BarPlugin"); | |
103 scoped_ptr<webkit::npapi::PluginGroup> bar_group( | |
104 webkit::npapi::PluginGroup::FromWebPluginInfo(bar_plugin)); | |
105 plugins.push_back(*bar_group); | |
106 | |
107 table_model_->set_plugins(plugins); | |
108 table_model_->ReloadSettings(); | |
109 } | |
110 | |
111 protected: | |
112 void CheckInvariants() const { | |
113 typedef std::vector<PluginExceptionsTableModel::SettingsEntry> Entries; | |
114 const Entries& settings = table_model_->settings_; | |
115 const std::vector<int>& row_counts = table_model_->row_counts_; | |
116 const std::vector<std::string>& resources = table_model_->resources_; | |
117 const ui::TableModel::Groups& groups = table_model_->groups_; | |
118 | |
119 EXPECT_EQ(groups.size(), row_counts.size()); | |
120 EXPECT_EQ(groups.size(), resources.size()); | |
121 | |
122 int last_plugin = 0; | |
123 int count = 0; | |
124 for (Entries::const_iterator it = settings.begin(); | |
125 it != settings.end(); ++it) { | |
126 // Plugin IDs are indices into |groups|. | |
127 EXPECT_GE(it->plugin_id, 0); | |
128 EXPECT_LT(it->plugin_id, static_cast<int>(groups.size())); | |
129 if (it->plugin_id == last_plugin) { | |
130 count++; | |
131 } else { | |
132 // Plugin IDs are ascending one by one. | |
133 EXPECT_EQ(last_plugin+1, it->plugin_id); | |
134 | |
135 // Consecutive runs of plugins with id |x| are |row_counts[x]| long. | |
136 EXPECT_EQ(count, row_counts[last_plugin]); | |
137 count = 1; | |
138 last_plugin = it->plugin_id; | |
139 } | |
140 } | |
141 if (!row_counts.empty()) | |
142 EXPECT_EQ(count, row_counts[last_plugin]); | |
143 } | |
144 | |
145 protected: | |
146 MessageLoop message_loop_; | |
147 BrowserThread ui_thread_; | |
148 | |
149 scoped_ptr<TestingProfile> profile_; | |
150 scoped_ptr<MockPluginExceptionsTableModel> table_model_; | |
151 | |
152 private: | |
153 AutoReset<CommandLine> command_line_; | |
154 }; | |
155 | |
156 TEST_F(PluginExceptionsTableModelTest, Basic) { | |
157 EXPECT_EQ(3, table_model_->RowCount()); | |
158 CheckInvariants(); | |
159 } | |
160 | |
161 TEST_F(PluginExceptionsTableModelTest, RemoveOneRow) { | |
162 plugin_test_internal::MockTableModelObserver observer(table_model_.get()); | |
163 table_model_->SetObserver(&observer); | |
164 | |
165 EXPECT_CALL(observer, OnItemsRemoved(1, 1)); | |
166 RemoveRowsTableModel::Rows rows; | |
167 rows.insert(1); | |
168 table_model_->RemoveRows(rows); | |
169 EXPECT_EQ(2, table_model_->RowCount()); | |
170 EXPECT_EQ(2, static_cast<int>(table_model_->GetGroups().size())); | |
171 CheckInvariants(); | |
172 table_model_->SetObserver(NULL); | |
173 } | |
174 | |
175 TEST_F(PluginExceptionsTableModelTest, RemoveLastRowInGroup) { | |
176 plugin_test_internal::MockTableModelObserver observer(table_model_.get()); | |
177 table_model_->SetObserver(&observer); | |
178 | |
179 EXPECT_CALL(observer, OnModelChanged()); | |
180 RemoveRowsTableModel::Rows rows; | |
181 rows.insert(0); | |
182 table_model_->RemoveRows(rows); | |
183 EXPECT_EQ(2, table_model_->RowCount()); | |
184 EXPECT_EQ(1, static_cast<int>(table_model_->GetGroups().size())); | |
185 CheckInvariants(); | |
186 | |
187 HostContentSettingsMap* map = profile_->GetHostContentSettingsMap(); | |
188 EXPECT_CALL(observer, OnModelChanged()); | |
189 map->SetContentSetting(ContentSettingsPattern::FromString("[*.]blurp.net"), | |
190 ContentSettingsPattern::Wildcard(), | |
191 CONTENT_SETTINGS_TYPE_PLUGINS, | |
192 "b-bar", | |
193 CONTENT_SETTING_BLOCK); | |
194 EXPECT_EQ(3, table_model_->RowCount()); | |
195 | |
196 InSequence s; | |
197 EXPECT_CALL(observer, OnItemsRemoved(2, 1)); | |
198 EXPECT_CALL(observer, OnItemsRemoved(0, 1)); | |
199 rows.clear(); | |
200 rows.insert(0); | |
201 rows.insert(2); | |
202 table_model_->RemoveRows(rows); | |
203 EXPECT_EQ(1, table_model_->RowCount()); | |
204 EXPECT_EQ(1, static_cast<int>(table_model_->GetGroups().size())); | |
205 CheckInvariants(); | |
206 | |
207 table_model_->SetObserver(NULL); | |
208 } | |
209 | |
210 TEST_F(PluginExceptionsTableModelTest, RemoveAllRows) { | |
211 plugin_test_internal::MockTableModelObserver observer(table_model_.get()); | |
212 table_model_->SetObserver(&observer); | |
213 | |
214 EXPECT_CALL(observer, OnModelChanged()); | |
215 table_model_->RemoveAll(); | |
216 EXPECT_EQ(0, table_model_->RowCount()); | |
217 EXPECT_EQ(0, static_cast<int>(table_model_->GetGroups().size())); | |
218 CheckInvariants(); | |
219 table_model_->SetObserver(NULL); | |
220 } | |
OLD | NEW |