Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(56)

Side by Side Diff: components/autofill/core/browser/autofill_metrics_unittest.cc

Issue 1494373003: [Autofill] Send Autofill upload when active form loses focus. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fixed test Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/autofill/core/browser/autofill_manager_unittest.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 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 4
5 #include "components/autofill/core/browser/autofill_metrics.h" 5 #include "components/autofill/core/browser/autofill_metrics.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "base/prefs/pref_service.h" 10 #include "base/prefs/pref_service.h"
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after
246 } 246 }
247 247
248 // |form_structure| will be owned by |form_structures()|. 248 // |form_structure| will be owned by |form_structures()|.
249 TestFormStructure* form_structure = new TestFormStructure(empty_form); 249 TestFormStructure* form_structure = new TestFormStructure(empty_form);
250 form_structure->SetFieldTypes(heuristic_types, server_types); 250 form_structure->SetFieldTypes(heuristic_types, server_types);
251 form_structures()->push_back(form_structure); 251 form_structures()->push_back(form_structure);
252 } 252 }
253 253
254 // Calls AutofillManager::OnWillSubmitForm and waits for it to complete. 254 // Calls AutofillManager::OnWillSubmitForm and waits for it to complete.
255 void WillSubmitForm(const FormData& form, const TimeTicks& timestamp) { 255 void WillSubmitForm(const FormData& form, const TimeTicks& timestamp) {
256 run_loop_.reset(new base::RunLoop()); 256 ResetRunLoop();
257 if (!OnWillSubmitForm(form, timestamp)) 257 if (!OnWillSubmitForm(form, timestamp))
258 return; 258 return;
259 259
260 // Wait for the asynchronous OnWillSubmitForm() call to complete. 260 // Wait for the asynchronous OnWillSubmitForm() call to complete.
261 run_loop_->Run(); 261 RunRunLoop();
262 } 262 }
263 263
264 // Calls both AutofillManager::OnWillSubmitForm and 264 // Calls both AutofillManager::OnWillSubmitForm and
265 // AutofillManager::OnFormSubmitted. 265 // AutofillManager::OnFormSubmitted.
266 void SubmitForm(const FormData& form, const TimeTicks& timestamp) { 266 void SubmitForm(const FormData& form, const TimeTicks& timestamp) {
267 WillSubmitForm(form, timestamp); 267 WillSubmitForm(form, timestamp);
268 OnFormSubmitted(form); 268 OnFormSubmitted(form);
269 } 269 }
270 270
271 void UploadFormDataAsyncCallback( 271 // Control the run loop from within tests.
272 const FormStructure* submitted_form, 272 void ResetRunLoop() { run_loop_.reset(new base::RunLoop()); }
273 const base::TimeTicks& load_time, 273 void RunRunLoop() { run_loop_->Run(); }
274 const base::TimeTicks& interaction_time, 274
275 const base::TimeTicks& submission_time) override { 275 void UploadFormDataAsyncCallback(const FormStructure* submitted_form,
276 const base::TimeTicks& load_time,
277 const base::TimeTicks& interaction_time,
278 const base::TimeTicks& submission_time,
279 bool observed_submission) override {
276 run_loop_->Quit(); 280 run_loop_->Quit();
277 281
278 AutofillManager::UploadFormDataAsyncCallback(submitted_form, 282 AutofillManager::UploadFormDataAsyncCallback(
279 load_time, 283 submitted_form, load_time, interaction_time, submission_time,
280 interaction_time, 284 observed_submission);
281 submission_time);
282 } 285 }
283 286
284 private: 287 private:
285 bool autofill_enabled_; 288 bool autofill_enabled_;
286 scoped_ptr<base::RunLoop> run_loop_; 289 scoped_ptr<base::RunLoop> run_loop_;
287 290
288 DISALLOW_COPY_AND_ASSIGN(TestAutofillManager); 291 DISALLOW_COPY_AND_ASSIGN(TestAutofillManager);
289 }; 292 };
290 293
291 } // namespace 294 } // namespace
(...skipping 2555 matching lines...) Expand 10 before | Expand all | Expand 10 after
2847 { 2850 {
2848 base::HistogramTester histogram_tester; 2851 base::HistogramTester histogram_tester;
2849 autofill_manager_->DidShowSuggestions(true, form, form.fields[1]); 2852 autofill_manager_->DidShowSuggestions(true, form, form.fields[1]);
2850 histogram_tester.ExpectUniqueSample("Autofill.UserHappiness", 2853 histogram_tester.ExpectUniqueSample("Autofill.UserHappiness",
2851 AutofillMetrics::SUGGESTIONS_SHOWN, 1); 2854 AutofillMetrics::SUGGESTIONS_SHOWN, 1);
2852 } 2855 }
2853 2856
2854 // Simulate invoking autofill. 2857 // Simulate invoking autofill.
2855 { 2858 {
2856 base::HistogramTester histogram_tester; 2859 base::HistogramTester histogram_tester;
2857 autofill_manager_->OnDidFillAutofillFormData(TimeTicks()); 2860 autofill_manager_->OnDidFillAutofillFormData(form, TimeTicks());
2858 histogram_tester.ExpectBucketCount("Autofill.UserHappiness", 2861 histogram_tester.ExpectBucketCount("Autofill.UserHappiness",
2859 AutofillMetrics::USER_DID_AUTOFILL, 1); 2862 AutofillMetrics::USER_DID_AUTOFILL, 1);
2860 histogram_tester.ExpectBucketCount( 2863 histogram_tester.ExpectBucketCount(
2861 "Autofill.UserHappiness", AutofillMetrics::USER_DID_AUTOFILL_ONCE, 1); 2864 "Autofill.UserHappiness", AutofillMetrics::USER_DID_AUTOFILL_ONCE, 1);
2862 } 2865 }
2863 2866
2864 // Simulate editing an autofilled field. 2867 // Simulate editing an autofilled field.
2865 { 2868 {
2866 base::HistogramTester histogram_tester; 2869 base::HistogramTester histogram_tester;
2867 std::string guid("00000000-0000-0000-0000-000000000001"); 2870 std::string guid("00000000-0000-0000-0000-000000000001");
2868 autofill_manager_->FillOrPreviewForm( 2871 autofill_manager_->FillOrPreviewForm(
2869 AutofillDriver::FORM_DATA_ACTION_FILL, 0, form, form.fields.front(), 2872 AutofillDriver::FORM_DATA_ACTION_FILL, 0, form, form.fields.front(),
2870 autofill_manager_->MakeFrontendID(std::string(), guid)); 2873 autofill_manager_->MakeFrontendID(std::string(), guid));
2871 autofill_manager_->OnTextFieldDidChange(form, form.fields.front(), 2874 autofill_manager_->OnTextFieldDidChange(form, form.fields.front(),
2872 TimeTicks()); 2875 TimeTicks());
2873 // Simulate a second keystroke; make sure we don't log the metric twice. 2876 // Simulate a second keystroke; make sure we don't log the metric twice.
2874 autofill_manager_->OnTextFieldDidChange(form, form.fields.front(), 2877 autofill_manager_->OnTextFieldDidChange(form, form.fields.front(),
2875 TimeTicks()); 2878 TimeTicks());
2876 histogram_tester.ExpectBucketCount( 2879 histogram_tester.ExpectBucketCount(
2877 "Autofill.UserHappiness", 2880 "Autofill.UserHappiness",
2878 AutofillMetrics::USER_DID_EDIT_AUTOFILLED_FIELD, 1); 2881 AutofillMetrics::USER_DID_EDIT_AUTOFILLED_FIELD, 1);
2879 histogram_tester.ExpectBucketCount( 2882 histogram_tester.ExpectBucketCount(
2880 "Autofill.UserHappiness", 2883 "Autofill.UserHappiness",
2881 AutofillMetrics::USER_DID_EDIT_AUTOFILLED_FIELD_ONCE, 1); 2884 AutofillMetrics::USER_DID_EDIT_AUTOFILLED_FIELD_ONCE, 1);
2882 } 2885 }
2883 2886
2884 // Simulate invoking autofill again. 2887 // Simulate invoking autofill again.
2885 { 2888 {
2886 base::HistogramTester histogram_tester; 2889 base::HistogramTester histogram_tester;
2887 autofill_manager_->OnDidFillAutofillFormData(TimeTicks()); 2890 autofill_manager_->OnDidFillAutofillFormData(form, TimeTicks());
2888 histogram_tester.ExpectUniqueSample("Autofill.UserHappiness", 2891 histogram_tester.ExpectUniqueSample("Autofill.UserHappiness",
2889 AutofillMetrics::USER_DID_AUTOFILL, 1); 2892 AutofillMetrics::USER_DID_AUTOFILL, 1);
2890 } 2893 }
2891 2894
2892 // Simulate editing another autofilled field. 2895 // Simulate editing another autofilled field.
2893 { 2896 {
2894 base::HistogramTester histogram_tester; 2897 base::HistogramTester histogram_tester;
2895 autofill_manager_->OnTextFieldDidChange(form, form.fields[1], TimeTicks()); 2898 autofill_manager_->OnTextFieldDidChange(form, form.fields[1], TimeTicks());
2896 histogram_tester.ExpectUniqueSample( 2899 histogram_tester.ExpectUniqueSample(
2897 "Autofill.UserHappiness", 2900 "Autofill.UserHappiness",
(...skipping 10 matching lines...) Expand all
2908 form.action = GURL("http://example.com/submit.html"); 2911 form.action = GURL("http://example.com/submit.html");
2909 2912
2910 FormFieldData field; 2913 FormFieldData field;
2911 test::CreateTestFormField("Name", "name", "", "text", &field); 2914 test::CreateTestFormField("Name", "name", "", "text", &field);
2912 form.fields.push_back(field); 2915 form.fields.push_back(field);
2913 test::CreateTestFormField("Email", "email", "", "text", &field); 2916 test::CreateTestFormField("Email", "email", "", "text", &field);
2914 form.fields.push_back(field); 2917 form.fields.push_back(field);
2915 test::CreateTestFormField("Phone", "phone", "", "text", &field); 2918 test::CreateTestFormField("Phone", "phone", "", "text", &field);
2916 form.fields.push_back(field); 2919 form.fields.push_back(field);
2917 2920
2918 std::vector<FormData> forms(1, form); 2921 const std::vector<FormData> forms(1, form);
2919 2922
2920 // Fill additional form. 2923 // Fill additional form.
2921 FormData second_form = form; 2924 FormData second_form = form;
2922 test::CreateTestFormField("Second Phone", "second_phone", "", "text", &field); 2925 test::CreateTestFormField("Second Phone", "second_phone", "", "text", &field);
2923 second_form.fields.push_back(field); 2926 second_form.fields.push_back(field);
2924 2927
2925 std::vector<FormData> second_forms(1, second_form); 2928 std::vector<FormData> second_forms(1, second_form);
2926 2929
2927 // Fill the field values for form submission. 2930 // Fill the field values for form submission.
2928 form.fields[0].value = ASCIIToUTF16("Elvis Aaron Presley"); 2931 form.fields[0].value = ASCIIToUTF16("Elvis Aaron Presley");
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
2964 2967
2965 histogram_tester.ExpectTotalCount( 2968 histogram_tester.ExpectTotalCount(
2966 "Autofill.FillDuration.FromLoad.WithAutofill", 0); 2969 "Autofill.FillDuration.FromLoad.WithAutofill", 0);
2967 histogram_tester.ExpectUniqueSample( 2970 histogram_tester.ExpectUniqueSample(
2968 "Autofill.FillDuration.FromLoad.WithoutAutofill", 16, 1); 2971 "Autofill.FillDuration.FromLoad.WithoutAutofill", 16, 1);
2969 histogram_tester.ExpectTotalCount( 2972 histogram_tester.ExpectTotalCount(
2970 "Autofill.FillDuration.FromInteraction.WithAutofill", 0); 2973 "Autofill.FillDuration.FromInteraction.WithAutofill", 0);
2971 histogram_tester.ExpectUniqueSample( 2974 histogram_tester.ExpectUniqueSample(
2972 "Autofill.FillDuration.FromInteraction.WithoutAutofill", 14, 1); 2975 "Autofill.FillDuration.FromInteraction.WithoutAutofill", 14, 1);
2973 2976
2977 // We expected an upload to be triggered when the manager is reset.
2978 autofill_manager_->ResetRunLoop();
2974 autofill_manager_->Reset(); 2979 autofill_manager_->Reset();
2980 autofill_manager_->RunRunLoop();
2975 } 2981 }
2976 2982
2977 // Expect metric to be logged if the user autofilled the form. 2983 // Expect metric to be logged if the user autofilled the form.
2978 form.fields[0].is_autofilled = true; 2984 form.fields[0].is_autofilled = true;
2979 { 2985 {
2980 base::HistogramTester histogram_tester; 2986 base::HistogramTester histogram_tester;
2981 autofill_manager_->OnFormsSeen(forms, TimeTicks::FromInternalValue(1)); 2987 autofill_manager_->OnFormsSeen(forms, TimeTicks::FromInternalValue(1));
2982 autofill_manager_->OnDidFillAutofillFormData( 2988 autofill_manager_->OnDidFillAutofillFormData(
2983 TimeTicks::FromInternalValue(5)); 2989 form, TimeTicks::FromInternalValue(5));
2984 autofill_manager_->SubmitForm(form, TimeTicks::FromInternalValue(17)); 2990 autofill_manager_->SubmitForm(form, TimeTicks::FromInternalValue(17));
2985 2991
2986 histogram_tester.ExpectUniqueSample( 2992 histogram_tester.ExpectUniqueSample(
2987 "Autofill.FillDuration.FromLoad.WithAutofill", 16, 1); 2993 "Autofill.FillDuration.FromLoad.WithAutofill", 16, 1);
2988 histogram_tester.ExpectTotalCount( 2994 histogram_tester.ExpectTotalCount(
2989 "Autofill.FillDuration.FromLoad.WithoutAutofill", 0); 2995 "Autofill.FillDuration.FromLoad.WithoutAutofill", 0);
2990 histogram_tester.ExpectUniqueSample( 2996 histogram_tester.ExpectUniqueSample(
2991 "Autofill.FillDuration.FromInteraction.WithAutofill", 12, 1); 2997 "Autofill.FillDuration.FromInteraction.WithAutofill", 12, 1);
2992 histogram_tester.ExpectTotalCount( 2998 histogram_tester.ExpectTotalCount(
2993 "Autofill.FillDuration.FromInteraction.WithoutAutofill", 0); 2999 "Autofill.FillDuration.FromInteraction.WithoutAutofill", 0);
2994 3000
3001 // We expected an upload to be triggered when the manager is reset.
3002 autofill_manager_->ResetRunLoop();
2995 autofill_manager_->Reset(); 3003 autofill_manager_->Reset();
3004 autofill_manager_->RunRunLoop();
2996 } 3005 }
2997 3006
2998 // Expect metric to be logged if the user both manually filled some fields 3007 // Expect metric to be logged if the user both manually filled some fields
2999 // and autofilled others. Messages can arrive out of order, so make sure they 3008 // and autofilled others. Messages can arrive out of order, so make sure they
3000 // take precedence appropriately. 3009 // take precedence appropriately.
3001 { 3010 {
3002 base::HistogramTester histogram_tester; 3011 base::HistogramTester histogram_tester;
3003 3012
3004 autofill_manager_->OnFormsSeen(forms, TimeTicks::FromInternalValue(1)); 3013 autofill_manager_->OnFormsSeen(forms, TimeTicks::FromInternalValue(1));
3005 autofill_manager_->OnDidFillAutofillFormData( 3014 autofill_manager_->OnDidFillAutofillFormData(
3006 TimeTicks::FromInternalValue(5)); 3015 form, TimeTicks::FromInternalValue(5));
3007 autofill_manager_->OnTextFieldDidChange(form, form.fields.front(), 3016 autofill_manager_->OnTextFieldDidChange(form, form.fields.front(),
3008 TimeTicks::FromInternalValue(3)); 3017 TimeTicks::FromInternalValue(3));
3009 autofill_manager_->SubmitForm(form, TimeTicks::FromInternalValue(17)); 3018 autofill_manager_->SubmitForm(form, TimeTicks::FromInternalValue(17));
3010 3019
3011 histogram_tester.ExpectUniqueSample( 3020 histogram_tester.ExpectUniqueSample(
3012 "Autofill.FillDuration.FromLoad.WithAutofill", 16, 1); 3021 "Autofill.FillDuration.FromLoad.WithAutofill", 16, 1);
3013 histogram_tester.ExpectTotalCount( 3022 histogram_tester.ExpectTotalCount(
3014 "Autofill.FillDuration.FromLoad.WithoutAutofill", 0); 3023 "Autofill.FillDuration.FromLoad.WithoutAutofill", 0);
3015 histogram_tester.ExpectUniqueSample( 3024 histogram_tester.ExpectUniqueSample(
3016 "Autofill.FillDuration.FromInteraction.WithAutofill", 14, 1); 3025 "Autofill.FillDuration.FromInteraction.WithAutofill", 14, 1);
3017 histogram_tester.ExpectTotalCount( 3026 histogram_tester.ExpectTotalCount(
3018 "Autofill.FillDuration.FromInteraction.WithoutAutofill", 0); 3027 "Autofill.FillDuration.FromInteraction.WithoutAutofill", 0);
3019 3028
3029 // We expected an upload to be triggered when the manager is reset.
3030 autofill_manager_->ResetRunLoop();
3020 autofill_manager_->Reset(); 3031 autofill_manager_->Reset();
3032 autofill_manager_->RunRunLoop();
3021 } 3033 }
3022 3034
3023 // Make sure that loading another form doesn't affect metrics from the first 3035 // Make sure that loading another form doesn't affect metrics from the first
3024 // form. 3036 // form.
3025 { 3037 {
3026 base::HistogramTester histogram_tester; 3038 base::HistogramTester histogram_tester;
3027 autofill_manager_->OnFormsSeen(forms, TimeTicks::FromInternalValue(1)); 3039 autofill_manager_->OnFormsSeen(forms, TimeTicks::FromInternalValue(1));
3028 autofill_manager_->OnFormsSeen(second_forms, 3040 autofill_manager_->OnFormsSeen(second_forms,
3029 TimeTicks::FromInternalValue(3)); 3041 TimeTicks::FromInternalValue(3));
3030 autofill_manager_->OnDidFillAutofillFormData( 3042 autofill_manager_->OnDidFillAutofillFormData(
3031 TimeTicks::FromInternalValue(5)); 3043 form, TimeTicks::FromInternalValue(5));
3032 autofill_manager_->OnTextFieldDidChange(form, form.fields.front(), 3044 autofill_manager_->OnTextFieldDidChange(form, form.fields.front(),
3033 TimeTicks::FromInternalValue(3)); 3045 TimeTicks::FromInternalValue(3));
3034 autofill_manager_->SubmitForm(form, TimeTicks::FromInternalValue(17)); 3046 autofill_manager_->SubmitForm(form, TimeTicks::FromInternalValue(17));
3035 3047
3036 histogram_tester.ExpectUniqueSample( 3048 histogram_tester.ExpectUniqueSample(
3037 "Autofill.FillDuration.FromLoad.WithAutofill", 16, 1); 3049 "Autofill.FillDuration.FromLoad.WithAutofill", 16, 1);
3038 histogram_tester.ExpectTotalCount( 3050 histogram_tester.ExpectTotalCount(
3039 "Autofill.FillDuration.FromLoad.WithoutAutofill", 0); 3051 "Autofill.FillDuration.FromLoad.WithoutAutofill", 0);
3040 histogram_tester.ExpectUniqueSample( 3052 histogram_tester.ExpectUniqueSample(
3041 "Autofill.FillDuration.FromInteraction.WithAutofill", 14, 1); 3053 "Autofill.FillDuration.FromInteraction.WithAutofill", 14, 1);
3042 histogram_tester.ExpectTotalCount( 3054 histogram_tester.ExpectTotalCount(
3043 "Autofill.FillDuration.FromInteraction.WithoutAutofill", 0); 3055 "Autofill.FillDuration.FromInteraction.WithoutAutofill", 0);
3044 3056
3057 // We expected an upload to be triggered when the manager is reset.
3058 autofill_manager_->ResetRunLoop();
3045 autofill_manager_->Reset(); 3059 autofill_manager_->Reset();
3060 autofill_manager_->RunRunLoop();
3061 ;
3046 } 3062 }
3047 3063
3048 // Make sure that submitting a form that was loaded later will report the 3064 // Make sure that submitting a form that was loaded later will report the
3049 // later loading time. 3065 // later loading time.
3050 { 3066 {
3051 base::HistogramTester histogram_tester; 3067 base::HistogramTester histogram_tester;
3052 autofill_manager_->OnFormsSeen(forms, TimeTicks::FromInternalValue(1)); 3068 autofill_manager_->OnFormsSeen(forms, TimeTicks::FromInternalValue(1));
3053 autofill_manager_->OnFormsSeen(second_forms, 3069 autofill_manager_->OnFormsSeen(second_forms,
3054 TimeTicks::FromInternalValue(5)); 3070 TimeTicks::FromInternalValue(5));
3055 autofill_manager_->SubmitForm(second_form, 3071 autofill_manager_->SubmitForm(second_form,
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after
3323 EXPECT_THAT( 3339 EXPECT_THAT(
3324 histogram_tester.GetAllSamples("Autofill.ServerResponseHasDataForForm"), 3340 histogram_tester.GetAllSamples("Autofill.ServerResponseHasDataForForm"),
3325 ElementsAre(Bucket(true, 2))); 3341 ElementsAre(Bucket(true, 2)));
3326 3342
3327 // No RAPPOR metrics are logged in the case there is at least some server data 3343 // No RAPPOR metrics are logged in the case there is at least some server data
3328 // available for all forms. 3344 // available for all forms.
3329 EXPECT_EQ(0, rappor_service_.GetReportsCount()); 3345 EXPECT_EQ(0, rappor_service_.GetReportsCount());
3330 } 3346 }
3331 3347
3332 } // namespace autofill 3348 } // namespace autofill
OLDNEW
« no previous file with comments | « components/autofill/core/browser/autofill_manager_unittest.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698