Home | History | Annotate | Download | only in extensions
      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
      3 // found in the LICENSE file.
      4 
      5 #include "base/values.h"
      6 #include "chrome/browser/extensions/extension_omnibox_api.h"
      7 #include "testing/gtest/include/gtest/gtest.h"
      8 #include "testing/platform_test.h"
      9 
     10 namespace {
     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 
     17 void AppendStyle(const std::string& type,
     18                  int offset, int length,
     19                  ListValue* styles) {
     20   DictionaryValue* style = new DictionaryValue;
     21   style->SetString("type", type);
     22   style->SetInteger("offset", offset);
     23   style->SetInteger("length", length);
     24   styles->Append(style);
     25 }
     26 
     27 void CompareClassification(const ACMatchClassifications& expected,
     28                            const ACMatchClassifications& actual) {
     29   EXPECT_EQ(expected.size(), actual.size());
     30   for (size_t i = 0; i < expected.size() && i < actual.size(); ++i) {
     31     EXPECT_EQ(expected[i].offset, actual[i].offset) << "Index:" << i;
     32     EXPECT_EQ(expected[i].style, actual[i].style) << "Index:" << i;
     33   }
     34 }
     35 
     36 }  // namespace
     37 
     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.
     40 
     41 //   0123456789
     42 //    mmmm
     43 // +       ddd
     44 // = nmmmmndddn
     45 TEST(ExtensionOmniboxTest, DescriptionStylesSimple) {
     46   ListValue styles_value;
     47   AppendStyle("match", 1, 4, &styles_value);
     48   AppendStyle("dim", 6, 3, &styles_value);
     49 
     50   ACMatchClassifications styles_expected;
     51   styles_expected.push_back(ACMatchClassification(0, kNone));
     52   styles_expected.push_back(ACMatchClassification(1, kMatch));
     53   styles_expected.push_back(ACMatchClassification(5, kNone));
     54   styles_expected.push_back(ACMatchClassification(6, kDim));
     55   styles_expected.push_back(ACMatchClassification(9, kNone));
     56 
     57   ExtensionOmniboxSuggestion suggestions;
     58   suggestions.description.resize(10);
     59   EXPECT_TRUE(suggestions.ReadStylesFromValue(styles_value));
     60   CompareClassification(styles_expected, suggestions.description_styles);
     61 
     62   // Same input, but swap the order. Ensure it still works.
     63   styles_value.Clear();
     64   AppendStyle("dim", 6, 3, &styles_value);
     65   AppendStyle("match", 1, 4, &styles_value);
     66   EXPECT_TRUE(suggestions.ReadStylesFromValue(styles_value));
     67   CompareClassification(styles_expected, suggestions.description_styles);
     68 }
     69 
     70 //   0123456789
     71 //   uuuuu
     72 // +          dd
     73 // +          mm
     74 // + mmmm
     75 // +  dd
     76 // = 3773unnnn66
     77 TEST(ExtensionOmniboxTest, DescriptionStylesCombine) {
     78   ListValue styles_value;
     79   AppendStyle("url", 0, 5, &styles_value);
     80   AppendStyle("dim", 9, 2, &styles_value);
     81   AppendStyle("match", 9, 2, &styles_value);
     82   AppendStyle("match", 0, 4, &styles_value);
     83   AppendStyle("dim", 1, 2, &styles_value);
     84 
     85   ACMatchClassifications styles_expected;
     86   styles_expected.push_back(ACMatchClassification(0, kUrl | kMatch));
     87   styles_expected.push_back(ACMatchClassification(1, kUrl | kMatch | kDim));
     88   styles_expected.push_back(ACMatchClassification(3, kUrl | kMatch));
     89   styles_expected.push_back(ACMatchClassification(4, kUrl));
     90   styles_expected.push_back(ACMatchClassification(5, kNone));
     91   styles_expected.push_back(ACMatchClassification(9, kMatch | kDim));
     92 
     93   ExtensionOmniboxSuggestion suggestions;
     94   suggestions.description.resize(10);
     95   EXPECT_TRUE(suggestions.ReadStylesFromValue(styles_value));
     96   CompareClassification(styles_expected, suggestions.description_styles);
     97 
     98   // Try moving the "dim/match" style pair at offset 9. Output should be the
     99   // same.
    100   styles_value.Clear();
    101   AppendStyle("url", 0, 5, &styles_value);
    102   AppendStyle("match", 0, 4, &styles_value);
    103   AppendStyle("dim", 9, 2, &styles_value);
    104   AppendStyle("match", 9, 2, &styles_value);
    105   AppendStyle("dim", 1, 2, &styles_value);
    106   EXPECT_TRUE(suggestions.ReadStylesFromValue(styles_value));
    107   CompareClassification(styles_expected, suggestions.description_styles);
    108 }
    109 
    110 //   0123456789
    111 //   uuuuu
    112 // + mmmmm
    113 // + mmm
    114 // +   ddd
    115 // + ddd
    116 // = 77777nnnnn
    117 TEST(ExtensionOmniboxTest, DescriptionStylesCombine2) {
    118   ListValue styles_value;
    119   AppendStyle("url", 0, 5, &styles_value);
    120   AppendStyle("match", 0, 5, &styles_value);
    121   AppendStyle("match", 0, 3, &styles_value);
    122   AppendStyle("dim", 2, 3, &styles_value);
    123   AppendStyle("dim", 0, 3, &styles_value);
    124 
    125   ACMatchClassifications styles_expected;
    126   styles_expected.push_back(ACMatchClassification(0, kUrl | kMatch | kDim));
    127   styles_expected.push_back(ACMatchClassification(5, kNone));
    128 
    129   ExtensionOmniboxSuggestion suggestions;
    130   suggestions.description.resize(10);
    131   EXPECT_TRUE(suggestions.ReadStylesFromValue(styles_value));
    132   CompareClassification(styles_expected, suggestions.description_styles);
    133 }
    134