OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/app_list/search/tokenized_string_match.h" | 5 #include "ui/app_list/search/tokenized_string_match.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
12 | 12 |
13 namespace app_list { | 13 namespace app_list { |
14 namespace test { | 14 namespace test { |
15 | 15 |
16 // Returns a string of |text| marked the hits in |match| using block bracket. | 16 // Returns a string of |text| marked the hits in |match| using block bracket. |
17 // e.g. text= "Text", hits = [{0,1}], returns "[T]ext". | 17 // e.g. text= "Text", match.hits = [{0,1}], returns "[T]ext". |
18 std::string MatchHit(const base::string16& text, | 18 std::string MatchHit(const base::string16& text, |
19 const TokenizedStringMatch& match) { | 19 const TokenizedStringMatch& match) { |
20 base::string16 marked = text; | 20 base::string16 marked = text; |
21 | 21 |
22 const TokenizedStringMatch::Hits& hits = match.hits(); | 22 const TokenizedStringMatch::Hits& hits = match.hits(); |
23 for (TokenizedStringMatch::Hits::const_reverse_iterator it = hits.rbegin(); | 23 for (TokenizedStringMatch::Hits::const_reverse_iterator it = hits.rbegin(); |
24 it != hits.rend(); ++it) { | 24 it != hits.rend(); ++it) { |
25 const gfx::Range& hit = *it; | 25 const gfx::Range& hit = *it; |
26 marked.insert(hit.end(), 1, ']'); | 26 marked.insert(hit.end(), 1, ']'); |
27 marked.insert(hit.start(), 1, '['); | 27 marked.insert(hit.start(), 1, '['); |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 EXPECT_TRUE(match_high.Calculate( | 112 EXPECT_TRUE(match_high.Calculate( |
113 base::UTF8ToUTF16(kTestCases[i].query_high), text)); | 113 base::UTF8ToUTF16(kTestCases[i].query_high), text)); |
114 EXPECT_LT(match_low.relevance(), match_high.relevance()) | 114 EXPECT_LT(match_low.relevance(), match_high.relevance()) |
115 << "Test case " << i | 115 << "Test case " << i |
116 << " : text=" << kTestCases[i].text | 116 << " : text=" << kTestCases[i].text |
117 << ", query_low=" << kTestCases[i].query_low | 117 << ", query_low=" << kTestCases[i].query_low |
118 << ", query_high=" << kTestCases[i].query_high; | 118 << ", query_high=" << kTestCases[i].query_high; |
119 } | 119 } |
120 } | 120 } |
121 | 121 |
| 122 // More specialized tests of the absolute relevance scores. (These tests are |
| 123 // minimal, because they are so brittle. Changing the scoring algorithm will |
| 124 // require updating this test.) |
| 125 TEST(TokenizedStringMatchTest, AbsoluteRelevance) { |
| 126 const double kEpsilon = 0.006; |
| 127 struct { |
| 128 const char* text; |
| 129 const char* query; |
| 130 double expected_score; |
| 131 } kTestCases[] = { |
| 132 // The first few chars should increase the score extremely high. After |
| 133 // that, they should count less. |
| 134 // NOTE: 0.87 is a magic number, as it is the Omnibox score for a "pretty |
| 135 // good" match. We want a 3-letter prefix match to be slightly above 0.87. |
| 136 {"Google Chrome", "g", 0.5}, |
| 137 {"Google Chrome", "go", 0.75}, |
| 138 {"Google Chrome", "goo", 0.88}, |
| 139 {"Google Chrome", "goog", 0.94}, |
| 140 }; |
| 141 |
| 142 TokenizedStringMatch match; |
| 143 for (size_t i = 0; i < arraysize(kTestCases); ++i) { |
| 144 const base::string16 text(base::UTF8ToUTF16(kTestCases[i].text)); |
| 145 EXPECT_TRUE(match.Calculate(base::UTF8ToUTF16(kTestCases[i].query), text)); |
| 146 EXPECT_NEAR(match.relevance(), kTestCases[i].expected_score, kEpsilon) |
| 147 << "Test case " << i << " : text=" << kTestCases[i].text |
| 148 << ", query=" << kTestCases[i].query |
| 149 << ", expected_score=" << kTestCases[i].expected_score; |
| 150 } |
| 151 } |
| 152 |
122 } // namespace test | 153 } // namespace test |
123 } // namespace app_list | 154 } // namespace app_list |
OLD | NEW |