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