Add ICU message format support
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / omnibox / omnibox_popup_cell_unittest.mm
blobed1967c0f1c6939a87f04a7b058d733fcc39c90f
1 // Copyright (c) 2013 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.
5 #import "chrome/browser/ui/cocoa/omnibox/omnibox_popup_cell.h"
7 #include "base/json/json_reader.h"
8 #include "base/mac/scoped_nsobject.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "base/values.h"
11 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h"
12 #include "components/omnibox/browser/suggestion_answer.h"
13 #import "testing/gtest_mac.h"
15 namespace {
17 class OmniboxPopupCellTest : public CocoaTest {
18  public:
19   OmniboxPopupCellTest() {
20   }
22   void SetUp() override {
23     CocoaTest::SetUp();
24     control_.reset([[NSControl alloc] initWithFrame:NSMakeRect(0, 0, 200, 20)]);
25     [control_ setCell:cell_];
26     [[test_window() contentView] addSubview:control_];
27   };
29  protected:
30   base::scoped_nsobject<OmniboxPopupCellData> cellData_;
31   base::scoped_nsobject<OmniboxPopupCell> cell_;
32   base::scoped_nsobject<NSControl> control_;
34  private:
35   DISALLOW_COPY_AND_ASSIGN(OmniboxPopupCellTest);
38 TEST_VIEW(OmniboxPopupCellTest, control_);
40 TEST_F(OmniboxPopupCellTest, Image) {
41   AutocompleteMatch match;
42   cellData_.reset([[OmniboxPopupCellData alloc]
43        initWithMatch:match
44       contentsOffset:0
45                image:[NSImage imageNamed:NSImageNameInfo]
46          answerImage:nil]);
47   [cell_ setObjectValue:cellData_];
48   [control_ display];
51 TEST_F(OmniboxPopupCellTest, Title) {
52   AutocompleteMatch match;
53   match.contents =
54       base::ASCIIToUTF16("The quick brown fox jumps over the lazy dog.");
55   cellData_.reset([[OmniboxPopupCellData alloc] initWithMatch:match
56                                                contentsOffset:0
57                                                         image:nil
58                                                   answerImage:nil]);
59   [cell_ setObjectValue:cellData_];
60   [control_ display];
63 TEST_F(OmniboxPopupCellTest, AnswerStyle) {
64   const char* weatherJson =
65       "{\"l\": [ {\"il\": {\"t\": [ {"
66       "\"t\": \"weather in pari&lt;b&gt;s&lt;/b&gt;\", \"tt\": 8} ]}}, {"
67       "\"il\": {\"at\": {\"t\": \"Thu\",\"tt\": 12}, "
68       "\"i\": {\"d\": \"//ssl.gstatic.com/onebox/weather/64/cloudy.png\","
69       "\"t\": 3}, \"t\": [ {\"t\": \"46\",\"tt\": 1}, {"
70       "\"t\": \"°F\",\"tt\": 3} ]}} ]}";
71   NSString* finalString = @"46°F Thu";
73   scoped_ptr<base::Value> root(base::JSONReader::Read(weatherJson));
74   ASSERT_NE(root, nullptr);
75   base::DictionaryValue* dictionary;
76   root->GetAsDictionary(&dictionary);
77   ASSERT_NE(dictionary, nullptr);
78   AutocompleteMatch match;
79   match.answer = SuggestionAnswer::ParseAnswer(dictionary);
80   EXPECT_TRUE(match.answer);
81   cellData_.reset([[OmniboxPopupCellData alloc] initWithMatch:match
82                                                contentsOffset:0
83                                                         image:nil
84                                                   answerImage:nil]);
85   EXPECT_NSEQ([[cellData_ description] string], finalString);
86   size_t length = [[[cellData_ description] string] length];
87   const NSRange checkValues[] = {{0, 2}, {2, 2}, {4, 4}};
88   EXPECT_EQ(length, 8UL);
89   NSDictionary* lastAttributes = nil;
90   for (const NSRange& value : checkValues) {
91     NSRange range;
92     NSDictionary* currentAttributes =
93         [[cellData_ description] attributesAtIndex:value.location
94                                     effectiveRange:&range];
95     EXPECT_TRUE(NSEqualRanges(value, range));
96     EXPECT_FALSE([currentAttributes isEqualToDictionary:lastAttributes]);
97     lastAttributes = currentAttributes;
98   }
101 }  // namespace