1 // Copyright 2017 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/bookmarks/browser/titled_url_match.h"
6 
7 #include "base/macros.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 
11 namespace bookmarks {
12 
13 using MatchPositions = TitledUrlMatch::MatchPositions;
14 
TEST(TitledUrlMatchTest,EmptyOffsetsForEmptyMatchPositions)15 TEST(TitledUrlMatchTest, EmptyOffsetsForEmptyMatchPositions) {
16   auto offsets = TitledUrlMatch::OffsetsFromMatchPositions(MatchPositions());
17   EXPECT_TRUE(offsets.empty());
18 }
19 
TEST(TitledUrlMatchTest,OffsetsFromMatchPositions)20 TEST(TitledUrlMatchTest, OffsetsFromMatchPositions) {
21   MatchPositions match_positions = {{1, 3}, {4, 5}, {10, 15}};
22   std::vector<size_t> expected_offsets = {1, 3, 4, 5, 10, 15};
23   auto offsets = TitledUrlMatch::OffsetsFromMatchPositions(match_positions);
24   EXPECT_TRUE(
25       std::equal(offsets.begin(), offsets.end(), expected_offsets.begin()));
26 }
27 
TEST(TitledUrlMatchTest,ReplaceOffsetsInEmptyMatchPositions)28 TEST(TitledUrlMatchTest, ReplaceOffsetsInEmptyMatchPositions) {
29   auto match_positions = TitledUrlMatch::ReplaceOffsetsInMatchPositions(
30       MatchPositions(), std::vector<size_t>());
31   EXPECT_TRUE(match_positions.empty());
32 }
33 
TEST(TitledUrlMatchTest,ReplaceOffsetsInMatchPositions)34 TEST(TitledUrlMatchTest, ReplaceOffsetsInMatchPositions) {
35   MatchPositions orig_match_positions = {{1, 3}, {4, 5}, {10, 15}};
36   std::vector<size_t> offsets = {0, 2, 3, 4, 9, 14};
37   MatchPositions expected_match_positions = {{0, 2}, {3, 4}, {9, 14}};
38   auto match_positions = TitledUrlMatch::ReplaceOffsetsInMatchPositions(
39       orig_match_positions, offsets);
40   EXPECT_TRUE(std::equal(match_positions.begin(), match_positions.end(),
41                          expected_match_positions.begin()));
42 }
43 
TEST(TitledUrlMatchTest,ReplaceOffsetsRemovesItemsWithNposOffsets)44 TEST(TitledUrlMatchTest, ReplaceOffsetsRemovesItemsWithNposOffsets) {
45   MatchPositions orig_match_positions = {{1, 3}, {4, 5}, {10, 15}, {17, 20}};
46   std::vector<size_t> offsets = {0,
47                                  base::string16::npos,
48                                  base::string16::npos,
49                                  4,
50                                  base::string16::npos,
51                                  base::string16::npos,
52                                  17,
53                                  20};
54   MatchPositions expected_match_positions = {{17, 20}};
55   auto match_positions = TitledUrlMatch::ReplaceOffsetsInMatchPositions(
56       orig_match_positions, offsets);
57   EXPECT_TRUE(std::equal(match_positions.begin(), match_positions.end(),
58                          expected_match_positions.begin()));
59 }
60 
61 }
62