single_client_bookmarks_sync_test.cc revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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.
4
5#include "chrome/browser/profiles/profile.h"
6#include "chrome/browser/sync/profile_sync_service_harness.h"
7#include "chrome/browser/sync/test/integration/bookmarks_helper.h"
8#include "chrome/browser/sync/test/integration/sync_test.h"
9#include "ui/base/layout.h"
10
11using bookmarks_helper::AddFolder;
12using bookmarks_helper::AddURL;
13using bookmarks_helper::Create1xFaviconFromPNGFile;
14using bookmarks_helper::GetBookmarkBarNode;
15using bookmarks_helper::GetBookmarkModel;
16using bookmarks_helper::GetOtherNode;
17using bookmarks_helper::ModelMatchesVerifier;
18using bookmarks_helper::Move;
19using bookmarks_helper::Remove;
20using bookmarks_helper::SetFavicon;
21using bookmarks_helper::SetTitle;
22
23class SingleClientBookmarksSyncTest : public SyncTest {
24 public:
25  SingleClientBookmarksSyncTest() : SyncTest(SINGLE_CLIENT) {}
26  virtual ~SingleClientBookmarksSyncTest() {}
27
28 private:
29  DISALLOW_COPY_AND_ASSIGN(SingleClientBookmarksSyncTest);
30};
31
32IN_PROC_BROWSER_TEST_F(SingleClientBookmarksSyncTest, OfflineToOnline) {
33  ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
34
35  DisableNetwork(GetProfile(0));
36  const BookmarkNode* node = AddFolder(0, L"title");
37  SetTitle(0, node, L"new_title");
38  // Expect that we backoff exponentially while we are unable to contact the
39  // server.
40  ASSERT_TRUE(GetClient(0)->AwaitExponentialBackoffVerification());
41
42  EnableNetwork(GetProfile(0));
43  ASSERT_TRUE(GetClient(0)->AwaitFullSyncCompletion("Commit changes."));
44  ASSERT_TRUE(ModelMatchesVerifier(0));
45}
46
47IN_PROC_BROWSER_TEST_F(SingleClientBookmarksSyncTest, Sanity) {
48  ASSERT_TRUE(SetupClients()) << "SetupClients() failed.";
49
50  // Starting state:
51  // other_node
52  //    -> top
53  //      -> tier1_a
54  //        -> http://mail.google.com  "tier1_a_url0"
55  //        -> http://www.pandora.com  "tier1_a_url1"
56  //        -> http://www.facebook.com "tier1_a_url2"
57  //      -> tier1_b
58  //        -> http://www.nhl.com "tier1_b_url0"
59  const BookmarkNode* top = AddFolder(0, GetOtherNode(0), 0, L"top");
60  const BookmarkNode* tier1_a = AddFolder(0, top, 0, L"tier1_a");
61  const BookmarkNode* tier1_b = AddFolder(0, top, 1, L"tier1_b");
62  const BookmarkNode* tier1_a_url0 = AddURL(
63      0, tier1_a, 0, L"tier1_a_url0", GURL("http://mail.google.com"));
64  const BookmarkNode* tier1_a_url1 = AddURL(
65      0, tier1_a, 1, L"tier1_a_url1", GURL("http://www.pandora.com"));
66  const BookmarkNode* tier1_a_url2 = AddURL(
67      0, tier1_a, 2, L"tier1_a_url2", GURL("http://www.facebook.com"));
68  const BookmarkNode* tier1_b_url0 = AddURL(
69      0, tier1_b, 0, L"tier1_b_url0", GURL("http://www.nhl.com"));
70
71  ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
72  ASSERT_TRUE(GetClient(0)->AwaitFullSyncCompletion(
73      "Waiting for initial sync completed."));
74  ASSERT_TRUE(ModelMatchesVerifier(0));
75
76  //  Ultimately we want to end up with the following model; but this test is
77  //  more about the journey than the destination.
78  //
79  //  bookmark_bar
80  //    -> CNN (www.cnn.com)
81  //    -> tier1_a
82  //      -> tier1_a_url2 (www.facebook.com)
83  //      -> tier1_a_url1 (www.pandora.com)
84  //    -> Porsche (www.porsche.com)
85  //    -> Bank of America (www.bankofamerica.com)
86  //    -> Seattle Bubble
87  //  other_node
88  //    -> top
89  //      -> tier1_b
90  //        -> Wired News (www.wired.com)
91  //        -> tier2_b
92  //          -> tier1_b_url0
93  //          -> tier3_b
94  //            -> Toronto Maple Leafs (mapleleafs.nhl.com)
95  //            -> Wynn (www.wynnlasvegas.com)
96  //      -> tier1_a_url0
97  const BookmarkNode* bar = GetBookmarkBarNode(0);
98  const BookmarkNode* cnn = AddURL(0, bar, 0, L"CNN",
99      GURL("http://www.cnn.com"));
100  ASSERT_TRUE(cnn != NULL);
101  Move(0, tier1_a, bar, 1);
102  ASSERT_TRUE(GetClient(0)->AwaitFullSyncCompletion("Bookmark moved."));
103  ASSERT_TRUE(ModelMatchesVerifier(0));
104
105  const BookmarkNode* porsche = AddURL(0, bar, 2, L"Porsche",
106      GURL("http://www.porsche.com"));
107  // Rearrange stuff in tier1_a.
108  ASSERT_EQ(tier1_a, tier1_a_url2->parent());
109  ASSERT_EQ(tier1_a, tier1_a_url1->parent());
110  Move(0, tier1_a_url2, tier1_a, 0);
111  Move(0, tier1_a_url1, tier1_a, 2);
112  ASSERT_TRUE(GetClient(0)->AwaitFullSyncCompletion(
113      "Rearrange stuff in tier1_a"));
114  ASSERT_TRUE(ModelMatchesVerifier(0));
115
116  ASSERT_EQ(1, tier1_a_url0->parent()->GetIndexOf(tier1_a_url0));
117  Move(0, tier1_a_url0, bar, bar->child_count());
118  const BookmarkNode* boa = AddURL(0, bar, bar->child_count(),
119      L"Bank of America", GURL("https://www.bankofamerica.com"));
120  ASSERT_TRUE(boa != NULL);
121  Move(0, tier1_a_url0, top, top->child_count());
122  const BookmarkNode* bubble = AddURL(
123      0, bar, bar->child_count(), L"Seattle Bubble",
124          GURL("http://seattlebubble.com"));
125  ASSERT_TRUE(bubble != NULL);
126  const BookmarkNode* wired = AddURL(0, bar, 2, L"Wired News",
127      GURL("http://www.wired.com"));
128  const BookmarkNode* tier2_b = AddFolder(
129      0, tier1_b, 0, L"tier2_b");
130  Move(0, tier1_b_url0, tier2_b, 0);
131  Move(0, porsche, bar, 0);
132  SetTitle(0, wired, L"News Wired");
133  SetTitle(0, porsche, L"ICanHazPorsche?");
134  ASSERT_TRUE(GetClient(0)->AwaitFullSyncCompletion("Change title."));
135  ASSERT_TRUE(ModelMatchesVerifier(0));
136
137  ASSERT_EQ(tier1_a_url0->id(), top->GetChild(top->child_count() - 1)->id());
138  Remove(0, top, top->child_count() - 1);
139  Move(0, wired, tier1_b, 0);
140  Move(0, porsche, bar, 3);
141  const BookmarkNode* tier3_b = AddFolder(0, tier2_b, 1, L"tier3_b");
142  const BookmarkNode* leafs = AddURL(
143      0, tier1_a, 0, L"Toronto Maple Leafs", GURL("http://mapleleafs.nhl.com"));
144  const BookmarkNode* wynn = AddURL(0, bar, 1, L"Wynn",
145      GURL("http://www.wynnlasvegas.com"));
146
147  Move(0, wynn, tier3_b, 0);
148  Move(0, leafs, tier3_b, 0);
149  ASSERT_TRUE(GetClient(0)->AwaitFullSyncCompletion(
150      "Move after addition of bookmarks."));
151  ASSERT_TRUE(ModelMatchesVerifier(0));
152}
153
154// Restart the sync service on a client and make sure sync is up and running.
155IN_PROC_BROWSER_TEST_F(SingleClientBookmarksSyncTest,
156                       DISABLED_RestartSyncService) {
157  ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
158
159  ASSERT_TRUE(AddURL(0, L"Google", GURL("http://www.google.com")));
160  ASSERT_TRUE(GetClient(0)->AwaitFullSyncCompletion("Added a bookmark."));
161  ASSERT_TRUE(ModelMatchesVerifier(0));
162
163  RestartSyncService(0);
164  ASSERT_TRUE(GetClient(0)->AwaitFullSyncCompletion("Restarted sync."));
165  ASSERT_TRUE(ModelMatchesVerifier(0));
166}
167
168// Test that a client doesn't mutate the favicon data in the process
169// of storing the favicon data from sync to the database or in the process
170// of requesting data from the database for sync.
171IN_PROC_BROWSER_TEST_F(SingleClientBookmarksSyncTest,
172                       SetFaviconHiDPIDifferentCodec) {
173  // Set the supported scale factors to 1x and 2x such that
174  // BookmarkModel::GetFavicon() requests both 1x and 2x.
175  // 1x -> for sync, 2x -> for the UI.
176  std::vector<ui::ScaleFactor> supported_scale_factors;
177  supported_scale_factors.push_back(ui::SCALE_FACTOR_100P);
178  supported_scale_factors.push_back(ui::SCALE_FACTOR_200P);
179  ui::test::SetSupportedScaleFactors(supported_scale_factors);
180
181  ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
182  ASSERT_TRUE(ModelMatchesVerifier(0));
183
184  const GURL page_url("http://www.google.com");
185  const GURL icon_url("http://www.google.com/favicon.ico");
186  const BookmarkNode* bookmark = AddURL(0, L"title", page_url);
187
188  // Simulate receiving a favicon from sync encoded by a different PNG encoder
189  // than the one native to the OS. This tests the PNG data is not decoded to
190  // SkBitmap (or any other image format) then encoded back to PNG on the path
191  // between sync and the database.
192  gfx::Image original_favicon = Create1xFaviconFromPNGFile(
193      "favicon_cocoa_png_codec.png");
194  ASSERT_FALSE(original_favicon.IsEmpty());
195  SetFavicon(0, bookmark, icon_url, original_favicon,
196             bookmarks_helper::FROM_SYNC);
197
198  ASSERT_TRUE(GetClient(0)->AwaitFullSyncCompletion("Added bookmark"));
199  ASSERT_TRUE(ModelMatchesVerifier(0));
200
201  scoped_refptr<base::RefCountedMemory> original_favicon_bytes =
202      original_favicon.As1xPNGBytes();
203  gfx::Image final_favicon = GetBookmarkModel(0)->GetFavicon(bookmark);
204  scoped_refptr<base::RefCountedMemory> final_favicon_bytes =
205      final_favicon.As1xPNGBytes();
206
207  // Check that the data was not mutated from the original.
208  EXPECT_TRUE(original_favicon_bytes.get());
209  EXPECT_TRUE(original_favicon_bytes->Equals(final_favicon_bytes));
210}
211