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