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 "grit/generated_resources.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "testing/gtest_mac.h"
18 #include "testing/platform_test.h"
19 #include "ui/base/models/combobox_model.h"
20 #include "ui/base/models/simple_menu_model.h"
21 #import "ui/base/test/ui_cocoa_test_helper.h"
23 using base::ASCIIToUTF16;
27 class AutofillSectionContainerTest : public ui::CocoaTest {
29 AutofillSectionContainerTest() : section_(autofill::SECTION_BILLING) {}
30 virtual void SetUp() {
35 void ResetContainer() {
37 [[AutofillSectionContainer alloc]
38 initWithDelegate:&delegate_
39 forSection:section_]);
40 [[test_window() contentView] setSubviews:@[ [container_ view] ]];
44 base::scoped_nsobject<AutofillSectionContainer> container_;
45 testing::NiceMock<autofill::MockAutofillDialogViewDelegate> delegate_;
46 autofill::DialogSection section_;
51 TEST_VIEW(AutofillSectionContainerTest, [container_ view])
53 TEST_F(AutofillSectionContainerTest, HasSubviews) {
54 bool hasLayoutView = false;
55 bool hasTextField = false;
56 bool hasSuggestButton = false;
57 bool hasSuggestionView = false;
59 ASSERT_EQ(4U, [[[container_ view] subviews] count]);
60 for (NSView* view in [[container_ view] subviews]) {
61 if ([view isKindOfClass:[NSTextField class]]) {
63 } else if ([view isKindOfClass:[LayoutView class]]) {
65 } else if ([view isKindOfClass:[MenuButton class]]) {
66 hasSuggestButton = true;
67 } else if ([view isKindOfClass:[NSView class]]) {
68 hasSuggestionView = true;
72 EXPECT_TRUE(hasSuggestButton);
73 EXPECT_TRUE(hasLayoutView);
74 EXPECT_TRUE(hasTextField);
75 EXPECT_TRUE(hasSuggestionView);
78 TEST_F(AutofillSectionContainerTest, ModelsPopulateComboboxes) {
79 using namespace autofill;
80 using namespace testing;
82 const DetailInput kTestInputs[] = {
83 { DetailInput::SHORT, CREDIT_CARD_EXP_MONTH },
84 { DetailInput::SHORT, CREDIT_CARD_EXP_4_DIGIT_YEAR },
88 inputs.push_back(kTestInputs[0]);
89 inputs.push_back(kTestInputs[1]);
91 autofill::MonthComboboxModel monthComboModel;
92 autofill::YearComboboxModel yearComboModel;
93 EXPECT_CALL(delegate_, RequestedFieldsForSection(section_))
94 .WillOnce(ReturnRef(inputs));
95 EXPECT_CALL(delegate_, ComboboxModelForAutofillType(CREDIT_CARD_EXP_MONTH))
96 .WillRepeatedly(Return(&monthComboModel));
98 delegate_, ComboboxModelForAutofillType(CREDIT_CARD_EXP_4_DIGIT_YEAR))
99 .WillRepeatedly(Return(&yearComboModel));
102 NSPopUpButton* popup = base::mac::ObjCCastStrict<NSPopUpButton>(
103 [container_ getField:CREDIT_CARD_EXP_MONTH]);
104 EXPECT_EQ(13, [popup numberOfItems]);
105 EXPECT_NSEQ(@"Month", [popup itemTitleAtIndex:0]);
106 EXPECT_NSEQ(@"01", [popup itemTitleAtIndex:1]);
107 EXPECT_NSEQ(@"02", [popup itemTitleAtIndex:2]);
108 EXPECT_NSEQ(@"03", [popup itemTitleAtIndex:3]);
109 EXPECT_NSEQ(@"12", [popup itemTitleAtIndex:12]);
111 NSPopUpButton* yearPopup = base::mac::ObjCCastStrict<NSPopUpButton>(
112 [container_ getField:CREDIT_CARD_EXP_4_DIGIT_YEAR]);
113 EXPECT_EQ(11, [yearPopup numberOfItems]);
114 EXPECT_NSEQ(@"Year", [yearPopup itemTitleAtIndex:0]);
117 TEST_F(AutofillSectionContainerTest, OutputMatchesDefinition) {
118 using namespace autofill;
119 using namespace testing;
121 const DetailInput kTestInputs[] = {
122 { DetailInput::LONG, EMAIL_ADDRESS },
123 { DetailInput::SHORT, CREDIT_CARD_EXP_MONTH },
125 autofill::MonthComboboxModel comboModel;
127 inputs.push_back(kTestInputs[0]);
128 inputs.push_back(kTestInputs[1]);
130 EXPECT_CALL(delegate_, RequestedFieldsForSection(section_))
131 .WillOnce(ReturnRef(inputs));
132 EXPECT_CALL(delegate_, ComboboxModelForAutofillType(EMAIL_ADDRESS))
133 .WillRepeatedly(ReturnNull());
134 EXPECT_CALL(delegate_, ComboboxModelForAutofillType(CREDIT_CARD_EXP_MONTH))
135 .WillRepeatedly(Return(&comboModel));
139 NSPopUpButton* popup = base::mac::ObjCCastStrict<NSPopUpButton>(
140 [container_ getField:CREDIT_CARD_EXP_MONTH]);
141 [popup selectItemWithTitle:@"02"];
142 [[container_ getField:EMAIL_ADDRESS] setStringValue:@"magic@example.org"];
144 autofill::FieldValueMap output;
145 [container_ getInputs:&output];
147 ASSERT_EQ(inputs.size(), output.size());
148 EXPECT_EQ(ASCIIToUTF16("magic@example.org"), output[inputs[0].type]);
149 EXPECT_EQ(ASCIIToUTF16("02"), output[inputs[1].type]);
152 TEST_F(AutofillSectionContainerTest, SuggestionsPopulatedByController) {
153 ui::SimpleMenuModel model(NULL);
154 model.AddItem(10, ASCIIToUTF16("a"));
155 model.AddItem(11, ASCIIToUTF16("b"));
157 using namespace autofill;
158 using namespace testing;
160 EXPECT_CALL(delegate_, MenuModelForSection(section_))
161 .WillOnce(Return(&model));
164 MenuButton* button = nil;
165 for (id item in [[container_ view] subviews]) {
166 if ([item isKindOfClass:[MenuButton class]]) {
172 NSMenu* menu = [button attachedMenu];
173 // Expect _three_ items - popup menus need an empty first item.
174 ASSERT_EQ(3, [menu numberOfItems]);
176 EXPECT_NSEQ(@"a", [[menu itemAtIndex:1] title]);
177 EXPECT_NSEQ(@"b", [[menu itemAtIndex:2] title]);
180 TEST_F(AutofillSectionContainerTest, FieldsAreInitiallyValid) {
181 using namespace autofill;
182 using namespace testing;
184 const DetailInput kTestInputs[] = {
185 { DetailInput::LONG, EMAIL_ADDRESS },
186 { DetailInput::SHORT, CREDIT_CARD_EXP_MONTH },
189 MonthComboboxModel comboModel;
191 inputs.push_back(kTestInputs[0]);
192 inputs.push_back(kTestInputs[1]);
194 EXPECT_CALL(delegate_, RequestedFieldsForSection(section_))
195 .WillOnce(ReturnRef(inputs));
196 EXPECT_CALL(delegate_, ComboboxModelForAutofillType(EMAIL_ADDRESS))
197 .WillRepeatedly(ReturnNull());
198 EXPECT_CALL(delegate_, ComboboxModelForAutofillType(CREDIT_CARD_EXP_MONTH))
199 .WillRepeatedly(Return(&comboModel));
202 NSControl<AutofillInputField>* field = [container_ getField:EMAIL_ADDRESS];
203 EXPECT_FALSE([field invalid]);
204 field = [container_ getField:CREDIT_CARD_EXP_MONTH];
205 EXPECT_FALSE([field invalid]);
208 TEST_F(AutofillSectionContainerTest, ControllerInformsValidity) {
209 using namespace autofill;
210 using namespace testing;
212 const DetailInput kTestInputs[] = {
213 { DetailInput::LONG, EMAIL_ADDRESS },
214 { DetailInput::SHORT, CREDIT_CARD_EXP_MONTH },
217 MonthComboboxModel comboModel;
219 inputs.push_back(kTestInputs[0]);
220 inputs.push_back(kTestInputs[1]);
222 ValidityMessages validity, validity2;
224 validity.Set(EMAIL_ADDRESS,
225 ValidityMessage(ASCIIToUTF16("Some error message"), false));
226 validity2.Set(CREDIT_CARD_EXP_MONTH,
227 ValidityMessage(ASCIIToUTF16("Some other error message"), false));
228 EXPECT_CALL(delegate_, RequestedFieldsForSection(section_))
229 .WillOnce(ReturnRef(inputs));
230 EXPECT_CALL(delegate_, ComboboxModelForAutofillType(EMAIL_ADDRESS))
231 .WillRepeatedly(ReturnNull());
232 EXPECT_CALL(delegate_, ComboboxModelForAutofillType(CREDIT_CARD_EXP_MONTH))
233 .WillRepeatedly(Return(&comboModel));
236 autofill::FieldValueMap output;
237 [container_ getInputs:&output];
238 EXPECT_CALL(delegate_, InputsAreValid(section_, output))
239 .WillOnce(Return(validity))
240 .WillOnce(Return(validity2));
242 [container_ validateFor:VALIDATE_FINAL];
243 NSControl<AutofillInputField>* field = [container_ getField:EMAIL_ADDRESS];
244 EXPECT_TRUE([field invalid]);
245 field = [container_ getField:CREDIT_CARD_EXP_MONTH];
246 EXPECT_FALSE([field invalid]);
248 [container_ validateFor:VALIDATE_FINAL];
249 field = [container_ getField:EMAIL_ADDRESS];
250 EXPECT_FALSE([field invalid]);
251 field = [container_ getField:CREDIT_CARD_EXP_MONTH];
252 EXPECT_TRUE([field invalid]);