Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(117)

Side by Side Diff: ios/chrome/browser/metrics/ios_stability_metrics_provider_unittest.mm

Issue 1207353005: [iOS] Upstream some stability code (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #include "ios/chrome/browser/metrics/ios_stability_metrics_provider.h"
6
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"
18
19 namespace {
20 bool IsMetricsReportingEnabled() {
21 return false;
22 }
23 }
24
25 // An IOSStabilityMetricsProvider that returns fake values for the last session
26 // environment query methods.
27 class IOSStabilityMetricsProviderForTesting
28 : public IOSStabilityMetricsProvider {
29 public:
30 explicit IOSStabilityMetricsProviderForTesting(
31 metrics::MetricsService* metrics_service)
32 : IOSStabilityMetricsProvider(metrics_service) {}
33
34 void SetIsFirstLaunchAfterUpgrade(bool value) {
35 is_first_launch_after_upgrade_ = value;
36 }
37 void SetHasCrashLogs(bool value) { has_crash_logs_ = value; }
38 void SetHasUploadedCrashReportsInBackground(bool value) {
39 has_uploaded_crash_reports_in_background_ = value;
40 }
41 void SetReceivedMemoryWarningBeforeLastShutdown(bool value) {
42 received_memory_warning_before_last_shutdown_ = value;
43 }
44
45 protected:
46 // IOSStabilityMetricsProvider:
47 bool IsFirstLaunchAfterUpgrade() override {
48 return is_first_launch_after_upgrade_;
49 }
50 bool HasCrashLogs() override { return has_crash_logs_; }
51 bool HasUploadedCrashReportsInBackground() override {
52 return has_uploaded_crash_reports_in_background_;
53 }
54 bool ReceivedMemoryWarningBeforeLastShutdown() override {
55 return received_memory_warning_before_last_shutdown_;
56 }
57
58 private:
59 bool is_first_launch_after_upgrade_;
60 bool was_last_shutdown_clean_;
61 bool has_crash_logs_;
62 bool has_uploaded_crash_reports_in_background_;
63 bool received_memory_warning_before_last_shutdown_;
64
65 DISALLOW_COPY_AND_ASSIGN(IOSStabilityMetricsProviderForTesting);
66 };
67
68 class IOSStabilityMetricsProviderTest : public testing::TestWithParam<int> {
69 public:
70 IOSStabilityMetricsProviderTest() {
71 metrics::MetricsService::RegisterPrefs(local_state_.registry());
72 }
73
74 protected:
75 TestingPrefServiceSimple local_state_;
76 metrics::TestMetricsServiceClient metrics_client_;
77 scoped_ptr<metrics::MetricsStateManager> metrics_state_;
78 scoped_ptr<metrics::MetricsService> metrics_service_;
79 scoped_ptr<IOSStabilityMetricsProviderForTesting> metrics_provider_;
80
81 private:
82 DISALLOW_COPY_AND_ASSIGN(IOSStabilityMetricsProviderTest);
83 };
84
85 // Verifies that a sample is recorded in the correct bucket of the shutdown type
86 // histogram when ProvideStabilityMetrics is invoked.
87 //
88 // This parameterized test receives a parameter in the range [0, 32), which is
89 // used to generate values for five booleans based on the binary
90 // representation of the parameter. The bits are assigned as follows (from
91 // least significant to most significant):
92 // - received memory warning;
93 // - crash log present;
94 // - uploaded crash reports in background;
95 // - last shutdown was clean;
96 // - first launch after upgrade.
97 TEST_P(IOSStabilityMetricsProviderTest, ProvideStabilityMetrics) {
98 const bool received_memory_warning = GetParam() % 2;
99 const bool has_crash_logs = (GetParam() >> 1) % 2;
100 const bool has_uploaded_crash_reports_in_background = (GetParam() >> 2) % 2;
101 const bool was_last_shutdown_clean = (GetParam() >> 3) % 2;
102 const bool is_first_launch_after_upgrade = (GetParam() >> 4) % 2;
103
104 // Expected bucket for each possible value of GetParam().
105 const MobileSessionShutdownType expected_buckets[] = {
106 SHUTDOWN_IN_FOREGROUND_NO_CRASH_LOG_NO_MEMORY_WARNING,
lpromero 2015/07/08 13:37:16 Shouldn't this be indented 2 instead of 4?
droger 2015/07/08 14:04:37 I don't know, I don't see anything in the style gu
107 SHUTDOWN_IN_FOREGROUND_NO_CRASH_LOG_WITH_MEMORY_WARNING,
108 SHUTDOWN_IN_FOREGROUND_WITH_CRASH_LOG_NO_MEMORY_WARNING,
109 SHUTDOWN_IN_FOREGROUND_WITH_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 // If wasLastShutdownClean is true, the memory warning and crash log don't
115 // matter.
116 SHUTDOWN_IN_BACKGROUND,
117 SHUTDOWN_IN_BACKGROUND,
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 // If firstLaunchAfterUpgrade is true, the other flags don't matter.
125 FIRST_LAUNCH_AFTER_UPGRADE,
126 FIRST_LAUNCH_AFTER_UPGRADE,
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 };
142
143 // Setup the MetricsService.
144 local_state_.SetBoolean(metrics::prefs::kStabilityExitedCleanly,
145 was_last_shutdown_clean);
146 metrics_state_ = metrics::MetricsStateManager::Create(
147 &local_state_, base::Bind(&IsMetricsReportingEnabled),
148 metrics::MetricsStateManager::StoreClientInfoCallback(),
149 metrics::MetricsStateManager::LoadClientInfoCallback());
150 metrics_service_.reset(new metrics::MetricsService(
151 metrics_state_.get(), &metrics_client_, &local_state_));
152
153 // Create the metrics provider to test.
154 metrics_provider_.reset(
155 new IOSStabilityMetricsProviderForTesting(metrics_service_.get()));
156
157 // Setup the metrics provider for the current test.
158 metrics_provider_->SetIsFirstLaunchAfterUpgrade(
159 is_first_launch_after_upgrade);
160 metrics_provider_->SetReceivedMemoryWarningBeforeLastShutdown(
161 received_memory_warning);
162 metrics_provider_->SetHasCrashLogs(has_crash_logs);
163 metrics_provider_->SetHasUploadedCrashReportsInBackground(
164 has_uploaded_crash_reports_in_background);
165
166 // Create a histogram tester for verifying samples written to the shutdown
167 // type histogram.
168 base::HistogramTester histogram_tester;
169
170 // Now call the method under test and verify exactly one sample is written to
171 // the expected bucket.
172 metrics_provider_->ProvideInitialStabilityMetrics(nullptr);
173 histogram_tester.ExpectUniqueSample("Stability.MobileSessionShutdownType",
174 expected_buckets[GetParam()], 1);
175 }
176
177 INSTANTIATE_TEST_CASE_P(/* No InstantiationName */,
178 IOSStabilityMetricsProviderTest,
179 testing::Range(0, 32));
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698