Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011 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 #include "base/basictypes.h" | |
| 6 #include "base/logging.h" | |
| 7 #include "base/metrics/histogram.h" | |
| 8 #include "chrome/browser/chromeos/login/enterprise_enrollment_screen.h" | |
| 9 #include "chrome/browser/chromeos/login/wizard_controller.h" | |
| 10 #include "chrome/browser/chromeos/login/wizard_in_process_browser_test.h" | |
| 11 #include "chrome/browser/policy/cloud_policy_subsystem.h" | |
| 12 #include "chrome/browser/policy/enterprise_metrics.h" | |
| 13 #include "chrome/common/net/gaia/gaia_constants.h" | |
| 14 #include "chrome/common/net/gaia/google_service_auth_error.h" | |
| 15 #include "content/common/test_url_fetcher_factory.h" | |
| 16 #include "testing/gtest/include/gtest/gtest.h" | |
| 17 | |
| 18 namespace chromeos { | |
| 19 | |
| 20 namespace { | |
| 21 | |
| 22 void assert_handler(const std::string& str) { | |
| 23 LOG(INFO) << "Previous failure was expected, ignoring."; | |
| 24 } | |
| 25 | |
| 26 } // namespace | |
| 27 | |
| 28 class EnterpriseMetricsEnrollmentTest : public WizardInProcessBrowserTest { | |
| 29 protected: | |
| 30 EnterpriseMetricsEnrollmentTest() | |
| 31 : WizardInProcessBrowserTest( | |
| 32 WizardController::kEnterpriseEnrollmentScreenName) {} | |
| 33 | |
| 34 virtual void SetUpOnMainThread() OVERRIDE { | |
| 35 WizardInProcessBrowserTest::SetUpOnMainThread(); | |
| 36 | |
| 37 ASSERT_TRUE(controller() != NULL); | |
| 38 | |
| 39 // Use mock URLFetchers. | |
| 40 URLFetcher::set_factory(&factory_); | |
| 41 | |
| 42 screen_ = controller()->GetEnterpriseEnrollmentScreen(); | |
| 43 | |
| 44 ASSERT_TRUE(screen_ != NULL); | |
| 45 ASSERT_EQ(controller()->current_screen(), screen_); | |
| 46 } | |
| 47 | |
| 48 virtual void CleanUpOnMainThread() OVERRIDE { | |
| 49 WizardInProcessBrowserTest::CleanUpOnMainThread(); | |
| 50 | |
| 51 // Check that no other counters were sampled. | |
| 52 base::Histogram::SampleSet samples; | |
| 53 GetSampleSet(&samples); | |
| 54 EXPECT_EQ(1, samples.TotalCount()); | |
| 55 } | |
| 56 | |
| 57 // This is used to let expected NOTREACHED paths go on instead of making | |
| 58 // the test fail. It will also let CHECKs and DCHECKs pass, but that should be | |
| 59 // tested elsewhere. | |
| 60 // The reason is that some code paths are not expected, but if they are | |
| 61 // executed we want to know about it, so there's a UMA metric for that. | |
| 62 // However, the NOTREACHED would make the test fail, so we disable it. | |
| 63 // Other unit tests should check the same code with the NOTREACHED enabled, | |
|
gfeher
2011/07/19 13:45:33
I really like this expanded explanation!
Just a ni
Joao da Silva
2011/07/19 15:19:39
Done.
gfeher
2011/07/20 11:56:22
I wanted to say:
"There should be other unit tests
| |
| 64 // but here we are just interested in the UMA samples. | |
| 65 void ExpectNOTREACHED() { | |
| 66 logging::SetLogAssertHandler(assert_handler); | |
| 67 } | |
| 68 | |
| 69 void ExpectSample(int sample) { | |
| 70 ASSERT_GE(sample, 0); | |
| 71 ASSERT_LT(sample, policy::kMetricEnrollmentSize); | |
| 72 | |
| 73 base::Histogram::SampleSet samples; | |
| 74 GetSampleSet(&samples); | |
| 75 EXPECT_EQ(1, samples.counts(sample)); | |
| 76 } | |
| 77 | |
| 78 EnterpriseEnrollmentScreen* screen_; | |
| 79 | |
| 80 private: | |
| 81 void GetSampleSet(base::Histogram::SampleSet* set) { | |
| 82 base::Histogram* histogram = NULL; | |
| 83 EXPECT_TRUE(base::StatisticsRecorder::FindHistogram( | |
| 84 policy::kMetricEnrollment, &histogram)); | |
| 85 ASSERT_TRUE(histogram != NULL); | |
| 86 histogram->SnapshotSample(set); | |
| 87 } | |
| 88 | |
| 89 TestURLFetcherFactory factory_; | |
| 90 | |
| 91 DISALLOW_COPY_AND_ASSIGN(EnterpriseMetricsEnrollmentTest); | |
| 92 }; | |
| 93 | |
| 94 IN_PROC_BROWSER_TEST_F(EnterpriseMetricsEnrollmentTest, EnrollmentStart) { | |
| 95 std::string empty; | |
| 96 screen_->Authenticate(empty, empty, empty, empty); | |
|
gfeher
2011/07/19 13:45:33
Why not Authenticate("", "", "", "")?
Joao da Silva
2011/07/19 15:19:39
Done.
| |
| 97 ExpectSample(policy::kMetricEnrollmentStarted); | |
| 98 } | |
| 99 | |
| 100 IN_PROC_BROWSER_TEST_F(EnterpriseMetricsEnrollmentTest, EnrollmentCancelled) { | |
| 101 screen_->CancelEnrollment(); | |
| 102 ExpectSample(policy::kMetricEnrollmentCancelled); | |
| 103 } | |
| 104 | |
| 105 IN_PROC_BROWSER_TEST_F(EnterpriseMetricsEnrollmentTest, AuthTokenWrongService) { | |
| 106 ExpectNOTREACHED(); | |
| 107 screen_->OnIssueAuthTokenSuccess(std::string(), std::string()); | |
| 108 ExpectSample(policy::kMetricEnrollmentOtherFailed); | |
| 109 } | |
| 110 | |
| 111 IN_PROC_BROWSER_TEST_F(EnterpriseMetricsEnrollmentTest, | |
| 112 AuthTokenCloudPolicyNotReady) { | |
| 113 ExpectNOTREACHED(); | |
| 114 screen_->OnIssueAuthTokenSuccess(GaiaConstants::kDeviceManagementService, | |
| 115 std::string()); | |
| 116 ExpectSample(policy::kMetricEnrollmentOtherFailed); | |
| 117 } | |
| 118 | |
| 119 IN_PROC_BROWSER_TEST_F(EnterpriseMetricsEnrollmentTest, AuthTokenFailure) { | |
| 120 ExpectNOTREACHED(); | |
| 121 GoogleServiceAuthError error(GoogleServiceAuthError::SERVICE_UNAVAILABLE); | |
| 122 screen_->OnIssueAuthTokenFailure(std::string(), error); | |
| 123 ExpectSample(policy::kMetricEnrollmentOtherFailed); | |
| 124 } | |
| 125 | |
| 126 IN_PROC_BROWSER_TEST_F(EnterpriseMetricsEnrollmentTest, OnPolicyBadToken) { | |
| 127 screen_->OnPolicyStateChanged(policy::CloudPolicySubsystem::BAD_GAIA_TOKEN, | |
| 128 policy::CloudPolicySubsystem::NO_DETAILS); | |
| 129 ExpectSample(policy::kMetricEnrollmentPolicyFailed); | |
| 130 } | |
| 131 | |
| 132 IN_PROC_BROWSER_TEST_F(EnterpriseMetricsEnrollmentTest, OnPolicyNetworkError) { | |
| 133 screen_->OnPolicyStateChanged(policy::CloudPolicySubsystem::NETWORK_ERROR, | |
| 134 policy::CloudPolicySubsystem::NO_DETAILS); | |
| 135 ExpectSample(policy::kMetricEnrollmentPolicyFailed); | |
| 136 } | |
| 137 | |
| 138 IN_PROC_BROWSER_TEST_F(EnterpriseMetricsEnrollmentTest, OnPolicyUnmanaged) { | |
| 139 screen_->OnPolicyStateChanged(policy::CloudPolicySubsystem::UNMANAGED, | |
| 140 policy::CloudPolicySubsystem::NO_DETAILS); | |
| 141 ExpectSample(policy::kMetricEnrollmentNotSupported); | |
| 142 } | |
| 143 | |
| 144 IN_PROC_BROWSER_TEST_F(EnterpriseMetricsEnrollmentTest, EnrollmentOK) { | |
| 145 screen_->OnPolicyStateChanged(policy::CloudPolicySubsystem::SUCCESS, | |
| 146 policy::CloudPolicySubsystem::NO_DETAILS); | |
| 147 ExpectSample(policy::kMetricEnrollmentOK); | |
| 148 } | |
| 149 | |
| 150 IN_PROC_BROWSER_TEST_F(EnterpriseMetricsEnrollmentTest, AuthErrorConnection) { | |
| 151 GoogleServiceAuthError error(GoogleServiceAuthError::CONNECTION_FAILED); | |
| 152 screen_->OnClientLoginFailure(error); | |
| 153 ExpectSample(policy::kMetricEnrollmentNetworkFailed); | |
| 154 } | |
| 155 | |
| 156 IN_PROC_BROWSER_TEST_F(EnterpriseMetricsEnrollmentTest, AuthErrorLoginFailed) { | |
| 157 GoogleServiceAuthError error(GoogleServiceAuthError::TWO_FACTOR); | |
| 158 screen_->OnClientLoginFailure(error); | |
| 159 ExpectSample(policy::kMetricEnrollmentLoginFailed); | |
| 160 } | |
| 161 | |
| 162 IN_PROC_BROWSER_TEST_F(EnterpriseMetricsEnrollmentTest, AccountDisabled) { | |
| 163 GoogleServiceAuthError error(GoogleServiceAuthError::ACCOUNT_DISABLED); | |
| 164 screen_->OnClientLoginFailure(error); | |
| 165 ExpectSample(policy::kMetricEnrollmentNotSupported); | |
| 166 } | |
| 167 | |
| 168 IN_PROC_BROWSER_TEST_F(EnterpriseMetricsEnrollmentTest, AuthErrorUnexpected) { | |
| 169 GoogleServiceAuthError error(GoogleServiceAuthError::REQUEST_CANCELED); | |
| 170 screen_->OnClientLoginFailure(error); | |
| 171 ExpectSample(policy::kMetricEnrollmentNetworkFailed); | |
| 172 } | |
| 173 | |
| 174 } // namespace chromeos | |
| OLD | NEW |