1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
|
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/string_util.h"
#include "base/time.h"
#include "chrome/browser/bookmarks/bookmark_table_model.h"
#include "chrome/test/testing_profile.h"
#include "grit/generated_resources.h"
#include "testing/gtest/include/gtest/gtest.h"
using base::Time;
using base::TimeDelta;
// Base class for bookmark model tests.
// Initial state of the bookmark model is as follows:
// bb
// url1 (t0)
// f1
// o
// url2 (t0 + 2)
// f2
// url3 (t0 + 1)
class BookmarkTableModelTest : public testing::Test,
public views::TableModelObserver {
public:
BookmarkTableModelTest()
: url1_("http://1"),
url2_("http://2"),
url3_("http://3"),
changed_count_(0),
item_changed_count_(0),
added_count_(0),
removed_count_(0) {
}
virtual void SetUp() {
profile_.reset(new TestingProfile());
profile_->CreateBookmarkModel(true);
// Populate with some default data.
Time t0 = Time::Now();
BookmarkNode* bb = bookmark_model()->GetBookmarkBarNode();
bookmark_model()->AddURLWithCreationTime(bb, 0, L"a", url1_, t0);
bookmark_model()->AddGroup(bb, 1, L"f1");
BookmarkNode* other = bookmark_model()->other_node();
bookmark_model()->AddURLWithCreationTime(other, 0, L"b",
url2_, t0 + TimeDelta::FromDays(2));
bookmark_model()->AddGroup(other, 1, L"f2");
bookmark_model()->AddURLWithCreationTime(other, 2, L"c", url3_,
t0 + TimeDelta::FromDays(1));
}
virtual void TearDown() {
model_.reset(NULL);
profile_.reset(NULL);
}
BookmarkModel* bookmark_model() const {
return profile_->GetBookmarkModel();
}
virtual void OnModelChanged() {
changed_count_++;
}
virtual void OnItemsChanged(int start, int length) {
item_changed_count_++;
}
virtual void OnItemsAdded(int start, int length) {
added_count_++;
}
virtual void OnItemsRemoved(int start, int length) {
removed_count_++;
}
void VerifyAndClearOberserverCounts(int changed_count, int item_changed_count,
int added_count, int removed_count) {
EXPECT_EQ(changed_count, changed_count_);
EXPECT_EQ(item_changed_count, item_changed_count_);
EXPECT_EQ(added_count, added_count_);
EXPECT_EQ(removed_count, removed_count_);
ResetCounts();
}
void ResetCounts() {
changed_count_ = item_changed_count_ = removed_count_ = added_count_ = 0;
}
void SetModel(BookmarkTableModel* model) {
if (model_.get())
model_->SetObserver(NULL);
model_.reset(model);
if (model_.get())
model_->SetObserver(this);
}
scoped_ptr<BookmarkTableModel> model_;
const GURL url1_;
const GURL url2_;
const GURL url3_;
private:
int changed_count_;
int item_changed_count_;
int added_count_;
int removed_count_;
scoped_ptr<TestingProfile> profile_;
};
// Verifies the count when showing various nodes.
TEST_F(BookmarkTableModelTest, FolderInitialState) {
SetModel(BookmarkTableModel::CreateBookmarkTableModelForFolder(
bookmark_model(), bookmark_model()->GetBookmarkBarNode()));
ASSERT_EQ(2, model_->RowCount());
EXPECT_TRUE(bookmark_model()->GetBookmarkBarNode()->GetChild(0) ==
model_->GetNodeForRow(0));
EXPECT_TRUE(bookmark_model()->GetBookmarkBarNode()->GetChild(1) ==
model_->GetNodeForRow(1));
SetModel(BookmarkTableModel::CreateBookmarkTableModelForFolder(
bookmark_model(), bookmark_model()->other_node()));
EXPECT_EQ(3, model_->RowCount());
}
// Verifies adding an item to folder model generates the correct event.
TEST_F(BookmarkTableModelTest, AddToFolder) {
BookmarkNode* other = bookmark_model()->other_node();
SetModel(BookmarkTableModel::CreateBookmarkTableModelForFolder(
bookmark_model(), other));
BookmarkNode* new_node = bookmark_model()->AddURL(other, 0, L"new", url1_);
// Should have gotten notification of the add.
VerifyAndClearOberserverCounts(0, 0, 1, 0);
ASSERT_EQ(4, model_->RowCount());
EXPECT_TRUE(new_node == model_->GetNodeForRow(0));
// Add to the bookmark bar, this shouldn't generate an event.
bookmark_model()->AddURL(bookmark_model()->GetBookmarkBarNode(), 0, L"new",
url1_);
VerifyAndClearOberserverCounts(0, 0, 0, 0);
}
// Verifies removing an item from folder model generates the correct event.
TEST_F(BookmarkTableModelTest, RemoveFromFolder) {
BookmarkNode* other = bookmark_model()->other_node();
SetModel(BookmarkTableModel::CreateBookmarkTableModelForFolder(
bookmark_model(), other));
bookmark_model()->Remove(other, 0);
// Should have gotten notification of the remove.
VerifyAndClearOberserverCounts(0, 0, 0, 1);
EXPECT_EQ(2, model_->RowCount());
// Remove from the bookmark bar, this shouldn't generate an event.
bookmark_model()->Remove(bookmark_model()->GetBookmarkBarNode(), 0);
VerifyAndClearOberserverCounts(0, 0, 0, 0);
}
// Verifies changing an item in the folder model generates the correct event.
TEST_F(BookmarkTableModelTest, ChangeFolder) {
BookmarkNode* other = bookmark_model()->other_node();
SetModel(BookmarkTableModel::CreateBookmarkTableModelForFolder(
bookmark_model(), other));
bookmark_model()->SetTitle(other->GetChild(0), L"new");
// Should have gotten notification of the change.
VerifyAndClearOberserverCounts(0, 1, 0, 0);
EXPECT_EQ(3, model_->RowCount());
// Change a node in the bookmark bar, this shouldn't generate an event.
bookmark_model()->SetTitle(
bookmark_model()->GetBookmarkBarNode()->GetChild(0), L"new2");
VerifyAndClearOberserverCounts(0, 0, 0, 0);
}
// Verifies show recently added shows the recently added, in order.
TEST_F(BookmarkTableModelTest, RecentlyBookmarkedOrder) {
SetModel(BookmarkTableModel::CreateRecentlyBookmarkedModel(bookmark_model()));
EXPECT_EQ(3, model_->RowCount());
BookmarkNode* bb = bookmark_model()->GetBookmarkBarNode();
BookmarkNode* other = bookmark_model()->other_node();
EXPECT_TRUE(other->GetChild(0) == model_->GetNodeForRow(0));
EXPECT_TRUE(other->GetChild(2) == model_->GetNodeForRow(1));
EXPECT_TRUE(bb->GetChild(0) == model_->GetNodeForRow(2));
}
// Verifies adding an item to recently added notifies observer.
TEST_F(BookmarkTableModelTest, AddToRecentlyBookmarked) {
SetModel(BookmarkTableModel::CreateRecentlyBookmarkedModel(bookmark_model()));
bookmark_model()->AddURL(bookmark_model()->other_node(), 0, L"new", url1_);
// Should have gotten notification of the add.
VerifyAndClearOberserverCounts(1, 0, 0, 0);
EXPECT_EQ(4, model_->RowCount());
// Add a folder, this shouldn't change the model.
bookmark_model()->AddGroup(bookmark_model()->GetBookmarkBarNode(), 0, L"new");
VerifyAndClearOberserverCounts(0, 0, 0, 0);
}
// Verifies removing an item from recently added notifies observer.
TEST_F(BookmarkTableModelTest, RemoveFromRecentlyBookmarked) {
SetModel(BookmarkTableModel::CreateRecentlyBookmarkedModel(bookmark_model()));
bookmark_model()->Remove(bookmark_model()->other_node(), 0);
// Should have gotten notification of the remove.
VerifyAndClearOberserverCounts(1, 0, 0, 0);
EXPECT_EQ(2, model_->RowCount());
// Remove a folder, this shouldn't change the model.
bookmark_model()->Remove(bookmark_model()->other_node(), 0);
VerifyAndClearOberserverCounts(0, 0, 0, 0);
}
// Verifies changing an item in recently added notifies observer.
TEST_F(BookmarkTableModelTest, ChangeRecentlyBookmarked) {
SetModel(BookmarkTableModel::CreateRecentlyBookmarkedModel(bookmark_model()));
bookmark_model()->SetTitle(bookmark_model()->other_node()->GetChild(0),
L"new");
// Should have gotten notification of the change.
VerifyAndClearOberserverCounts(0, 1, 0, 0);
// Change a folder, this shouldn't change the model.
bookmark_model()->SetTitle(bookmark_model()->other_node()->GetChild(1),
L"new");
VerifyAndClearOberserverCounts(0, 0, 0, 0);
}
// Verifies search finds the correct bookmarks.
TEST_F(BookmarkTableModelTest, Search) {
SetModel(BookmarkTableModel::CreateSearchTableModel(bookmark_model(), L"c"));
ASSERT_EQ(1, model_->RowCount());
EXPECT_TRUE(bookmark_model()->other_node()->GetChild(2) ==
model_->GetNodeForRow(0));
// Make sure IndexOfNode works.
EXPECT_EQ(0,
model_->IndexOfNode(bookmark_model()->other_node()->GetChild(2)));
}
// Verifies adding an item to search notifies observers.
TEST_F(BookmarkTableModelTest, AddToSearch) {
SetModel(BookmarkTableModel::CreateSearchTableModel(bookmark_model(), L"c"));
BookmarkNode* new_node =
bookmark_model()->AddURL(bookmark_model()->other_node(), 0, L"c", url1_);
// Should have gotten notification of the add.
VerifyAndClearOberserverCounts(0, 0, 1, 0);
ASSERT_EQ(2, model_->RowCount());
// New node should have gone to end.
EXPECT_TRUE(model_->GetNodeForRow(1) == new_node);
// Add a folder, this shouldn't change the model.
bookmark_model()->AddGroup(bookmark_model()->GetBookmarkBarNode(), 0, L"new");
VerifyAndClearOberserverCounts(0, 0, 0, 0);
EXPECT_EQ(2, model_->RowCount());
// Add a url that doesn't match search, this shouldn't change the model.
bookmark_model()->AddGroup(bookmark_model()->GetBookmarkBarNode(), 0, L"new");
VerifyAndClearOberserverCounts(0, 0, 0, 0);
EXPECT_EQ(2, model_->RowCount());
}
// Verifies removing an item updates search.
TEST_F(BookmarkTableModelTest, RemoveFromSearch) {
SetModel(BookmarkTableModel::CreateSearchTableModel(bookmark_model(), L"c"));
bookmark_model()->Remove(bookmark_model()->other_node(), 2);
// Should have gotten notification of the remove.
VerifyAndClearOberserverCounts(0, 0, 0, 1);
EXPECT_EQ(0, model_->RowCount());
// Remove a folder, this shouldn't change the model.
bookmark_model()->Remove(bookmark_model()->other_node(), 1);
VerifyAndClearOberserverCounts(0, 0, 0, 0);
// Remove another url that isn't in the model, this shouldn't change anything.
bookmark_model()->Remove(bookmark_model()->other_node(), 0);
VerifyAndClearOberserverCounts(0, 0, 0, 0);
}
// Verifies changing an item in search notifies observer.
TEST_F(BookmarkTableModelTest, ChangeSearch) {
SetModel(BookmarkTableModel::CreateSearchTableModel(bookmark_model(), L"c"));
bookmark_model()->SetTitle(bookmark_model()->other_node()->GetChild(2),
L"new");
// Should have gotten notification of the change.
VerifyAndClearOberserverCounts(0, 1, 0, 0);
// Change a folder, this shouldn't change the model.
bookmark_model()->SetTitle(bookmark_model()->other_node()->GetChild(1),
L"new");
VerifyAndClearOberserverCounts(0, 0, 0, 0);
// Change a url that isn't in the model, this shouldn't send change.
bookmark_model()->SetTitle(bookmark_model()->other_node()->GetChild(0),
L"new");
VerifyAndClearOberserverCounts(0, 0, 0, 0);
}
|