OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "app/table_model_observer.h" | 5 #include "app/table_model_observer.h" |
| 6 #include "base/auto_reset.h" |
6 #include "base/command_line.h" | 7 #include "base/command_line.h" |
7 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
8 #include "chrome/browser/plugin_exceptions_table_model.h" | 9 #include "chrome/browser/mock_plugin_exceptions_table_model.h" |
9 #include "chrome/common/chrome_switches.h" | 10 #include "chrome/common/chrome_switches.h" |
10 #include "chrome/common/pref_names.h" | 11 #include "chrome/common/pref_names.h" |
11 #include "chrome/test/testing_pref_service.h" | 12 #include "chrome/test/testing_pref_service.h" |
12 #include "chrome/test/testing_profile.h" | 13 #include "chrome/test/testing_profile.h" |
13 #include "testing/gmock/include/gmock/gmock.h" | 14 #include "testing/gmock/include/gmock/gmock.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
15 #include "webkit/glue/plugins/webplugininfo.h" | 16 #include "webkit/glue/plugins/webplugininfo.h" |
16 | 17 |
17 // Can't be an internal namespace because PluginExceptionsTableModel declares | 18 // Can't be an internal namespace because PluginExceptionsTableModel declares |
18 // as a friend. | 19 // as a friend. |
19 namespace plugin_test_internal { | 20 namespace plugin_test_internal { |
20 | 21 |
21 class MockTableModelObserver : public TableModelObserver { | 22 class MockTableModelObserver : public TableModelObserver { |
22 public: | 23 public: |
23 MOCK_METHOD0(OnModelChanged, void()); | 24 MOCK_METHOD0(OnModelChanged, void()); |
24 MOCK_METHOD2(OnItemsChanged, void(int start, int length)); | 25 MOCK_METHOD2(OnItemsChanged, void(int start, int length)); |
25 MOCK_METHOD2(OnItemsAdded, void(int start, int length)); | 26 MOCK_METHOD2(OnItemsAdded, void(int start, int length)); |
26 MOCK_METHOD2(OnItemsRemoved, void(int start, int length)); | 27 MOCK_METHOD2(OnItemsRemoved, void(int start, int length)); |
27 }; | 28 }; |
28 | 29 |
29 class TestingPluginExceptionsTableModel : public PluginExceptionsTableModel { | |
30 public: | |
31 TestingPluginExceptionsTableModel(HostContentSettingsMap* map, | |
32 HostContentSettingsMap* otr_map) | |
33 : PluginExceptionsTableModel(map, otr_map) {} | |
34 virtual ~TestingPluginExceptionsTableModel() {} | |
35 | |
36 void set_plugins(const std::vector<WebPluginInfo>& plugins) { | |
37 plugins_ = plugins; | |
38 } | |
39 | |
40 protected: | |
41 virtual void GetPlugins(std::vector<WebPluginInfo>* plugins) { | |
42 *plugins = plugins_; | |
43 } | |
44 | |
45 private: | |
46 std::vector<WebPluginInfo> plugins_; | |
47 }; | |
48 | |
49 class PluginExceptionsTableModelTest : public testing::Test { | 30 class PluginExceptionsTableModelTest : public testing::Test { |
50 public: | 31 public: |
51 PluginExceptionsTableModelTest() | 32 PluginExceptionsTableModelTest() |
52 : ui_thread_(ChromeThread::UI, &message_loop_), | 33 : ui_thread_(ChromeThread::UI, &message_loop_), |
53 command_line_(*CommandLine::ForCurrentProcess()) {} | 34 command_line_(CommandLine::ForCurrentProcess(), |
| 35 *CommandLine::ForCurrentProcess()) {} |
54 | 36 |
55 virtual void SetUp() { | 37 virtual void SetUp() { |
56 CommandLine::ForCurrentProcess()->AppendSwitch( | 38 CommandLine::ForCurrentProcess()->AppendSwitch( |
57 switches::kEnableResourceContentSettings); | 39 switches::kEnableResourceContentSettings); |
58 | 40 |
59 profile_.reset(new TestingProfile()); | 41 profile_.reset(new TestingProfile()); |
60 | 42 |
61 HostContentSettingsMap* map = profile_->GetHostContentSettingsMap(); | 43 HostContentSettingsMap* map = profile_->GetHostContentSettingsMap(); |
62 | 44 |
63 HostContentSettingsMap::Pattern example_com("[*.]example.com"); | 45 HostContentSettingsMap::Pattern example_com("[*.]example.com"); |
64 HostContentSettingsMap::Pattern moose_org("[*.]moose.org"); | 46 HostContentSettingsMap::Pattern moose_org("[*.]moose.org"); |
65 map->SetContentSetting(example_com, | 47 map->SetContentSetting(example_com, |
66 CONTENT_SETTINGS_TYPE_PLUGINS, | 48 CONTENT_SETTINGS_TYPE_PLUGINS, |
67 "foo", | 49 "foo", |
68 CONTENT_SETTING_ALLOW); | 50 CONTENT_SETTING_ALLOW); |
69 map->SetContentSetting(moose_org, | 51 map->SetContentSetting(moose_org, |
70 CONTENT_SETTINGS_TYPE_PLUGINS, | 52 CONTENT_SETTINGS_TYPE_PLUGINS, |
71 "bar", | 53 "bar", |
72 CONTENT_SETTING_BLOCK); | 54 CONTENT_SETTING_BLOCK); |
73 map->SetContentSetting(example_com, | 55 map->SetContentSetting(example_com, |
74 CONTENT_SETTINGS_TYPE_PLUGINS, | 56 CONTENT_SETTINGS_TYPE_PLUGINS, |
75 "bar", | 57 "bar", |
76 CONTENT_SETTING_ALLOW); | 58 CONTENT_SETTING_ALLOW); |
77 | 59 |
78 table_model_.reset(new TestingPluginExceptionsTableModel(map, NULL)); | 60 table_model_.reset(new MockPluginExceptionsTableModel(map, NULL)); |
79 | 61 |
80 std::vector<WebPluginInfo> plugins; | 62 std::vector<WebPluginInfo> plugins; |
81 WebPluginInfo foo_plugin; | 63 WebPluginInfo foo_plugin; |
82 foo_plugin.path = FilePath(FILE_PATH_LITERAL("foo")); | 64 foo_plugin.path = FilePath(FILE_PATH_LITERAL("foo")); |
83 foo_plugin.name = ASCIIToUTF16("FooPlugin"); | 65 foo_plugin.name = ASCIIToUTF16("FooPlugin"); |
84 foo_plugin.enabled = true; | 66 foo_plugin.enabled = true; |
85 plugins.push_back(foo_plugin); | 67 plugins.push_back(foo_plugin); |
86 WebPluginInfo bar_plugin; | 68 WebPluginInfo bar_plugin; |
87 bar_plugin.path = FilePath(FILE_PATH_LITERAL("bar")); | 69 bar_plugin.path = FilePath(FILE_PATH_LITERAL("bar")); |
88 bar_plugin.name = ASCIIToUTF16("BarPlugin"); | 70 bar_plugin.name = ASCIIToUTF16("BarPlugin"); |
89 bar_plugin.enabled = true; | 71 bar_plugin.enabled = true; |
90 plugins.push_back(bar_plugin); | 72 plugins.push_back(bar_plugin); |
91 | 73 |
92 table_model_->set_plugins(plugins); | 74 table_model_->set_plugins(plugins); |
93 table_model_->ReloadSettings(); | 75 table_model_->ReloadSettings(); |
94 } | 76 } |
95 | 77 |
96 virtual void TearDown() { | |
97 *CommandLine::ForCurrentProcess() = command_line_; | |
98 } | |
99 | |
100 protected: | 78 protected: |
101 void CheckInvariants() { | 79 void CheckInvariants() { |
102 typedef std::deque<PluginExceptionsTableModel::SettingsEntry> Entries; | 80 typedef std::deque<PluginExceptionsTableModel::SettingsEntry> Entries; |
103 Entries& settings = table_model_->settings_; | 81 Entries& settings = table_model_->settings_; |
104 std::deque<int>& row_counts = table_model_->row_counts_; | 82 std::deque<int>& row_counts = table_model_->row_counts_; |
105 std::deque<std::string>& resources = table_model_->resources_; | 83 std::deque<std::string>& resources = table_model_->resources_; |
106 TableModel::Groups& groups = table_model_->groups_; | 84 TableModel::Groups& groups = table_model_->groups_; |
107 | 85 |
108 EXPECT_EQ(groups.size(), row_counts.size()); | 86 EXPECT_EQ(groups.size(), row_counts.size()); |
109 EXPECT_EQ(groups.size(), resources.size()); | 87 EXPECT_EQ(groups.size(), resources.size()); |
(...skipping 19 matching lines...) Expand all Loading... |
129 } | 107 } |
130 if (row_counts.size() > 0) | 108 if (row_counts.size() > 0) |
131 EXPECT_EQ(count, row_counts[last_plugin]); | 109 EXPECT_EQ(count, row_counts[last_plugin]); |
132 } | 110 } |
133 | 111 |
134 protected: | 112 protected: |
135 MessageLoop message_loop_; | 113 MessageLoop message_loop_; |
136 ChromeThread ui_thread_; | 114 ChromeThread ui_thread_; |
137 | 115 |
138 scoped_ptr<TestingProfile> profile_; | 116 scoped_ptr<TestingProfile> profile_; |
139 scoped_ptr<TestingPluginExceptionsTableModel> table_model_; | 117 scoped_ptr<MockPluginExceptionsTableModel> table_model_; |
140 | 118 |
141 private: | 119 private: |
142 CommandLine command_line_; | 120 AutoReset<CommandLine> command_line_; |
143 }; | 121 }; |
144 | 122 |
145 TEST_F(PluginExceptionsTableModelTest, Basic) { | 123 TEST_F(PluginExceptionsTableModelTest, Basic) { |
146 EXPECT_EQ(3, table_model_->RowCount()); | 124 EXPECT_EQ(3, table_model_->RowCount()); |
147 CheckInvariants(); | 125 CheckInvariants(); |
148 } | 126 } |
149 | 127 |
150 TEST_F(PluginExceptionsTableModelTest, RemoveOneRow) { | 128 TEST_F(PluginExceptionsTableModelTest, RemoveOneRow) { |
151 MockTableModelObserver observer; | 129 MockTableModelObserver observer; |
152 table_model_->SetObserver(&observer); | 130 table_model_->SetObserver(&observer); |
(...skipping 19 matching lines...) Expand all Loading... |
172 EXPECT_EQ(2, table_model_->RowCount()); | 150 EXPECT_EQ(2, table_model_->RowCount()); |
173 EXPECT_EQ(1, static_cast<int>(table_model_->GetGroups().size())); | 151 EXPECT_EQ(1, static_cast<int>(table_model_->GetGroups().size())); |
174 CheckInvariants(); | 152 CheckInvariants(); |
175 table_model_->SetObserver(NULL); | 153 table_model_->SetObserver(NULL); |
176 } | 154 } |
177 | 155 |
178 TEST_F(PluginExceptionsTableModelTest, RemoveAllRows) { | 156 TEST_F(PluginExceptionsTableModelTest, RemoveAllRows) { |
179 MockTableModelObserver observer; | 157 MockTableModelObserver observer; |
180 table_model_->SetObserver(&observer); | 158 table_model_->SetObserver(&observer); |
181 | 159 |
182 EXPECT_CALL(observer, OnItemsRemoved(0, 3)); | 160 EXPECT_CALL(observer, OnModelChanged()); |
183 table_model_->RemoveAll(); | 161 table_model_->RemoveAll(); |
184 EXPECT_EQ(0, table_model_->RowCount()); | 162 EXPECT_EQ(0, table_model_->RowCount()); |
185 EXPECT_EQ(0, static_cast<int>(table_model_->GetGroups().size())); | 163 EXPECT_EQ(0, static_cast<int>(table_model_->GetGroups().size())); |
186 CheckInvariants(); | 164 CheckInvariants(); |
187 table_model_->SetObserver(NULL); | 165 table_model_->SetObserver(NULL); |
188 } | 166 } |
189 | 167 |
190 } // namespace plugin_test_internal | 168 } // namespace plugin_test_internal |
OLD | NEW |