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/values.h"
10 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h"
11 #include "components/omnibox/suggestion_answer.h"
12 #import "testing/gtest_mac.h"
16 class OmniboxPopupCellTest : public CocoaTest {
18 OmniboxPopupCellTest() {
21 void SetUp() override {
23 cell_.reset([[OmniboxPopupCell alloc] initTextCell:@""]);
24 button_.reset([[NSButton alloc] initWithFrame:NSMakeRect(0, 0, 200, 20)]);
25 [button_ setCell:cell_];
26 [[test_window() contentView] addSubview:button_];
30 base::scoped_nsobject<OmniboxPopupCell> cell_;
31 base::scoped_nsobject<NSButton> button_;
34 DISALLOW_COPY_AND_ASSIGN(OmniboxPopupCellTest);
37 TEST_VIEW(OmniboxPopupCellTest, button_);
39 TEST_F(OmniboxPopupCellTest, Image) {
40 [cell_ setImage:[NSImage imageNamed:NSImageNameInfo]];
44 TEST_F(OmniboxPopupCellTest, Title) {
45 base::scoped_nsobject<NSAttributedString> text([[NSAttributedString alloc]
46 initWithString:@"The quick brown fox jumps over the lazy dog."]);
47 [cell_ setAttributedTitle:text];
51 TEST_F(OmniboxPopupCellTest, AnswerStyle) {
52 const char* weatherJson =
53 "{\"l\": [ {\"il\": {\"t\": [ {"
54 "\"t\": \"weather in pari<b>s</b>\", \"tt\": 8} ]}}, {"
55 "\"il\": {\"at\": {\"t\": \"Thu\",\"tt\": 12}, "
56 "\"i\": {\"d\": \"//ssl.gstatic.com/onebox/weather/64/cloudy.png\","
57 "\"t\": 3}, \"t\": [ {\"t\": \"46\",\"tt\": 1}, {"
58 "\"t\": \"°F\",\"tt\": 3} ]}} ]}";
59 NSString* finalString = @"46°F Thu";
61 scoped_ptr<base::Value> root(base::JSONReader::Read(weatherJson));
62 ASSERT_NE(root, nullptr);
63 base::DictionaryValue* dictionary;
64 root->GetAsDictionary(&dictionary);
65 ASSERT_NE(dictionary, nullptr);
66 AutocompleteMatch match;
67 match.answer = SuggestionAnswer::ParseAnswer(dictionary);
68 EXPECT_TRUE(match.answer);
69 [cell_ setMatch:match];
70 EXPECT_NSEQ([[cell_ description] string], finalString);
71 size_t length = [[cell_ description] length];
72 const NSRange checkValues[] = {{0, 2}, {2, 2}, {4, 4}};
73 EXPECT_EQ(length, 8UL);
74 NSDictionary* lastAttributes = nil;
75 for (const NSRange& value : checkValues) {
77 NSDictionary* currentAttributes =
78 [[cell_ description] attributesAtIndex:value.location
79 effectiveRange:&range];
80 EXPECT_TRUE(NSEqualRanges(value, range));
81 EXPECT_FALSE([currentAttributes isEqualToDictionary:lastAttributes]);
82 lastAttributes = currentAttributes;