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.
5 #import "chrome/browser/ui/cocoa/browser/password_generation_bubble_controller.h"
7 #include "base/logging.h"
8 #include "base/metrics/histogram.h"
9 #include "base/metrics/histogram_samples.h"
10 #include "base/metrics/statistics_recorder.h"
11 #include "base/strings/sys_string_conversions.h"
12 #include "chrome/browser/ui/cocoa/cocoa_profile_test.h"
13 #include "components/autofill/core/browser/password_generator.h"
14 #include "components/autofill/core/common/password_form.h"
15 #include "testing/gtest_mac.h"
17 using base::HistogramBase;
18 using base::HistogramSamples;
19 using base::StatisticsRecorder;
21 const char kHistogramName[] = "PasswordGeneration.UserActions";
23 class PasswordGenerationBubbleControllerTest : public CocoaProfileTest {
25 PasswordGenerationBubbleControllerTest()
28 static void SetUpTestCase() {
29 StatisticsRecorder::Initialize();
32 virtual void SetUp() {
33 CocoaProfileTest::SetUp();
35 generator_.reset(new autofill::PasswordGenerator(20));
37 HistogramBase* histogram =
38 StatisticsRecorder::FindHistogram(kHistogramName);
40 original_ = histogram->SnapshotSamples();
45 PasswordGenerationBubbleController* controller() { return controller_; }
47 void SetUpController() {
48 autofill::PasswordForm form;
49 NSRect frame = [test_window() frame];
50 NSPoint point = NSMakePoint(NSMidX(frame), NSMidY(frame));
52 // |controller_| is self deleting.
53 controller_ = [[PasswordGenerationBubbleController alloc]
54 initWithWindow:test_window()
58 usingGenerator:generator_.get()
62 void CloseController() {
64 [controller_ windowWillClose:nil];
68 HistogramSamples* GetHistogramSamples() {
69 HistogramBase* histogram =
70 StatisticsRecorder::FindHistogram(kHistogramName);
72 current_ = histogram->SnapshotSamples();
74 current_->Subtract(*original_.get());
76 return current_.get();
81 PasswordGenerationBubbleController* controller_;
83 // Used to determine the histogram changes made just for this specific
85 scoped_ptr<HistogramSamples> original_;
87 scoped_ptr<HistogramSamples> current_;
89 scoped_ptr<autofill::PasswordGenerator> generator_;
92 TEST_F(PasswordGenerationBubbleControllerTest, Regenerate) {
93 [controller() showWindow:nil];
95 PasswordGenerationTextField* textfield = controller().textField;
96 // Grab the starting password value.
97 NSString* before = [textfield stringValue];
99 // Click on the regenerate icon.
100 [textfield simulateIconClick];
102 // 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
105 NSString* after = [textfield stringValue];
106 EXPECT_FALSE([before isEqualToString:after]);
109 TEST_F(PasswordGenerationBubbleControllerTest, UMALogging) {
110 [controller() showWindow:nil];
115 HistogramSamples* samples = GetHistogramSamples();
118 samples->GetCount(autofill::password_generation::IGNORE_FEATURE));
121 samples->GetCount(autofill::password_generation::ACCEPT_AFTER_EDITING));
125 autofill::password_generation::ACCEPT_ORIGINAL_PASSWORD));
129 // Pretend like the user changed the password and accepted it.
130 [controller() controlTextDidChange:nil];
131 [controller() fillPassword:nil];
134 samples = GetHistogramSamples();
137 samples->GetCount(autofill::password_generation::IGNORE_FEATURE));
140 samples->GetCount(autofill::password_generation::ACCEPT_AFTER_EDITING));
144 autofill::password_generation::ACCEPT_ORIGINAL_PASSWORD));
148 // Just accept the password
149 [controller() fillPassword:nil];
152 samples = GetHistogramSamples();
155 samples->GetCount(autofill::password_generation::IGNORE_FEATURE));
158 samples->GetCount(autofill::password_generation::ACCEPT_AFTER_EDITING));
162 autofill::password_generation::ACCEPT_ORIGINAL_PASSWORD));