| 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"; | |
| 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( |
| 38 StatisticsRecorder::FindHistogram(kHistogramName); | 27 new base::HistogramRecorder("PasswordGeneration")); |
| 39 if (histogram) | |
| 40 original_ = histogram->SnapshotSamples(); | |
| 41 | 28 |
| 42 SetUpController(); | 29 SetUpController(); |
| 43 } | 30 } |
| 44 | 31 |
| 45 PasswordGenerationBubbleController* controller() { return controller_; } | 32 PasswordGenerationBubbleController* controller() { return controller_; } |
| 46 | 33 |
| 47 void SetUpController() { | 34 void SetUpController() { |
| 48 content::PasswordForm form; | 35 content::PasswordForm form; |
| 49 NSRect frame = [test_window() frame]; | 36 NSRect frame = [test_window() frame]; |
| 50 NSPoint point = NSMakePoint(NSMidX(frame), NSMidY(frame)); | 37 NSPoint point = NSMakePoint(NSMidX(frame), NSMidY(frame)); |
| 51 | 38 |
| 52 // |controller_| is self deleting. | 39 // |controller_| is self deleting. |
| 53 controller_ = [[PasswordGenerationBubbleController alloc] | 40 controller_ = [[PasswordGenerationBubbleController alloc] |
| 54 initWithWindow:test_window() | 41 initWithWindow:test_window() |
| 55 anchoredAt:point | 42 anchoredAt:point |
| 56 renderViewHost:nil | 43 renderViewHost:nil |
| 57 passwordManager:nil | 44 passwordManager:nil |
| 58 usingGenerator:generator_.get() | 45 usingGenerator:generator_.get() |
| 59 forForm:form]; | 46 forForm:form]; |
| 60 } | 47 } |
| 61 | 48 |
| 62 void CloseController() { | 49 void CloseController() { |
| 63 [controller_ close]; | 50 [controller_ close]; |
| 64 [controller_ windowWillClose:nil]; | 51 [controller_ windowWillClose:nil]; |
| 65 controller_ = nil; | 52 controller_ = nil; |
| 66 } | 53 } |
| 67 | 54 |
| 68 HistogramSamples* GetHistogramSamples() { | 55 base::HistogramSamples* GetHistogramSamples() { |
| 69 HistogramBase* histogram = | 56 return histogram_recorder_->GetHistogramSamples("PasswordGeneration." |
| 70 StatisticsRecorder::FindHistogram(kHistogramName); | 57 "UserActions"); |
| 71 if (histogram) { | |
| 72 current_ = histogram->SnapshotSamples(); | |
| 73 if (original_.get()) | |
| 74 current_->Subtract(*original_.get()); | |
| 75 } | |
| 76 return current_.get(); | |
| 77 } | 58 } |
| 78 | 59 |
| 79 protected: | 60 protected: |
| 80 // Weak. | 61 // Weak. |
| 81 PasswordGenerationBubbleController* controller_; | 62 PasswordGenerationBubbleController* controller_; |
| 82 | 63 |
| 83 // Used to determine the histogram changes made just for this specific | 64 scoped_ptr<base::HistogramRecorder> histogram_recorder_; |
| 84 // test run. | |
| 85 scoped_ptr<HistogramSamples> original_; | |
| 86 | |
| 87 scoped_ptr<HistogramSamples> current_; | |
| 88 | 65 |
| 89 scoped_ptr<autofill::PasswordGenerator> generator_; | 66 scoped_ptr<autofill::PasswordGenerator> generator_; |
| 90 }; | 67 }; |
| 91 | 68 |
| 92 TEST_F(PasswordGenerationBubbleControllerTest, Regenerate) { | 69 TEST_F(PasswordGenerationBubbleControllerTest, Regenerate) { |
| 93 [controller() showWindow:nil]; | 70 [controller() showWindow:nil]; |
| 94 | 71 |
| 95 PasswordGenerationTextField* textfield = controller().textField; | 72 PasswordGenerationTextField* textfield = controller().textField; |
| 96 // Grab the starting password value. | 73 // Grab the starting password value. |
| 97 NSString* before = [textfield stringValue]; | 74 NSString* before = [textfield stringValue]; |
| 98 | 75 |
| 99 // Click on the regenerate icon. | 76 // Click on the regenerate icon. |
| 100 [textfield simulateIconClick]; | 77 [textfield simulateIconClick]; |
| 101 | 78 |
| 102 // Make sure that the password has changed. Technically this will fail | 79 // 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 | 80 // about once every 1e28 times, but not something we really need to worry |
| 104 // about. | 81 // about. |
| 105 NSString* after = [textfield stringValue]; | 82 NSString* after = [textfield stringValue]; |
| 106 EXPECT_FALSE([before isEqualToString:after]); | 83 EXPECT_FALSE([before isEqualToString:after]); |
| 107 } | 84 } |
| 108 | 85 |
| 109 TEST_F(PasswordGenerationBubbleControllerTest, UMALogging) { | 86 TEST_F(PasswordGenerationBubbleControllerTest, UMALogging) { |
| 110 [controller() showWindow:nil]; | 87 [controller() showWindow:nil]; |
| 111 | 88 |
| 112 // Do nothing. | 89 // Do nothing. |
| 113 CloseController(); | 90 CloseController(); |
| 114 | 91 |
| 115 HistogramSamples* samples = GetHistogramSamples(); | 92 base::HistogramSamples* samples = GetHistogramSamples(); |
| 116 EXPECT_EQ( | 93 EXPECT_EQ( |
| 117 1, | 94 1, |
| 118 samples->GetCount(autofill::password_generation::IGNORE_FEATURE)); | 95 samples->GetCount(autofill::password_generation::IGNORE_FEATURE)); |
| 119 EXPECT_EQ( | 96 EXPECT_EQ( |
| 120 0, | 97 0, |
| 121 samples->GetCount(autofill::password_generation::ACCEPT_AFTER_EDITING)); | 98 samples->GetCount(autofill::password_generation::ACCEPT_AFTER_EDITING)); |
| 122 EXPECT_EQ( | 99 EXPECT_EQ( |
| 123 0, | 100 0, |
| 124 samples->GetCount( | 101 samples->GetCount( |
| 125 autofill::password_generation::ACCEPT_ORIGINAL_PASSWORD)); | 102 autofill::password_generation::ACCEPT_ORIGINAL_PASSWORD)); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 155 samples->GetCount(autofill::password_generation::IGNORE_FEATURE)); | 132 samples->GetCount(autofill::password_generation::IGNORE_FEATURE)); |
| 156 EXPECT_EQ( | 133 EXPECT_EQ( |
| 157 1, | 134 1, |
| 158 samples->GetCount(autofill::password_generation::ACCEPT_AFTER_EDITING)); | 135 samples->GetCount(autofill::password_generation::ACCEPT_AFTER_EDITING)); |
| 159 EXPECT_EQ( | 136 EXPECT_EQ( |
| 160 1, | 137 1, |
| 161 samples->GetCount( | 138 samples->GetCount( |
| 162 autofill::password_generation::ACCEPT_ORIGINAL_PASSWORD)); | 139 autofill::password_generation::ACCEPT_ORIGINAL_PASSWORD)); |
| 163 | 140 |
| 164 } | 141 } |
| OLD | NEW |