Revert "Fix broken channel icon in chrome://help on CrOS" and try again
[chromium-blink-merge.git] / ios / chrome / browser / metrics / ios_stability_metrics_provider_unittest.mm
blob1f08314a089989da52487b948fbe30ca5934eaec
1 // Copyright 2015 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 #include "ios/chrome/browser/metrics/ios_stability_metrics_provider.h"
7 #include "base/bind.h"
8 #include "base/macros.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/prefs/testing_pref_service.h"
11 #include "base/test/histogram_tester.h"
12 #include "components/metrics/metrics_pref_names.h"
13 #include "components/metrics/metrics_service.h"
14 #include "components/metrics/metrics_state_manager.h"
15 #include "components/metrics/test_metrics_service_client.h"
16 #include "testing/gtest/include/gtest/gtest-param-test.h"
17 #include "testing/gtest/include/gtest/gtest.h"
19 namespace {
21 bool IsMetricsReportingEnabled() {
22   return false;
25 }  // namespace
27 // An IOSStabilityMetricsProvider that returns fake values for the last session
28 // environment query methods.
29 class IOSStabilityMetricsProviderForTesting
30     : public IOSStabilityMetricsProvider {
31  public:
32   explicit IOSStabilityMetricsProviderForTesting(
33       metrics::MetricsService* metrics_service)
34       : IOSStabilityMetricsProvider(metrics_service) {}
36   void SetIsFirstLaunchAfterUpgrade(bool value) {
37     is_first_launch_after_upgrade_ = value;
38   }
39   void SetHasCrashLogs(bool value) { has_crash_logs_ = value; }
40   void SetHasUploadedCrashReportsInBackground(bool value) {
41     has_uploaded_crash_reports_in_background_ = value;
42   }
43   void SetReceivedMemoryWarningBeforeLastShutdown(bool value) {
44     received_memory_warning_before_last_shutdown_ = value;
45   }
47  protected:
48   // IOSStabilityMetricsProvider:
49   bool IsFirstLaunchAfterUpgrade() override {
50     return is_first_launch_after_upgrade_;
51   }
52   bool HasCrashLogs() override { return has_crash_logs_; }
53   bool HasUploadedCrashReportsInBackground() override {
54     return has_uploaded_crash_reports_in_background_;
55   }
56   bool ReceivedMemoryWarningBeforeLastShutdown() override {
57     return received_memory_warning_before_last_shutdown_;
58   }
60  private:
61   bool is_first_launch_after_upgrade_;
62   bool was_last_shutdown_clean_;
63   bool has_crash_logs_;
64   bool has_uploaded_crash_reports_in_background_;
65   bool received_memory_warning_before_last_shutdown_;
67   DISALLOW_COPY_AND_ASSIGN(IOSStabilityMetricsProviderForTesting);
70 class IOSStabilityMetricsProviderTest : public testing::TestWithParam<int> {
71  public:
72   IOSStabilityMetricsProviderTest() {
73     metrics::MetricsService::RegisterPrefs(local_state_.registry());
74   }
76  protected:
77   TestingPrefServiceSimple local_state_;
78   metrics::TestMetricsServiceClient metrics_client_;
79   scoped_ptr<metrics::MetricsStateManager> metrics_state_;
80   scoped_ptr<metrics::MetricsService> metrics_service_;
81   scoped_ptr<IOSStabilityMetricsProviderForTesting> metrics_provider_;
83  private:
84   DISALLOW_COPY_AND_ASSIGN(IOSStabilityMetricsProviderTest);
87 // Verifies that a sample is recorded in the correct bucket of the shutdown type
88 // histogram when ProvideStabilityMetrics is invoked.
90 // This parameterized test receives a parameter in the range [0, 32), which is
91 // used to generate values for five booleans based on the binary
92 // representation of the parameter. The bits are assigned as follows (from
93 // least significant to most significant):
94 //  - received memory warning;
95 //  - crash log present;
96 //  - uploaded crash reports in background;
97 //  - last shutdown was clean;
98 //  - first launch after upgrade.
99 TEST_P(IOSStabilityMetricsProviderTest, ProvideStabilityMetrics) {
100   const bool received_memory_warning = GetParam() % 2;
101   const bool has_crash_logs = (GetParam() >> 1) % 2;
102   const bool has_uploaded_crash_reports_in_background = (GetParam() >> 2) % 2;
103   const bool was_last_shutdown_clean = (GetParam() >> 3) % 2;
104   const bool is_first_launch_after_upgrade = (GetParam() >> 4) % 2;
106   // Expected bucket for each possible value of GetParam().
107   const MobileSessionShutdownType expected_buckets[] = {
108       SHUTDOWN_IN_FOREGROUND_NO_CRASH_LOG_NO_MEMORY_WARNING,
109       SHUTDOWN_IN_FOREGROUND_NO_CRASH_LOG_WITH_MEMORY_WARNING,
110       SHUTDOWN_IN_FOREGROUND_WITH_CRASH_LOG_NO_MEMORY_WARNING,
111       SHUTDOWN_IN_FOREGROUND_WITH_CRASH_LOG_WITH_MEMORY_WARNING,
112       SHUTDOWN_IN_FOREGROUND_WITH_CRASH_LOG_NO_MEMORY_WARNING,
113       SHUTDOWN_IN_FOREGROUND_WITH_CRASH_LOG_WITH_MEMORY_WARNING,
114       SHUTDOWN_IN_FOREGROUND_WITH_CRASH_LOG_NO_MEMORY_WARNING,
115       SHUTDOWN_IN_FOREGROUND_WITH_CRASH_LOG_WITH_MEMORY_WARNING,
116       // If wasLastShutdownClean is true, the memory warning and crash log don't
117       // matter.
118       SHUTDOWN_IN_BACKGROUND,
119       SHUTDOWN_IN_BACKGROUND,
120       SHUTDOWN_IN_BACKGROUND,
121       SHUTDOWN_IN_BACKGROUND,
122       SHUTDOWN_IN_BACKGROUND,
123       SHUTDOWN_IN_BACKGROUND,
124       SHUTDOWN_IN_BACKGROUND,
125       SHUTDOWN_IN_BACKGROUND,
126       // If firstLaunchAfterUpgrade is true, the other flags don't matter.
127       FIRST_LAUNCH_AFTER_UPGRADE,
128       FIRST_LAUNCH_AFTER_UPGRADE,
129       FIRST_LAUNCH_AFTER_UPGRADE,
130       FIRST_LAUNCH_AFTER_UPGRADE,
131       FIRST_LAUNCH_AFTER_UPGRADE,
132       FIRST_LAUNCH_AFTER_UPGRADE,
133       FIRST_LAUNCH_AFTER_UPGRADE,
134       FIRST_LAUNCH_AFTER_UPGRADE,
135       FIRST_LAUNCH_AFTER_UPGRADE,
136       FIRST_LAUNCH_AFTER_UPGRADE,
137       FIRST_LAUNCH_AFTER_UPGRADE,
138       FIRST_LAUNCH_AFTER_UPGRADE,
139       FIRST_LAUNCH_AFTER_UPGRADE,
140       FIRST_LAUNCH_AFTER_UPGRADE,
141       FIRST_LAUNCH_AFTER_UPGRADE,
142       FIRST_LAUNCH_AFTER_UPGRADE,
143   };
145   // Setup the MetricsService.
146   local_state_.SetBoolean(metrics::prefs::kStabilityExitedCleanly,
147                           was_last_shutdown_clean);
148   metrics_state_ = metrics::MetricsStateManager::Create(
149       &local_state_, base::Bind(&IsMetricsReportingEnabled),
150       metrics::MetricsStateManager::StoreClientInfoCallback(),
151       metrics::MetricsStateManager::LoadClientInfoCallback());
152   metrics_service_.reset(new metrics::MetricsService(
153       metrics_state_.get(), &metrics_client_, &local_state_));
155   // Create the metrics provider to test.
156   metrics_provider_.reset(
157       new IOSStabilityMetricsProviderForTesting(metrics_service_.get()));
159   // Setup the metrics provider for the current test.
160   metrics_provider_->SetIsFirstLaunchAfterUpgrade(
161       is_first_launch_after_upgrade);
162   metrics_provider_->SetReceivedMemoryWarningBeforeLastShutdown(
163       received_memory_warning);
164   metrics_provider_->SetHasCrashLogs(has_crash_logs);
165   metrics_provider_->SetHasUploadedCrashReportsInBackground(
166       has_uploaded_crash_reports_in_background);
168   // Create a histogram tester for verifying samples written to the shutdown
169   // type histogram.
170   base::HistogramTester histogram_tester;
172   // Now call the method under test and verify exactly one sample is written to
173   // the expected bucket.
174   metrics_provider_->ProvideInitialStabilityMetrics(nullptr);
175   histogram_tester.ExpectUniqueSample("Stability.MobileSessionShutdownType",
176                                       expected_buckets[GetParam()], 1);
179 INSTANTIATE_TEST_CASE_P(/* No InstantiationName */,
180                         IOSStabilityMetricsProviderTest,
181                         testing::Range(0, 32));