blob: 9d1eb4ec29731a86c945f424d2974457c92ad822 [file] [log] [blame]
Mohamed Amir Yosefb0664a82018-05-09 12:19:251// Copyright 2018 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 "components/sync_bookmarks/synced_bookmark_tracker.h"
6
Mohamed Amir Yosefc80bdd02018-06-18 12:26:327#include "base/base64.h"
8#include "base/sha1.h"
Mohamed Amir Yosefb0664a82018-05-09 12:19:259#include "components/bookmarks/browser/bookmark_node.h"
Mohamed Amir Yosefc80bdd02018-06-18 12:26:3210#include "components/sync/base/time.h"
11#include "components/sync/model/entity_data.h"
Mohamed Amir Yosefb0664a82018-05-09 12:19:2512#include "testing/gmock/include/gmock/gmock.h"
13#include "testing/gtest/include/gtest/gtest.h"
14
15using testing::Eq;
16using testing::IsNull;
17using testing::NotNull;
18
19namespace sync_bookmarks {
20
21namespace {
22
Mohamed Amir Yosefc80bdd02018-06-18 12:26:3223sync_pb::EntitySpecifics GenerateSpecifics(const std::string& title,
24 const std::string& url) {
25 sync_pb::EntitySpecifics specifics;
26 specifics.mutable_bookmark()->set_title(title);
27 specifics.mutable_bookmark()->set_url(url);
28 return specifics;
29}
30
Mohamed Amir Yosefb0664a82018-05-09 12:19:2531TEST(SyncedBookmarkTrackerTest, ShouldGetAssociatedNodes) {
Mohamed Amir Yosefc80bdd02018-06-18 12:26:3232 SyncedBookmarkTracker tracker(std::vector<NodeMetadataPair>(),
33 std::make_unique<sync_pb::ModelTypeState>());
Mohamed Amir Yosefb0664a82018-05-09 12:19:2534 const std::string kSyncId = "SYNC_ID";
Mohamed Amir Yosefc80bdd02018-06-18 12:26:3235 const std::string kTitle = "Title";
36 const GURL kUrl("http://www.foo.com");
Mohamed Amir Yosefb0664a82018-05-09 12:19:2537 const int64_t kId = 1;
Mohamed Amir Yosefc80bdd02018-06-18 12:26:3238 const int64_t kServerVersion = 1000;
39 const base::Time kCreationTime(base::Time::Now() -
40 base::TimeDelta::FromSeconds(1));
41 const sync_pb::EntitySpecifics specifics =
Mohamed Amir Yosef77ac6fbe2018-06-20 10:48:4642 GenerateSpecifics(/*title=*/std::string(), /*url=*/std::string());
Mohamed Amir Yosefc80bdd02018-06-18 12:26:3243
44 bookmarks::BookmarkNode node(kId, kUrl);
45 tracker.Add(kSyncId, &node, kServerVersion, kCreationTime, specifics);
Mohamed Amir Yosefb0664a82018-05-09 12:19:2546 const SyncedBookmarkTracker::Entity* entity =
47 tracker.GetEntityForSyncId(kSyncId);
48 ASSERT_THAT(entity, NotNull());
49 EXPECT_THAT(entity->bookmark_node(), Eq(&node));
Mohamed Amir Yosefc80bdd02018-06-18 12:26:3250 EXPECT_THAT(entity->metadata()->server_id(), Eq(kSyncId));
51 EXPECT_THAT(entity->metadata()->server_version(), Eq(kServerVersion));
52 EXPECT_THAT(entity->metadata()->creation_time(),
53 Eq(syncer::TimeToProtoTime(kCreationTime)));
54 syncer::EntityData data;
55 *data.specifics.mutable_bookmark() = specifics.bookmark();
56 EXPECT_TRUE(entity->MatchesData(data));
Mohamed Amir Yosefb0664a82018-05-09 12:19:2557 EXPECT_THAT(tracker.GetEntityForSyncId("unknown id"), IsNull());
58}
59
Mohamed Amir Yosefb2895112018-05-18 15:32:3860TEST(SyncedBookmarkTrackerTest, ShouldReturnNullForDisassociatedNodes) {
Mohamed Amir Yosefc80bdd02018-06-18 12:26:3261 SyncedBookmarkTracker tracker(std::vector<NodeMetadataPair>(),
62 std::make_unique<sync_pb::ModelTypeState>());
Mohamed Amir Yosefb2895112018-05-18 15:32:3863 const std::string kSyncId = "SYNC_ID";
64 const int64_t kId = 1;
Mohamed Amir Yosefc80bdd02018-06-18 12:26:3265 const int64_t kServerVersion = 1000;
66 const base::Time kModificationTime(base::Time::Now() -
67 base::TimeDelta::FromSeconds(1));
68 const sync_pb::EntitySpecifics specifics =
Mohamed Amir Yosef77ac6fbe2018-06-20 10:48:4669 GenerateSpecifics(/*title=*/std::string(), /*url=*/std::string());
Mohamed Amir Yosefb2895112018-05-18 15:32:3870 bookmarks::BookmarkNode node(kId, GURL());
Mohamed Amir Yosefc80bdd02018-06-18 12:26:3271 tracker.Add(kSyncId, &node, kServerVersion, kModificationTime, specifics);
Mohamed Amir Yosefb2895112018-05-18 15:32:3872 ASSERT_THAT(tracker.GetEntityForSyncId(kSyncId), NotNull());
Mohamed Amir Yosef20451342018-06-06 08:44:1773 tracker.Remove(kSyncId);
Mohamed Amir Yosefb2895112018-05-18 15:32:3874 EXPECT_THAT(tracker.GetEntityForSyncId(kSyncId), IsNull());
75}
76
Mohamed Amir Yosef77ac6fbe2018-06-20 10:48:4677TEST(SyncedBookmarkTrackerTest,
78 ShouldRequireCommitRequestWhenSequenceNumberIsIncremented) {
79 SyncedBookmarkTracker tracker(std::vector<NodeMetadataPair>(),
80 std::make_unique<sync_pb::ModelTypeState>());
81 const std::string kSyncId = "SYNC_ID";
82 const int64_t kId = 1;
83 const int64_t kServerVersion = 1000;
84 const base::Time kModificationTime(base::Time::Now() -
85 base::TimeDelta::FromSeconds(1));
86 const sync_pb::EntitySpecifics specifics =
87 GenerateSpecifics(/*title=*/std::string(), /*url=*/std::string());
88 bookmarks::BookmarkNode node(kId, GURL());
89 tracker.Add(kSyncId, &node, kServerVersion, kModificationTime, specifics);
90
91 EXPECT_THAT(tracker.HasLocalChanges(), Eq(false));
92 tracker.IncrementSequenceNumber(kSyncId);
93 EXPECT_THAT(tracker.HasLocalChanges(), Eq(true));
94 // TODO(crbug.com/516866): Test HasLocalChanges after submitting commit
95 // request in a separate test probably.
96}
97
Mohamed Amir Yosefb0664a82018-05-09 12:19:2598} // namespace
99
100} // namespace sync_bookmarks