| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "base/values.h" | 5 #include "base/values.h" |
| 6 #include "chrome/browser/extensions/api/omnibox/omnibox_api.h" | 6 #include "chrome/browser/extensions/api/omnibox/omnibox_api.h" |
| 7 #include "chrome/common/extensions/api/omnibox.h" |
| 8 #include "chrome/common/extensions/value_builder.h" |
| 7 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 8 #include "testing/platform_test.h" | 10 #include "testing/platform_test.h" |
| 9 | 11 |
| 10 namespace extensions { | 12 namespace extensions { |
| 11 | 13 |
| 14 namespace omnibox = api::omnibox; |
| 15 namespace SendSuggestions = omnibox::SendSuggestions; |
| 16 |
| 12 namespace { | 17 namespace { |
| 13 | 18 |
| 14 const int kNone = ACMatchClassification::NONE; | 19 const int kNone = ACMatchClassification::NONE; |
| 15 const int kUrl = ACMatchClassification::URL; | 20 const int kUrl = ACMatchClassification::URL; |
| 16 const int kMatch = ACMatchClassification::MATCH; | 21 const int kMatch = ACMatchClassification::MATCH; |
| 17 const int kDim = ACMatchClassification::DIM; | 22 const int kDim = ACMatchClassification::DIM; |
| 18 | 23 |
| 19 void AppendStyle(const std::string& type, | |
| 20 int offset, int length, | |
| 21 ListValue* styles) { | |
| 22 DictionaryValue* style = new DictionaryValue; | |
| 23 style->SetString("type", type); | |
| 24 style->SetInteger("offset", offset); | |
| 25 style->SetInteger("length", length); | |
| 26 styles->Append(style); | |
| 27 } | |
| 28 | |
| 29 void CompareClassification(const ACMatchClassifications& expected, | 24 void CompareClassification(const ACMatchClassifications& expected, |
| 30 const ACMatchClassifications& actual) { | 25 const ACMatchClassifications& actual) { |
| 31 EXPECT_EQ(expected.size(), actual.size()); | 26 EXPECT_EQ(expected.size(), actual.size()); |
| 32 for (size_t i = 0; i < expected.size() && i < actual.size(); ++i) { | 27 for (size_t i = 0; i < expected.size() && i < actual.size(); ++i) { |
| 33 EXPECT_EQ(expected[i].offset, actual[i].offset) << "Index:" << i; | 28 EXPECT_EQ(expected[i].offset, actual[i].offset) << "Index:" << i; |
| 34 EXPECT_EQ(expected[i].style, actual[i].style) << "Index:" << i; | 29 EXPECT_EQ(expected[i].style, actual[i].style) << "Index:" << i; |
| 35 } | 30 } |
| 36 } | 31 } |
| 37 | 32 |
| 38 } // namespace | 33 } // namespace |
| 39 | 34 |
| 40 // Test output key: n = character with no styling, d = dim, m = match, u = url | 35 // Test output key: n = character with no styling, d = dim, m = match, u = url |
| 41 // u = 1, m = 2, d = 4. u+d = 5, etc. | 36 // u = 1, m = 2, d = 4. u+d = 5, etc. |
| 42 | 37 |
| 43 // 0123456789 | 38 // 0123456789 |
| 44 // mmmm | 39 // mmmm |
| 45 // + ddd | 40 // + ddd |
| 46 // = nmmmmndddn | 41 // = nmmmmndddn |
| 47 TEST(ExtensionOmniboxTest, DescriptionStylesSimple) { | 42 TEST(ExtensionOmniboxTest, DescriptionStylesSimple) { |
| 48 ListValue styles_value; | 43 scoped_ptr<ListValue> list = ListBuilder() |
| 49 AppendStyle("match", 1, 4, &styles_value); | 44 .Append(42) |
| 50 AppendStyle("dim", 6, 3, &styles_value); | 45 .Append(ListBuilder() |
| 46 .Append(DictionaryBuilder() |
| 47 .Set("content", "content") |
| 48 .Set("description", "description") |
| 49 .Set("descriptionStyles", ListBuilder() |
| 50 .Append(DictionaryBuilder() |
| 51 .Set("type", "match") |
| 52 .Set("offset", 1) |
| 53 .Set("length", 4)) |
| 54 .Append(DictionaryBuilder() |
| 55 .Set("type", "dim") |
| 56 .Set("offset", 6) |
| 57 .Set("length", 3))))).Build(); |
| 51 | 58 |
| 52 ACMatchClassifications styles_expected; | 59 ACMatchClassifications styles_expected; |
| 53 styles_expected.push_back(ACMatchClassification(0, kNone)); | 60 styles_expected.push_back(ACMatchClassification(0, kNone)); |
| 54 styles_expected.push_back(ACMatchClassification(1, kMatch)); | 61 styles_expected.push_back(ACMatchClassification(1, kMatch)); |
| 55 styles_expected.push_back(ACMatchClassification(5, kNone)); | 62 styles_expected.push_back(ACMatchClassification(5, kNone)); |
| 56 styles_expected.push_back(ACMatchClassification(6, kDim)); | 63 styles_expected.push_back(ACMatchClassification(6, kDim)); |
| 57 styles_expected.push_back(ACMatchClassification(9, kNone)); | 64 styles_expected.push_back(ACMatchClassification(9, kNone)); |
| 58 | 65 |
| 59 ExtensionOmniboxSuggestion suggestions; | 66 scoped_ptr<SendSuggestions::Params> params( |
| 60 suggestions.description.resize(10); | 67 SendSuggestions::Params::Create(*list)); |
| 61 EXPECT_TRUE(suggestions.ReadStylesFromValue(styles_value)); | 68 EXPECT_TRUE(params); |
| 62 CompareClassification(styles_expected, suggestions.description_styles); | 69 EXPECT_TRUE(params->suggest_results[0].get()); |
| 70 CompareClassification(styles_expected, StyleTypesToACMatchClassifications( |
| 71 *params->suggest_results[0])); |
| 63 | 72 |
| 64 // Same input, but swap the order. Ensure it still works. | 73 // Same input, but swap the order. Ensure it still works. |
| 65 styles_value.Clear(); | 74 scoped_ptr<ListValue> swap_list = ListBuilder() |
| 66 AppendStyle("dim", 6, 3, &styles_value); | 75 .Append(42) |
| 67 AppendStyle("match", 1, 4, &styles_value); | 76 .Append(ListBuilder() |
| 68 EXPECT_TRUE(suggestions.ReadStylesFromValue(styles_value)); | 77 .Append(DictionaryBuilder() |
| 69 CompareClassification(styles_expected, suggestions.description_styles); | 78 .Set("content", "content") |
| 79 .Set("description", "description") |
| 80 .Set("descriptionStyles", ListBuilder() |
| 81 .Append(DictionaryBuilder() |
| 82 .Set("type", "dim") |
| 83 .Set("offset", 6) |
| 84 .Set("length", 3)) |
| 85 .Append(DictionaryBuilder() |
| 86 .Set("type", "match") |
| 87 .Set("offset", 1) |
| 88 .Set("length", 4))))).Build(); |
| 89 |
| 90 scoped_ptr<SendSuggestions::Params> swapped_params( |
| 91 SendSuggestions::Params::Create(*swap_list)); |
| 92 EXPECT_TRUE(swapped_params); |
| 93 EXPECT_TRUE(swapped_params->suggest_results[0].get()); |
| 94 CompareClassification(styles_expected, StyleTypesToACMatchClassifications( |
| 95 *swapped_params->suggest_results[0])); |
| 70 } | 96 } |
| 71 | 97 |
| 72 // 0123456789 | 98 // 0123456789 |
| 73 // uuuuu | 99 // uuuuu |
| 74 // + dd | 100 // + dd |
| 75 // + mm | 101 // + mm |
| 76 // + mmmm | 102 // + mmmm |
| 77 // + dd | 103 // + dd |
| 78 // = 3773unnnn66 | 104 // = 3773unnnn66 |
| 79 TEST(ExtensionOmniboxTest, DescriptionStylesCombine) { | 105 TEST(ExtensionOmniboxTest, DescriptionStylesCombine) { |
| 80 ListValue styles_value; | 106 scoped_ptr<ListValue> list = ListBuilder() |
| 81 AppendStyle("url", 0, 5, &styles_value); | 107 .Append(42) |
| 82 AppendStyle("dim", 9, 2, &styles_value); | 108 .Append(ListBuilder() |
| 83 AppendStyle("match", 9, 2, &styles_value); | 109 .Append(DictionaryBuilder() |
| 84 AppendStyle("match", 0, 4, &styles_value); | 110 .Set("content", "content") |
| 85 AppendStyle("dim", 1, 2, &styles_value); | 111 .Set("description", "description") |
| 112 .Set("descriptionStyles", ListBuilder() |
| 113 .Append(DictionaryBuilder() |
| 114 .Set("type", "url") |
| 115 .Set("offset", 0) |
| 116 .Set("length", 5)) |
| 117 .Append(DictionaryBuilder() |
| 118 .Set("type", "dim") |
| 119 .Set("offset", 9) |
| 120 .Set("length", 2)) |
| 121 .Append(DictionaryBuilder() |
| 122 .Set("type", "match") |
| 123 .Set("offset", 9) |
| 124 .Set("length", 2)) |
| 125 .Append(DictionaryBuilder() |
| 126 .Set("type", "match") |
| 127 .Set("offset", 0) |
| 128 .Set("length", 4)) |
| 129 .Append(DictionaryBuilder() |
| 130 .Set("type", "dim") |
| 131 .Set("offset", 1) |
| 132 .Set("length", 2))))).Build(); |
| 86 | 133 |
| 87 ACMatchClassifications styles_expected; | 134 ACMatchClassifications styles_expected; |
| 88 styles_expected.push_back(ACMatchClassification(0, kUrl | kMatch)); | 135 styles_expected.push_back(ACMatchClassification(0, kUrl | kMatch)); |
| 89 styles_expected.push_back(ACMatchClassification(1, kUrl | kMatch | kDim)); | 136 styles_expected.push_back(ACMatchClassification(1, kUrl | kMatch | kDim)); |
| 90 styles_expected.push_back(ACMatchClassification(3, kUrl | kMatch)); | 137 styles_expected.push_back(ACMatchClassification(3, kUrl | kMatch)); |
| 91 styles_expected.push_back(ACMatchClassification(4, kUrl)); | 138 styles_expected.push_back(ACMatchClassification(4, kUrl)); |
| 92 styles_expected.push_back(ACMatchClassification(5, kNone)); | 139 styles_expected.push_back(ACMatchClassification(5, kNone)); |
| 93 styles_expected.push_back(ACMatchClassification(9, kMatch | kDim)); | 140 styles_expected.push_back(ACMatchClassification(9, kMatch | kDim)); |
| 94 | 141 |
| 95 ExtensionOmniboxSuggestion suggestions; | 142 scoped_ptr<SendSuggestions::Params> params( |
| 96 suggestions.description.resize(10); | 143 SendSuggestions::Params::Create(*list)); |
| 97 EXPECT_TRUE(suggestions.ReadStylesFromValue(styles_value)); | 144 EXPECT_TRUE(params); |
| 98 CompareClassification(styles_expected, suggestions.description_styles); | 145 EXPECT_TRUE(params->suggest_results[0].get()); |
| 146 CompareClassification(styles_expected, StyleTypesToACMatchClassifications( |
| 147 *params->suggest_results[0])); |
| 99 | 148 |
| 100 // Try moving the "dim/match" style pair at offset 9. Output should be the | 149 // Try moving the "dim/match" style pair at offset 9. Output should be the |
| 101 // same. | 150 // same. |
| 102 styles_value.Clear(); | 151 scoped_ptr<ListValue> moved_list = ListBuilder() |
| 103 AppendStyle("url", 0, 5, &styles_value); | 152 .Append(42) |
| 104 AppendStyle("match", 0, 4, &styles_value); | 153 .Append(ListBuilder() |
| 105 AppendStyle("dim", 9, 2, &styles_value); | 154 .Append(DictionaryBuilder() |
| 106 AppendStyle("match", 9, 2, &styles_value); | 155 .Set("content", "content") |
| 107 AppendStyle("dim", 1, 2, &styles_value); | 156 .Set("description", "description") |
| 108 EXPECT_TRUE(suggestions.ReadStylesFromValue(styles_value)); | 157 .Set("descriptionStyles", ListBuilder() |
| 109 CompareClassification(styles_expected, suggestions.description_styles); | 158 .Append(DictionaryBuilder() |
| 159 .Set("type", "url") |
| 160 .Set("offset", 0) |
| 161 .Set("length", 5)) |
| 162 .Append(DictionaryBuilder() |
| 163 .Set("type", "match") |
| 164 .Set("offset", 0) |
| 165 .Set("length", 4)) |
| 166 .Append(DictionaryBuilder() |
| 167 .Set("type", "dim") |
| 168 .Set("offset", 9) |
| 169 .Set("length", 2)) |
| 170 .Append(DictionaryBuilder() |
| 171 .Set("type", "match") |
| 172 .Set("offset", 9) |
| 173 .Set("length", 2)) |
| 174 .Append(DictionaryBuilder() |
| 175 .Set("type", "dim") |
| 176 .Set("offset", 1) |
| 177 .Set("length", 2))))).Build(); |
| 178 |
| 179 scoped_ptr<SendSuggestions::Params> moved_params( |
| 180 SendSuggestions::Params::Create(*moved_list)); |
| 181 EXPECT_TRUE(moved_params); |
| 182 EXPECT_TRUE(moved_params->suggest_results[0].get()); |
| 183 CompareClassification(styles_expected, StyleTypesToACMatchClassifications( |
| 184 *moved_params->suggest_results[0])); |
| 110 } | 185 } |
| 111 | 186 |
| 112 // 0123456789 | 187 // 0123456789 |
| 113 // uuuuu | 188 // uuuuu |
| 114 // + mmmmm | 189 // + mmmmm |
| 115 // + mmm | 190 // + mmm |
| 116 // + ddd | 191 // + ddd |
| 117 // + ddd | 192 // + ddd |
| 118 // = 77777nnnnn | 193 // = 77777nnnnn |
| 119 TEST(ExtensionOmniboxTest, DescriptionStylesCombine2) { | 194 TEST(ExtensionOmniboxTest, DescriptionStylesCombine2) { |
| 120 ListValue styles_value; | 195 scoped_ptr<ListValue> list = ListBuilder() |
| 121 AppendStyle("url", 0, 5, &styles_value); | 196 .Append(42) |
| 122 AppendStyle("match", 0, 5, &styles_value); | 197 .Append(ListBuilder() |
| 123 AppendStyle("match", 0, 3, &styles_value); | 198 .Append(DictionaryBuilder() |
| 124 AppendStyle("dim", 2, 3, &styles_value); | 199 .Set("content", "content") |
| 125 AppendStyle("dim", 0, 3, &styles_value); | 200 .Set("description", "description") |
| 201 .Set("descriptionStyles", ListBuilder() |
| 202 .Append(DictionaryBuilder() |
| 203 .Set("type", "url") |
| 204 .Set("offset", 0) |
| 205 .Set("length", 5)) |
| 206 .Append(DictionaryBuilder() |
| 207 .Set("type", "match") |
| 208 .Set("offset", 0) |
| 209 .Set("length", 5)) |
| 210 .Append(DictionaryBuilder() |
| 211 .Set("type", "match") |
| 212 .Set("offset", 0) |
| 213 .Set("length", 3)) |
| 214 .Append(DictionaryBuilder() |
| 215 .Set("type", "dim") |
| 216 .Set("offset", 2) |
| 217 .Set("length", 3)) |
| 218 .Append(DictionaryBuilder() |
| 219 .Set("type", "dim") |
| 220 .Set("offset", 0) |
| 221 .Set("length", 3))))).Build(); |
| 126 | 222 |
| 127 ACMatchClassifications styles_expected; | 223 ACMatchClassifications styles_expected; |
| 128 styles_expected.push_back(ACMatchClassification(0, kUrl | kMatch | kDim)); | 224 styles_expected.push_back(ACMatchClassification(0, kUrl | kMatch | kDim)); |
| 129 styles_expected.push_back(ACMatchClassification(5, kNone)); | 225 styles_expected.push_back(ACMatchClassification(5, kNone)); |
| 130 | 226 |
| 131 ExtensionOmniboxSuggestion suggestions; | 227 scoped_ptr<SendSuggestions::Params> params( |
| 132 suggestions.description.resize(10); | 228 SendSuggestions::Params::Create(*list)); |
| 133 EXPECT_TRUE(suggestions.ReadStylesFromValue(styles_value)); | 229 EXPECT_TRUE(params); |
| 134 CompareClassification(styles_expected, suggestions.description_styles); | 230 EXPECT_TRUE(params->suggest_results[0].get()); |
| 231 CompareClassification(styles_expected, StyleTypesToACMatchClassifications( |
| 232 *params->suggest_results[0])); |
| 135 } | 233 } |
| 136 | 234 |
| 137 } // namespace extensions | 235 } // namespace extensions |
| OLD | NEW |