1 // Copyright (c) 2014 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 "components/browser_watcher/watcher_metrics_provider_win.h"
9 #include "base/process/process_handle.h"
10 #include "base/strings/string16.h"
11 #include "base/strings/stringprintf.h"
12 #include "base/test/histogram_tester.h"
13 #include "base/test/test_reg_util_win.h"
14 #include "base/win/registry.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 namespace browser_watcher
{
21 const wchar_t kRegistryPath
[] = L
"Software\\WatcherMetricsProviderWinTest";
23 class WatcherMetricsProviderWinTest
: public testing::Test
{
25 typedef testing::Test Super
;
27 virtual void SetUp() override
{
30 override_manager_
.OverrideRegistry(HKEY_CURRENT_USER
);
33 void AddProcessExitCode(bool use_own_pid
, int exit_code
) {
36 pid
= base::GetCurrentProcId();
38 // Make sure not to accidentally collide with own pid.
41 } while (pid
== static_cast<int>(base::GetCurrentProcId()));
44 base::win::RegKey
key(HKEY_CURRENT_USER
, kRegistryPath
, KEY_WRITE
);
46 // Make up a unique key, starting with the given pid.
47 base::string16
key_name(base::StringPrintf(L
"%d-%d", pid
, rand()));
49 // Write the exit code to registry.
50 ULONG result
= key
.WriteValue(key_name
.c_str(), exit_code
);
51 ASSERT_EQ(result
, ERROR_SUCCESS
);
54 size_t ExitCodeRegistryPathValueCount() {
55 base::win::RegKey
key(HKEY_CURRENT_USER
, kRegistryPath
, KEY_READ
);
56 return key
.GetValueCount();
60 registry_util::RegistryOverrideManager override_manager_
;
61 base::HistogramTester histogram_tester_
;
66 TEST_F(WatcherMetricsProviderWinTest
, RecordsStabilityHistogram
) {
67 // Record multiple success exits.
68 for (size_t i
= 0; i
< 11; ++i
)
69 AddProcessExitCode(false, 0);
71 // Record a single failure.
72 AddProcessExitCode(false, 100);
74 WatcherMetricsProviderWin
provider(kRegistryPath
);
76 provider
.ProvideStabilityMetrics(NULL
);
77 histogram_tester_
.ExpectBucketCount(
78 WatcherMetricsProviderWin::kBrowserExitCodeHistogramName
, 0, 11);
79 histogram_tester_
.ExpectBucketCount(
80 WatcherMetricsProviderWin::kBrowserExitCodeHistogramName
, 100, 1);
81 histogram_tester_
.ExpectTotalCount(
82 WatcherMetricsProviderWin::kBrowserExitCodeHistogramName
, 12);
84 // Verify that the reported values are gone.
85 EXPECT_EQ(ExitCodeRegistryPathValueCount(), 0);
88 TEST_F(WatcherMetricsProviderWinTest
, DoesNotReportOwnProcessId
) {
89 // Record multiple success exits.
90 for (size_t i
= 0; i
< 11; ++i
)
91 AddProcessExitCode(i
, 0);
93 // Record own process as STILL_ACTIVE.
94 AddProcessExitCode(true, STILL_ACTIVE
);
96 WatcherMetricsProviderWin
provider(kRegistryPath
);
98 provider
.ProvideStabilityMetrics(NULL
);
99 histogram_tester_
.ExpectUniqueSample(
100 WatcherMetricsProviderWin::kBrowserExitCodeHistogramName
, 0, 11);
102 // Verify that the reported values are gone.
103 EXPECT_EQ(ExitCodeRegistryPathValueCount(), 1);
106 } // namespace browser_watcher