1// Copyright (c) 2012 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 "chrome/browser/autocomplete/autocomplete_match.h"
6
7#include "base/basictypes.h"
8#include "testing/gtest/include/gtest/gtest.h"
9
10TEST(AutocompleteMatchTest, MoreRelevant) {
11  struct RelevantCases {
12    int r1;
13    int r2;
14    bool expected_result;
15  } cases[] = {
16    {  10,   0, true  },
17    {  10,  -5, true  },
18    {  -5,  10, false },
19    {   0,  10, false },
20    { -10,  -5, false  },
21    {  -5, -10, true },
22  };
23
24  AutocompleteMatch m1(NULL, 0, false,
25                       AutocompleteMatchType::URL_WHAT_YOU_TYPED);
26  AutocompleteMatch m2(NULL, 0, false,
27                       AutocompleteMatchType::URL_WHAT_YOU_TYPED);
28
29  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
30    m1.relevance = cases[i].r1;
31    m2.relevance = cases[i].r2;
32    EXPECT_EQ(cases[i].expected_result,
33              AutocompleteMatch::MoreRelevant(m1, m2));
34  }
35}
36
37TEST(AutocompleteMatchTest, MergeClassifications) {
38  // Merging two empty vectors should result in an empty vector.
39  EXPECT_EQ(std::string(),
40      AutocompleteMatch::ClassificationsToString(
41          AutocompleteMatch::MergeClassifications(
42              AutocompleteMatch::ACMatchClassifications(),
43              AutocompleteMatch::ACMatchClassifications())));
44
45  // If one vector is empty and the other is "trivial" but non-empty (i.e. (0,
46  // NONE)), the non-empty vector should be returned.
47  EXPECT_EQ("0,0",
48      AutocompleteMatch::ClassificationsToString(
49          AutocompleteMatch::MergeClassifications(
50              AutocompleteMatch::ClassificationsFromString("0,0"),
51              AutocompleteMatch::ACMatchClassifications())));
52  EXPECT_EQ("0,0",
53      AutocompleteMatch::ClassificationsToString(
54          AutocompleteMatch::MergeClassifications(
55              AutocompleteMatch::ACMatchClassifications(),
56              AutocompleteMatch::ClassificationsFromString("0,0"))));
57
58  // Ditto if the one-entry vector is non-trivial.
59  EXPECT_EQ("0,1",
60      AutocompleteMatch::ClassificationsToString(
61          AutocompleteMatch::MergeClassifications(
62              AutocompleteMatch::ClassificationsFromString("0,1"),
63              AutocompleteMatch::ACMatchClassifications())));
64  EXPECT_EQ("0,1",
65      AutocompleteMatch::ClassificationsToString(
66          AutocompleteMatch::MergeClassifications(
67              AutocompleteMatch::ACMatchClassifications(),
68              AutocompleteMatch::ClassificationsFromString("0,1"))));
69
70  // Merge an unstyled one-entry vector with a styled one-entry vector.
71  EXPECT_EQ("0,1",
72      AutocompleteMatch::ClassificationsToString(
73          AutocompleteMatch::MergeClassifications(
74              AutocompleteMatch::ClassificationsFromString("0,0"),
75              AutocompleteMatch::ClassificationsFromString("0,1"))));
76
77  // Test simple cases of overlap.
78  EXPECT_EQ("0,3," "1,2",
79      AutocompleteMatch::ClassificationsToString(
80          AutocompleteMatch::MergeClassifications(
81              AutocompleteMatch::ClassificationsFromString("0,1," "1,0"),
82              AutocompleteMatch::ClassificationsFromString("0,2"))));
83  EXPECT_EQ("0,3," "1,2",
84      AutocompleteMatch::ClassificationsToString(
85          AutocompleteMatch::MergeClassifications(
86              AutocompleteMatch::ClassificationsFromString("0,2"),
87              AutocompleteMatch::ClassificationsFromString("0,1," "1,0"))));
88
89  // Test the case where both vectors have classifications at the same
90  // positions.
91  EXPECT_EQ("0,3",
92      AutocompleteMatch::ClassificationsToString(
93          AutocompleteMatch::MergeClassifications(
94              AutocompleteMatch::ClassificationsFromString("0,1," "1,2"),
95              AutocompleteMatch::ClassificationsFromString("0,2," "1,1"))));
96
97  // Test an arbitrary complicated case.
98  EXPECT_EQ("0,2," "1,0," "2,1," "4,3," "5,7," "6,3," "7,7," "15,1," "17,0",
99      AutocompleteMatch::ClassificationsToString(
100          AutocompleteMatch::MergeClassifications(
101              AutocompleteMatch::ClassificationsFromString(
102                  "0,0," "2,1," "4,3," "7,7," "10,6," "15,0"),
103              AutocompleteMatch::ClassificationsFromString(
104                  "0,2," "1,0," "5,7," "6,1," "17,0"))));
105}
106
107TEST(AutocompleteMatchTest, SupportsDeletion) {
108  // A non-deletable match with no duplicates.
109  AutocompleteMatch m(NULL, 0, false,
110                      AutocompleteMatchType::URL_WHAT_YOU_TYPED);
111  EXPECT_FALSE(m.SupportsDeletion());
112
113  // A deletable match with no duplicates.
114  AutocompleteMatch m1(NULL, 0, true,
115                       AutocompleteMatchType::URL_WHAT_YOU_TYPED);
116  EXPECT_TRUE(m1.SupportsDeletion());
117
118  // A non-deletable match, with non-deletable duplicates.
119  m.duplicate_matches.push_back(AutocompleteMatch(
120      NULL, 0, false, AutocompleteMatchType::URL_WHAT_YOU_TYPED));
121  m.duplicate_matches.push_back(AutocompleteMatch(
122      NULL, 0, false, AutocompleteMatchType::URL_WHAT_YOU_TYPED));
123  EXPECT_FALSE(m.SupportsDeletion());
124
125  // A non-deletable match, with at least one deletable duplicate.
126  m.duplicate_matches.push_back(AutocompleteMatch(
127      NULL, 0, true, AutocompleteMatchType::URL_WHAT_YOU_TYPED));
128  EXPECT_TRUE(m.SupportsDeletion());
129}
130