1 // Copyright (c) 2012 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/profiles/profile.h"
6 #include "chrome/browser/sync/test/integration/bookmarks_helper.h"
7 #include "chrome/browser/sync/test/integration/profile_sync_service_harness.h"
8 #include "chrome/browser/sync/test/integration/sync_test.h"
9 #include "ui/base/layout.h"
11 using bookmarks_helper::AddFolder
;
12 using bookmarks_helper::AddURL
;
13 using bookmarks_helper::Create1xFaviconFromPNGFile
;
14 using bookmarks_helper::GetBookmarkBarNode
;
15 using bookmarks_helper::GetBookmarkModel
;
16 using bookmarks_helper::GetOtherNode
;
17 using bookmarks_helper::ModelMatchesVerifier
;
18 using bookmarks_helper::Move
;
19 using bookmarks_helper::Remove
;
20 using bookmarks_helper::RemoveAll
;
21 using bookmarks_helper::SetFavicon
;
22 using bookmarks_helper::SetTitle
;
24 class SingleClientBookmarksSyncTest
: public SyncTest
{
26 SingleClientBookmarksSyncTest() : SyncTest(SINGLE_CLIENT
) {}
27 virtual ~SingleClientBookmarksSyncTest() {}
30 DISALLOW_COPY_AND_ASSIGN(SingleClientBookmarksSyncTest
);
33 IN_PROC_BROWSER_TEST_F(SingleClientBookmarksSyncTest
, Sanity
) {
34 ASSERT_TRUE(SetupClients()) << "SetupClients() failed.";
40 // -> http://mail.google.com "tier1_a_url0"
41 // -> http://www.pandora.com "tier1_a_url1"
42 // -> http://www.facebook.com "tier1_a_url2"
44 // -> http://www.nhl.com "tier1_b_url0"
45 const BookmarkNode
* top
= AddFolder(0, GetOtherNode(0), 0, L
"top");
46 const BookmarkNode
* tier1_a
= AddFolder(0, top
, 0, L
"tier1_a");
47 const BookmarkNode
* tier1_b
= AddFolder(0, top
, 1, L
"tier1_b");
48 const BookmarkNode
* tier1_a_url0
= AddURL(
49 0, tier1_a
, 0, L
"tier1_a_url0", GURL("http://mail.google.com"));
50 const BookmarkNode
* tier1_a_url1
= AddURL(
51 0, tier1_a
, 1, L
"tier1_a_url1", GURL("http://www.pandora.com"));
52 const BookmarkNode
* tier1_a_url2
= AddURL(
53 0, tier1_a
, 2, L
"tier1_a_url2", GURL("http://www.facebook.com"));
54 const BookmarkNode
* tier1_b_url0
= AddURL(
55 0, tier1_b
, 0, L
"tier1_b_url0", GURL("http://www.nhl.com"));
57 // Setup sync, wait for its completion, and make sure changes were synced.
58 ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
59 ASSERT_TRUE(GetClient(0)->AwaitFullSyncCompletion());
60 ASSERT_TRUE(ModelMatchesVerifier(0));
62 // Ultimately we want to end up with the following model; but this test is
63 // more about the journey than the destination.
66 // -> CNN (www.cnn.com)
68 // -> tier1_a_url2 (www.facebook.com)
69 // -> tier1_a_url1 (www.pandora.com)
70 // -> Porsche (www.porsche.com)
71 // -> Bank of America (www.bankofamerica.com)
76 // -> Wired News (www.wired.com)
80 // -> Toronto Maple Leafs (mapleleafs.nhl.com)
81 // -> Wynn (www.wynnlasvegas.com)
83 const BookmarkNode
* bar
= GetBookmarkBarNode(0);
84 const BookmarkNode
* cnn
= AddURL(0, bar
, 0, L
"CNN",
85 GURL("http://www.cnn.com"));
86 ASSERT_TRUE(cnn
!= NULL
);
87 Move(0, tier1_a
, bar
, 1);
89 // Wait for the bookmark position change to sync.
90 ASSERT_TRUE(GetClient(0)->AwaitFullSyncCompletion());
91 ASSERT_TRUE(ModelMatchesVerifier(0));
93 const BookmarkNode
* porsche
= AddURL(0, bar
, 2, L
"Porsche",
94 GURL("http://www.porsche.com"));
95 // Rearrange stuff in tier1_a.
96 ASSERT_EQ(tier1_a
, tier1_a_url2
->parent());
97 ASSERT_EQ(tier1_a
, tier1_a_url1
->parent());
98 Move(0, tier1_a_url2
, tier1_a
, 0);
99 Move(0, tier1_a_url1
, tier1_a
, 2);
101 // Wait for the rearranged hierarchy to sync.
102 ASSERT_TRUE(GetClient(0)->AwaitFullSyncCompletion());
103 ASSERT_TRUE(ModelMatchesVerifier(0));
105 ASSERT_EQ(1, tier1_a_url0
->parent()->GetIndexOf(tier1_a_url0
));
106 Move(0, tier1_a_url0
, bar
, bar
->child_count());
107 const BookmarkNode
* boa
= AddURL(0, bar
, bar
->child_count(),
108 L
"Bank of America", GURL("https://www.bankofamerica.com"));
109 ASSERT_TRUE(boa
!= NULL
);
110 Move(0, tier1_a_url0
, top
, top
->child_count());
111 const BookmarkNode
* bubble
= AddURL(
112 0, bar
, bar
->child_count(), L
"Seattle Bubble",
113 GURL("http://seattlebubble.com"));
114 ASSERT_TRUE(bubble
!= NULL
);
115 const BookmarkNode
* wired
= AddURL(0, bar
, 2, L
"Wired News",
116 GURL("http://www.wired.com"));
117 const BookmarkNode
* tier2_b
= AddFolder(
118 0, tier1_b
, 0, L
"tier2_b");
119 Move(0, tier1_b_url0
, tier2_b
, 0);
120 Move(0, porsche
, bar
, 0);
121 SetTitle(0, wired
, L
"News Wired");
122 SetTitle(0, porsche
, L
"ICanHazPorsche?");
124 // Wait for the title change to sync.
125 ASSERT_TRUE(GetClient(0)->AwaitFullSyncCompletion());
126 ASSERT_TRUE(ModelMatchesVerifier(0));
128 ASSERT_EQ(tier1_a_url0
->id(), top
->GetChild(top
->child_count() - 1)->id());
129 Remove(0, top
, top
->child_count() - 1);
130 Move(0, wired
, tier1_b
, 0);
131 Move(0, porsche
, bar
, 3);
132 const BookmarkNode
* tier3_b
= AddFolder(0, tier2_b
, 1, L
"tier3_b");
133 const BookmarkNode
* leafs
= AddURL(
134 0, tier1_a
, 0, L
"Toronto Maple Leafs", GURL("http://mapleleafs.nhl.com"));
135 const BookmarkNode
* wynn
= AddURL(0, bar
, 1, L
"Wynn",
136 GURL("http://www.wynnlasvegas.com"));
138 Move(0, wynn
, tier3_b
, 0);
139 Move(0, leafs
, tier3_b
, 0);
141 // Wait for newly added bookmarks to sync.
142 ASSERT_TRUE(GetClient(0)->AwaitFullSyncCompletion());
143 ASSERT_TRUE(ModelMatchesVerifier(0));
146 // Test that a client doesn't mutate the favicon data in the process
147 // of storing the favicon data from sync to the database or in the process
148 // of requesting data from the database for sync.
149 IN_PROC_BROWSER_TEST_F(SingleClientBookmarksSyncTest
,
150 SetFaviconHiDPIDifferentCodec
) {
151 // Set the supported scale factors to 1x and 2x such that
152 // BookmarkModel::GetFavicon() requests both 1x and 2x.
153 // 1x -> for sync, 2x -> for the UI.
154 std::vector
<ui::ScaleFactor
> supported_scale_factors
;
155 supported_scale_factors
.push_back(ui::SCALE_FACTOR_100P
);
156 supported_scale_factors
.push_back(ui::SCALE_FACTOR_200P
);
157 ui::SetSupportedScaleFactors(supported_scale_factors
);
159 ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
160 ASSERT_TRUE(ModelMatchesVerifier(0));
162 const GURL
page_url("http://www.google.com");
163 const GURL
icon_url("http://www.google.com/favicon.ico");
164 const BookmarkNode
* bookmark
= AddURL(0, L
"title", page_url
);
166 // Simulate receiving a favicon from sync encoded by a different PNG encoder
167 // than the one native to the OS. This tests the PNG data is not decoded to
168 // SkBitmap (or any other image format) then encoded back to PNG on the path
169 // between sync and the database.
170 gfx::Image original_favicon
= Create1xFaviconFromPNGFile(
171 "favicon_cocoa_png_codec.png");
172 ASSERT_FALSE(original_favicon
.IsEmpty());
173 SetFavicon(0, bookmark
, icon_url
, original_favicon
,
174 bookmarks_helper::FROM_SYNC
);
176 ASSERT_TRUE(GetClient(0)->AwaitFullSyncCompletion());
177 ASSERT_TRUE(ModelMatchesVerifier(0));
179 scoped_refptr
<base::RefCountedMemory
> original_favicon_bytes
=
180 original_favicon
.As1xPNGBytes();
181 gfx::Image final_favicon
= GetBookmarkModel(0)->GetFavicon(bookmark
);
182 scoped_refptr
<base::RefCountedMemory
> final_favicon_bytes
=
183 final_favicon
.As1xPNGBytes();
185 // Check that the data was not mutated from the original.
186 EXPECT_TRUE(original_favicon_bytes
.get());
187 EXPECT_TRUE(original_favicon_bytes
->Equals(final_favicon_bytes
));
190 IN_PROC_BROWSER_TEST_F(SingleClientBookmarksSyncTest
,
191 BookmarkAllNodesRemovedEvent
) {
192 ASSERT_TRUE(SetupClients()) << "SetupClients() failed.";
197 // -> http://mail.google.com
198 // -> http://www.google.com
199 // -> http://news.google.com
200 // -> http://yahoo.com
201 // -> http://www.cnn.com
205 // -> http://yahoo.com
206 // -> http://gmail.com
208 const BookmarkNode
* folder0
= AddFolder(0, GetOtherNode(0), 0, L
"folder0");
209 const BookmarkNode
* tier1_a
= AddFolder(0, folder0
, 0, L
"tier1_a");
210 ASSERT_TRUE(AddURL(0, folder0
, 1, L
"News", GURL("http://news.google.com")));
211 ASSERT_TRUE(AddURL(0, folder0
, 2, L
"Yahoo", GURL("http://www.yahoo.com")));
212 ASSERT_TRUE(AddURL(0, tier1_a
, 0, L
"Gmail", GURL("http://mail.google.com")));
213 ASSERT_TRUE(AddURL(0, tier1_a
, 1, L
"Google", GURL("http://www.google.com")));
215 AddURL(0, GetOtherNode(0), 1, L
"CNN", GURL("http://www.cnn.com")));
217 ASSERT_TRUE(AddFolder(0, GetBookmarkBarNode(0), 0, L
"empty_folder"));
218 const BookmarkNode
* folder1
=
219 AddFolder(0, GetBookmarkBarNode(0), 1, L
"folder1");
220 ASSERT_TRUE(AddURL(0, folder1
, 0, L
"Yahoo", GURL("http://www.yahoo.com")));
222 AddURL(0, GetBookmarkBarNode(0), 2, L
"Gmail", GURL("http://gmail.com")));
224 // Set up sync, wait for its completion and verify that changes propagated.
225 ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
226 ASSERT_TRUE(GetClient(0)->AwaitFullSyncCompletion());
227 ASSERT_TRUE(ModelMatchesVerifier(0));
229 // Remove all bookmarks and wait for sync completion.
231 ASSERT_TRUE(GetClient(0)->AwaitFullSyncCompletion());
232 // Verify other node has no children now.
233 EXPECT_EQ(0, GetOtherNode(0)->child_count());
234 EXPECT_EQ(0, GetBookmarkBarNode(0)->child_count());
235 // Verify model matches verifier.
236 ASSERT_TRUE(ModelMatchesVerifier(0));