OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
sail
2013/04/15 19:09:54
2013
groby-ooo-7-16
2013/04/15 22:12:08
Done.
| |
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 #import "chrome/browser/ui/cocoa/autofill/autofill_dialog_cocoa.h" | |
4 | 5 |
5 #include "base/bind.h" | 6 #include "base/bind.h" |
6 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
7 #include "base/time.h" | 8 #include "chrome/browser/profiles/profile.h" |
8 #include "base/utf_string_conversions.h" | |
9 #include "chrome/browser/ui/autofill/autofill_dialog_controller_impl.h" | 9 #include "chrome/browser/ui/autofill/autofill_dialog_controller_impl.h" |
10 #include "chrome/browser/ui/autofill/autofill_dialog_view.h" | |
11 #include "chrome/browser/ui/browser.h" | 10 #include "chrome/browser/ui/browser.h" |
12 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 11 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
13 #include "chrome/test/base/in_process_browser_test.h" | 12 #include "chrome/test/base/in_process_browser_test.h" |
14 #include "components/autofill/browser/autofill_metrics.h" | |
15 #include "components/autofill/common/form_data.h" | 13 #include "components/autofill/common/form_data.h" |
16 #include "components/autofill/common/form_field_data.h" | 14 #include "content/public/browser/web_contents.h" |
15 #include "content/public/browser/web_contents_view.h" | |
17 #include "content/public/test/test_utils.h" | 16 #include "content/public/test/test_utils.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
19 | 18 |
20 namespace autofill { | 19 namespace autofill { |
21 | 20 |
22 namespace { | 21 namespace { |
23 | 22 |
24 void MockCallback(const FormStructure*, const std::string&) {} | 23 void MockCallback(const FormStructure*, const std::string&) {} |
25 | 24 |
26 class MockAutofillMetrics : public AutofillMetrics { | |
27 public: | |
28 MockAutofillMetrics() | |
29 : dialog_type_(static_cast<DialogType>(-1)), | |
30 dialog_dismissal_action_( | |
31 static_cast<AutofillMetrics::DialogDismissalAction>(-1)), | |
32 autocheckout_status_( | |
33 static_cast<AutofillMetrics::AutocheckoutCompletionStatus>(-1)) {} | |
34 virtual ~MockAutofillMetrics() {} | |
35 | |
36 // AutofillMetrics: | |
37 virtual void LogAutocheckoutDuration( | |
38 const base::TimeDelta& duration, | |
39 AutocheckoutCompletionStatus status) const OVERRIDE { | |
40 // Ignore constness for testing. | |
41 MockAutofillMetrics* mutable_this = const_cast<MockAutofillMetrics*>(this); | |
42 mutable_this->autocheckout_status_ = status; | |
43 } | |
44 | |
45 virtual void LogDialogUiDuration( | |
46 const base::TimeDelta& duration, | |
47 DialogType dialog_type, | |
48 DialogDismissalAction dismissal_action) const OVERRIDE { | |
49 // Ignore constness for testing. | |
50 MockAutofillMetrics* mutable_this = const_cast<MockAutofillMetrics*>(this); | |
51 mutable_this->dialog_type_ = dialog_type; | |
52 mutable_this->dialog_dismissal_action_ = dismissal_action; | |
53 } | |
54 | |
55 DialogType dialog_type() const { return dialog_type_; } | |
56 AutofillMetrics::DialogDismissalAction dialog_dismissal_action() const { | |
57 return dialog_dismissal_action_; | |
58 } | |
59 | |
60 AutofillMetrics::AutocheckoutCompletionStatus autocheckout_status() const { | |
61 return autocheckout_status_; | |
62 } | |
63 | |
64 private: | |
65 DialogType dialog_type_; | |
66 AutofillMetrics::DialogDismissalAction dialog_dismissal_action_; | |
67 AutofillMetrics::AutocheckoutCompletionStatus autocheckout_status_; | |
68 | |
69 DISALLOW_COPY_AND_ASSIGN(MockAutofillMetrics); | |
70 }; | |
71 | |
72 class TestAutofillDialogController : public AutofillDialogControllerImpl { | 25 class TestAutofillDialogController : public AutofillDialogControllerImpl { |
73 public: | 26 public: |
74 TestAutofillDialogController(content::WebContents* contents, | 27 TestAutofillDialogController( |
75 const FormData& form_data, | 28 content::WebContents* contents, |
76 const AutofillMetrics& metric_logger, | 29 const FormData& form_structure, |
77 const DialogType dialog_type) | 30 const AutofillMetrics& metric_logger, |
31 const DialogType dialog_type) | |
78 : AutofillDialogControllerImpl(contents, | 32 : AutofillDialogControllerImpl(contents, |
79 form_data, | 33 form_structure, |
80 GURL(), | 34 GURL(), |
81 dialog_type, | 35 dialog_type, |
82 base::Bind(&MockCallback)), | 36 base::Bind(MockCallback)), |
83 metric_logger_(metric_logger) { | 37 metric_logger_(metric_logger) { |
84 DisableWallet(); | 38 DisableWallet(); |
85 } | 39 } |
86 | 40 |
87 virtual ~TestAutofillDialogController() {} | 41 virtual ~TestAutofillDialogController() {} |
88 | 42 |
89 virtual void ViewClosed() OVERRIDE { | 43 virtual void ViewClosed() OVERRIDE { |
90 AutofillDialogControllerImpl::ViewClosed(); | 44 AutofillDialogControllerImpl::ViewClosed(); |
91 MessageLoop::current()->Quit(); | 45 base::MessageLoop::current()->Quit(); |
92 } | |
93 | |
94 virtual bool InputIsValid(AutofillFieldType type, | |
95 const string16& value) OVERRIDE { | |
96 return true; | |
97 } | |
98 | |
99 virtual std::vector<AutofillFieldType> InputsAreValid( | |
100 const DetailOutputMap& inputs, ValidationType validation_type) OVERRIDE { | |
101 return std::vector<AutofillFieldType>(); | |
102 } | 46 } |
103 | 47 |
104 // Increase visibility for testing. | 48 // Increase visibility for testing. |
105 AutofillDialogView* view() { return AutofillDialogControllerImpl::view(); } | 49 AutofillDialogView* view() { return AutofillDialogControllerImpl::view(); } |
106 | 50 |
107 private: | 51 private: |
108 // To specify our own metric logger. | 52 // To specify our own metric logger. |
109 virtual const AutofillMetrics& GetMetricLogger() const OVERRIDE { | 53 virtual const AutofillMetrics& GetMetricLogger() const OVERRIDE { |
110 return metric_logger_; | 54 return metric_logger_; |
111 } | 55 } |
112 | 56 |
113 const AutofillMetrics& metric_logger_; | 57 const AutofillMetrics& metric_logger_; |
114 | 58 |
115 DISALLOW_COPY_AND_ASSIGN(TestAutofillDialogController); | 59 DISALLOW_COPY_AND_ASSIGN(TestAutofillDialogController); |
116 }; | 60 }; |
117 | 61 |
62 class AutofillDialogCocoaBrowserTest : public InProcessBrowserTest { | |
63 public: | |
64 AutofillDialogCocoaBrowserTest() : InProcessBrowserTest() {} | |
65 | |
66 virtual ~AutofillDialogCocoaBrowserTest() {} | |
67 | |
68 virtual void SetUpOnMainThread() OVERRIDE { | |
69 FormFieldData field; | |
70 field.autocomplete_attribute = "cc-number"; | |
71 FormData form_data; | |
72 form_data.fields.push_back(field); | |
73 | |
74 controller_ = new TestAutofillDialogController( | |
75 browser()->tab_strip_model()->GetActiveWebContents(), | |
76 form_data, | |
77 metric_logger_, | |
78 DIALOG_TYPE_REQUEST_AUTOCOMPLETE); | |
79 } | |
80 | |
81 virtual void TearDown() OVERRIDE { | |
82 //controller_->ViewClosed(); | |
sail
2013/04/15 19:09:54
remove?
groby-ooo-7-16
2013/04/15 22:12:08
Done.
| |
83 } | |
84 | |
85 TestAutofillDialogController* controller() { return controller_; } | |
86 | |
87 private: | |
88 // The controller owns itself. | |
89 TestAutofillDialogController* controller_; | |
90 // Must outlive the controller. | |
91 AutofillMetrics metric_logger_; | |
92 | |
93 DISALLOW_COPY_AND_ASSIGN(AutofillDialogCocoaBrowserTest); | |
sail
2013/04/15 19:09:54
wrong indent?
groby-ooo-7-16
2013/04/15 22:12:08
Done.
| |
94 }; | |
95 | |
96 IN_PROC_BROWSER_TEST_F(AutofillDialogCocoaBrowserTest, DisplayUI) { | |
97 controller()->Show(); | |
98 controller()->view()->CancelForTesting(); | |
99 | |
100 content::RunAllPendingInMessageLoop(); | |
101 } | |
102 | |
118 } // namespace | 103 } // namespace |
119 | 104 |
120 class AutofillDialogControllerTest : public InProcessBrowserTest { | 105 } // namespace autofill |
121 public: | |
122 AutofillDialogControllerTest() {} | |
123 virtual ~AutofillDialogControllerTest() {} | |
124 | |
125 content::WebContents* GetActiveWebContents() { | |
126 return browser()->tab_strip_model()->GetActiveWebContents(); | |
127 } | |
128 | |
129 private: | |
130 DISALLOW_COPY_AND_ASSIGN(AutofillDialogControllerTest); | |
131 }; | |
132 | |
133 // TODO(isherman): Enable this test on other platforms once the UI is | |
134 // implemented on those platforms. | |
135 #if defined(TOOLKIT_VIEWS) | |
136 #define MAYBE_RequestAutocompleteUiDurationMetrics \ | |
137 RequestAutocompleteUiDurationMetrics | |
138 #else | |
139 #define MAYBE_RequestAutocompleteUiDurationMetrics \ | |
140 DISABLED_RequestAutocompleteUiDurationMetrics | |
141 #endif // defined(TOOLKIT_VIEWS) | |
142 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest, | |
143 MAYBE_RequestAutocompleteUiDurationMetrics) { | |
144 FormData form; | |
145 form.name = ASCIIToUTF16("TestForm"); | |
146 form.method = ASCIIToUTF16("POST"); | |
147 form.origin = GURL("http://example.com/form.html"); | |
148 form.action = GURL("http://example.com/submit.html"); | |
149 form.user_submitted = true; | |
150 | |
151 FormFieldData field; | |
152 field.autocomplete_attribute = "email"; | |
153 form.fields.push_back(field); | |
154 | |
155 // Submit the form data. | |
156 { | |
157 MockAutofillMetrics metric_logger; | |
158 TestAutofillDialogController* dialog_controller = | |
159 new TestAutofillDialogController( | |
160 GetActiveWebContents(), form, metric_logger, | |
161 DIALOG_TYPE_REQUEST_AUTOCOMPLETE); | |
162 dialog_controller->Show(); | |
163 dialog_controller->view()->SubmitForTesting(); | |
164 | |
165 content::RunMessageLoop(); | |
166 | |
167 EXPECT_EQ(AutofillMetrics::DIALOG_ACCEPTED, | |
168 metric_logger.dialog_dismissal_action()); | |
169 EXPECT_EQ(DIALOG_TYPE_REQUEST_AUTOCOMPLETE, metric_logger.dialog_type()); | |
170 } | |
171 | |
172 // Cancel out of the dialog. | |
173 { | |
174 MockAutofillMetrics metric_logger; | |
175 TestAutofillDialogController* dialog_controller = | |
176 new TestAutofillDialogController( | |
177 GetActiveWebContents(), form, metric_logger, | |
178 DIALOG_TYPE_AUTOCHECKOUT); | |
179 dialog_controller->Show(); | |
180 dialog_controller->view()->CancelForTesting(); | |
181 | |
182 content::RunMessageLoop(); | |
183 | |
184 EXPECT_EQ(AutofillMetrics::DIALOG_CANCELED, | |
185 metric_logger.dialog_dismissal_action()); | |
186 EXPECT_EQ(DIALOG_TYPE_AUTOCHECKOUT, metric_logger.dialog_type()); | |
187 } | |
188 | |
189 // Take some other action that dismisses the dialog. | |
190 { | |
191 MockAutofillMetrics metric_logger; | |
192 TestAutofillDialogController* dialog_controller = | |
193 new TestAutofillDialogController( | |
194 GetActiveWebContents(), form, metric_logger, | |
195 DIALOG_TYPE_AUTOCHECKOUT); | |
196 dialog_controller->Show(); | |
197 dialog_controller->Hide(); | |
198 | |
199 content::RunMessageLoop(); | |
200 | |
201 EXPECT_EQ(AutofillMetrics::DIALOG_CANCELED, | |
202 metric_logger.dialog_dismissal_action()); | |
203 EXPECT_EQ(DIALOG_TYPE_AUTOCHECKOUT, metric_logger.dialog_type()); | |
204 } | |
205 | |
206 // Test Autocheckout success metrics. | |
207 { | |
208 MockAutofillMetrics metric_logger; | |
209 TestAutofillDialogController* dialog_controller = | |
210 new TestAutofillDialogController( | |
211 GetActiveWebContents(), form, metric_logger, | |
212 DIALOG_TYPE_AUTOCHECKOUT); | |
213 dialog_controller->Show(); | |
214 dialog_controller->view()->SubmitForTesting(); | |
215 | |
216 EXPECT_EQ(AutofillMetrics::DIALOG_ACCEPTED, | |
217 metric_logger.dialog_dismissal_action()); | |
218 EXPECT_EQ(DIALOG_TYPE_AUTOCHECKOUT, metric_logger.dialog_type()); | |
219 | |
220 dialog_controller->Hide(); | |
221 | |
222 content::RunMessageLoop(); | |
223 | |
224 EXPECT_EQ(AutofillMetrics::AUTOCHECKOUT_SUCCEEDED, | |
225 metric_logger.autocheckout_status()); | |
226 } | |
227 | |
228 // Test Autocheckout failure metric. | |
229 { | |
230 MockAutofillMetrics metric_logger; | |
231 TestAutofillDialogController* dialog_controller = | |
232 new TestAutofillDialogController( | |
233 GetActiveWebContents(), form, metric_logger, | |
234 DIALOG_TYPE_AUTOCHECKOUT); | |
235 dialog_controller->Show(); | |
236 dialog_controller->view()->SubmitForTesting(); | |
237 | |
238 EXPECT_EQ(AutofillMetrics::DIALOG_ACCEPTED, | |
239 metric_logger.dialog_dismissal_action()); | |
240 EXPECT_EQ(DIALOG_TYPE_AUTOCHECKOUT, metric_logger.dialog_type()); | |
241 | |
242 dialog_controller->OnAutocheckoutError(); | |
243 dialog_controller->view()->CancelForTesting(); | |
244 | |
245 content::RunMessageLoop(); | |
246 | |
247 EXPECT_EQ(AutofillMetrics::AUTOCHECKOUT_FAILED, | |
248 metric_logger.autocheckout_status()); | |
249 } | |
250 } | |
251 | |
252 } // namespace autofill | |
OLD | NEW |