| OLD | NEW |
| (Empty) |
| 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_samples.h" | |
| 9 #include "base/metrics/statistics_recorder.h" | |
| 10 #include "base/strings/sys_string_conversions.h" | |
| 11 #include "base/test/histogram_tester.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 const char kHistogramName[] = "PasswordGeneration.UserActions"; | |
| 18 | |
| 19 class PasswordGenerationBubbleControllerTest : public CocoaProfileTest { | |
| 20 public: | |
| 21 PasswordGenerationBubbleControllerTest() | |
| 22 : controller_(nil) {} | |
| 23 | |
| 24 static void SetUpTestCase() { | |
| 25 base::StatisticsRecorder::Initialize(); | |
| 26 } | |
| 27 | |
| 28 virtual void SetUp() { | |
| 29 CocoaProfileTest::SetUp(); | |
| 30 | |
| 31 generator_.reset(new autofill::PasswordGenerator(20)); | |
| 32 | |
| 33 SetUpController(); | |
| 34 } | |
| 35 | |
| 36 PasswordGenerationBubbleController* controller() { return controller_; } | |
| 37 | |
| 38 void SetUpController() { | |
| 39 autofill::PasswordForm form; | |
| 40 NSRect frame = [test_window() frame]; | |
| 41 NSPoint point = NSMakePoint(NSMidX(frame), NSMidY(frame)); | |
| 42 | |
| 43 // |controller_| is self deleting. | |
| 44 controller_ = [[PasswordGenerationBubbleController alloc] | |
| 45 initWithWindow:test_window() | |
| 46 anchoredAt:point | |
| 47 renderViewHost:nil | |
| 48 passwordManager:nil | |
| 49 usingGenerator:generator_.get() | |
| 50 forForm:form]; | |
| 51 } | |
| 52 | |
| 53 void CloseController() { | |
| 54 [controller_ close]; | |
| 55 [controller_ windowWillClose:nil]; | |
| 56 controller_ = nil; | |
| 57 } | |
| 58 | |
| 59 protected: | |
| 60 // Weak. | |
| 61 PasswordGenerationBubbleController* controller_; | |
| 62 | |
| 63 scoped_ptr<autofill::PasswordGenerator> generator_; | |
| 64 }; | |
| 65 | |
| 66 TEST_F(PasswordGenerationBubbleControllerTest, Regenerate) { | |
| 67 [controller() showWindow:nil]; | |
| 68 | |
| 69 PasswordGenerationTextField* textfield = controller().textField; | |
| 70 // Grab the starting password value. | |
| 71 NSString* before = [textfield stringValue]; | |
| 72 | |
| 73 // Click on the regenerate icon. | |
| 74 [textfield simulateIconClick]; | |
| 75 | |
| 76 // Make sure that the password has changed. Technically this will fail | |
| 77 // about once every 1e28 times, but not something we really need to worry | |
| 78 // about. | |
| 79 NSString* after = [textfield stringValue]; | |
| 80 EXPECT_FALSE([before isEqualToString:after]); | |
| 81 } | |
| 82 | |
| 83 TEST_F(PasswordGenerationBubbleControllerTest, UMALogging) { | |
| 84 base::HistogramTester histogram_tester; | |
| 85 [controller() showWindow:nil]; | |
| 86 | |
| 87 // Do nothing. | |
| 88 CloseController(); | |
| 89 | |
| 90 histogram_tester.ExpectUniqueSample( | |
| 91 kHistogramName, autofill::password_generation::IGNORE_FEATURE, 1); | |
| 92 | |
| 93 SetUpController(); | |
| 94 | |
| 95 // Pretend like the user changed the password and accepted it. | |
| 96 [controller() controlTextDidChange:nil]; | |
| 97 [controller() fillPassword:nil]; | |
| 98 CloseController(); | |
| 99 | |
| 100 histogram_tester.ExpectBucketCount( | |
| 101 kHistogramName, autofill::password_generation::IGNORE_FEATURE, 1); | |
| 102 histogram_tester.ExpectBucketCount( | |
| 103 kHistogramName, autofill::password_generation::ACCEPT_AFTER_EDITING, 1); | |
| 104 histogram_tester.ExpectTotalCount(kHistogramName, 2); | |
| 105 | |
| 106 SetUpController(); | |
| 107 | |
| 108 // Just accept the password | |
| 109 [controller() fillPassword:nil]; | |
| 110 CloseController(); | |
| 111 | |
| 112 histogram_tester.ExpectBucketCount( | |
| 113 kHistogramName, autofill::password_generation::IGNORE_FEATURE, 1); | |
| 114 histogram_tester.ExpectBucketCount( | |
| 115 kHistogramName, autofill::password_generation::ACCEPT_AFTER_EDITING, 1); | |
| 116 histogram_tester.ExpectBucketCount( | |
| 117 kHistogramName, | |
| 118 autofill::password_generation::ACCEPT_ORIGINAL_PASSWORD, | |
| 119 1); | |
| 120 histogram_tester.ExpectTotalCount(kHistogramName, 3); | |
| 121 } | |
| OLD | NEW |