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"
17 class OmniboxPopupCellTest : public CocoaTest {
19 OmniboxPopupCellTest() {
22 void SetUp() override {
24 control_.reset([[NSControl alloc] initWithFrame:NSMakeRect(0, 0, 200, 20)]);
25 [control_ setCell:cell_];
26 [[test_window() contentView] addSubview:control_];
30 base::scoped_nsobject<OmniboxPopupCellData> cellData_;
31 base::scoped_nsobject<OmniboxPopupCell> cell_;
32 base::scoped_nsobject<NSControl> control_;
35 DISALLOW_COPY_AND_ASSIGN(OmniboxPopupCellTest);
38 TEST_VIEW(OmniboxPopupCellTest, control_);
40 TEST_F(OmniboxPopupCellTest, Image) {
41 AutocompleteMatch match;
42 cellData_.reset([[OmniboxPopupCellData alloc]
45 image:[NSImage imageNamed:NSImageNameInfo]
47 [cell_ setObjectValue:cellData_];
51 TEST_F(OmniboxPopupCellTest, Title) {
52 AutocompleteMatch match;
54 base::ASCIIToUTF16("The quick brown fox jumps over the lazy dog.");
55 cellData_.reset([[OmniboxPopupCellData alloc] initWithMatch:match
59 [cell_ setObjectValue:cellData_];
63 TEST_F(OmniboxPopupCellTest, AnswerStyle) {
64 const char* weatherJson =
65 "{\"l\": [ {\"il\": {\"t\": [ {"
66 "\"t\": \"weather in pari<b>s</b>\", \"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
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) {
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;