Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame^] | 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. |
| 4 | |
| 5 | #import "chrome/browser/ui/cocoa/autofill/autofill_section_container.h" |
| 6 | |
| 7 | #include "base/mac/foundation_util.h" |
| 8 | #include "base/memory/scoped_nsobject.h" |
| 9 | #include "base/utf_string_conversions.h" |
| 10 | #include "chrome/browser/ui/autofill/autofill_dialog_models.h" |
| 11 | #include "chrome/browser/ui/autofill/mock_autofill_dialog_controller.h" |
| 12 | #import "chrome/browser/ui/cocoa/autofill/layout_view.h" |
| 13 | #import "chrome/browser/ui/cocoa/menu_button.h" |
| 14 | #include "grit/generated_resources.h" |
| 15 | #include "testing/gtest_mac.h" |
| 16 | #include "testing/gtest/include/gtest/gtest.h" |
| 17 | #include "testing/platform_test.h" |
| 18 | #include "ui/base/models/combobox_model.h" |
| 19 | #include "ui/base/models/simple_menu_model.h" |
| 20 | #import "ui/base/test/ui_cocoa_test_helper.h" |
| 21 | |
| 22 | namespace { |
| 23 | |
| 24 | class AutofillSectionContainerTest : public ui::CocoaTest { |
| 25 | public: |
| 26 | virtual void SetUp() { |
| 27 | CocoaTest::SetUp(); |
| 28 | container_.reset( |
| 29 | [[AutofillSectionContainer alloc] |
| 30 | initWithController:&controller_ |
| 31 | forSection:autofill::SECTION_CC]); |
| 32 | [[test_window() contentView] addSubview:[container_ view]]; |
| 33 | } |
| 34 | |
| 35 | void ResetContainer() { |
| 36 | container_.reset( |
| 37 | [[AutofillSectionContainer alloc] |
| 38 | initWithController:&controller_ |
| 39 | forSection:autofill::SECTION_CC]); |
| 40 | [[test_window() contentView] setSubviews:@[ [container_ view] ]]; |
| 41 | } |
| 42 | |
| 43 | protected: |
| 44 | scoped_nsobject<AutofillSectionContainer> container_; |
| 45 | testing::NiceMock<autofill::MockAutofillDialogController> controller_; |
| 46 | }; |
| 47 | |
| 48 | } // namespace |
| 49 | |
| 50 | TEST_VIEW(AutofillSectionContainerTest, [container_ view]) |
| 51 | |
| 52 | TEST_F(AutofillSectionContainerTest, HasSubviews) { |
| 53 | bool hasLayoutView = false; |
| 54 | bool hasTextField = false; |
| 55 | bool hasSuggestButton = false; |
| 56 | |
| 57 | ASSERT_EQ(3U, [[[container_ view] subviews] count]); |
| 58 | for (NSView* view in [[container_ view] subviews]) { |
| 59 | if ([view isKindOfClass:[NSTextField class]]) { |
| 60 | hasTextField = true; |
| 61 | } else if ([view isKindOfClass:[LayoutView class]]) { |
| 62 | hasLayoutView = true; |
| 63 | } else if ([view isKindOfClass:[MenuButton class]]) { |
| 64 | hasSuggestButton = true; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | EXPECT_TRUE(hasSuggestButton); |
| 69 | EXPECT_TRUE(hasLayoutView); |
| 70 | EXPECT_TRUE(hasTextField); |
| 71 | } |
| 72 | |
| 73 | TEST_F(AutofillSectionContainerTest, ModelsPopulateComboboxes) { |
| 74 | using namespace autofill; |
| 75 | using namespace testing; |
| 76 | |
| 77 | const DetailInput kTestInputs[] = { |
| 78 | { 2, CREDIT_CARD_EXP_MONTH } |
| 79 | }; |
| 80 | |
| 81 | DetailInputs inputs; |
| 82 | inputs.push_back(kTestInputs[0]); |
| 83 | |
| 84 | autofill::MonthComboboxModel comboModel; |
| 85 | EXPECT_CALL(controller_, RequestedFieldsForSection(autofill::SECTION_CC)) |
| 86 | .WillOnce(ReturnRef(inputs)); |
| 87 | EXPECT_CALL(controller_, ComboboxModelForAutofillType(CREDIT_CARD_EXP_MONTH)) |
| 88 | .WillRepeatedly(Return(&comboModel)); |
| 89 | ResetContainer(); |
| 90 | |
| 91 | NSPopUpButton* popup = base::mac::ObjCCastStrict<NSPopUpButton>( |
| 92 | [container_ getField:CREDIT_CARD_EXP_MONTH]); |
| 93 | EXPECT_EQ(13, [popup numberOfItems]); |
| 94 | EXPECT_NSEQ(@"", [popup itemTitleAtIndex:0]); |
| 95 | EXPECT_NSEQ(@"01", [popup itemTitleAtIndex:1]); |
| 96 | EXPECT_NSEQ(@"02", [popup itemTitleAtIndex:2]); |
| 97 | EXPECT_NSEQ(@"03", [popup itemTitleAtIndex:3]); |
| 98 | EXPECT_NSEQ(@"12", [popup itemTitleAtIndex:12]); |
| 99 | }; |
| 100 | |
| 101 | TEST_F(AutofillSectionContainerTest, OutputMatchesDefinition) { |
| 102 | using namespace autofill; |
| 103 | using namespace testing; |
| 104 | |
| 105 | const DetailInput kTestInputs[] = { |
| 106 | { 1, EMAIL_ADDRESS, IDS_AUTOFILL_DIALOG_PLACEHOLDER_EMAIL }, |
| 107 | { 2, CREDIT_CARD_EXP_MONTH } |
| 108 | }; |
| 109 | autofill::MonthComboboxModel comboModel; |
| 110 | DetailInputs inputs; |
| 111 | inputs.push_back(kTestInputs[0]); |
| 112 | inputs.push_back(kTestInputs[1]); |
| 113 | |
| 114 | EXPECT_CALL(controller_, RequestedFieldsForSection(autofill::SECTION_CC)) |
| 115 | .WillOnce(ReturnRef(inputs)); |
| 116 | EXPECT_CALL(controller_, ComboboxModelForAutofillType(EMAIL_ADDRESS)) |
| 117 | .WillRepeatedly(ReturnNull()); |
| 118 | EXPECT_CALL(controller_, ComboboxModelForAutofillType(CREDIT_CARD_EXP_MONTH)) |
| 119 | .WillRepeatedly(Return(&comboModel)); |
| 120 | |
| 121 | ResetContainer(); |
| 122 | |
| 123 | NSPopUpButton* popup = base::mac::ObjCCastStrict<NSPopUpButton>( |
| 124 | [container_ getField:CREDIT_CARD_EXP_MONTH]); |
| 125 | [popup selectItemWithTitle:@"02"]; |
| 126 | [[container_ getField:EMAIL_ADDRESS] setStringValue:@"magic@example.org"]; |
| 127 | |
| 128 | autofill::DetailOutputMap output; |
| 129 | [container_ getInputs:&output]; |
| 130 | |
| 131 | ASSERT_EQ(inputs.size(), output.size()); |
| 132 | EXPECT_EQ(ASCIIToUTF16("magic@example.org"), output[&inputs[0]]); |
| 133 | EXPECT_EQ(ASCIIToUTF16("02"), output[&inputs[1]]); |
| 134 | } |
| 135 | |
| 136 | TEST_F(AutofillSectionContainerTest, SuggestionsPopulatedByController) { |
| 137 | ui::SimpleMenuModel model(NULL); |
| 138 | model.AddItem(10, ASCIIToUTF16("a")); |
| 139 | model.AddItem(11, ASCIIToUTF16("b")); |
| 140 | |
| 141 | using namespace autofill; |
| 142 | using namespace testing; |
| 143 | |
| 144 | EXPECT_CALL(controller_, MenuModelForSection(autofill::SECTION_CC)) |
| 145 | .WillOnce(Return(&model)); |
| 146 | |
| 147 | ResetContainer(); |
| 148 | MenuButton* button = nil; |
| 149 | for (id item in [[container_ view] subviews]) { |
| 150 | if ([item isKindOfClass:[MenuButton class]]) { |
| 151 | button = item; |
| 152 | break; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | NSMenu* menu = [button attachedMenu]; |
| 157 | // Expect _three_ items - popup menus need an empty first item. |
| 158 | ASSERT_EQ(3, [menu numberOfItems]); |
| 159 | |
| 160 | EXPECT_NSEQ(@"a", [[menu itemAtIndex:1] title]); |
| 161 | EXPECT_NSEQ(@"b", [[menu itemAtIndex:2] title]); |
| 162 | } |