1 // Copyright 2013 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.
5 #include "chrome/browser/android/bookmarks/partner_bookmarks_shim.h"
7 #include "base/message_loop/message_loop.h"
8 #include "base/strings/string16.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
11 #include "chrome/test/base/testing_profile.h"
12 #include "components/bookmarks/browser/bookmark_model.h"
13 #include "components/bookmarks/test/bookmark_test_helpers.h"
14 #include "content/public/browser/browser_thread.h"
15 #include "content/public/test/test_browser_thread.h"
16 #include "testing/gmock/include/gmock/gmock.h"
17 #include "testing/gtest/include/gtest/gtest.h"
20 using bookmarks::BookmarkModel
;
21 using bookmarks::BookmarkNode
;
22 using bookmarks::BookmarkPermanentNode
;
25 class MockObserver
: public PartnerBookmarksShim::Observer
{
28 MOCK_METHOD1(PartnerShimChanged
, void(PartnerBookmarksShim
*));
29 MOCK_METHOD1(PartnerShimLoaded
, void(PartnerBookmarksShim
*));
32 DISALLOW_COPY_AND_ASSIGN(MockObserver
);
35 class PartnerBookmarksShimTest
: public testing::Test
{
37 PartnerBookmarksShimTest()
38 : ui_thread_(content::BrowserThread::UI
, &message_loop_
),
39 file_thread_(content::BrowserThread::FILE, &message_loop_
),
43 TestingProfile
* profile() const { return profile_
.get(); }
44 PartnerBookmarksShim
* partner_bookmarks_shim() const {
45 return PartnerBookmarksShim::BuildForBrowserContext(profile_
.get());
48 const BookmarkNode
* AddBookmark(const BookmarkNode
* parent
,
50 const base::string16
& title
) {
52 parent
= model_
->bookmark_bar_node();
53 return model_
->AddURL(parent
, parent
->child_count(), title
, url
);
56 const BookmarkNode
* AddFolder(const BookmarkNode
* parent
,
57 const base::string16
& title
) {
59 parent
= model_
->bookmark_bar_node();
60 return model_
->AddFolder(parent
, parent
->child_count(), title
);
65 void SetUp() override
{
66 profile_
.reset(new TestingProfile());
67 profile_
->CreateBookmarkModel(true);
69 model_
= BookmarkModelFactory::GetForProfile(profile_
.get());
70 bookmarks::test::WaitForBookmarkModelToLoad(model_
);
73 void TearDown() override
{
74 PartnerBookmarksShim::ClearInBrowserContextForTesting(profile_
.get());
75 PartnerBookmarksShim::ClearPartnerModelForTesting();
76 PartnerBookmarksShim::EnablePartnerBookmarksEditing();
80 scoped_ptr
<TestingProfile
> profile_
;
82 base::MessageLoopForUI message_loop_
;
83 content::TestBrowserThread ui_thread_
;
84 content::TestBrowserThread file_thread_
;
86 BookmarkModel
* model_
;
87 MockObserver observer_
;
89 DISALLOW_COPY_AND_ASSIGN(PartnerBookmarksShimTest
);
92 TEST_F(PartnerBookmarksShimTest
, GetNodeByID
) {
93 BookmarkNode
* root_partner_node
= new BookmarkPermanentNode(0);
94 BookmarkNode
* partner_folder1
= new BookmarkNode(1, GURL());
95 partner_folder1
->set_type(BookmarkNode::FOLDER
);
96 root_partner_node
->Add(partner_folder1
, root_partner_node
->child_count());
98 BookmarkNode
* partner_folder2
= new BookmarkNode(2, GURL());
99 partner_folder2
->set_type(BookmarkNode::FOLDER
);
100 partner_folder1
->Add(partner_folder2
, partner_folder1
->child_count());
102 BookmarkNode
* partner_bookmark1
= new BookmarkNode(3,
103 GURL("http://www.a.com"));
104 partner_bookmark1
->set_type(BookmarkNode::URL
);
105 partner_folder1
->Add(partner_bookmark1
, partner_folder1
->child_count());
107 BookmarkNode
* partner_bookmark2
= new BookmarkNode(4,
108 GURL("http://www.b.com"));
109 partner_bookmark2
->set_type(BookmarkNode::URL
);
110 partner_folder2
->Add(partner_bookmark2
, partner_folder2
->child_count());
112 PartnerBookmarksShim
* shim
= partner_bookmarks_shim();
113 ASSERT_FALSE(shim
->IsLoaded());
114 shim
->SetPartnerBookmarksRoot(root_partner_node
);
115 ASSERT_TRUE(shim
->IsLoaded());
117 ASSERT_TRUE(shim
->IsPartnerBookmark(root_partner_node
));
118 ASSERT_EQ(shim
->GetNodeByID(0), root_partner_node
);
119 ASSERT_EQ(shim
->GetNodeByID(1), partner_folder1
);
120 ASSERT_EQ(shim
->GetNodeByID(4), partner_bookmark2
);
123 TEST_F(PartnerBookmarksShimTest
, ObserverNotifiedOfLoadNoPartnerBookmarks
) {
124 EXPECT_CALL(observer_
, PartnerShimLoaded(_
)).Times(0);
125 PartnerBookmarksShim
* shim
= partner_bookmarks_shim();
126 shim
->AddObserver(&observer_
);
128 EXPECT_CALL(observer_
, PartnerShimLoaded(shim
)).Times(1);
129 shim
->SetPartnerBookmarksRoot(NULL
);
132 TEST_F(PartnerBookmarksShimTest
, ObserverNotifiedOfLoadWithPartnerBookmarks
) {
133 EXPECT_CALL(observer_
, PartnerShimLoaded(_
)).Times(0);
135 BookmarkNode
* root_partner_node
= new BookmarkPermanentNode(id
++);
136 BookmarkNode
* partner_bookmark1
= new BookmarkNode(id
++,
137 GURL("http://www.a.com"));
138 partner_bookmark1
->set_type(BookmarkNode::URL
);
139 root_partner_node
->Add(partner_bookmark1
, root_partner_node
->child_count());
141 PartnerBookmarksShim
* shim
= partner_bookmarks_shim();
142 shim
->AddObserver(&observer_
);
144 EXPECT_CALL(observer_
, PartnerShimLoaded(shim
)).Times(1);
145 shim
->SetPartnerBookmarksRoot(root_partner_node
);
148 TEST_F(PartnerBookmarksShimTest
, RemoveBookmarks
) {
149 PartnerBookmarksShim
* shim
= partner_bookmarks_shim();
150 shim
->AddObserver(&observer_
);
152 EXPECT_CALL(observer_
, PartnerShimLoaded(shim
)).Times(0);
153 EXPECT_CALL(observer_
, PartnerShimChanged(shim
)).Times(0);
155 BookmarkNode
* root_partner_node
= new BookmarkPermanentNode(0);
156 root_partner_node
->SetTitle(base::ASCIIToUTF16("Partner bookmarks"));
158 BookmarkNode
* partner_folder1
= new BookmarkNode(1, GURL("http://www.a.net"));
159 partner_folder1
->set_type(BookmarkNode::FOLDER
);
160 root_partner_node
->Add(partner_folder1
, root_partner_node
->child_count());
162 BookmarkNode
* partner_folder2
= new BookmarkNode(2, GURL("http://www.b.net"));
163 partner_folder2
->set_type(BookmarkNode::FOLDER
);
164 root_partner_node
->Add(partner_folder2
, root_partner_node
->child_count());
166 BookmarkNode
* partner_bookmark1
= new BookmarkNode(3,
167 GURL("http://www.a.com"));
168 partner_bookmark1
->set_type(BookmarkNode::URL
);
169 partner_folder1
->Add(partner_bookmark1
, partner_folder1
->child_count());
171 BookmarkNode
* partner_bookmark2
= new BookmarkNode(4,
172 GURL("http://www.b.com"));
173 partner_bookmark2
->set_type(BookmarkNode::URL
);
174 partner_folder2
->Add(partner_bookmark2
, partner_folder2
->child_count());
176 BookmarkNode
* partner_folder3
= new BookmarkNode(5, GURL("http://www.c.net"));
177 partner_folder3
->set_type(BookmarkNode::FOLDER
);
178 partner_folder2
->Add(partner_folder3
, partner_folder2
->child_count());
180 BookmarkNode
* partner_bookmark3
= new BookmarkNode(6,
181 GURL("http://www.c.com"));
182 partner_bookmark3
->set_type(BookmarkNode::URL
);
183 partner_folder3
->Add(partner_bookmark3
, partner_folder3
->child_count());
185 ASSERT_FALSE(shim
->IsLoaded());
186 EXPECT_CALL(observer_
, PartnerShimLoaded(shim
)).Times(1);
187 shim
->SetPartnerBookmarksRoot(root_partner_node
);
188 ASSERT_TRUE(shim
->IsLoaded());
190 EXPECT_EQ(root_partner_node
, shim
->GetNodeByID(0));
191 EXPECT_EQ(partner_folder1
, shim
->GetNodeByID(1));
192 EXPECT_EQ(partner_folder2
, shim
->GetNodeByID(2));
193 EXPECT_EQ(partner_bookmark1
, shim
->GetNodeByID(3));
194 EXPECT_EQ(partner_bookmark2
, shim
->GetNodeByID(4));
195 EXPECT_EQ(partner_folder3
, shim
->GetNodeByID(5));
196 EXPECT_EQ(partner_bookmark3
, shim
->GetNodeByID(6));
198 EXPECT_TRUE(shim
->IsReachable(root_partner_node
));
199 EXPECT_TRUE(shim
->IsReachable(partner_folder1
));
200 EXPECT_TRUE(shim
->IsReachable(partner_folder2
));
201 EXPECT_TRUE(shim
->IsReachable(partner_bookmark1
));
202 EXPECT_TRUE(shim
->IsReachable(partner_bookmark2
));
203 EXPECT_TRUE(shim
->IsReachable(partner_folder3
));
204 EXPECT_TRUE(shim
->IsReachable(partner_bookmark3
));
206 EXPECT_CALL(observer_
, PartnerShimChanged(shim
)).Times(1);
207 shim
->RemoveBookmark(partner_bookmark2
);
208 EXPECT_TRUE(shim
->IsReachable(root_partner_node
));
209 EXPECT_TRUE(shim
->IsReachable(partner_folder1
));
210 EXPECT_TRUE(shim
->IsReachable(partner_folder2
));
211 EXPECT_TRUE(shim
->IsReachable(partner_bookmark1
));
212 EXPECT_FALSE(shim
->IsReachable(partner_bookmark2
));
213 EXPECT_TRUE(shim
->IsReachable(partner_folder3
));
214 EXPECT_TRUE(shim
->IsReachable(partner_bookmark3
));
216 EXPECT_CALL(observer_
, PartnerShimChanged(shim
)).Times(1);
217 shim
->RemoveBookmark(partner_folder1
);
218 EXPECT_TRUE(shim
->IsReachable(root_partner_node
));
219 EXPECT_FALSE(shim
->IsReachable(partner_folder1
));
220 EXPECT_TRUE(shim
->IsReachable(partner_folder2
));
221 EXPECT_FALSE(shim
->IsReachable(partner_bookmark1
));
222 EXPECT_FALSE(shim
->IsReachable(partner_bookmark2
));
223 EXPECT_TRUE(shim
->IsReachable(partner_folder3
));
224 EXPECT_TRUE(shim
->IsReachable(partner_bookmark3
));
226 EXPECT_CALL(observer_
, PartnerShimChanged(shim
)).Times(1);
227 shim
->RemoveBookmark(root_partner_node
);
228 EXPECT_FALSE(shim
->IsReachable(root_partner_node
));
229 EXPECT_FALSE(shim
->IsReachable(partner_folder1
));
230 EXPECT_FALSE(shim
->IsReachable(partner_folder2
));
231 EXPECT_FALSE(shim
->IsReachable(partner_bookmark1
));
232 EXPECT_FALSE(shim
->IsReachable(partner_bookmark2
));
233 EXPECT_FALSE(shim
->IsReachable(partner_folder3
));
234 EXPECT_FALSE(shim
->IsReachable(partner_bookmark3
));
237 TEST_F(PartnerBookmarksShimTest
, RenameBookmarks
) {
238 PartnerBookmarksShim
* shim
= partner_bookmarks_shim();
239 shim
->AddObserver(&observer_
);
241 EXPECT_CALL(observer_
, PartnerShimLoaded(shim
)).Times(0);
242 EXPECT_CALL(observer_
, PartnerShimChanged(shim
)).Times(0);
244 BookmarkNode
* root_partner_node
= new BookmarkPermanentNode(0);
245 root_partner_node
->SetTitle(base::ASCIIToUTF16("Partner bookmarks"));
247 BookmarkNode
* partner_folder1
= new BookmarkNode(1, GURL("http://www.a.net"));
248 partner_folder1
->set_type(BookmarkNode::FOLDER
);
249 partner_folder1
->SetTitle(base::ASCIIToUTF16("a.net"));
250 root_partner_node
->Add(partner_folder1
, root_partner_node
->child_count());
252 BookmarkNode
* partner_folder2
= new BookmarkNode(2, GURL("http://www.b.net"));
253 partner_folder2
->set_type(BookmarkNode::FOLDER
);
254 partner_folder2
->SetTitle(base::ASCIIToUTF16("b.net"));
255 root_partner_node
->Add(partner_folder2
, root_partner_node
->child_count());
257 BookmarkNode
* partner_bookmark1
= new BookmarkNode(3,
258 GURL("http://www.a.com"));
259 partner_bookmark1
->set_type(BookmarkNode::URL
);
260 partner_bookmark1
->SetTitle(base::ASCIIToUTF16("a.com"));
261 partner_folder1
->Add(partner_bookmark1
, partner_folder1
->child_count());
263 BookmarkNode
* partner_bookmark2
= new BookmarkNode(4,
264 GURL("http://www.b.com"));
265 partner_bookmark2
->set_type(BookmarkNode::URL
);
266 partner_bookmark2
->SetTitle(base::ASCIIToUTF16("b.com"));
267 partner_folder2
->Add(partner_bookmark2
, partner_folder2
->child_count());
269 ASSERT_FALSE(shim
->IsLoaded());
270 EXPECT_CALL(observer_
, PartnerShimLoaded(shim
)).Times(1);
271 shim
->SetPartnerBookmarksRoot(root_partner_node
);
272 ASSERT_TRUE(shim
->IsLoaded());
274 EXPECT_EQ(root_partner_node
, shim
->GetNodeByID(0));
275 EXPECT_EQ(partner_folder1
, shim
->GetNodeByID(1));
276 EXPECT_EQ(partner_folder2
, shim
->GetNodeByID(2));
277 EXPECT_EQ(partner_bookmark1
, shim
->GetNodeByID(3));
278 EXPECT_EQ(partner_bookmark2
, shim
->GetNodeByID(4));
280 EXPECT_TRUE(shim
->IsReachable(root_partner_node
));
281 EXPECT_TRUE(shim
->IsReachable(partner_folder1
));
282 EXPECT_TRUE(shim
->IsReachable(partner_folder2
));
283 EXPECT_TRUE(shim
->IsReachable(partner_bookmark1
));
284 EXPECT_TRUE(shim
->IsReachable(partner_bookmark2
));
286 EXPECT_CALL(observer_
, PartnerShimChanged(shim
)).Times(1);
287 EXPECT_EQ(base::ASCIIToUTF16("b.com"), shim
->GetTitle(partner_bookmark2
));
288 shim
->RenameBookmark(partner_bookmark2
, base::ASCIIToUTF16("b2.com"));
289 EXPECT_EQ(base::ASCIIToUTF16("b2.com"), shim
->GetTitle(partner_bookmark2
));
291 EXPECT_TRUE(shim
->IsReachable(root_partner_node
));
292 EXPECT_TRUE(shim
->IsReachable(partner_folder1
));
293 EXPECT_TRUE(shim
->IsReachable(partner_folder2
));
294 EXPECT_TRUE(shim
->IsReachable(partner_bookmark1
));
295 EXPECT_TRUE(shim
->IsReachable(partner_bookmark2
));
297 EXPECT_CALL(observer_
, PartnerShimChanged(shim
)).Times(1);
298 EXPECT_EQ(base::ASCIIToUTF16("a.net"), shim
->GetTitle(partner_folder1
));
299 shim
->RenameBookmark(partner_folder1
, base::ASCIIToUTF16("a2.net"));
300 EXPECT_EQ(base::ASCIIToUTF16("a2.net"), shim
->GetTitle(partner_folder1
));
302 EXPECT_TRUE(shim
->IsReachable(root_partner_node
));
303 EXPECT_TRUE(shim
->IsReachable(partner_folder1
));
304 EXPECT_TRUE(shim
->IsReachable(partner_folder2
));
305 EXPECT_TRUE(shim
->IsReachable(partner_bookmark1
));
306 EXPECT_TRUE(shim
->IsReachable(partner_bookmark2
));
308 EXPECT_CALL(observer_
, PartnerShimChanged(shim
)).Times(1);
309 EXPECT_EQ(base::ASCIIToUTF16("Partner bookmarks"),
310 shim
->GetTitle(root_partner_node
));
311 shim
->RenameBookmark(root_partner_node
, base::ASCIIToUTF16("Partner"));
312 EXPECT_EQ(base::ASCIIToUTF16("Partner"), shim
->GetTitle(root_partner_node
));
314 EXPECT_TRUE(shim
->IsReachable(root_partner_node
));
315 EXPECT_TRUE(shim
->IsReachable(partner_folder1
));
316 EXPECT_TRUE(shim
->IsReachable(partner_folder2
));
317 EXPECT_TRUE(shim
->IsReachable(partner_bookmark1
));
318 EXPECT_TRUE(shim
->IsReachable(partner_bookmark2
));
321 TEST_F(PartnerBookmarksShimTest
, SaveLoadProfile
) {
323 PartnerBookmarksShim
* shim
= partner_bookmarks_shim();
324 shim
->AddObserver(&observer_
);
326 EXPECT_CALL(observer_
, PartnerShimLoaded(shim
)).Times(0);
327 EXPECT_CALL(observer_
, PartnerShimChanged(shim
)).Times(0);
329 BookmarkNode
* root_partner_node
= new BookmarkPermanentNode(0);
330 root_partner_node
->SetTitle(base::ASCIIToUTF16("Partner bookmarks"));
332 BookmarkNode
* partner_folder1
= new BookmarkNode(1, GURL("http://a.net"));
333 partner_folder1
->set_type(BookmarkNode::FOLDER
);
334 partner_folder1
->SetTitle(base::ASCIIToUTF16("a.net"));
335 root_partner_node
->Add(partner_folder1
, root_partner_node
->child_count());
337 BookmarkNode
* partner_bookmark1
= new BookmarkNode(3,
338 GURL("http://a.com"));
339 partner_bookmark1
->set_type(BookmarkNode::URL
);
340 partner_bookmark1
->SetTitle(base::ASCIIToUTF16("a.com"));
341 partner_folder1
->Add(partner_bookmark1
, partner_folder1
->child_count());
343 BookmarkNode
* partner_bookmark2
= new BookmarkNode(5,
344 GURL("http://b.com"));
345 partner_bookmark2
->set_type(BookmarkNode::URL
);
346 partner_bookmark2
->SetTitle(base::ASCIIToUTF16("b.com"));
347 partner_folder1
->Add(partner_bookmark2
, partner_folder1
->child_count());
349 ASSERT_FALSE(shim
->IsLoaded());
350 EXPECT_CALL(observer_
, PartnerShimLoaded(shim
)).Times(1);
351 shim
->SetPartnerBookmarksRoot(root_partner_node
);
352 ASSERT_TRUE(shim
->IsLoaded());
354 EXPECT_CALL(observer_
, PartnerShimChanged(shim
)).Times(2);
355 shim
->RenameBookmark(partner_bookmark1
, base::ASCIIToUTF16("a2.com"));
356 shim
->RemoveBookmark(partner_bookmark2
);
357 EXPECT_EQ(base::ASCIIToUTF16("a2.com"), shim
->GetTitle(partner_bookmark1
));
358 EXPECT_FALSE(shim
->IsReachable(partner_bookmark2
));
361 PartnerBookmarksShim::ClearInBrowserContextForTesting(profile_
.get());
364 PartnerBookmarksShim
* shim
= partner_bookmarks_shim();
365 shim
->AddObserver(&observer_
);
367 EXPECT_CALL(observer_
, PartnerShimLoaded(shim
)).Times(0);
368 EXPECT_CALL(observer_
, PartnerShimChanged(shim
)).Times(0);
369 ASSERT_TRUE(shim
->IsLoaded());
371 const BookmarkNode
* partner_bookmark1
= shim
->GetNodeByID(3);
372 const BookmarkNode
* partner_bookmark2
= shim
->GetNodeByID(5);
374 EXPECT_EQ(base::ASCIIToUTF16("a2.com"), shim
->GetTitle(partner_bookmark1
));
375 EXPECT_FALSE(shim
->IsReachable(partner_bookmark2
));
379 TEST_F(PartnerBookmarksShimTest
, DisableEditing
) {
380 PartnerBookmarksShim
* shim
= partner_bookmarks_shim();
381 shim
->AddObserver(&observer_
);
383 EXPECT_CALL(observer_
, PartnerShimLoaded(shim
)).Times(0);
384 EXPECT_CALL(observer_
, PartnerShimChanged(shim
)).Times(0);
386 BookmarkNode
* root_partner_node
= new BookmarkPermanentNode(0);
387 root_partner_node
->SetTitle(base::ASCIIToUTF16("Partner bookmarks"));
389 BookmarkNode
* partner_bookmark1
= new BookmarkNode(3, GURL("http://a"));
390 partner_bookmark1
->set_type(BookmarkNode::URL
);
391 partner_bookmark1
->SetTitle(base::ASCIIToUTF16("a"));
392 root_partner_node
->Add(partner_bookmark1
, root_partner_node
->child_count());
394 BookmarkNode
* partner_bookmark2
= new BookmarkNode(3, GURL("http://b"));
395 partner_bookmark2
->set_type(BookmarkNode::URL
);
396 partner_bookmark2
->SetTitle(base::ASCIIToUTF16("b"));
397 root_partner_node
->Add(partner_bookmark2
, root_partner_node
->child_count());
399 ASSERT_FALSE(shim
->IsLoaded());
400 EXPECT_CALL(observer_
, PartnerShimLoaded(shim
)).Times(1);
401 shim
->SetPartnerBookmarksRoot(root_partner_node
);
402 ASSERT_TRUE(shim
->IsLoaded());
404 // Check that edits work by default.
405 EXPECT_CALL(observer_
, PartnerShimChanged(shim
)).Times(2);
406 shim
->RenameBookmark(partner_bookmark1
, base::ASCIIToUTF16("a2.com"));
407 shim
->RemoveBookmark(partner_bookmark2
);
408 EXPECT_EQ(base::ASCIIToUTF16("a2.com"), shim
->GetTitle(partner_bookmark1
));
409 EXPECT_FALSE(shim
->IsReachable(partner_bookmark2
));
411 // Disable edits and check that edits are not applied anymore.
412 PartnerBookmarksShim::DisablePartnerBookmarksEditing();
413 EXPECT_EQ(base::ASCIIToUTF16("a"), shim
->GetTitle(partner_bookmark1
));
414 EXPECT_TRUE(shim
->IsReachable(partner_bookmark2
));