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