- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / browser / password_generation_bubble_controller_unittest.mm
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 #import "chrome/browser/ui/cocoa/browser/password_generation_bubble_controller.h"
6
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"
16
17 using base::HistogramBase;
18 using base::HistogramSamples;
19 using base::StatisticsRecorder;
20
21 const char kHistogramName[] = "PasswordGeneration.UserActions";
22
23 class PasswordGenerationBubbleControllerTest : public CocoaProfileTest {
24  public:
25   PasswordGenerationBubbleControllerTest()
26       : controller_(nil) {}
27
28   static void SetUpTestCase() {
29     StatisticsRecorder::Initialize();
30   }
31
32   virtual void SetUp() {
33     CocoaProfileTest::SetUp();
34
35     generator_.reset(new autofill::PasswordGenerator(20));
36
37     HistogramBase* histogram =
38         StatisticsRecorder::FindHistogram(kHistogramName);
39     if (histogram)
40       original_ = histogram->SnapshotSamples();
41
42     SetUpController();
43   }
44
45   PasswordGenerationBubbleController* controller() { return controller_; }
46
47   void SetUpController() {
48     autofill::PasswordForm form;
49     NSRect frame = [test_window() frame];
50     NSPoint point = NSMakePoint(NSMidX(frame), NSMidY(frame));
51
52     // |controller_| is self deleting.
53     controller_ = [[PasswordGenerationBubbleController alloc]
54                     initWithWindow:test_window()
55                         anchoredAt:point
56                     renderViewHost:nil
57                    passwordManager:nil
58                     usingGenerator:generator_.get()
59                            forForm:form];
60   }
61
62   void CloseController() {
63     [controller_ close];
64     [controller_ windowWillClose:nil];
65     controller_ = nil;
66   }
67
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:
80   // Weak.
81   PasswordGenerationBubbleController* controller_;
82
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_;
90 };
91
92 TEST_F(PasswordGenerationBubbleControllerTest, Regenerate) {
93   [controller() showWindow:nil];
94
95   PasswordGenerationTextField* textfield = controller().textField;
96   // Grab the starting password value.
97   NSString* before = [textfield stringValue];
98
99   // Click on the regenerate icon.
100   [textfield simulateIconClick];
101
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
104   // about.
105   NSString* after = [textfield stringValue];
106   EXPECT_FALSE([before isEqualToString:after]);
107 }
108
109 TEST_F(PasswordGenerationBubbleControllerTest, UMALogging) {
110   [controller() showWindow:nil];
111
112   // Do nothing.
113   CloseController();
114
115   HistogramSamples* samples = GetHistogramSamples();
116   EXPECT_EQ(
117       1,
118       samples->GetCount(autofill::password_generation::IGNORE_FEATURE));
119   EXPECT_EQ(
120       0,
121       samples->GetCount(autofill::password_generation::ACCEPT_AFTER_EDITING));
122   EXPECT_EQ(
123       0,
124       samples->GetCount(
125           autofill::password_generation::ACCEPT_ORIGINAL_PASSWORD));
126
127   SetUpController();
128
129   // Pretend like the user changed the password and accepted it.
130   [controller() controlTextDidChange:nil];
131   [controller() fillPassword:nil];
132   CloseController();
133
134   samples = GetHistogramSamples();
135   EXPECT_EQ(
136       1,
137       samples->GetCount(autofill::password_generation::IGNORE_FEATURE));
138   EXPECT_EQ(
139       1,
140       samples->GetCount(autofill::password_generation::ACCEPT_AFTER_EDITING));
141   EXPECT_EQ(
142       0,
143       samples->GetCount(
144           autofill::password_generation::ACCEPT_ORIGINAL_PASSWORD));
145
146   SetUpController();
147
148   // Just accept the password
149   [controller() fillPassword:nil];
150   CloseController();
151
152   samples = GetHistogramSamples();
153   EXPECT_EQ(
154       1,
155       samples->GetCount(autofill::password_generation::IGNORE_FEATURE));
156   EXPECT_EQ(
157       1,
158       samples->GetCount(autofill::password_generation::ACCEPT_AFTER_EDITING));
159   EXPECT_EQ(
160       1,
161       samples->GetCount(
162           autofill::password_generation::ACCEPT_ORIGINAL_PASSWORD));
163
164 }