| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/extension_omnibox_api.h" | 6 #include "chrome/browser/extensions/extension_omnibox_api.h" |
| 7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
| 8 #include "testing/platform_test.h" | 8 #include "testing/platform_test.h" |
| 9 | 9 |
| 10 namespace { | 10 namespace { |
| 11 | 11 |
| 12 const int kNone = ACMatchClassification::NONE; |
| 13 const int kUrl = ACMatchClassification::URL; |
| 14 const int kMatch = ACMatchClassification::MATCH; |
| 15 const int kDim = ACMatchClassification::DIM; |
| 16 |
| 12 void AppendStyle(const std::string& type, | 17 void AppendStyle(const std::string& type, |
| 13 int offset, int length, | 18 int offset, int length, |
| 14 ListValue* styles) { | 19 ListValue* styles) { |
| 15 DictionaryValue* style = new DictionaryValue; | 20 DictionaryValue* style = new DictionaryValue; |
| 16 style->SetString("type", type); | 21 style->SetString("type", type); |
| 17 style->SetInteger("offset", offset); | 22 style->SetInteger("offset", offset); |
| 18 style->SetInteger("length", length); | 23 style->SetInteger("length", length); |
| 19 styles->Append(style); | 24 styles->Append(style); |
| 20 } | 25 } |
| 21 | 26 |
| 22 void CompareClassification(const ACMatchClassifications& expected, | 27 void CompareClassification(const ACMatchClassifications& expected, |
| 23 const ACMatchClassifications& actual) { | 28 const ACMatchClassifications& actual) { |
| 24 EXPECT_EQ(expected.size(), actual.size()); | 29 EXPECT_EQ(expected.size(), actual.size()); |
| 25 for (size_t i = 0; i < expected.size() && i < actual.size(); ++i) { | 30 for (size_t i = 0; i < expected.size() && i < actual.size(); ++i) { |
| 26 EXPECT_EQ(expected[i].offset, actual[i].offset) << "Index:" << i; | 31 EXPECT_EQ(expected[i].offset, actual[i].offset) << "Index:" << i; |
| 27 EXPECT_EQ(expected[i].style, actual[i].style) << "Index:" << i; | 32 EXPECT_EQ(expected[i].style, actual[i].style) << "Index:" << i; |
| 28 } | 33 } |
| 29 } | 34 } |
| 30 | 35 |
| 31 } // namespace | 36 } // namespace |
| 32 | 37 |
| 33 // Test output key: n = character with no styling, d = dim, m = match, u = url | 38 // Test output key: n = character with no styling, d = dim, m = match, u = url |
| 39 // u = 1, m = 2, d = 4. u+d = 5, etc. |
| 34 | 40 |
| 35 // 0123456789 | 41 // 0123456789 |
| 36 // mmmm | 42 // mmmm |
| 37 // + ddd | 43 // + ddd |
| 38 // = nmmmmndddn | 44 // = nmmmmndddn |
| 39 TEST(ExtensionOmniboxTest, DescriptionStylesSimple) { | 45 TEST(ExtensionOmniboxTest, DescriptionStylesSimple) { |
| 40 ListValue styles_value; | 46 ListValue styles_value; |
| 41 AppendStyle("match", 1, 4, &styles_value); | 47 AppendStyle("match", 1, 4, &styles_value); |
| 42 AppendStyle("dim", 6, 3, &styles_value); | 48 AppendStyle("dim", 6, 3, &styles_value); |
| 43 | 49 |
| 44 ACMatchClassifications styles_expected; | 50 ACMatchClassifications styles_expected; |
| 45 styles_expected.push_back( | 51 styles_expected.push_back(ACMatchClassification(0, kNone)); |
| 46 ACMatchClassification(0, ACMatchClassification::NONE)); | 52 styles_expected.push_back(ACMatchClassification(1, kMatch)); |
| 47 styles_expected.push_back( | 53 styles_expected.push_back(ACMatchClassification(5, kNone)); |
| 48 ACMatchClassification(1, ACMatchClassification::MATCH)); | 54 styles_expected.push_back(ACMatchClassification(6, kDim)); |
| 49 styles_expected.push_back( | 55 styles_expected.push_back(ACMatchClassification(9, kNone)); |
| 50 ACMatchClassification(5, ACMatchClassification::NONE)); | |
| 51 styles_expected.push_back( | |
| 52 ACMatchClassification(6, ACMatchClassification::DIM)); | |
| 53 styles_expected.push_back( | |
| 54 ACMatchClassification(9, ACMatchClassification::NONE)); | |
| 55 | 56 |
| 56 ExtensionOmniboxSuggestion suggestions; | 57 ExtensionOmniboxSuggestion suggestions; |
| 57 suggestions.description.resize(10); | 58 suggestions.description.resize(10); |
| 58 EXPECT_TRUE(suggestions.ReadStylesFromValue(styles_value)); | 59 EXPECT_TRUE(suggestions.ReadStylesFromValue(styles_value)); |
| 59 CompareClassification(styles_expected, suggestions.description_styles); | 60 CompareClassification(styles_expected, suggestions.description_styles); |
| 60 | 61 |
| 61 // Same input, but swap the order. Ensure it still works. | 62 // Same input, but swap the order. Ensure it still works. |
| 62 styles_value.Clear(); | 63 styles_value.Clear(); |
| 63 AppendStyle("dim", 6, 3, &styles_value); | 64 AppendStyle("dim", 6, 3, &styles_value); |
| 64 AppendStyle("match", 1, 4, &styles_value); | 65 AppendStyle("match", 1, 4, &styles_value); |
| 65 EXPECT_TRUE(suggestions.ReadStylesFromValue(styles_value)); | 66 EXPECT_TRUE(suggestions.ReadStylesFromValue(styles_value)); |
| 66 CompareClassification(styles_expected, suggestions.description_styles); | 67 CompareClassification(styles_expected, suggestions.description_styles); |
| 67 } | 68 } |
| 68 | 69 |
| 69 // 0123456789 | 70 // 0123456789 |
| 70 // uuuuuu | 71 // uuuuu |
| 71 // + dd | 72 // + dd |
| 72 // + mm | 73 // + mm |
| 73 // + mmmm | 74 // + mmmm |
| 74 // + dd | 75 // + dd |
| 75 // = mddmunnnnm | 76 // = 3773unnnn66 |
| 76 TEST(ExtensionOmniboxTest, DescriptionStylesOverlap) { | 77 TEST(ExtensionOmniboxTest, DescriptionStylesCombine) { |
| 77 ListValue styles_value; | 78 ListValue styles_value; |
| 78 AppendStyle("url", 0, 5, &styles_value); | 79 AppendStyle("url", 0, 5, &styles_value); |
| 79 AppendStyle("dim", 9, 2, &styles_value); | 80 AppendStyle("dim", 9, 2, &styles_value); |
| 80 AppendStyle("match", 9, 2, &styles_value); | 81 AppendStyle("match", 9, 2, &styles_value); |
| 81 AppendStyle("match", 0, 4, &styles_value); | 82 AppendStyle("match", 0, 4, &styles_value); |
| 82 AppendStyle("dim", 1, 2, &styles_value); | 83 AppendStyle("dim", 1, 2, &styles_value); |
| 83 | 84 |
| 84 ACMatchClassifications styles_expected; | 85 ACMatchClassifications styles_expected; |
| 85 styles_expected.push_back( | 86 styles_expected.push_back(ACMatchClassification(0, kUrl | kMatch)); |
| 86 ACMatchClassification(0, ACMatchClassification::MATCH)); | 87 styles_expected.push_back(ACMatchClassification(1, kUrl | kMatch | kDim)); |
| 87 styles_expected.push_back( | 88 styles_expected.push_back(ACMatchClassification(3, kUrl | kMatch)); |
| 88 ACMatchClassification(1, ACMatchClassification::DIM)); | 89 styles_expected.push_back(ACMatchClassification(4, kUrl)); |
| 89 styles_expected.push_back( | 90 styles_expected.push_back(ACMatchClassification(5, kNone)); |
| 90 ACMatchClassification(3, ACMatchClassification::MATCH)); | 91 styles_expected.push_back(ACMatchClassification(9, kMatch | kDim)); |
| 91 styles_expected.push_back( | |
| 92 ACMatchClassification(4, ACMatchClassification::URL)); | |
| 93 styles_expected.push_back( | |
| 94 ACMatchClassification(5, ACMatchClassification::NONE)); | |
| 95 styles_expected.push_back( | |
| 96 ACMatchClassification(9, ACMatchClassification::MATCH)); | |
| 97 | 92 |
| 98 ExtensionOmniboxSuggestion suggestions; | 93 ExtensionOmniboxSuggestion suggestions; |
| 99 suggestions.description.resize(10); | 94 suggestions.description.resize(10); |
| 100 EXPECT_TRUE(suggestions.ReadStylesFromValue(styles_value)); | 95 EXPECT_TRUE(suggestions.ReadStylesFromValue(styles_value)); |
| 101 CompareClassification(styles_expected, suggestions.description_styles); | 96 CompareClassification(styles_expected, suggestions.description_styles); |
| 102 | 97 |
| 103 // Try moving the "dim/match" style pair at offset 9. Output should be the | 98 // Try moving the "dim/match" style pair at offset 9. Output should be the |
| 104 // same. | 99 // same. |
| 105 styles_value.Clear(); | 100 styles_value.Clear(); |
| 106 AppendStyle("url", 0, 5, &styles_value); | 101 AppendStyle("url", 0, 5, &styles_value); |
| 107 AppendStyle("match", 0, 4, &styles_value); | 102 AppendStyle("match", 0, 4, &styles_value); |
| 108 AppendStyle("dim", 9, 2, &styles_value); | 103 AppendStyle("dim", 9, 2, &styles_value); |
| 109 AppendStyle("match", 9, 2, &styles_value); | 104 AppendStyle("match", 9, 2, &styles_value); |
| 110 AppendStyle("dim", 1, 2, &styles_value); | 105 AppendStyle("dim", 1, 2, &styles_value); |
| 111 EXPECT_TRUE(suggestions.ReadStylesFromValue(styles_value)); | 106 EXPECT_TRUE(suggestions.ReadStylesFromValue(styles_value)); |
| 112 CompareClassification(styles_expected, suggestions.description_styles); | 107 CompareClassification(styles_expected, suggestions.description_styles); |
| 113 } | 108 } |
| 114 | 109 |
| 115 // 0123456789 | 110 // 0123456789 |
| 116 // uuuuu | 111 // uuuuu |
| 117 // + mmmmm | 112 // + mmmmm |
| 118 // + mmm | 113 // + mmm |
| 119 // + ddd | 114 // + ddd |
| 120 // + ddd | 115 // + ddd |
| 121 // = dddddnnnnn | 116 // = 77777nnnnn |
| 122 TEST(ExtensionOmniboxTest, DescriptionStylesOverlap2) { | 117 TEST(ExtensionOmniboxTest, DescriptionStylesCombine2) { |
| 123 ListValue styles_value; | 118 ListValue styles_value; |
| 124 AppendStyle("url", 0, 5, &styles_value); | 119 AppendStyle("url", 0, 5, &styles_value); |
| 125 AppendStyle("match", 0, 5, &styles_value); | 120 AppendStyle("match", 0, 5, &styles_value); |
| 126 AppendStyle("match", 0, 3, &styles_value); | 121 AppendStyle("match", 0, 3, &styles_value); |
| 127 AppendStyle("dim", 2, 3, &styles_value); | 122 AppendStyle("dim", 2, 3, &styles_value); |
| 128 AppendStyle("dim", 0, 3, &styles_value); | 123 AppendStyle("dim", 0, 3, &styles_value); |
| 129 | 124 |
| 130 // We don't merge adjacent identical styles, but the autocomplete system | |
| 131 // doesn't mind. | |
| 132 ACMatchClassifications styles_expected; | 125 ACMatchClassifications styles_expected; |
| 133 styles_expected.push_back( | 126 styles_expected.push_back(ACMatchClassification(0, kUrl | kMatch | kDim)); |
| 134 ACMatchClassification(0, ACMatchClassification::DIM)); | 127 styles_expected.push_back(ACMatchClassification(5, kNone)); |
| 135 styles_expected.push_back( | |
| 136 ACMatchClassification(3, ACMatchClassification::DIM)); | |
| 137 styles_expected.push_back( | |
| 138 ACMatchClassification(5, ACMatchClassification::NONE)); | |
| 139 | 128 |
| 140 ExtensionOmniboxSuggestion suggestions; | 129 ExtensionOmniboxSuggestion suggestions; |
| 141 suggestions.description.resize(10); | 130 suggestions.description.resize(10); |
| 142 EXPECT_TRUE(suggestions.ReadStylesFromValue(styles_value)); | 131 EXPECT_TRUE(suggestions.ReadStylesFromValue(styles_value)); |
| 143 CompareClassification(styles_expected, suggestions.description_styles); | 132 CompareClassification(styles_expected, suggestions.description_styles); |
| 144 } | 133 } |
| OLD | NEW |