chromium/components/bookmarks/browser/bookmark_model_unittest.cc

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

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include "components/bookmarks/browser/bookmark_model.h"

#include <stddef.h>
#include <stdint.h>

#include <optional>
#include <set>
#include <string>
#include <unordered_set>
#include <utility>

#include "base/base_paths.h"
#include "base/command_line.h"
#include "base/compiler_specific.h"
#include "base/containers/contains.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/logging.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/metrics/user_action_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "base/uuid.h"
#include "build/build_config.h"
#include "components/bookmarks/browser/bookmark_model_observer.h"
#include "components/bookmarks/browser/bookmark_node.h"
#include "components/bookmarks/browser/bookmark_undo_provider.h"
#include "components/bookmarks/browser/bookmark_utils.h"
#include "components/bookmarks/browser/bookmark_uuids.h"
#include "components/bookmarks/browser/titled_url_match.h"
#include "components/bookmarks/browser/url_and_title.h"
#include "components/bookmarks/common/bookmark_features.h"
#include "components/bookmarks/common/bookmark_metrics.h"
#include "components/bookmarks/test/bookmark_test_helpers.h"
#include "components/bookmarks/test/mock_bookmark_model_observer.h"
#include "components/bookmarks/test/test_bookmark_client.h"
#include "components/favicon_base/favicon_callback.h"
#include "components/favicon_base/favicon_types.h"
#include "components/query_parser/query_parser.h"
#include "components/sync/base/features.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/base/models/tree_node_iterator.h"
#include "ui/base/models/tree_node_model.h"
#include "ui/gfx/image/image.h"
#include "url/gurl.h"

namespace bookmarks {
namespace {

NodeTypeForUuidLookup;

ASCIIToUTF16;
Time;
ElementsAre;
Invoke;
WithArg;

// Test cases used to test the removal of extra whitespace when adding
// a new folder/bookmark or updating a title of a folder/bookmark.
// Note that whitespace characters are all replaced with spaces, but spaces are
// not collapsed or trimmed.
static struct {} url_whitespace_test_cases[] =;

// Test cases used to test the removal of extra whitespace when adding
// a new folder/bookmark or updating a title of a folder/bookmark.
static struct {} title_whitespace_test_cases[] =;

// TestBookmarkClient that also has basic support for undoing removals.
class TestBookmarkClientWithUndo : public TestBookmarkClient {};

// Helper to get a mutable bookmark node.
BookmarkNode* AsMutable(const BookmarkNode* node) {}

void SwapDateAdded(BookmarkNode* n1, BookmarkNode* n2) {}

void SwapDateUsed(BookmarkNode* n1, BookmarkNode* n2) {}

// See comment in PopulateNodeFromString.
TestNode;

// Does the work of PopulateNodeFromString. index gives the index of the current
// element in description to process.
void PopulateNodeImpl(const std::vector<std::string>& description,
                      size_t* index,
                      TestNode* parent) {}

// Creates and adds nodes to parent based on description. description consists
// of the following tokens (all space separated):
//   [ : creates a new USER_FOLDER node. All elements following the [ until the
//       next balanced ] is encountered are added as children to the node.
//   ] : closes the last folder created by [ so that any further nodes are added
//       to the current folders parent.
//   text: creates a new URL node.
// For example, "a [b] c" creates the following nodes:
//   a 1 c
//     |
//     b
// In words: a node of type URL with the title a, followed by a folder node with
// the title 1 having the single child of type url with name b, followed by
// the url node with the title c.
//
// NOTE: each name must be unique, and folders are assigned a unique title by
// way of an increasing integer.
void PopulateNodeFromString(const std::string& description, TestNode* parent) {}

// Populates the BookmarkNode with the children of parent.
void PopulateBookmarkNode(TestNode* parent,
                          BookmarkModel* model,
                          const BookmarkNode* bb_node) {}

// Verifies the contents of the bookmark bar node match the contents of the
// TestNode.
void VerifyModelMatchesNode(TestNode* expected, const BookmarkNode* actual) {}

void VerifyNoDuplicateIDs(BookmarkModel* model) {}

class BookmarkModelTest : public testing::Test, public BookmarkModelObserver {};

TEST_F(BookmarkModelTest, InitialState) {}

TEST_F(BookmarkModelTest, AddURL) {}

TEST_F(BookmarkModelTest, AddNewURL) {}

// Tests recording user action when adding a bookmark in account storage when
// a shared BookmarkModel instance is used for account bookmarks and
// local-or-syncable ones.
TEST_F(BookmarkModelTest,
       AddNewURLAccountStorageOnSharedBookmarkModelInstance) {}

// Tests recording user action when adding a bookmark in local storage not
// syncing.
TEST_F(BookmarkModelTest, AddNewURLLocalStorageNotSyncing) {}

// Tests recording user action when adding a bookmark in local storage syncing.
TEST_F(BookmarkModelTest, AddNewURLLocalStorageSyncing) {}

// Tests recording user action when adding a folder in account storage.
TEST_F(BookmarkModelTest, AddNewFolderAccountStorage) {}

// Tests recording user action when adding a folder in local storage not
// syncing.
TEST_F(BookmarkModelTest, AddNewFolderLocalStorageNotSyncing) {}

// Tests recording user action when adding a folder in local storage syncing.
TEST_F(BookmarkModelTest, AddNewFolderLocalStorageSyncing) {}

TEST_F(BookmarkModelTest, AddURLWithUnicodeTitle) {}

TEST_F(BookmarkModelTest, AddURLWithWhitespaceTitle) {}

TEST_F(BookmarkModelTest, AddURLWithCreationTimeAndMetaInfo) {}

TEST_F(BookmarkModelTest, AddURLWithGUID) {}

TEST_F(BookmarkModelTest, AddURLToMobileBookmarks) {}

TEST_F(BookmarkModelTest, AddFolder) {}

TEST_F(BookmarkModelTest, AddFolderWithCreationTime) {}

TEST_F(BookmarkModelTest, AddFolderWithGUID) {}

TEST_F(BookmarkModelTest, AddFolderWithWhitespaceTitle) {}

TEST_F(BookmarkModelTest, RemoveURL) {}

TEST_F(BookmarkModelTest, RemoveFolder) {}

TEST_F(BookmarkModelTest, RemoveAllUserBookmarks) {}

TEST_F(BookmarkModelTest, UpdateLastUsedTimeInRange) {}

TEST_F(BookmarkModelTest, ClearLastUsedTimeInRange) {}

TEST_F(BookmarkModelTest, ClearLastUsedTimeInRangeForAllTime) {}

TEST_F(BookmarkModelTest, SetTitle) {}

TEST_F(BookmarkModelTest, SetTitleWithWhitespace) {}

TEST_F(BookmarkModelTest, SetFolderTitle) {}

TEST_F(BookmarkModelTest, SetURL) {}

TEST_F(BookmarkModelTest, SetDateAdded) {}

TEST_F(BookmarkModelTest, Move) {}

TEST_F(BookmarkModelTest, NonMovingMoveCall) {}

TEST_F(BookmarkModelTest, MoveURLFromFolder) {}

TEST_F(BookmarkModelTest, MoveFolder) {}

TEST_F(BookmarkModelTest, MoveWithUuidCollision) {}

TEST_F(BookmarkModelTest, Copy) {}

// Tests the default node if no bookmarks have been added yet
TEST_F(BookmarkModelTest, ParentForNewNodesWithEmptyModel) {}

#if BUILDFLAG(IS_ANDROID)
// Tests that the bookmark_bar_node can still be returned even on Android in
// case the last bookmark was added to it.
TEST_F(BookmarkModelTest, ParentCanBeBookmarkBarOnAndroid) {
  const std::u16string title(u"foo");
  const GURL url("http://foo.com");

  model_->AddURL(model_->bookmark_bar_node(), 0, title, url);
  ASSERT_EQ(model_->bookmark_bar_node(), GetParentForNewNodes(model_.get()));
}
#endif

// Tests that adding a URL to a folder updates the last modified time.
TEST_F(BookmarkModelTest, ParentForNewNodes) {}

// Tests that adding a URL to a folder updates the last modified time.
TEST_F(BookmarkModelTest, ParentForNewMobileNodes) {}

// Make sure recently modified stays in sync when adding a URL.
TEST_F(BookmarkModelTest, MostRecentlyModifiedFolders) {}

// Make sure MostRecentlyAddedEntries stays in sync.
TEST_F(BookmarkModelTest, MostRecentlyAddedEntries) {}

// Make sure GetMostRecentlyUsedEntries stays in sync.
TEST_F(BookmarkModelTest, GetMostRecentlyUsedEntries) {}

// Makes sure GetMostRecentlyAddedUserNodeForURL stays in sync.
TEST_F(BookmarkModelTest, GetMostRecentlyAddedUserNodeForURL) {}

// Makes sure GetUniqueUrls removes duplicates.
TEST_F(BookmarkModelTest, GetUniqueUrlsWithDups) {}

TEST_F(BookmarkModelTest, HasBookmarks) {}

TEST_F(BookmarkModelTest, Sort) {}

TEST_F(BookmarkModelTest, Reorder) {}

TEST_F(BookmarkModelTest, NodeVisibility) {}

TEST_F(BookmarkModelTest, NodeVisibility_AllBookmarksPhase0) {}

TEST_F(BookmarkModelTest, MobileNodeVisibleWithChildren) {}

TEST_F(BookmarkModelTest, ExtensiveChangesObserver) {}

TEST_F(BookmarkModelTest, MultipleExtensiveChangesObserver) {}

// Verifies that IsBookmarked is true if any bookmark matches the given URL,
// and that IsBookmarkedByUser is true only if at least one of the matching
// bookmarks can be edited by the user.
TEST_F(BookmarkModelTest, IsBookmarked) {}

// Verifies that GetMostRecentlyAddedUserNodeForURL skips bookmarks that
// are not owned by the user.
TEST_F(BookmarkModelTest, GetMostRecentlyAddedUserNodeForURLSkipsManagedNodes) {}

// Verifies that renaming a bookmark folder does not add the folder node to the
// autocomplete index. crbug.com/778266
TEST_F(BookmarkModelTest, RenamedFolderNodeExcludedFromIndex) {}

TEST_F(BookmarkModelTest, GetBookmarksMatching) {}

// Verifies that TitledUrlIndex is updated when a bookmark is removed.
TEST_F(BookmarkModelTest, TitledUrlIndexUpdatedOnRemove) {}

// Verifies that TitledUrlIndex is updated when a bookmark's title changes.
TEST_F(BookmarkModelTest, TitledUrlIndexUpdatedOnChangeTitle) {}

// Verifies that TitledUrlIndex is updated when a bookmark's URL changes.
TEST_F(BookmarkModelTest, TitledUrlIndexUpdatedOnChangeURL) {}

TEST_F(BookmarkModelTest, GetNodeByUuid) {}

TEST_F(BookmarkModelTest, GetAccountNodeByUuid) {}

TEST_F(BookmarkModelTest, GetPermanentNodeByUuid) {}

TEST_F(BookmarkModelTest, GetAccountPermanentNodeByUuid) {}

TEST_F(BookmarkModelTest, GetNodeByUuidAfterRemove) {}

TEST_F(BookmarkModelTest, GetNodeByUuidAfterRemoveAllUserBookmarks) {}

TEST(BookmarkModelLoadTest, NodesPopulatedOnLoad) {}

TEST(BookmarkModelLoadTest, NodesPopulatedIncludingAccountNodesOnLoad) {}

TEST(BookmarkModelLoadTest, AccountSyncMetadataPopulatedWithoutNodesOnLoad) {}

// Verifies the TitledUrlIndex is properly loaded.
TEST(BookmarkModelLoadTest, TitledUrlIndexPopulatedOnLoad) {}

// Verifies the TitledUrlIndex is properly loaded for account bookmarks.
TEST(BookmarkModelLoadTest, TitledUrlIndexPopulatedForAccountNodesOnLoad) {}

// Verifies the UUID index is properly loaded.
TEST(BookmarkModelLoadTest, UuidIndexPopulatedOnLoad) {}

// Verifies the UUID index is properly loaded, for account nodes.
TEST(BookmarkModelLoadTest, UuidIndexPopulatedForAccountNodesOnLoad) {}

TEST(BookmarkModelStorageTest, SaveExactlyOneFile) {}

TEST(BookmarkNodeTest, NodeMetaInfo) {}

// Creates a set of nodes in the bookmark model, and checks that the loaded
// structure is what we first created.
TEST(BookmarkModelTest2, CreateAndRestore) {}

TEST_F(BookmarkModelTest, CreateAccountPermanentFolders) {}

TEST_F(BookmarkModelTest, RemoveAccountPermanentFolders) {}

TEST_F(BookmarkModelTest, IsLocalOnlyNodeWithSyncFeatureOff) {}

TEST_F(BookmarkModelTest, IsLocalOnlyNodeWithSyncFeatureOn) {}

TEST_F(BookmarkModelTest, IsLocalOnlyNodeWithSyncFeatureOnAndDettachedNode) {}

}  // namespace

class BookmarkModelFaviconTest : public testing::Test,
                                 public BookmarkModelObserver {};

// Test that BookmarkModel::OnFaviconsChanged() sends a notification that the
// favicon changed to each BookmarkNode which has either a matching page URL
// (e.g. http://www.google.com) or a matching icon URL
// (e.g. http://www.google.com/favicon.ico).
TEST_F(BookmarkModelFaviconTest, FaviconsChangedObserver) {}

TEST_F(BookmarkModelFaviconTest, ShouldResetFaviconStatusAfterRestore) {}

}  // namespace bookmarks