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

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

Issue 2876032: GTK: Fix sorting in content exception window. (Closed) Base URL: http://src.chromium.org/git/chromium.git
Patch Set: Unit tests 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
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 void AddException(const std::string& pattern, ContentSetting value) {
22 host_content_settings_map_->SetContentSetting(
23 HostContentSettingsMap::Pattern(pattern),
24 CONTENT_SETTINGS_TYPE_JAVASCRIPT,
25 value);
26 }
27
28 void BuildWindow() {
29 window_.reset(new ContentExceptionsWindowGtk(
30 NULL,
31 host_content_settings_map_,
32 CONTENT_SETTINGS_TYPE_JAVASCRIPT));
33 }
34
35 // Actions:
36 void SelectRows(const std::vector<int>& rows) {
37 gtk_tree_selection_unselect_all(window_->treeview_selection_);
38
39 for (std::vector<int>::const_iterator it = rows.begin(); it != rows.end();
40 ++it) {
41 GtkTreeIter iter;
42 if (!gtk_tree_model_iter_nth_child(window_->sort_list_store_,
43 &iter, NULL, *it)) {
44 NOTREACHED();
45 return;
46 }
47
48 gtk_tree_selection_select_iter(window_->treeview_selection_, &iter);
49 }
50 }
51
52 void Remove() {
53 window_->Remove(window_->remove_button_);
54 }
55
56 // Getters:
57
58 void GetSelectedRows(std::set<std::pair<int, int> >* selected) {
59 window_->GetSelectedModelIndices(selected);
60 }
61
62 int StoreListStoreCount() {
63 return gtk_tree_model_iter_n_children(
64 GTK_TREE_MODEL(window_->sort_list_store_), NULL);
65 }
66
67 int ListStoreCount() {
68 return gtk_tree_model_iter_n_children(
69 GTK_TREE_MODEL(window_->list_store_), NULL);
70 }
71
72 std::set<std::string> Paths() {
73 std::set<std::string> paths;
74 GtkTreeIter iter;
75 bool valid = gtk_tree_model_get_iter_first(
76 GTK_TREE_MODEL(window_->sort_list_store_), &iter);
77
78 while (valid) {
79 gchar* str = NULL;
80 gtk_tree_model_get(GTK_TREE_MODEL(window_->sort_list_store_), &iter,
81 0, &str, -1);
82 paths.insert(str);
83 g_free(str);
84
85 valid = gtk_tree_model_iter_next(
86 GTK_TREE_MODEL(window_->sort_list_store_), &iter);
87 }
88
89 return paths;
90 }
91
92 // Whether certain widgets are enabled:
93 bool EditButtonSensitive() {
94 return GTK_WIDGET_SENSITIVE(window_->edit_button_);
95 }
96
97 bool RemoveButtonSensitive() {
98 return GTK_WIDGET_SENSITIVE(window_->remove_button_);
99 }
100
101 bool RemoveAllSensitive() {
102 return GTK_WIDGET_SENSITIVE(window_->remove_all_button_);
103 }
104
105 private:
106 MessageLoop message_loop_;
107 ChromeThread ui_thread_;
108
109 TestingProfile profile_;
110 HostContentSettingsMap* host_content_settings_map_;
111
112 scoped_ptr<ContentExceptionsWindowGtk> window_;
113 };
114
115 TEST_F(ContentExceptionsWindowGtkUnittest, TestEmptyDisplay) {
116 BuildWindow();
117
118 EXPECT_EQ(0, StoreListStoreCount());
119 EXPECT_EQ(0, ListStoreCount());
120
121 EXPECT_FALSE(RemoveAllSensitive());
122 }
123
124 TEST_F(ContentExceptionsWindowGtkUnittest, TestDisplay) {
125 AddException("a", CONTENT_SETTING_BLOCK);
126 AddException("b", CONTENT_SETTING_ALLOW);
127
128 BuildWindow();
129
130 EXPECT_EQ(2, StoreListStoreCount());
131 EXPECT_EQ(2, ListStoreCount());
132
133 EXPECT_TRUE(RemoveAllSensitive());
134 }
135
136 TEST_F(ContentExceptionsWindowGtkUnittest, TestSelection) {
137 AddException("a", CONTENT_SETTING_BLOCK);
138
139 BuildWindow();
140
141 EXPECT_FALSE(EditButtonSensitive());
142 EXPECT_FALSE(RemoveButtonSensitive());
143
144 std::vector<int> rows;
145 rows.push_back(0);
146 SelectRows(rows);
147
148 EXPECT_TRUE(EditButtonSensitive());
149 EXPECT_TRUE(RemoveButtonSensitive());
150 }
151
152 TEST_F(ContentExceptionsWindowGtkUnittest, TestSimpleRemoval) {
153 AddException("a", CONTENT_SETTING_BLOCK);
154 AddException("b", CONTENT_SETTING_ALLOW);
155 AddException("c", CONTENT_SETTING_BLOCK);
156 AddException("d", CONTENT_SETTING_ALLOW);
157
158 BuildWindow();
159
160 std::vector<int> rows;
161 rows.push_back(1);
162 SelectRows(rows);
163
164 Remove();
165
166 EXPECT_EQ(3, StoreListStoreCount());
167 EXPECT_EQ(3, ListStoreCount());
168 EXPECT_TRUE(EditButtonSensitive());
169 EXPECT_TRUE(RemoveButtonSensitive());
170
171 std::set<std::pair<int, int> > selected;
172 GetSelectedRows(&selected);
173
174 ASSERT_EQ(1u, selected.size());
175 EXPECT_EQ(1, selected.begin()->first);
176 EXPECT_EQ(1, selected.begin()->second);
177 EXPECT_THAT(Paths(), ElementsAre("a", "c", "d"));
178 }
179
180 TEST_F(ContentExceptionsWindowGtkUnittest, TestComplexRemoval) {
181 AddException("a", CONTENT_SETTING_BLOCK);
182 AddException("b", CONTENT_SETTING_ALLOW);
183 AddException("c", CONTENT_SETTING_BLOCK);
184 AddException("d", CONTENT_SETTING_ALLOW);
185 AddException("e", CONTENT_SETTING_BLOCK);
186
187 BuildWindow();
188
189 std::vector<int> rows;
190 rows.push_back(1);
191 rows.push_back(3);
192 SelectRows(rows);
193
194 Remove();
195
196 std::set<std::pair<int, int> > selected;
197 GetSelectedRows(&selected);
198
199 ASSERT_EQ(1u, selected.size());
200 EXPECT_EQ(1, selected.begin()->first);
201 EXPECT_EQ(1, selected.begin()->second);
202 EXPECT_THAT(Paths(), ElementsAre("a", "c", "e"));
203 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698