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