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" | |
11 #include "base/strings/sys_string_conversions.h" | 9 #include "base/strings/sys_string_conversions.h" |
10 #include "base/test/histogram_recorder.h" | |
12 #include "chrome/browser/ui/cocoa/cocoa_profile_test.h" | 11 #include "chrome/browser/ui/cocoa/cocoa_profile_test.h" |
13 #include "components/autofill/core/browser/password_generator.h" | 12 #include "components/autofill/core/browser/password_generator.h" |
14 #include "content/public/common/password_form.h" | 13 #include "content/public/common/password_form.h" |
15 #include "testing/gtest_mac.h" | 14 #include "testing/gtest_mac.h" |
16 | 15 |
17 using base::HistogramBase; | |
18 using base::HistogramSamples; | |
19 using base::StatisticsRecorder; | |
20 | |
21 const char kHistogramName[] = "PasswordGeneration.UserActions"; | |
Nico
2013/07/15 17:38:27
Keep this?
lpromero
2013/07/17 13:40:48
Done.
| |
22 | |
23 class PasswordGenerationBubbleControllerTest : public CocoaProfileTest { | 16 class PasswordGenerationBubbleControllerTest : public CocoaProfileTest { |
24 public: | 17 public: |
25 PasswordGenerationBubbleControllerTest() | 18 PasswordGenerationBubbleControllerTest() |
26 : controller_(nil) {} | 19 : controller_(nil) {} |
27 | 20 |
28 static void SetUpTestCase() { | |
29 StatisticsRecorder::Initialize(); | |
30 } | |
31 | |
32 virtual void SetUp() { | 21 virtual void SetUp() { |
33 CocoaProfileTest::SetUp(); | 22 CocoaProfileTest::SetUp(); |
34 | 23 |
35 generator_.reset(new autofill::PasswordGenerator(20)); | 24 generator_.reset(new autofill::PasswordGenerator(20)); |
36 | 25 |
37 HistogramBase* histogram = | 26 histogram_recorder_.reset(new base::HistogramRecorder()); |
38 StatisticsRecorder::FindHistogram(kHistogramName); | |
39 if (histogram) | |
40 original_ = histogram->SnapshotSamples(); | |
41 | 27 |
42 SetUpController(); | 28 SetUpController(); |
43 } | 29 } |
44 | 30 |
45 PasswordGenerationBubbleController* controller() { return controller_; } | 31 PasswordGenerationBubbleController* controller() { return controller_; } |
46 | 32 |
47 void SetUpController() { | 33 void SetUpController() { |
48 content::PasswordForm form; | 34 content::PasswordForm form; |
49 NSRect frame = [test_window() frame]; | 35 NSRect frame = [test_window() frame]; |
50 NSPoint point = NSMakePoint(NSMidX(frame), NSMidY(frame)); | 36 NSPoint point = NSMakePoint(NSMidX(frame), NSMidY(frame)); |
51 | 37 |
52 // |controller_| is self deleting. | 38 // |controller_| is self deleting. |
53 controller_ = [[PasswordGenerationBubbleController alloc] | 39 controller_ = [[PasswordGenerationBubbleController alloc] |
54 initWithWindow:test_window() | 40 initWithWindow:test_window() |
55 anchoredAt:point | 41 anchoredAt:point |
56 renderViewHost:nil | 42 renderViewHost:nil |
57 passwordManager:nil | 43 passwordManager:nil |
58 usingGenerator:generator_.get() | 44 usingGenerator:generator_.get() |
59 forForm:form]; | 45 forForm:form]; |
60 } | 46 } |
61 | 47 |
62 void CloseController() { | 48 void CloseController() { |
63 [controller_ close]; | 49 [controller_ close]; |
64 [controller_ windowWillClose:nil]; | 50 [controller_ windowWillClose:nil]; |
65 controller_ = nil; | 51 controller_ = nil; |
66 } | 52 } |
67 | 53 |
68 HistogramSamples* GetHistogramSamples() { | 54 scoped_ptr<base::HistogramSamples> GetHistogramSamples() { |
69 HistogramBase* histogram = | 55 return histogram_recorder_->GetHistogramSamplesSinceCreation( |
70 StatisticsRecorder::FindHistogram(kHistogramName); | 56 "PasswordGeneration.UserActions").Pass(); |
71 if (histogram) { | |
72 current_ = histogram->SnapshotSamples(); | |
73 if (original_.get()) | |
74 current_->Subtract(*original_.get()); | |
75 } | |
76 return current_.get(); | |
77 } | 57 } |
78 | 58 |
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 | 63 scoped_ptr<base::HistogramRecorder> histogram_recorder_; |
84 // test run. | |
85 scoped_ptr<HistogramSamples> original_; | |
86 | |
87 scoped_ptr<HistogramSamples> current_; | |
88 | 64 |
89 scoped_ptr<autofill::PasswordGenerator> generator_; | 65 scoped_ptr<autofill::PasswordGenerator> generator_; |
90 }; | 66 }; |
91 | 67 |
92 TEST_F(PasswordGenerationBubbleControllerTest, Regenerate) { | 68 TEST_F(PasswordGenerationBubbleControllerTest, Regenerate) { |
93 [controller() showWindow:nil]; | 69 [controller() showWindow:nil]; |
94 | 70 |
95 PasswordGenerationTextField* textfield = controller().textField; | 71 PasswordGenerationTextField* textfield = controller().textField; |
96 // Grab the starting password value. | 72 // Grab the starting password value. |
97 NSString* before = [textfield stringValue]; | 73 NSString* before = [textfield stringValue]; |
98 | 74 |
99 // Click on the regenerate icon. | 75 // Click on the regenerate icon. |
100 [textfield simulateIconClick]; | 76 [textfield simulateIconClick]; |
101 | 77 |
102 // Make sure that the password has changed. Technically this will fail | 78 // 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 | 79 // about once every 1e28 times, but not something we really need to worry |
104 // about. | 80 // about. |
105 NSString* after = [textfield stringValue]; | 81 NSString* after = [textfield stringValue]; |
106 EXPECT_FALSE([before isEqualToString:after]); | 82 EXPECT_FALSE([before isEqualToString:after]); |
107 } | 83 } |
108 | 84 |
109 TEST_F(PasswordGenerationBubbleControllerTest, UMALogging) { | 85 TEST_F(PasswordGenerationBubbleControllerTest, UMALogging) { |
110 [controller() showWindow:nil]; | 86 [controller() showWindow:nil]; |
111 | 87 |
112 // Do nothing. | 88 // Do nothing. |
113 CloseController(); | 89 CloseController(); |
114 | 90 |
115 HistogramSamples* samples = GetHistogramSamples(); | 91 scoped_ptr<base::HistogramSamples> samples(GetHistogramSamples()); |
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)); |
(...skipping 29 matching lines...) Expand all Loading... | |
155 samples->GetCount(autofill::password_generation::IGNORE_FEATURE)); | 131 samples->GetCount(autofill::password_generation::IGNORE_FEATURE)); |
156 EXPECT_EQ( | 132 EXPECT_EQ( |
157 1, | 133 1, |
158 samples->GetCount(autofill::password_generation::ACCEPT_AFTER_EDITING)); | 134 samples->GetCount(autofill::password_generation::ACCEPT_AFTER_EDITING)); |
159 EXPECT_EQ( | 135 EXPECT_EQ( |
160 1, | 136 1, |
161 samples->GetCount( | 137 samples->GetCount( |
162 autofill::password_generation::ACCEPT_ORIGINAL_PASSWORD)); | 138 autofill::password_generation::ACCEPT_ORIGINAL_PASSWORD)); |
163 | 139 |
164 } | 140 } |
OLD | NEW |