OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 #import "chrome/browser/ui/cocoa/browser/password_generation_bubble_controller.h
" | 5 #import "chrome/browser/ui/cocoa/browser/password_generation_bubble_controller.h
" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/metrics/histogram.h" | |
9 #include "base/metrics/histogram_samples.h" | 8 #include "base/metrics/histogram_samples.h" |
10 #include "base/metrics/statistics_recorder.h" | 9 #include "base/metrics/statistics_recorder.h" |
11 #include "base/strings/sys_string_conversions.h" | 10 #include "base/strings/sys_string_conversions.h" |
| 11 #include "base/test/statistics_delta_reader.h" |
12 #include "chrome/browser/ui/cocoa/cocoa_profile_test.h" | 12 #include "chrome/browser/ui/cocoa/cocoa_profile_test.h" |
13 #include "components/autofill/core/browser/password_generator.h" | 13 #include "components/autofill/core/browser/password_generator.h" |
14 #include "components/autofill/core/common/password_form.h" | 14 #include "components/autofill/core/common/password_form.h" |
15 #include "testing/gtest_mac.h" | 15 #include "testing/gtest_mac.h" |
16 | 16 |
17 using base::HistogramBase; | |
18 using base::HistogramSamples; | |
19 using base::StatisticsRecorder; | |
20 | |
21 const char kHistogramName[] = "PasswordGeneration.UserActions"; | 17 const char kHistogramName[] = "PasswordGeneration.UserActions"; |
22 | 18 |
23 class PasswordGenerationBubbleControllerTest : public CocoaProfileTest { | 19 class PasswordGenerationBubbleControllerTest : public CocoaProfileTest { |
24 public: | 20 public: |
25 PasswordGenerationBubbleControllerTest() | 21 PasswordGenerationBubbleControllerTest() |
26 : controller_(nil) {} | 22 : controller_(nil) {} |
27 | 23 |
28 static void SetUpTestCase() { | 24 static void SetUpTestCase() { |
29 StatisticsRecorder::Initialize(); | 25 base::StatisticsRecorder::Initialize(); |
30 } | 26 } |
31 | 27 |
32 virtual void SetUp() { | 28 virtual void SetUp() { |
33 CocoaProfileTest::SetUp(); | 29 CocoaProfileTest::SetUp(); |
34 | 30 |
35 generator_.reset(new autofill::PasswordGenerator(20)); | 31 generator_.reset(new autofill::PasswordGenerator(20)); |
36 | 32 |
37 HistogramBase* histogram = | |
38 StatisticsRecorder::FindHistogram(kHistogramName); | |
39 if (histogram) | |
40 original_ = histogram->SnapshotSamples(); | |
41 | |
42 SetUpController(); | 33 SetUpController(); |
43 } | 34 } |
44 | 35 |
45 PasswordGenerationBubbleController* controller() { return controller_; } | 36 PasswordGenerationBubbleController* controller() { return controller_; } |
46 | 37 |
47 void SetUpController() { | 38 void SetUpController() { |
48 autofill::PasswordForm form; | 39 autofill::PasswordForm form; |
49 NSRect frame = [test_window() frame]; | 40 NSRect frame = [test_window() frame]; |
50 NSPoint point = NSMakePoint(NSMidX(frame), NSMidY(frame)); | 41 NSPoint point = NSMakePoint(NSMidX(frame), NSMidY(frame)); |
51 | 42 |
52 // |controller_| is self deleting. | 43 // |controller_| is self deleting. |
53 controller_ = [[PasswordGenerationBubbleController alloc] | 44 controller_ = [[PasswordGenerationBubbleController alloc] |
54 initWithWindow:test_window() | 45 initWithWindow:test_window() |
55 anchoredAt:point | 46 anchoredAt:point |
56 renderViewHost:nil | 47 renderViewHost:nil |
57 passwordManager:nil | 48 passwordManager:nil |
58 usingGenerator:generator_.get() | 49 usingGenerator:generator_.get() |
59 forForm:form]; | 50 forForm:form]; |
60 } | 51 } |
61 | 52 |
62 void CloseController() { | 53 void CloseController() { |
63 [controller_ close]; | 54 [controller_ close]; |
64 [controller_ windowWillClose:nil]; | 55 [controller_ windowWillClose:nil]; |
65 controller_ = nil; | 56 controller_ = nil; |
66 } | 57 } |
67 | 58 |
68 HistogramSamples* GetHistogramSamples() { | |
69 HistogramBase* histogram = | |
70 StatisticsRecorder::FindHistogram(kHistogramName); | |
71 if (histogram) { | |
72 current_ = histogram->SnapshotSamples(); | |
73 if (original_.get()) | |
74 current_->Subtract(*original_.get()); | |
75 } | |
76 return current_.get(); | |
77 } | |
78 | |
79 protected: | 59 protected: |
80 // Weak. | 60 // Weak. |
81 PasswordGenerationBubbleController* controller_; | 61 PasswordGenerationBubbleController* controller_; |
82 | 62 |
83 // Used to determine the histogram changes made just for this specific | |
84 // test run. | |
85 scoped_ptr<HistogramSamples> original_; | |
86 | |
87 scoped_ptr<HistogramSamples> current_; | |
88 | |
89 scoped_ptr<autofill::PasswordGenerator> generator_; | 63 scoped_ptr<autofill::PasswordGenerator> generator_; |
90 }; | 64 }; |
91 | 65 |
92 TEST_F(PasswordGenerationBubbleControllerTest, Regenerate) { | 66 TEST_F(PasswordGenerationBubbleControllerTest, Regenerate) { |
93 [controller() showWindow:nil]; | 67 [controller() showWindow:nil]; |
94 | 68 |
95 PasswordGenerationTextField* textfield = controller().textField; | 69 PasswordGenerationTextField* textfield = controller().textField; |
96 // Grab the starting password value. | 70 // Grab the starting password value. |
97 NSString* before = [textfield stringValue]; | 71 NSString* before = [textfield stringValue]; |
98 | 72 |
99 // Click on the regenerate icon. | 73 // Click on the regenerate icon. |
100 [textfield simulateIconClick]; | 74 [textfield simulateIconClick]; |
101 | 75 |
102 // Make sure that the password has changed. Technically this will fail | 76 // Make sure that the password has changed. Technically this will fail |
103 // about once every 1e28 times, but not something we really need to worry | 77 // about once every 1e28 times, but not something we really need to worry |
104 // about. | 78 // about. |
105 NSString* after = [textfield stringValue]; | 79 NSString* after = [textfield stringValue]; |
106 EXPECT_FALSE([before isEqualToString:after]); | 80 EXPECT_FALSE([before isEqualToString:after]); |
107 } | 81 } |
108 | 82 |
109 TEST_F(PasswordGenerationBubbleControllerTest, UMALogging) { | 83 TEST_F(PasswordGenerationBubbleControllerTest, UMALogging) { |
| 84 base::StatisticsDeltaReader statistics_delta_reader; |
110 [controller() showWindow:nil]; | 85 [controller() showWindow:nil]; |
111 | 86 |
112 // Do nothing. | 87 // Do nothing. |
113 CloseController(); | 88 CloseController(); |
114 | 89 |
115 HistogramSamples* samples = GetHistogramSamples(); | 90 scoped_ptr<base::HistogramSamples> samples( |
| 91 statistics_delta_reader.GetHistogramSamplesSinceCreation(kHistogramName)); |
116 EXPECT_EQ( | 92 EXPECT_EQ( |
117 1, | 93 1, |
118 samples->GetCount(autofill::password_generation::IGNORE_FEATURE)); | 94 samples->GetCount(autofill::password_generation::IGNORE_FEATURE)); |
119 EXPECT_EQ( | 95 EXPECT_EQ( |
120 0, | 96 0, |
121 samples->GetCount(autofill::password_generation::ACCEPT_AFTER_EDITING)); | 97 samples->GetCount(autofill::password_generation::ACCEPT_AFTER_EDITING)); |
122 EXPECT_EQ( | 98 EXPECT_EQ( |
123 0, | 99 0, |
124 samples->GetCount( | 100 samples->GetCount( |
125 autofill::password_generation::ACCEPT_ORIGINAL_PASSWORD)); | 101 autofill::password_generation::ACCEPT_ORIGINAL_PASSWORD)); |
126 | 102 |
127 SetUpController(); | 103 SetUpController(); |
128 | 104 |
129 // Pretend like the user changed the password and accepted it. | 105 // Pretend like the user changed the password and accepted it. |
130 [controller() controlTextDidChange:nil]; | 106 [controller() controlTextDidChange:nil]; |
131 [controller() fillPassword:nil]; | 107 [controller() fillPassword:nil]; |
132 CloseController(); | 108 CloseController(); |
133 | 109 |
134 samples = GetHistogramSamples(); | 110 samples = |
| 111 statistics_delta_reader.GetHistogramSamplesSinceCreation(kHistogramName); |
135 EXPECT_EQ( | 112 EXPECT_EQ( |
136 1, | 113 1, |
137 samples->GetCount(autofill::password_generation::IGNORE_FEATURE)); | 114 samples->GetCount(autofill::password_generation::IGNORE_FEATURE)); |
138 EXPECT_EQ( | 115 EXPECT_EQ( |
139 1, | 116 1, |
140 samples->GetCount(autofill::password_generation::ACCEPT_AFTER_EDITING)); | 117 samples->GetCount(autofill::password_generation::ACCEPT_AFTER_EDITING)); |
141 EXPECT_EQ( | 118 EXPECT_EQ( |
142 0, | 119 0, |
143 samples->GetCount( | 120 samples->GetCount( |
144 autofill::password_generation::ACCEPT_ORIGINAL_PASSWORD)); | 121 autofill::password_generation::ACCEPT_ORIGINAL_PASSWORD)); |
145 | 122 |
146 SetUpController(); | 123 SetUpController(); |
147 | 124 |
148 // Just accept the password | 125 // Just accept the password |
149 [controller() fillPassword:nil]; | 126 [controller() fillPassword:nil]; |
150 CloseController(); | 127 CloseController(); |
151 | 128 |
152 samples = GetHistogramSamples(); | 129 samples = |
| 130 statistics_delta_reader.GetHistogramSamplesSinceCreation(kHistogramName); |
153 EXPECT_EQ( | 131 EXPECT_EQ( |
154 1, | 132 1, |
155 samples->GetCount(autofill::password_generation::IGNORE_FEATURE)); | 133 samples->GetCount(autofill::password_generation::IGNORE_FEATURE)); |
156 EXPECT_EQ( | 134 EXPECT_EQ( |
157 1, | 135 1, |
158 samples->GetCount(autofill::password_generation::ACCEPT_AFTER_EDITING)); | 136 samples->GetCount(autofill::password_generation::ACCEPT_AFTER_EDITING)); |
159 EXPECT_EQ( | 137 EXPECT_EQ( |
160 1, | 138 1, |
161 samples->GetCount( | 139 samples->GetCount( |
162 autofill::password_generation::ACCEPT_ORIGINAL_PASSWORD)); | 140 autofill::password_generation::ACCEPT_ORIGINAL_PASSWORD)); |
163 | |
164 } | 141 } |
OLD | NEW |