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 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
|
// Copyright 2019 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/engine/bookmark_update_preprocessing.h"
#include <stdint.h>
#include "base/test/metrics/histogram_tester.h"
#include "base/uuid.h"
#include "components/sync/base/unique_position.h"
#include "components/sync/protocol/bookmark_specifics.pb.h"
#include "components/sync/protocol/entity_data.h"
#include "components/sync/protocol/entity_specifics.pb.h"
#include "components/sync/protocol/sync_entity.pb.h"
#include "components/sync/protocol/unique_position.pb.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace syncer {
namespace {
using testing::Eq;
using testing::IsEmpty;
using testing::Ne;
enum class ExpectedBookmarkGuidSource {
kSpecifics = 0,
kValidOCII = 1,
kDeprecatedLeftEmpty = 2,
kInferred = 3,
kLeftEmptyPossiblyForClientTag = 4,
kMaxValue = kLeftEmptyPossiblyForClientTag,
};
TEST(BookmarkUpdatePreprocessingTest,
ShouldPropagateUniquePositionFromSpecifics) {
const UniquePosition kUniquePosition =
UniquePosition::InitialPosition(UniquePosition::RandomSuffix());
sync_pb::SyncEntity entity;
*entity.mutable_specifics()->mutable_bookmark()->mutable_unique_position() =
*entity.mutable_unique_position() = kUniquePosition.ToProto();
const bool is_preprocessed =
AdaptUniquePositionForBookmark(entity, entity.mutable_specifics());
EXPECT_FALSE(is_preprocessed);
EXPECT_TRUE(
UniquePosition::FromProto(entity.specifics().bookmark().unique_position())
.Equals(kUniquePosition));
}
TEST(BookmarkUpdatePreprocessingTest,
ShouldPropagateUniquePositionFromSyncEntity) {
const UniquePosition kUniquePosition =
UniquePosition::InitialPosition(UniquePosition::RandomSuffix());
sync_pb::SyncEntity entity;
*entity.mutable_unique_position() = kUniquePosition.ToProto();
const bool is_preprocessed =
AdaptUniquePositionForBookmark(entity, entity.mutable_specifics());
EXPECT_TRUE(is_preprocessed);
EXPECT_TRUE(
UniquePosition::FromProto(entity.specifics().bookmark().unique_position())
.Equals(kUniquePosition));
}
TEST(BookmarkUpdatePreprocessingTest,
ShouldComputeUniquePositionFromPositionInParent) {
sync_pb::SyncEntity entity;
entity.set_originator_cache_guid(
base::Uuid::GenerateRandomV4().AsLowercaseString());
entity.set_originator_client_item_id("1");
entity.set_position_in_parent(5);
sync_pb::EntitySpecifics specifics1;
bool is_preprocessed = AdaptUniquePositionForBookmark(entity, &specifics1);
EXPECT_TRUE(is_preprocessed);
sync_pb::EntitySpecifics specifics2;
entity.set_position_in_parent(6);
is_preprocessed = AdaptUniquePositionForBookmark(entity, &specifics2);
EXPECT_TRUE(is_preprocessed);
EXPECT_TRUE(UniquePosition::FromProto(specifics1.bookmark().unique_position())
.IsValid());
EXPECT_TRUE(UniquePosition::FromProto(specifics2.bookmark().unique_position())
.IsValid());
EXPECT_TRUE(UniquePosition::FromProto(specifics1.bookmark().unique_position())
.LessThan(UniquePosition::FromProto(
specifics2.bookmark().unique_position())));
}
TEST(BookmarkUpdatePreprocessingTest,
ShouldComputeUniquePositionFromInsertAfterItemId) {
sync_pb::SyncEntity entity;
entity.set_originator_cache_guid(
base::Uuid::GenerateRandomV4().AsLowercaseString());
entity.set_originator_client_item_id("1");
entity.set_insert_after_item_id("ITEM_ID");
const bool is_preprocessed =
AdaptUniquePositionForBookmark(entity, entity.mutable_specifics());
EXPECT_TRUE(is_preprocessed);
EXPECT_TRUE(
UniquePosition::FromProto(entity.specifics().bookmark().unique_position())
.IsValid());
}
TEST(BookmarkUpdatePreprocessingTest, ShouldFallBackToRandomUniquePosition) {
sync_pb::SyncEntity entity;
const bool is_preprocessed =
AdaptUniquePositionForBookmark(entity, entity.mutable_specifics());
EXPECT_TRUE(is_preprocessed);
EXPECT_TRUE(
UniquePosition::FromProto(entity.specifics().bookmark().unique_position())
.IsValid());
}
// Tests that AdaptGuidForBookmark() propagates GUID in specifics if the field
// is set (even if it doesn't match the originator client item ID).
TEST(BookmarkUpdatePreprocessingTest, ShouldPropagateGuidFromSpecifics) {
const std::string kGuidInSpecifics =
base::Uuid::GenerateRandomV4().AsLowercaseString();
sync_pb::SyncEntity entity;
entity.set_originator_cache_guid(
base::Uuid::GenerateRandomV4().AsLowercaseString());
entity.set_originator_client_item_id(
base::Uuid::GenerateRandomV4().AsLowercaseString());
entity.mutable_specifics()->mutable_bookmark()->set_guid(kGuidInSpecifics);
base::HistogramTester histogram_tester;
sync_pb::EntitySpecifics specifics = entity.specifics();
AdaptGuidForBookmark(entity, &specifics);
EXPECT_THAT(specifics.bookmark().guid(), Eq(kGuidInSpecifics));
histogram_tester.ExpectUniqueSample("Sync.BookmarkGUIDSource2",
/*sample=*/
ExpectedBookmarkGuidSource::kSpecifics,
/*expected_bucket_count=*/1);
}
// Tests that AdaptGuidForBookmark() uses the originator client item ID as GUID
// when it is a valid GUID, and the GUID in specifics is not set.
TEST(BookmarkUpdatePreprocessingTest, ShouldUseOriginatorClientItemIdAsGuid) {
const std::string kOriginatorClientItemId =
base::Uuid::GenerateRandomV4().AsLowercaseString();
sync_pb::SyncEntity entity;
entity.set_originator_cache_guid(
base::Uuid::GenerateRandomV4().AsLowercaseString());
entity.set_originator_client_item_id(kOriginatorClientItemId);
entity.mutable_specifics()->mutable_bookmark();
base::HistogramTester histogram_tester;
sync_pb::EntitySpecifics specifics = entity.specifics();
AdaptGuidForBookmark(entity, &specifics);
EXPECT_THAT(specifics.bookmark().guid(), Eq(kOriginatorClientItemId));
histogram_tester.ExpectUniqueSample("Sync.BookmarkGUIDSource2",
/*sample=*/
ExpectedBookmarkGuidSource::kValidOCII,
/*expected_bucket_count=*/1);
}
// Tests that AdaptGuidForBookmark() infers the GUID when the field in specifics
// is empty and originator client item ID is not a valid GUID.
TEST(BookmarkUpdatePreprocessingTest, ShouldInferGuid) {
const std::string kOriginatorClientItemId = "1";
sync_pb::SyncEntity entity;
entity.set_originator_cache_guid(
base::Uuid::GenerateRandomV4().AsLowercaseString());
entity.set_originator_client_item_id(kOriginatorClientItemId);
entity.mutable_specifics()->mutable_bookmark();
base::HistogramTester histogram_tester;
sync_pb::EntitySpecifics specifics = entity.specifics();
AdaptGuidForBookmark(entity, &specifics);
EXPECT_TRUE(
base::Uuid::ParseLowercase(specifics.bookmark().guid()).is_valid());
histogram_tester.ExpectUniqueSample("Sync.BookmarkGUIDSource2",
/*sample=*/
ExpectedBookmarkGuidSource::kInferred,
/*expected_bucket_count=*/1);
}
TEST(BookmarkUpdatePreprocessingTest,
ShouldNotInferGuidIfNoOriginatorInformation) {
const std::string kOriginatorClientItemId = "1";
sync_pb::SyncEntity entity;
entity.mutable_specifics()->mutable_bookmark();
base::HistogramTester histogram_tester;
sync_pb::EntitySpecifics specifics = entity.specifics();
AdaptGuidForBookmark(entity, &specifics);
EXPECT_FALSE(specifics.bookmark().has_guid());
histogram_tester.ExpectUniqueSample(
"Sync.BookmarkGUIDSource2",
/*sample=*/
ExpectedBookmarkGuidSource::kLeftEmptyPossiblyForClientTag,
/*expected_bucket_count=*/1);
}
// Tests that inferred GUIDs are computed deterministically.
TEST(BookmarkUpdatePreprocessingTest, ShouldInferDeterministicGuid) {
EXPECT_THAT(InferGuidForLegacyBookmarkForTesting("cacheguid1", "1"),
Eq(InferGuidForLegacyBookmarkForTesting("cacheguid1", "1")));
EXPECT_THAT(InferGuidForLegacyBookmarkForTesting("cacheguid1", "2"),
Eq(InferGuidForLegacyBookmarkForTesting("cacheguid1", "2")));
}
// Tests that unique GUIDs are produced if either of the two involved fields
// changes.
TEST(BookmarkUpdatePreprocessingTest, ShouldInferDistictGuids) {
EXPECT_THAT(InferGuidForLegacyBookmarkForTesting("cacheguid1", "1"),
Ne(InferGuidForLegacyBookmarkForTesting("cacheguid1", "2")));
EXPECT_THAT(InferGuidForLegacyBookmarkForTesting("cacheguid1", "1"),
Ne(InferGuidForLegacyBookmarkForTesting("cacheguid2", "1")));
}
TEST(BookmarkUpdatePreprocessingTest, ShouldUseTypeInSpecifics) {
sync_pb::SyncEntity entity;
sync_pb::EntitySpecifics specifics;
*specifics.mutable_bookmark() =
sync_pb::BookmarkSpecifics::default_instance();
specifics.mutable_bookmark()->set_type(sync_pb::BookmarkSpecifics::FOLDER);
AdaptTypeForBookmark(entity, &specifics);
EXPECT_THAT(specifics.bookmark().type(),
Eq(sync_pb::BookmarkSpecifics::FOLDER));
*specifics.mutable_bookmark() =
sync_pb::BookmarkSpecifics::default_instance();
specifics.mutable_bookmark()->set_type(sync_pb::BookmarkSpecifics::URL);
AdaptTypeForBookmark(entity, &specifics);
EXPECT_THAT(specifics.bookmark().type(), Eq(sync_pb::BookmarkSpecifics::URL));
// Even if SyncEntity says otherwise, specifics should prevail.
entity.set_folder(true);
*specifics.mutable_bookmark() =
sync_pb::BookmarkSpecifics::default_instance();
specifics.mutable_bookmark()->set_type(sync_pb::BookmarkSpecifics::URL);
AdaptTypeForBookmark(entity, &specifics);
EXPECT_THAT(specifics.bookmark().type(), Eq(sync_pb::BookmarkSpecifics::URL));
}
TEST(BookmarkUpdatePreprocessingTest,
ShouldInferTypeFromFolderFieldInSyncEntity) {
sync_pb::SyncEntity entity;
sync_pb::EntitySpecifics specifics;
*specifics.mutable_bookmark() =
sync_pb::BookmarkSpecifics::default_instance();
entity.set_folder(true);
AdaptTypeForBookmark(entity, &specifics);
EXPECT_THAT(specifics.bookmark().type(),
Eq(sync_pb::BookmarkSpecifics::FOLDER));
*specifics.mutable_bookmark() =
sync_pb::BookmarkSpecifics::default_instance();
entity.set_folder(false);
AdaptTypeForBookmark(entity, &specifics);
EXPECT_THAT(specifics.bookmark().type(), Eq(sync_pb::BookmarkSpecifics::URL));
}
TEST(BookmarkUpdatePreprocessingTest,
ShouldInferTypeFromPresenceOfUrlInSpecifics) {
sync_pb::SyncEntity entity;
sync_pb::EntitySpecifics specifics;
*specifics.mutable_bookmark() =
sync_pb::BookmarkSpecifics::default_instance();
AdaptTypeForBookmark(entity, &specifics);
EXPECT_THAT(specifics.bookmark().type(),
Eq(sync_pb::BookmarkSpecifics::FOLDER));
*specifics.mutable_bookmark() =
sync_pb::BookmarkSpecifics::default_instance();
specifics.mutable_bookmark()->set_url("http://foo.com");
AdaptTypeForBookmark(entity, &specifics);
EXPECT_THAT(specifics.bookmark().type(), Eq(sync_pb::BookmarkSpecifics::URL));
}
} // namespace
} // namespace syncer
|