OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 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 #include "base/bind.h" | |
6 #include "base/message_loop.h" | |
7 #include "base/time.h" | |
8 #include "base/utf_string_conversions.h" | |
9 #include "chrome/browser/autofill/autofill_metrics.h" | |
10 #include "chrome/browser/ui/autofill/autofill_dialog_controller_impl.h" | |
11 #include "chrome/browser/ui/autofill/autofill_dialog_view.h" | |
12 #include "chrome/browser/ui/browser.h" | |
13 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
14 #include "chrome/common/form_data.h" | |
15 #include "chrome/common/form_field_data.h" | |
16 #include "chrome/test/base/in_process_browser_test.h" | |
17 #include "content/public/test/test_utils.h" | |
18 #include "testing/gtest/include/gtest/gtest.h" | |
19 | |
20 namespace autofill { | |
21 | |
22 namespace { | |
23 | |
24 void MockCallback(const FormStructure*) {} | |
25 | |
26 class MockAutofillMetrics : public AutofillMetrics { | |
27 public: | |
28 MockAutofillMetrics() | |
29 : dialog_type_(static_cast<DialogType>(-1)), | |
Dan Beam
2013/02/07 00:48:05
^ what is this dark magic?
Ilya Sherman
2013/02/07 00:58:48
Is there a white magic version of this that you'd
Dan Beam
2013/02/07 19:13:11
does an INVALID member in the enum work?
Ilya Sherman
2013/02/11 05:57:48
Not really, because it would only be used by the t
Dan Beam
2013/02/11 16:48:23
I don't think that's a big deal, there's lots of *
benquan
2013/02/14 02:46:54
This will break switch statement w/o default optio
| |
30 metric_(static_cast<AutofillMetrics::DialogDismissalAction>(-1)) {} | |
31 virtual ~MockAutofillMetrics() {} | |
32 | |
33 // AutofillMetrics: | |
34 virtual void LogRequestAutocompleteUiDuration( | |
35 const base::TimeDelta& duration, | |
36 DialogType dialog_type, | |
37 DialogDismissalAction dismissal_action) const OVERRIDE { | |
38 // Ignore constness for testing. | |
39 MockAutofillMetrics* mutable_this = const_cast<MockAutofillMetrics*>(this); | |
40 mutable_this->dialog_type_ = dialog_type; | |
41 mutable_this->metric_ = dismissal_action; | |
42 } | |
43 | |
44 DialogType dialog_type() const { return dialog_type_; } | |
45 AutofillMetrics::DialogDismissalAction metric() const { return metric_; } | |
46 | |
47 private: | |
48 DialogType dialog_type_; | |
49 AutofillMetrics::DialogDismissalAction metric_; | |
50 | |
51 DISALLOW_COPY_AND_ASSIGN(MockAutofillMetrics); | |
52 }; | |
53 | |
54 class TestAutofillDialogController : public AutofillDialogControllerImpl { | |
55 public: | |
56 TestAutofillDialogController(content::WebContents* contents, | |
57 const FormData& form_data, | |
58 const AutofillMetrics& metric_logger, | |
59 const DialogType dialog_type) | |
60 : AutofillDialogControllerImpl(contents, | |
61 form_data, | |
62 GURL(), | |
63 content::SSLStatus(), | |
64 metric_logger, | |
65 dialog_type, | |
66 base::Bind(&MockCallback)) { | |
67 } | |
68 | |
69 virtual ~TestAutofillDialogController() {} | |
70 | |
71 virtual void ViewClosed(DialogAction action) OVERRIDE { | |
72 AutofillDialogControllerImpl::ViewClosed(action); | |
73 MessageLoop::current()->Quit(); | |
74 } | |
75 | |
76 virtual bool InputIsValid(const DetailInput* input, | |
77 const string16& value) OVERRIDE { | |
78 return true; | |
79 } | |
80 | |
81 // Increase visibility for testing. | |
82 AutofillDialogView* view() { return AutofillDialogControllerImpl::view(); } | |
83 | |
84 private: | |
85 DISALLOW_COPY_AND_ASSIGN(TestAutofillDialogController); | |
86 }; | |
87 | |
88 } // namespace | |
89 | |
90 class AutofillDialogControllerTest : public InProcessBrowserTest { | |
91 public: | |
92 AutofillDialogControllerTest() {} | |
93 virtual ~AutofillDialogControllerTest() {} | |
94 | |
95 content::WebContents* GetActiveWebContents() { | |
96 return browser()->tab_strip_model()->GetActiveWebContents(); | |
97 } | |
98 | |
99 private: | |
100 DISALLOW_COPY_AND_ASSIGN(AutofillDialogControllerTest); | |
101 }; | |
102 | |
103 // TODO(isherman): Enable this test on other platforms once the UI is | |
104 // implemented on those platforms. | |
105 #if defined(TOOLKIT_VIEWS) | |
106 #define MAYBE_RequestAutocompleteUiDurationMetrics \ | |
107 RequestAutocompleteUiDurationMetrics | |
108 #else | |
109 #define MAYBE_RequestAutocompleteUiDurationMetrics \ | |
110 DISABLED_RequestAutocompleteUiDurationMetrics | |
111 #endif // defined(TOOLKIT_VIEWS) | |
112 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest, | |
113 MAYBE_RequestAutocompleteUiDurationMetrics) { | |
114 FormData form; | |
115 form.name = ASCIIToUTF16("TestForm"); | |
116 form.method = ASCIIToUTF16("POST"); | |
117 form.origin = GURL("http://example.com/form.html"); | |
118 form.action = GURL("http://example.com/submit.html"); | |
119 form.user_submitted = true; | |
120 | |
121 FormFieldData field; | |
122 field.autocomplete_attribute = "email"; | |
123 form.fields.push_back(field); | |
124 | |
125 // Submit the form data. | |
126 { | |
127 MockAutofillMetrics metric_logger; | |
128 TestAutofillDialogController* dialog_controller = | |
129 new TestAutofillDialogController( | |
130 GetActiveWebContents(), form, metric_logger, | |
131 DIALOG_TYPE_REQUEST_AUTOCOMPLETE); | |
132 dialog_controller->Show(); | |
133 dialog_controller->view()->SubmitForTesting(); | |
134 | |
135 content::RunMessageLoop(); | |
136 | |
137 EXPECT_EQ(AutofillMetrics::DIALOG_ACCEPTED, metric_logger.metric()); | |
138 EXPECT_EQ(DIALOG_TYPE_REQUEST_AUTOCOMPLETE, metric_logger.dialog_type()); | |
139 } | |
140 | |
141 // Cancel out of the dialog. | |
142 { | |
143 MockAutofillMetrics metric_logger; | |
144 TestAutofillDialogController* dialog_controller = | |
145 new TestAutofillDialogController( | |
146 GetActiveWebContents(), form, metric_logger, | |
147 DIALOG_TYPE_AUTOCHECKOUT); | |
148 dialog_controller->Show(); | |
149 dialog_controller->view()->CancelForTesting(); | |
150 | |
151 content::RunMessageLoop(); | |
152 | |
153 EXPECT_EQ(AutofillMetrics::DIALOG_CANCELED, metric_logger.metric()); | |
154 EXPECT_EQ(DIALOG_TYPE_AUTOCHECKOUT, metric_logger.dialog_type()); | |
155 } | |
156 | |
157 // Take some other action that dismisses the dialog. | |
158 { | |
159 MockAutofillMetrics metric_logger; | |
160 TestAutofillDialogController* dialog_controller = | |
161 new TestAutofillDialogController( | |
162 GetActiveWebContents(), form, metric_logger, | |
163 DIALOG_TYPE_AUTOCHECKOUT); | |
164 dialog_controller->Show(); | |
165 dialog_controller->Hide(); | |
166 | |
167 content::RunMessageLoop(); | |
168 | |
169 EXPECT_EQ(AutofillMetrics::DIALOG_CANCELED, metric_logger.metric()); | |
170 EXPECT_EQ(DIALOG_TYPE_AUTOCHECKOUT, metric_logger.dialog_type()); | |
171 } | |
172 } | |
173 | |
174 } // namespace autofill | |
OLD | NEW |