File: parent_guid_preprocessing_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (233 lines) | stat: -rw-r--r-- 9,304 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/sync_bookmarks/parent_guid_preprocessing.h"

#include <memory>

#include "base/uuid.h"
#include "components/bookmarks/browser/bookmark_node.h"
#include "components/bookmarks/browser/bookmark_uuids.h"
#include "components/sync/protocol/bookmark_specifics.pb.h"
#include "components/sync/protocol/data_type_state.pb.h"
#include "components/sync/protocol/entity_data.h"
#include "components/sync/protocol/entity_specifics.pb.h"
#include "components/sync_bookmarks/bookmark_model_view.h"
#include "components/sync_bookmarks/synced_bookmark_tracker.h"
#include "components/sync_bookmarks/test_bookmark_model_view.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"

namespace sync_bookmarks {

namespace {

using testing::Eq;

TEST(ParentGuidPreprocessingTest, ShouldReturnGuidForSyncIdIncludedInUpdates) {
  const std::string kId1 = "sync_id1";
  const std::string kId2 = "sync_id2";
  const std::string kGuid1 = "guid1";
  const std::string kGuid2 = "guid2";

  syncer::UpdateResponseDataList updates;
  updates.emplace_back();
  updates.back().entity.id = kId1;
  updates.back().entity.specifics.mutable_bookmark()->set_guid(kGuid1);
  updates.emplace_back();
  updates.back().entity.id = kId2;
  updates.back().entity.specifics.mutable_bookmark()->set_guid(kGuid2);

  EXPECT_THAT(GetGuidForSyncIdInUpdatesForTesting(updates, kId1), Eq(kGuid1));
  EXPECT_THAT(GetGuidForSyncIdInUpdatesForTesting(updates, kId2), Eq(kGuid2));
}

TEST(ParentGuidPreprocessingTest,
     ShouldReturnInvalidGuidForSyncIdMissingInUpdates) {
  const std::string kId1 = "sync_id1";
  const std::string kGuid1 = "guid1";

  syncer::UpdateResponseDataList updates;

  EXPECT_THAT(GetGuidForSyncIdInUpdatesForTesting(updates, "missing_id"),
              Eq(""));

  updates.emplace_back();
  updates.back().entity.id = kId1;
  updates.back().entity.specifics.mutable_bookmark()->set_guid(kGuid1);

  ASSERT_THAT(GetGuidForSyncIdInUpdatesForTesting(updates, kId1), Eq(kGuid1));
  EXPECT_THAT(GetGuidForSyncIdInUpdatesForTesting(updates, "missing_id"),
              Eq(""));
}

TEST(ParentGuidPreprocessingTest, ShouldReturnGuidForPermanentFolders) {
  const std::string kBookmarkBarId = "id1";
  const std::string kMobileBookmarksId = "id2";
  const std::string kOtherBookmarksId = "id3";

  // Permanent folders may not include their GUIDs.
  syncer::UpdateResponseDataList updates;
  updates.emplace_back();
  updates.back().entity.id = kBookmarkBarId;
  updates.back().entity.server_defined_unique_tag = "bookmark_bar";
  updates.emplace_back();
  updates.back().entity.id = kMobileBookmarksId;
  updates.back().entity.server_defined_unique_tag = "synced_bookmarks";
  updates.emplace_back();
  updates.back().entity.id = kOtherBookmarksId;
  updates.back().entity.server_defined_unique_tag = "other_bookmarks";

  EXPECT_THAT(GetGuidForSyncIdInUpdatesForTesting(updates, kBookmarkBarId),
              Eq(bookmarks::kBookmarkBarNodeUuid));
  EXPECT_THAT(GetGuidForSyncIdInUpdatesForTesting(updates, kMobileBookmarksId),
              Eq(bookmarks::kMobileBookmarksNodeUuid));
  EXPECT_THAT(GetGuidForSyncIdInUpdatesForTesting(updates, kOtherBookmarksId),
              Eq(bookmarks::kOtherBookmarksNodeUuid));
}

TEST(ParentGuidPreprocessingTest, ShouldPopulateParentGuidInInitialUpdates) {
  const std::string kBookmarkBarId = "bookmark_bar_id";
  const std::string kParentFolderId = "parent_folder_id";
  const std::string kParentFolderUuid =
      base::Uuid::GenerateRandomV4().AsLowercaseString();

  // Populate updates representing:
  // bookmark_bar
  //  |- folder 1
  //    |- folder 2
  syncer::UpdateResponseDataList updates;
  updates.emplace_back();
  updates.back().entity.id = kBookmarkBarId;
  updates.back().entity.server_defined_unique_tag = "bookmark_bar";
  updates.emplace_back();
  updates.back().entity.id = kParentFolderId;
  updates.back().entity.legacy_parent_id = kBookmarkBarId;
  updates.back().entity.specifics.mutable_bookmark()->set_guid(
      kParentFolderUuid);
  updates.emplace_back();
  updates.back().entity.legacy_parent_id = kParentFolderId;
  updates.back().entity.specifics.mutable_bookmark()->set_guid("child_guid");

  PopulateParentGuidInSpecifics(/*tracker=*/nullptr, &updates);

  EXPECT_THAT(updates[0].entity.specifics.bookmark().parent_guid(), Eq(""));
  EXPECT_THAT(updates[1].entity.specifics.bookmark().parent_guid(),
              Eq(bookmarks::kBookmarkBarNodeUuid));
  EXPECT_THAT(updates[2].entity.specifics.bookmark().parent_guid(),
              Eq(kParentFolderUuid));
}

TEST(ParentGuidPreprocessingTest,
     ShouldNotOverridePreexistingParentGuidInSpecifics) {
  const std::string kBookmarkBarId = "bookmark_bar_id";
  const std::string kFolderId = "folder_id";

  const std::string kFolderUuid =
      base::Uuid::GenerateRandomV4().AsLowercaseString();
  const std::string kParentUuidInSpecifics =
      base::Uuid::GenerateRandomV4().AsLowercaseString();

  // Populate updates representing:
  // bookmark_bar
  //  |- folder 1
  //    |- folder 2
  syncer::UpdateResponseDataList updates;
  updates.emplace_back();
  updates.back().entity.id = kBookmarkBarId;
  updates.back().entity.server_defined_unique_tag = "bookmark_bar";
  updates.emplace_back();
  updates.back().entity.id = kFolderId;
  updates.back().entity.legacy_parent_id = kBookmarkBarId;
  updates.back().entity.specifics.mutable_bookmark()->set_guid(kFolderUuid);
  updates.back().entity.specifics.mutable_bookmark()->set_parent_guid(
      kParentUuidInSpecifics);

  // Although |parent_id| points to bookmarks bar, the |parent_guid| field
  // should prevail.
  ASSERT_THAT(GetGuidForSyncIdInUpdatesForTesting(updates, kBookmarkBarId),
              Eq(bookmarks::kBookmarkBarNodeUuid));

  PopulateParentGuidInSpecifics(/*tracker=*/nullptr, &updates);

  EXPECT_THAT(updates[1].entity.specifics.bookmark().parent_guid(),
              Eq(kParentUuidInSpecifics));
}

TEST(ParentGuidPreprocessingTest,
     ShouldPopulateParentGuidInIncrementalUpdates) {
  const std::string kSyncId = "id1";
  const std::string kBookmarkBarId = "bookmark_bar_id";

  std::unique_ptr<SyncedBookmarkTracker> tracker =
      SyncedBookmarkTracker::CreateEmpty(sync_pb::DataTypeState());

  // Non-empty specifics are needed for SyncedBookmarkTracker::Add(), with
  // unique position populated.
  sync_pb::EntitySpecifics fake_specifics;
  fake_specifics.mutable_bookmark()->mutable_unique_position();

  // BookmarkModelView is used here to pass DCHECKs that require that permanent
  // folders are tracked.
  TestBookmarkModelView bookmark_model;
  tracker->Add(bookmark_model.bookmark_bar_node(), /*sync_id=*/kBookmarkBarId,
               /*server_version=*/0, /*creation_time=*/base::Time::Now(),
               /*specifics=*/fake_specifics);
  tracker->Add(bookmark_model.other_node(), /*sync_id=*/"other_node_id",
               /*server_version=*/0, /*creation_time=*/base::Time::Now(),
               /*specifics=*/fake_specifics);
  tracker->Add(bookmark_model.mobile_node(), /*sync_id=*/"mobile_node_id",
               /*server_version=*/0, /*creation_time=*/base::Time::Now(),
               /*specifics=*/fake_specifics);

  // Add one regular (non-permanent) node.
  bookmarks::BookmarkNode tracked_node(/*id=*/1, base::Uuid::GenerateRandomV4(),
                                       GURL());
  tracker->Add(&tracked_node, kSyncId,
               /*server_version=*/0, /*creation_time=*/base::Time::Now(),
               /*specifics=*/fake_specifics);

  syncer::UpdateResponseDataList updates;
  updates.emplace_back();
  updates.back().entity.legacy_parent_id = kSyncId;
  updates.back().entity.specifics.mutable_bookmark()->set_guid("guid1");
  updates.emplace_back();
  updates.back().entity.legacy_parent_id = kBookmarkBarId;
  updates.back().entity.specifics.mutable_bookmark()->set_guid("guid2");
  PopulateParentGuidInSpecifics(tracker.get(), &updates);

  EXPECT_THAT(updates[0].entity.specifics.bookmark().parent_guid(),
              Eq(tracked_node.uuid().AsLowercaseString()));
  EXPECT_THAT(updates[1].entity.specifics.bookmark().parent_guid(),
              Eq(bookmarks::kBookmarkBarNodeUuid));
}

TEST(ParentGuidPreprocessingTest,
     ShouldPopulateWithFakeGuidIfParentSetButUnknown) {
  // Fork of the private constant in the .cc file.
  const std::string kInvalidParentUuid = "220a410e-37b9-5bbc-8674-ea982459f940";

  const std::string kParentFolderId = "parent_folder_id";
  const std::string kParentFolderUuid =
      base::Uuid::GenerateRandomV4().AsLowercaseString();

  // Populate updates representing:
  //  |- folder with unknown parent
  syncer::UpdateResponseDataList updates;
  updates.emplace_back();
  updates.back().entity.id = kParentFolderId;
  updates.back().entity.legacy_parent_id = "some_unknown_parent";
  updates.back().entity.specifics.mutable_bookmark()->set_guid(
      kParentFolderUuid);

  PopulateParentGuidInSpecifics(/*tracker=*/nullptr, &updates);

  EXPECT_THAT(updates[0].entity.specifics.bookmark().parent_guid(),
              Eq(kInvalidParentUuid));
}

}  // namespace

}  // namespace sync_bookmarks