omnibox_unittest.cc revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
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 "base/values.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" 9#include "testing/gtest/include/gtest/gtest.h" 10#include "testing/platform_test.h" 11 12namespace extensions { 13 14namespace omnibox = api::omnibox; 15namespace SendSuggestions = omnibox::SendSuggestions; 16 17namespace { 18 19const int kNone = ACMatchClassification::NONE; 20const int kUrl = ACMatchClassification::URL; 21const int kMatch = ACMatchClassification::MATCH; 22const int kDim = ACMatchClassification::DIM; 23 24void CompareClassification(const ACMatchClassifications& expected, 25 const ACMatchClassifications& actual) { 26 EXPECT_EQ(expected.size(), actual.size()); 27 for (size_t i = 0; i < expected.size() && i < actual.size(); ++i) { 28 EXPECT_EQ(expected[i].offset, actual[i].offset) << "Index:" << i; 29 EXPECT_EQ(expected[i].style, actual[i].style) << "Index:" << i; 30 } 31} 32 33} // namespace 34 35// Test output key: n = character with no styling, d = dim, m = match, u = url 36// u = 1, m = 2, d = 4. u+d = 5, etc. 37 38// 0123456789 39// mmmm 40// + ddd 41// = nmmmmndddn 42TEST(ExtensionOmniboxTest, DescriptionStylesSimple) { 43 scoped_ptr<ListValue> list = ListBuilder() 44 .Append(42) 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(); 58 59 ACMatchClassifications styles_expected; 60 styles_expected.push_back(ACMatchClassification(0, kNone)); 61 styles_expected.push_back(ACMatchClassification(1, kMatch)); 62 styles_expected.push_back(ACMatchClassification(5, kNone)); 63 styles_expected.push_back(ACMatchClassification(6, kDim)); 64 styles_expected.push_back(ACMatchClassification(9, kNone)); 65 66 scoped_ptr<SendSuggestions::Params> params( 67 SendSuggestions::Params::Create(*list)); 68 EXPECT_TRUE(params); 69 EXPECT_TRUE(params->suggest_results[0].get()); 70 CompareClassification(styles_expected, StyleTypesToACMatchClassifications( 71 *params->suggest_results[0])); 72 73 // Same input, but swap the order. Ensure it still works. 74 scoped_ptr<ListValue> swap_list = ListBuilder() 75 .Append(42) 76 .Append(ListBuilder() 77 .Append(DictionaryBuilder() 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])); 96} 97 98// 0123456789 99// uuuuu 100// + dd 101// + mm 102// + mmmm 103// + dd 104// = 3773unnnn66 105TEST(ExtensionOmniboxTest, DescriptionStylesCombine) { 106 scoped_ptr<ListValue> list = ListBuilder() 107 .Append(42) 108 .Append(ListBuilder() 109 .Append(DictionaryBuilder() 110 .Set("content", "content") 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(); 133 134 ACMatchClassifications styles_expected; 135 styles_expected.push_back(ACMatchClassification(0, kUrl | kMatch)); 136 styles_expected.push_back(ACMatchClassification(1, kUrl | kMatch | kDim)); 137 styles_expected.push_back(ACMatchClassification(3, kUrl | kMatch)); 138 styles_expected.push_back(ACMatchClassification(4, kUrl)); 139 styles_expected.push_back(ACMatchClassification(5, kNone)); 140 styles_expected.push_back(ACMatchClassification(9, kMatch | kDim)); 141 142 scoped_ptr<SendSuggestions::Params> params( 143 SendSuggestions::Params::Create(*list)); 144 EXPECT_TRUE(params); 145 EXPECT_TRUE(params->suggest_results[0].get()); 146 CompareClassification(styles_expected, StyleTypesToACMatchClassifications( 147 *params->suggest_results[0])); 148 149 // Try moving the "dim/match" style pair at offset 9. Output should be the 150 // same. 151 scoped_ptr<ListValue> moved_list = ListBuilder() 152 .Append(42) 153 .Append(ListBuilder() 154 .Append(DictionaryBuilder() 155 .Set("content", "content") 156 .Set("description", "description") 157 .Set("descriptionStyles", ListBuilder() 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])); 185} 186 187// 0123456789 188// uuuuu 189// + mmmmm 190// + mmm 191// + ddd 192// + ddd 193// = 77777nnnnn 194TEST(ExtensionOmniboxTest, DescriptionStylesCombine2) { 195 scoped_ptr<ListValue> list = ListBuilder() 196 .Append(42) 197 .Append(ListBuilder() 198 .Append(DictionaryBuilder() 199 .Set("content", "content") 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(); 222 223 ACMatchClassifications styles_expected; 224 styles_expected.push_back(ACMatchClassification(0, kUrl | kMatch | kDim)); 225 styles_expected.push_back(ACMatchClassification(5, kNone)); 226 227 scoped_ptr<SendSuggestions::Params> params( 228 SendSuggestions::Params::Create(*list)); 229 EXPECT_TRUE(params); 230 EXPECT_TRUE(params->suggest_results[0].get()); 231 CompareClassification(styles_expected, StyleTypesToACMatchClassifications( 232 *params->suggest_results[0])); 233} 234 235} // namespace extensions 236