chromium/chrome/browser/sync/test/integration/single_client_bookmarks_sync_test.cc

// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <memory>
#include <utility>

#include "base/functional/bind.h"
#include "base/hash/hash.h"
#include "base/memory/ref_counted_memory.h"
#include "base/metrics/statistics_recorder.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/time/time.h"
#include "base/uuid.h"
#include "build/build_config.h"
#include "build/buildflag.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/sync/account_bookmark_sync_service_factory.h"
#include "chrome/browser/sync/device_info_sync_service_factory.h"
#include "chrome/browser/sync/local_or_syncable_bookmark_sync_service_factory.h"
#include "chrome/browser/sync/sync_invalidations_service_factory.h"
#include "chrome/browser/sync/test/integration/bookmarks_helper.h"
#include "chrome/browser/sync/test/integration/committed_all_nudged_changes_checker.h"
#include "chrome/browser/sync/test/integration/single_client_status_change_checker.h"
#include "chrome/browser/sync/test/integration/sync_service_impl_harness.h"
#include "chrome/browser/sync/test/integration/sync_test.h"
#include "chrome/browser/sync/test/integration/updated_progress_marker_checker.h"
#include "components/bookmarks/browser/bookmark_model.h"
#include "components/bookmarks/browser/bookmark_node.h"
#include "components/bookmarks/browser/url_and_title.h"
#include "components/bookmarks/common/bookmark_metrics.h"
#include "components/browser_sync/browser_sync_switches.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "components/keyed_service/core/keyed_service.h"
#include "components/sync/base/client_tag_hash.h"
#include "components/sync/base/command_line_switches.h"
#include "components/sync/base/data_type.h"
#include "components/sync/base/features.h"
#include "components/sync/engine/bookmark_update_preprocessing.h"
#include "components/sync/engine/cycle/entity_change_metric_recording.h"
#include "components/sync/engine/loopback_server/loopback_server_entity.h"
#include "components/sync/invalidations/interested_data_types_handler.h"
#include "components/sync/invalidations/sync_invalidations_service.h"
#include "components/sync/protocol/bookmark_specifics.pb.h"
#include "components/sync/protocol/entity_specifics.pb.h"
#include "components/sync/protocol/sync_entity.pb.h"
#include "components/sync/service/sync_service_impl.h"
#include "components/sync/service/sync_user_settings.h"
#include "components/sync/test/bookmark_entity_builder.h"
#include "components/sync/test/entity_builder_factory.h"
#include "components/sync/test/fake_server.h"
#include "components/sync/test/fake_server_http_post_provider.h"
#include "components/sync/test/fake_server_verifier.h"
#include "components/sync/test/test_matchers.h"
#include "components/sync_bookmarks/bookmark_sync_service.h"
#include "components/sync_bookmarks/switches.h"
#include "components/sync_device_info/fake_device_info_sync_service.h"
#include "components/undo/bookmark_undo_service.h"
#include "components/version_info/version_info.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/test_launcher.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/resource/resource_scale_factor.h"

namespace {

BookmarkModel;
BookmarkNode;
AddFolder;
AddURL;
BookmarkFaviconLoadedChecker;
BookmarkModelMatchesFakeServerChecker;
BookmarksTitleChecker;
BookmarksUrlChecker;
BookmarksUuidChecker;
CheckHasNoFavicon;
ContainsBookmarkNodeWithUuid;
CountBookmarksWithTitlesMatching;
CountBookmarksWithUrlsMatching;
CountFoldersWithTitlesMatching;
Create1xFaviconFromPNGFile;
CreateFavicon;
GetBookmarkBarNode;
GetBookmarkModel;
GetBookmarkUndoService;
GetOtherNode;
GetUniqueNodeByURL;
IsFolderWithTitle;
IsFolderWithTitleAndChildrenAre;
IsUrlBookmarkWithTitleAndUrl;
Move;
Remove;
SetFavicon;
SetTitle;
BookmarkGeneration;
MatchesDeletionOrigin;
AllOf;
Contains;
Each;
ElementsAre;
Eq;
IsEmpty;
IsNull;
Not;
NotNull;
Pointee;
SizeIs;

// All tests in this file utilize a single profile.
// TODO(pvalenzuela): Standardize this pattern by moving this constant to
// SyncTest and using it in all single client tests.
const int kSingleProfileIndex =;

#if !BUILDFLAG(IS_ANDROID)
// An arbitrary GUID, to be used for injecting the same bookmark entity to the
// fake server across PRE_MyTest and MyTest.
const char kBookmarkGuid[] =;
#endif  // !BUILDFLAG(IS_ANDROID)

// A title and a URL which are used across PRE_MyTest and MyTest.
const char kBookmarkTitle[] =;
const char kBookmarkPageUrl[] =;

MATCHER(HasUniquePosition, "") {}

// Fake device info sync service that does the necessary setup to be used in a
// SyncTest. It basically disables DEVICE_INFO commits.
class FakeDeviceInfoSyncServiceWithInvalidations
    : public syncer::FakeDeviceInfoSyncService,
      public syncer::InterestedDataTypesHandler {};

std::unique_ptr<KeyedService> BuildFakeDeviceInfoSyncService(
    content::BrowserContext* context) {}

// Waits until the tasks posted by the error handler have been processed.
class BookmarksDataTypeErrorChecker : public SingleClientStatusChangeChecker {};

class SingleClientBookmarksSyncTest : public SyncTest {};

class SingleClientBookmarksSyncTestWithEnabledReuploadBookmarks
    : public SyncTest {};

class SingleClientBookmarksSyncTestWithDisabledReuploadBookmarks
    : public SyncTest {};

class SingleClientBookmarksSyncTestWithEnabledReuploadPreexistingBookmarks
    : public SingleClientBookmarksSyncTest {};

class SingleClientBookmarksThrottlingSyncTest : public SyncTest {};

IN_PROC_BROWSER_TEST_F(SingleClientBookmarksSyncTest, Sanity) {}

IN_PROC_BROWSER_TEST_F(SingleClientBookmarksSyncTest, CommitLocalCreations) {}

IN_PROC_BROWSER_TEST_F(SingleClientBookmarksSyncTest, InjectedBookmark) {}

IN_PROC_BROWSER_TEST_F(SingleClientBookmarksSyncTest,
                       DownloadTwoPre2015BookmarksWithSameItemId) {}

IN_PROC_BROWSER_TEST_F(SingleClientBookmarksSyncTest,
                       DownloadLegacyUppercaseGuid2016BookmarksAndCommit) {}

IN_PROC_BROWSER_TEST_F(SingleClientBookmarksSyncTest,
                       DownloadModernBookmarkCollidingPre2015BookmarkId) {}

// Test that a client doesn't mutate the favicon data in the process
// of storing the favicon data from sync to the database or in the process
// of requesting data from the database for sync.
IN_PROC_BROWSER_TEST_F(SingleClientBookmarksSyncTest,
                       SetFaviconHiDPIDifferentCodec) {}

// Test that a client deletes favicons from sync when they have been removed
// from the local database.
IN_PROC_BROWSER_TEST_F(SingleClientBookmarksSyncTest, DeleteFaviconFromSync) {}

IN_PROC_BROWSER_TEST_F(SingleClientBookmarksSyncTest, OneFolderRemovedEvent) {}

IN_PROC_BROWSER_TEST_F(SingleClientBookmarksSyncTest,
                       BookmarkAllNodesRemovedEvent) {}

IN_PROC_BROWSER_TEST_F(SingleClientBookmarksSyncTest, DownloadDeletedBookmark) {}

IN_PROC_BROWSER_TEST_F(SingleClientBookmarksSyncTest,
                       DownloadModifiedBookmark) {}

IN_PROC_BROWSER_TEST_F(SingleClientBookmarksSyncTest, DownloadBookmarkFolder) {}

IN_PROC_BROWSER_TEST_F(SingleClientBookmarksSyncTest,
                       DownloadLegacyBookmarkFolder) {}

// Legacy bookmark clients append a blank space to empty titles, ".", ".." tiles
// before committing them because historically they were illegal server titles.
// This test makes sure that this functionality is implemented for backward
// compatibility with legacy clients.
IN_PROC_BROWSER_TEST_F(SingleClientBookmarksSyncTest,
                       ShouldCommitBookmarksWithIllegalServerNames) {}

// This test the opposite functionality in the test above. Legacy bookmark
// clients omit a blank space from blank space title, ". ", ".. " tiles upon
// receiving the remote updates. An extra space has been appended during a
// commit because historically they were considered illegal server titles. This
// test makes sure that this functionality is implemented for backward
// compatibility with legacy clients.
IN_PROC_BROWSER_TEST_F(SingleClientBookmarksSyncTest,
                       ShouldCreateLocalBookmarksWithIllegalServerNames) {}

// Legacy bookmark clients append a blank space to empty titles. This tests that
// this is respected when merging local and remote hierarchies.
IN_PROC_BROWSER_TEST_F(SingleClientBookmarksSyncTest,
                       ShouldTruncateBlanksWhenMatchingTitles) {}

// Legacy bookmark clients truncate long titles up to 255 bytes. This tests that
// this is respected when merging local and remote hierarchies.
IN_PROC_BROWSER_TEST_F(SingleClientBookmarksSyncTest,
                       ShouldTruncateLongTitles) {}

IN_PROC_BROWSER_TEST_F(SingleClientBookmarksSyncTest,
                       DownloadBookmarkFoldersWithPositions) {}

IN_PROC_BROWSER_TEST_F(SingleClientBookmarksSyncTest, E2E_ONLY(SanitySetup)) {}

IN_PROC_BROWSER_TEST_F(
    SingleClientBookmarksSyncTest,
    RemoveRightAfterAddShouldNotSendCommitRequestsOrTombstones) {}

// Android doesn't currently support PRE_ tests, see crbug.com/1117345.
#if !BUILDFLAG(IS_ANDROID)
IN_PROC_BROWSER_TEST_F(SingleClientBookmarksSyncTest,
                       PRE_PersistProgressMarkerOnRestart) {}

IN_PROC_BROWSER_TEST_F(SingleClientBookmarksSyncTest,
                       PersistProgressMarkerOnRestart) {}
#endif  // !BUILDFLAG(IS_ANDROID)

IN_PROC_BROWSER_TEST_F(SingleClientBookmarksSyncTest,
                       ApplyRemoteCreationWithValidUuid) {}

IN_PROC_BROWSER_TEST_F(SingleClientBookmarksSyncTest,
                       ApplyRemoteCreationWithoutValidGUID) {}

IN_PROC_BROWSER_TEST_F(SingleClientBookmarksSyncTest,
                       ApplyRemoteCreationWithoutValidGUIDOrOCII) {}

IN_PROC_BROWSER_TEST_F(SingleClientBookmarksSyncTest,
                       MergeRemoteCreationWithValidUuid) {}

IN_PROC_BROWSER_TEST_F(SingleClientBookmarksSyncTest,
                       ShouldStartTrackingRestoredBookmark) {}

IN_PROC_BROWSER_TEST_F(SingleClientBookmarksSyncTest,
                       MergeRemoteCreationWithoutValidGUID) {}

IN_PROC_BROWSER_TEST_F(SingleClientBookmarksSyncTest,
                       MergeRemoteCreationWithoutValidGUIDOrOCII) {}

IN_PROC_BROWSER_TEST_F(SingleClientBookmarksSyncTest,
                       MergeRemoteUpdateWithValidGUID) {}

IN_PROC_BROWSER_TEST_F(
    SingleClientBookmarksSyncTest,
    ShouldReportErrorIfIncrementalLocalCreationCrossesMaxCountLimit) {}

IN_PROC_BROWSER_TEST_F(SingleClientBookmarksSyncTest,
                       ShouldReportErrorIfBookmarksCountExceedsLimitOnStartup) {}

IN_PROC_BROWSER_TEST_F(
    SingleClientBookmarksSyncTest,
    ShouldReportErrorIfBookmarksCountExceedsLimitAfterInitialUpdate) {}

IN_PROC_BROWSER_TEST_F(
    SingleClientBookmarksSyncTest,
    ShouldReportErrorIfBookmarksCountExceedsLimitAfterIncrementalUpdate) {}

IN_PROC_BROWSER_TEST_F(SingleClientBookmarksSyncTest,
                       ShouldReportErrorIfInitialUpdatesCrossMaxCountLimit) {}

// Android doesn't currently support PRE_ tests, see crbug.com/40200835 or
// crbug.com/40145099.
#if !BUILDFLAG(IS_ANDROID)
IN_PROC_BROWSER_TEST_F(
    SingleClientBookmarksSyncTestWithDisabledReuploadBookmarks,
    PRE_ShouldNotReploadUponFaviconLoad) {}

IN_PROC_BROWSER_TEST_F(SingleClientBookmarksSyncTest,
                       PRE_ShouldUploadUnsyncedEntityAfterRestart) {}

IN_PROC_BROWSER_TEST_F(SingleClientBookmarksSyncTest,
                       ShouldUploadUnsyncedEntityAfterRestart) {}

IN_PROC_BROWSER_TEST_F(
    SingleClientBookmarksSyncTestWithDisabledReuploadBookmarks,
    ShouldNotReploadUponFaviconLoad) {}
#endif  // !BUILDFLAG(IS_ANDROID)

IN_PROC_BROWSER_TEST_F(
    SingleClientBookmarksSyncTestWithEnabledReuploadBookmarks,
    ShouldReuploadBookmarkAfterInitialMerge) {}

// Android doesn't currently support PRE_ tests, see crbug.com/1117345.
#if !BUILDFLAG(IS_ANDROID)
// Initiate reupload after restart when the feature toggle has been just enabled
// (before restart the entity is in synced state).
IN_PROC_BROWSER_TEST_F(
    SingleClientBookmarksSyncTestWithEnabledReuploadPreexistingBookmarks,
    PRE_ShouldReuploadForOldClients) {}

IN_PROC_BROWSER_TEST_F(
    SingleClientBookmarksSyncTestWithEnabledReuploadPreexistingBookmarks,
    ShouldReuploadForOldClients) {}
#endif  // !BUILDFLAG(IS_ANDROID)

IN_PROC_BROWSER_TEST_F(
    SingleClientBookmarksSyncTestWithEnabledReuploadBookmarks,
    ShouldReuploadBookmarkWithFaviconOnInitialMerge) {}

IN_PROC_BROWSER_TEST_F(
    SingleClientBookmarksSyncTestWithEnabledReuploadBookmarks,
    ShouldReuploadUniquePositionOnIncrementalChange) {}

IN_PROC_BROWSER_TEST_F(SingleClientBookmarksSyncTest,
                       CommitLocalCreationWithClientTag) {}

IN_PROC_BROWSER_TEST_F(SingleClientBookmarksThrottlingSyncTest, DepleteQuota) {}

IN_PROC_BROWSER_TEST_F(SingleClientBookmarksThrottlingSyncTest,
                       DepletedQuotaDoesNotStopCommitCycle) {}

IN_PROC_BROWSER_TEST_F(SingleClientBookmarksThrottlingSyncTest,
                       DoNotDepleteQuota) {}

IN_PROC_BROWSER_TEST_F(SingleClientBookmarksThrottlingSyncTest,
                       DepleteQuotaAndRecover) {}

// On ChromeOS, Sync-the-feature gets started automatically once a primary
// account is signed in and the transport mode is not a thing.
#if !BUILDFLAG(IS_CHROMEOS_ASH)
class SingleClientBookmarksWithAccountStorageSyncTest
    : public SingleClientBookmarksSyncTest {};

IN_PROC_BROWSER_TEST_F(SingleClientBookmarksWithAccountStorageSyncTest,
                       ShouldDownloadDataUponSigninAndClearUponSignout) {}

IN_PROC_BROWSER_TEST_F(SingleClientBookmarksWithAccountStorageSyncTest,
                       ShouldHandleMovesAcrossStorageBoundaries) {}

// Regression test for crbug.com/329278277: turning sync-the-feature on, then
// off, and later signing in with account bookmarks enabled should lead to all
// bookmarks being duplicated (local bookmarks and account bookmarks). The user
// needs to take explicit action (e.g. exercise batch upload flow) to clean up
// these duplicates (but this part is not covered in the test).
IN_PROC_BROWSER_TEST_F(
    SingleClientBookmarksWithAccountStorageSyncTest,
    ShouldExposeDuplicatedBookmarksAfterTurningSyncOffAndSignIn) {}

// Android doesn't currently support PRE_ tests, see crbug.com/1117345.
#if !BUILDFLAG(IS_ANDROID)
IN_PROC_BROWSER_TEST_F(SingleClientBookmarksWithAccountStorageSyncTest,
                       PRE_PersistAccountBookmarksAcrossRestarts) {}

IN_PROC_BROWSER_TEST_F(SingleClientBookmarksWithAccountStorageSyncTest,
                       PersistAccountBookmarksAcrossRestarts) {}

IN_PROC_BROWSER_TEST_F(SingleClientBookmarksWithAccountStorageSyncTest,
                       PRE_ShouldPersistIfInitialUpdatesCrossMaxCountLimit) {}

IN_PROC_BROWSER_TEST_F(SingleClientBookmarksWithAccountStorageSyncTest,
                       ShouldPersistIfInitialUpdatesCrossMaxCountLimit) {}
#endif  // !BUILDFLAG(IS_ANDROID)

// Android doesn't currently support PRE_ tests, see crbug.com/40200835 or
// crbug.com/40145099.
#if !BUILDFLAG(IS_ANDROID)
class SingleClientBookmarksSyncTestWithEnabledMigrateSyncingUserToSignedIn
    : public SingleClientBookmarksWithAccountStorageSyncTest {};

IN_PROC_BROWSER_TEST_F(
    SingleClientBookmarksSyncTestWithEnabledMigrateSyncingUserToSignedIn,
    PRE_SyncToSigninMigration) {}

IN_PROC_BROWSER_TEST_F(
    SingleClientBookmarksSyncTestWithEnabledMigrateSyncingUserToSignedIn,
    SyncToSigninMigration) {}
#endif  // !BUILDFLAG(IS_ANDROID)

#endif  // !BUILDFLAG(IS_CHROMEOS_ASH)

}  // namespace