Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(212)

Side by Side Diff: chrome/browser/gtk/options/content_exceptions_window_gtk_unittest.cc

Issue 2881009: Reland r51414: "GTK: Fix sorting in content exception window." (Closed) Base URL: http://src.chromium.org/git/chromium.git
Patch Set: Created 10 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/browser/gtk/options/content_exceptions_window_gtk.cc ('k') | chrome/chrome_tests.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/gtk/options/content_exceptions_window_gtk.h"
6
7 #include "chrome/test/testing_profile.h"
8 #include "testing/gmock/include/gmock/gmock.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10
11 using ::testing::ElementsAre;
12
13 class ContentExceptionsWindowGtkUnittest : public testing::Test {
14 public:
15 ContentExceptionsWindowGtkUnittest()
16 : ui_thread_(ChromeThread::UI, &message_loop_),
17 host_content_settings_map_(profile_.GetHostContentSettingsMap()),
18 window_(NULL) {
19 }
20
21 ~ContentExceptionsWindowGtkUnittest() {
22 // This will delete window_ too.
23 gtk_widget_destroy(window_->dialog_);
24 }
25
26 void AddException(const std::string& pattern, ContentSetting value) {
27 host_content_settings_map_->SetContentSetting(
28 HostContentSettingsMap::Pattern(pattern),
29 CONTENT_SETTINGS_TYPE_JAVASCRIPT,
30 value);
31 }
32
33 void BuildWindow() {
34 window_ = new ContentExceptionsWindowGtk(
35 NULL,
36 host_content_settings_map_,
37 CONTENT_SETTINGS_TYPE_JAVASCRIPT);
38 }
39
40 // Actions:
41 void SelectRows(const std::vector<int>& rows) {
42 gtk_tree_selection_unselect_all(window_->treeview_selection_);
43
44 for (std::vector<int>::const_iterator it = rows.begin(); it != rows.end();
45 ++it) {
46 GtkTreeIter iter;
47 if (!gtk_tree_model_iter_nth_child(window_->sort_list_store_,
48 &iter, NULL, *it)) {
49 NOTREACHED();
50 return;
51 }
52
53 gtk_tree_selection_select_iter(window_->treeview_selection_, &iter);
54 }
55 }
56
57 void Remove() {
58 window_->Remove(window_->remove_button_);
59 }
60
61 // Getters:
62
63 void GetSelectedRows(std::set<std::pair<int, int> >* selected) {
64 window_->GetSelectedModelIndices(selected);
65 }
66
67 int StoreListStoreCount() {
68 return gtk_tree_model_iter_n_children(
69 GTK_TREE_MODEL(window_->sort_list_store_), NULL);
70 }
71
72 int ListStoreCount() {
73 return gtk_tree_model_iter_n_children(
74 GTK_TREE_MODEL(window_->list_store_), NULL);
75 }
76
77 std::set<std::string> Paths() {
78 std::set<std::string> paths;
79 GtkTreeIter iter;
80 bool valid = gtk_tree_model_get_iter_first(
81 GTK_TREE_MODEL(window_->sort_list_store_), &iter);
82
83 while (valid) {
84 gchar* str = NULL;
85 gtk_tree_model_get(GTK_TREE_MODEL(window_->sort_list_store_), &iter,
86 0, &str, -1);
87 paths.insert(str);
88 g_free(str);
89
90 valid = gtk_tree_model_iter_next(
91 GTK_TREE_MODEL(window_->sort_list_store_), &iter);
92 }
93
94 return paths;
95 }
96
97 // Whether certain widgets are enabled:
98 bool EditButtonSensitive() {
99 return GTK_WIDGET_SENSITIVE(window_->edit_button_);
100 }
101
102 bool RemoveButtonSensitive() {
103 return GTK_WIDGET_SENSITIVE(window_->remove_button_);
104 }
105
106 bool RemoveAllSensitive() {
107 return GTK_WIDGET_SENSITIVE(window_->remove_all_button_);
108 }
109
110 private:
111 MessageLoop message_loop_;
112 ChromeThread ui_thread_;
113
114 TestingProfile profile_;
115 HostContentSettingsMap* host_content_settings_map_;
116
117 ContentExceptionsWindowGtk* window_;
118 };
119
120 TEST_F(ContentExceptionsWindowGtkUnittest, TestEmptyDisplay) {
121 BuildWindow();
122
123 EXPECT_EQ(0, StoreListStoreCount());
124 EXPECT_EQ(0, ListStoreCount());
125
126 EXPECT_FALSE(RemoveAllSensitive());
127 }
128
129 TEST_F(ContentExceptionsWindowGtkUnittest, TestDisplay) {
130 AddException("a", CONTENT_SETTING_BLOCK);
131 AddException("b", CONTENT_SETTING_ALLOW);
132
133 BuildWindow();
134
135 EXPECT_EQ(2, StoreListStoreCount());
136 EXPECT_EQ(2, ListStoreCount());
137
138 EXPECT_TRUE(RemoveAllSensitive());
139 }
140
141 TEST_F(ContentExceptionsWindowGtkUnittest, TestSelection) {
142 AddException("a", CONTENT_SETTING_BLOCK);
143
144 BuildWindow();
145
146 EXPECT_FALSE(EditButtonSensitive());
147 EXPECT_FALSE(RemoveButtonSensitive());
148
149 std::vector<int> rows;
150 rows.push_back(0);
151 SelectRows(rows);
152
153 EXPECT_TRUE(EditButtonSensitive());
154 EXPECT_TRUE(RemoveButtonSensitive());
155 }
156
157 TEST_F(ContentExceptionsWindowGtkUnittest, TestSimpleRemoval) {
158 AddException("a", CONTENT_SETTING_BLOCK);
159 AddException("b", CONTENT_SETTING_ALLOW);
160 AddException("c", CONTENT_SETTING_BLOCK);
161 AddException("d", CONTENT_SETTING_ALLOW);
162
163 BuildWindow();
164
165 std::vector<int> rows;
166 rows.push_back(1);
167 SelectRows(rows);
168
169 Remove();
170
171 EXPECT_EQ(3, StoreListStoreCount());
172 EXPECT_EQ(3, ListStoreCount());
173 EXPECT_TRUE(EditButtonSensitive());
174 EXPECT_TRUE(RemoveButtonSensitive());
175
176 std::set<std::pair<int, int> > selected;
177 GetSelectedRows(&selected);
178
179 ASSERT_EQ(1u, selected.size());
180 EXPECT_EQ(1, selected.begin()->first);
181 EXPECT_EQ(1, selected.begin()->second);
182 EXPECT_THAT(Paths(), ElementsAre("a", "c", "d"));
183 }
184
185 TEST_F(ContentExceptionsWindowGtkUnittest, TestComplexRemoval) {
186 AddException("a", CONTENT_SETTING_BLOCK);
187 AddException("b", CONTENT_SETTING_ALLOW);
188 AddException("c", CONTENT_SETTING_BLOCK);
189 AddException("d", CONTENT_SETTING_ALLOW);
190 AddException("e", CONTENT_SETTING_BLOCK);
191
192 BuildWindow();
193
194 std::vector<int> rows;
195 rows.push_back(1);
196 rows.push_back(3);
197 SelectRows(rows);
198
199 Remove();
200
201 std::set<std::pair<int, int> > selected;
202 GetSelectedRows(&selected);
203
204 ASSERT_EQ(1u, selected.size());
205 EXPECT_EQ(1, selected.begin()->first);
206 EXPECT_EQ(1, selected.begin()->second);
207 EXPECT_THAT(Paths(), ElementsAre("a", "c", "e"));
208 }
OLDNEW
« no previous file with comments | « chrome/browser/gtk/options/content_exceptions_window_gtk.cc ('k') | chrome/chrome_tests.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698