Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / autofill / autofill_section_container_unittest.mm
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/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/gfx/test/ui_cocoa_test_helper.h"
22
23 using base::ASCIIToUTF16;
24
25 namespace {
26
27 class AutofillSectionContainerTest : public ui::CocoaTest {
28  public:
29   AutofillSectionContainerTest() : section_(autofill::SECTION_BILLING) {}
30   virtual void SetUp() {
31     CocoaTest::SetUp();
32     ResetContainer();
33   }
34
35   void ResetContainer() {
36     container_.reset(
37         [[AutofillSectionContainer alloc]
38             initWithDelegate:&delegate_
39                     forSection:section_]);
40     [[test_window() contentView] setSubviews:@[ [container_ view] ]];
41   }
42
43  protected:
44   base::scoped_nsobject<AutofillSectionContainer> container_;
45   testing::NiceMock<autofill::MockAutofillDialogViewDelegate> delegate_;
46   autofill::DialogSection section_;
47 };
48
49 }  // namespace
50
51 TEST_VIEW(AutofillSectionContainerTest, [container_ view])
52
53 TEST_F(AutofillSectionContainerTest, HasSubviews) {
54   bool hasLayoutView = false;
55   bool hasTextField = false;
56   bool hasSuggestButton = false;
57   bool hasSuggestionView = false;
58
59   ASSERT_EQ(4U, [[[container_ view] subviews] count]);
60   for (NSView* view in [[container_ view] subviews]) {
61     if ([view isKindOfClass:[NSTextField class]]) {
62       hasTextField = true;
63     } else if ([view isKindOfClass:[LayoutView class]]) {
64       hasLayoutView = true;
65     } else if ([view isKindOfClass:[MenuButton class]]) {
66       hasSuggestButton = true;
67     } else if ([view isKindOfClass:[NSView class]]) {
68       hasSuggestionView = true;
69     }
70   }
71
72   EXPECT_TRUE(hasSuggestButton);
73   EXPECT_TRUE(hasLayoutView);
74   EXPECT_TRUE(hasTextField);
75   EXPECT_TRUE(hasSuggestionView);
76 }
77
78 TEST_F(AutofillSectionContainerTest, ModelsPopulateComboboxes) {
79   using namespace autofill;
80   using namespace testing;
81
82   const DetailInput kTestInputs[] = {
83     { DetailInput::SHORT, CREDIT_CARD_EXP_MONTH },
84     { DetailInput::SHORT, CREDIT_CARD_EXP_4_DIGIT_YEAR },
85   };
86
87   DetailInputs inputs;
88   inputs.push_back(kTestInputs[0]);
89   inputs.push_back(kTestInputs[1]);
90
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));
97   EXPECT_CALL(
98     delegate_, ComboboxModelForAutofillType(CREDIT_CARD_EXP_4_DIGIT_YEAR))
99       .WillRepeatedly(Return(&yearComboModel));
100   ResetContainer();
101
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]);
110
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]);
115 };
116
117 TEST_F(AutofillSectionContainerTest, OutputMatchesDefinition) {
118   using namespace autofill;
119   using namespace testing;
120
121   const DetailInput kTestInputs[] = {
122     { DetailInput::LONG, EMAIL_ADDRESS },
123     { DetailInput::SHORT, CREDIT_CARD_EXP_MONTH },
124   };
125   autofill::MonthComboboxModel comboModel;
126   DetailInputs inputs;
127   inputs.push_back(kTestInputs[0]);
128   inputs.push_back(kTestInputs[1]);
129
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));
136
137   ResetContainer();
138
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"];
143
144   autofill::FieldValueMap output;
145   [container_ getInputs:&output];
146
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]);
150 }
151
152 TEST_F(AutofillSectionContainerTest, SuggestionsPopulatedByController) {
153   ui::SimpleMenuModel model(NULL);
154   model.AddItem(10, ASCIIToUTF16("a"));
155   model.AddItem(11, ASCIIToUTF16("b"));
156
157   using namespace autofill;
158   using namespace testing;
159
160   EXPECT_CALL(delegate_, MenuModelForSection(section_))
161       .WillOnce(Return(&model));
162
163   ResetContainer();
164   MenuButton* button = nil;
165   for (id item in [[container_ view] subviews]) {
166     if ([item isKindOfClass:[MenuButton class]]) {
167       button = item;
168       break;
169     }
170   }
171
172   NSMenu* menu = [button attachedMenu];
173   // Expect _three_ items - popup menus need an empty first item.
174   ASSERT_EQ(3, [menu numberOfItems]);
175
176   EXPECT_NSEQ(@"a", [[menu itemAtIndex:1] title]);
177   EXPECT_NSEQ(@"b", [[menu itemAtIndex:2] title]);
178 }
179
180 TEST_F(AutofillSectionContainerTest, FieldsAreInitiallyValid) {
181   using namespace autofill;
182   using namespace testing;
183
184   const DetailInput kTestInputs[] = {
185     { DetailInput::LONG, EMAIL_ADDRESS },
186     { DetailInput::SHORT, CREDIT_CARD_EXP_MONTH },
187   };
188
189   MonthComboboxModel comboModel;
190   DetailInputs inputs;
191   inputs.push_back(kTestInputs[0]);
192   inputs.push_back(kTestInputs[1]);
193
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));
200
201   ResetContainer();
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]);
206 }
207
208 TEST_F(AutofillSectionContainerTest, ControllerInformsValidity) {
209   using namespace autofill;
210   using namespace testing;
211
212   const DetailInput kTestInputs[] = {
213     { DetailInput::LONG, EMAIL_ADDRESS },
214     { DetailInput::SHORT, CREDIT_CARD_EXP_MONTH },
215   };
216
217   MonthComboboxModel comboModel;
218   DetailInputs inputs;
219   inputs.push_back(kTestInputs[0]);
220   inputs.push_back(kTestInputs[1]);
221
222   ValidityMessages validity, validity2;
223
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));
234
235   ResetContainer();
236   autofill::FieldValueMap output;
237   [container_ getInputs:&output];
238   EXPECT_CALL(delegate_, InputsAreValid(section_, output))
239       .WillOnce(Return(validity))
240       .WillOnce(Return(validity2));
241
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]);
247
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]);
253 }