| 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 image:[NSImage imageNamed:NSImageNameInfo] |
| 45 answerImage:nil]); |
| 46 [cell_ setObjectValue:cellData_]; |
| 47 [control_ display]; |
| 42 } | 48 } |
| 43 | 49 |
| 44 TEST_F(OmniboxPopupCellTest, Title) { | 50 TEST_F(OmniboxPopupCellTest, Title) { |
| 45 base::scoped_nsobject<NSAttributedString> text([[NSAttributedString alloc] | 51 AutocompleteMatch match; |
| 46 initWithString:@"The quick brown fox jumps over the lazy dog."]); | 52 match.contents = |
| 47 [cell_ setAttributedTitle:text]; | 53 base::ASCIIToUTF16("The quick brown fox jumps over the lazy dog."); |
| 48 [button_ display]; | 54 cellData_.reset([[OmniboxPopupCellData alloc] initWithMatch:match |
| 55 image:nil |
| 56 answerImage:nil]); |
| 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 |