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/autofill/autofill_section_container.h"
7 #include "base/mac/foundation_util.h"
8 #include "base/mac/scoped_nsobject.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/ui/autofill/autofill_dialog_models.h"
11 #include "chrome/browser/ui/autofill/mock_autofill_dialog_view_delegate.h"
12 #import "chrome/browser/ui/cocoa/autofill/autofill_input_field.h"
13 #import "chrome/browser/ui/cocoa/autofill/layout_view.h"
14 #import "chrome/browser/ui/cocoa/menu_button.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "testing/gtest_mac.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/gfx/test/ui_cocoa_test_helper.h"
22 using base::ASCIIToUTF16;
26 class AutofillSectionContainerTest : public ui::CocoaTest {
28 AutofillSectionContainerTest() : section_(autofill::SECTION_BILLING) {}
29 virtual void SetUp() {
34 void ResetContainer() {
36 [[AutofillSectionContainer alloc]
37 initWithDelegate:&delegate_
38 forSection:section_]);
39 [[test_window() contentView] setSubviews:@[ [container_ view] ]];
43 base::scoped_nsobject<AutofillSectionContainer> container_;
44 testing::NiceMock<autofill::MockAutofillDialogViewDelegate> delegate_;
45 autofill::DialogSection section_;
50 TEST_VIEW(AutofillSectionContainerTest, [container_ view])
52 TEST_F(AutofillSectionContainerTest, HasSubviews) {
53 bool hasLayoutView = false;
54 bool hasTextField = false;
55 bool hasSuggestButton = false;
56 bool hasSuggestionView = false;
58 ASSERT_EQ(4U, [[[container_ view] subviews] count]);
59 for (NSView* view in [[container_ view] subviews]) {
60 if ([view isKindOfClass:[NSTextField class]]) {
62 } else if ([view isKindOfClass:[LayoutView class]]) {
64 } else if ([view isKindOfClass:[MenuButton class]]) {
65 hasSuggestButton = true;
66 } else if ([view isKindOfClass:[NSView class]]) {
67 hasSuggestionView = true;
71 EXPECT_TRUE(hasSuggestButton);
72 EXPECT_TRUE(hasLayoutView);
73 EXPECT_TRUE(hasTextField);
74 EXPECT_TRUE(hasSuggestionView);
77 TEST_F(AutofillSectionContainerTest, ModelsPopulateComboboxes) {
78 using namespace autofill;
79 using namespace testing;
81 const DetailInput kTestInputs[] = {
82 { DetailInput::SHORT, CREDIT_CARD_EXP_MONTH },
83 { DetailInput::SHORT, CREDIT_CARD_EXP_4_DIGIT_YEAR },
87 inputs.push_back(kTestInputs[0]);
88 inputs.push_back(kTestInputs[1]);
90 autofill::MonthComboboxModel monthComboModel;
91 autofill::YearComboboxModel yearComboModel;
92 EXPECT_CALL(delegate_, RequestedFieldsForSection(section_))
93 .WillOnce(ReturnRef(inputs));
94 EXPECT_CALL(delegate_, ComboboxModelForAutofillType(CREDIT_CARD_EXP_MONTH))
95 .WillRepeatedly(Return(&monthComboModel));
97 delegate_, ComboboxModelForAutofillType(CREDIT_CARD_EXP_4_DIGIT_YEAR))
98 .WillRepeatedly(Return(&yearComboModel));
101 NSPopUpButton* popup = base::mac::ObjCCastStrict<NSPopUpButton>(
102 [container_ getField:CREDIT_CARD_EXP_MONTH]);
103 EXPECT_EQ(13, [popup numberOfItems]);
104 EXPECT_NSEQ(@"Month", [popup itemTitleAtIndex:0]);
105 EXPECT_NSEQ(@"01", [popup itemTitleAtIndex:1]);
106 EXPECT_NSEQ(@"02", [popup itemTitleAtIndex:2]);
107 EXPECT_NSEQ(@"03", [popup itemTitleAtIndex:3]);
108 EXPECT_NSEQ(@"12", [popup itemTitleAtIndex:12]);
110 NSPopUpButton* yearPopup = base::mac::ObjCCastStrict<NSPopUpButton>(
111 [container_ getField:CREDIT_CARD_EXP_4_DIGIT_YEAR]);
112 EXPECT_EQ(11, [yearPopup numberOfItems]);
113 EXPECT_NSEQ(@"Year", [yearPopup itemTitleAtIndex:0]);
116 TEST_F(AutofillSectionContainerTest, OutputMatchesDefinition) {
117 using namespace autofill;
118 using namespace testing;
120 const DetailInput kTestInputs[] = {
121 { DetailInput::LONG, EMAIL_ADDRESS },
122 { DetailInput::SHORT, CREDIT_CARD_EXP_MONTH },
124 autofill::MonthComboboxModel comboModel;
126 inputs.push_back(kTestInputs[0]);
127 inputs.push_back(kTestInputs[1]);
129 EXPECT_CALL(delegate_, RequestedFieldsForSection(section_))
130 .WillOnce(ReturnRef(inputs));
131 EXPECT_CALL(delegate_, ComboboxModelForAutofillType(EMAIL_ADDRESS))
132 .WillRepeatedly(ReturnNull());
133 EXPECT_CALL(delegate_, ComboboxModelForAutofillType(CREDIT_CARD_EXP_MONTH))
134 .WillRepeatedly(Return(&comboModel));
138 NSPopUpButton* popup = base::mac::ObjCCastStrict<NSPopUpButton>(
139 [container_ getField:CREDIT_CARD_EXP_MONTH]);
140 [popup selectItemWithTitle:@"02"];
141 [[container_ getField:EMAIL_ADDRESS] setStringValue:@"magic@example.org"];
143 autofill::FieldValueMap output;
144 [container_ getInputs:&output];
146 ASSERT_EQ(inputs.size(), output.size());
147 EXPECT_EQ(ASCIIToUTF16("magic@example.org"), output[inputs[0].type]);
148 EXPECT_EQ(ASCIIToUTF16("02"), output[inputs[1].type]);
151 TEST_F(AutofillSectionContainerTest, SuggestionsPopulatedByController) {
152 ui::SimpleMenuModel model(NULL);
153 model.AddItem(10, ASCIIToUTF16("a"));
154 model.AddItem(11, ASCIIToUTF16("b"));
156 using namespace autofill;
157 using namespace testing;
159 EXPECT_CALL(delegate_, MenuModelForSection(section_))
160 .WillOnce(Return(&model));
163 MenuButton* button = nil;
164 for (id item in [[container_ view] subviews]) {
165 if ([item isKindOfClass:[MenuButton class]]) {
171 NSMenu* menu = [button attachedMenu];
172 // Expect _three_ items - popup menus need an empty first item.
173 ASSERT_EQ(3, [menu numberOfItems]);
175 EXPECT_NSEQ(@"a", [[menu itemAtIndex:1] title]);
176 EXPECT_NSEQ(@"b", [[menu itemAtIndex:2] title]);
179 TEST_F(AutofillSectionContainerTest, FieldsAreInitiallyValid) {
180 using namespace autofill;
181 using namespace testing;
183 const DetailInput kTestInputs[] = {
184 { DetailInput::LONG, EMAIL_ADDRESS },
185 { DetailInput::SHORT, CREDIT_CARD_EXP_MONTH },
188 MonthComboboxModel comboModel;
190 inputs.push_back(kTestInputs[0]);
191 inputs.push_back(kTestInputs[1]);
193 EXPECT_CALL(delegate_, RequestedFieldsForSection(section_))
194 .WillOnce(ReturnRef(inputs));
195 EXPECT_CALL(delegate_, ComboboxModelForAutofillType(EMAIL_ADDRESS))
196 .WillRepeatedly(ReturnNull());
197 EXPECT_CALL(delegate_, ComboboxModelForAutofillType(CREDIT_CARD_EXP_MONTH))
198 .WillRepeatedly(Return(&comboModel));
201 NSControl<AutofillInputField>* field = [container_ getField:EMAIL_ADDRESS];
202 EXPECT_FALSE([field invalid]);
203 field = [container_ getField:CREDIT_CARD_EXP_MONTH];
204 EXPECT_FALSE([field invalid]);
207 TEST_F(AutofillSectionContainerTest, ControllerInformsValidity) {
208 using namespace autofill;
209 using namespace testing;
211 const DetailInput kTestInputs[] = {
212 { DetailInput::LONG, EMAIL_ADDRESS },
213 { DetailInput::SHORT, CREDIT_CARD_EXP_MONTH },
216 MonthComboboxModel comboModel;
218 inputs.push_back(kTestInputs[0]);
219 inputs.push_back(kTestInputs[1]);
221 ValidityMessages validity, validity2;
223 validity.Set(EMAIL_ADDRESS,
224 ValidityMessage(ASCIIToUTF16("Some error message"), false));
225 validity2.Set(CREDIT_CARD_EXP_MONTH,
226 ValidityMessage(ASCIIToUTF16("Some other error message"), false));
227 EXPECT_CALL(delegate_, RequestedFieldsForSection(section_))
228 .WillOnce(ReturnRef(inputs));
229 EXPECT_CALL(delegate_, ComboboxModelForAutofillType(EMAIL_ADDRESS))
230 .WillRepeatedly(ReturnNull());
231 EXPECT_CALL(delegate_, ComboboxModelForAutofillType(CREDIT_CARD_EXP_MONTH))
232 .WillRepeatedly(Return(&comboModel));
235 autofill::FieldValueMap output;
236 [container_ getInputs:&output];
237 EXPECT_CALL(delegate_, InputsAreValid(section_, output))
238 .WillOnce(Return(validity))
239 .WillOnce(Return(validity2));
241 [container_ validateFor:VALIDATE_FINAL];
242 NSControl<AutofillInputField>* field = [container_ getField:EMAIL_ADDRESS];
243 EXPECT_TRUE([field invalid]);
244 field = [container_ getField:CREDIT_CARD_EXP_MONTH];
245 EXPECT_FALSE([field invalid]);
247 [container_ validateFor:VALIDATE_FINAL];
248 field = [container_ getField:EMAIL_ADDRESS];
249 EXPECT_FALSE([field invalid]);
250 field = [container_ getField:CREDIT_CARD_EXP_MONTH];
251 EXPECT_TRUE([field invalid]);