OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "components/browser_watcher/watcher_metrics_provider_win.h" |
| 6 |
| 7 #include <cstdlib> |
| 8 |
| 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" |
| 16 |
| 17 namespace browser_watcher { |
| 18 |
| 19 namespace { |
| 20 |
| 21 const wchar_t kRegistryPath[] = L"Software\\WatcherMetricsProviderWinTest"; |
| 22 |
| 23 class WatcherMetricsProviderWinTest : public testing::Test { |
| 24 public: |
| 25 typedef testing::Test Super; |
| 26 |
| 27 virtual void SetUp() override { |
| 28 Super::SetUp(); |
| 29 |
| 30 override_manager_.OverrideRegistry(HKEY_CURRENT_USER); |
| 31 } |
| 32 |
| 33 void AddProcessExitCode(bool use_own_pid, int exit_code) { |
| 34 int pid = 0; |
| 35 if (use_own_pid) { |
| 36 pid = base::GetCurrentProcId(); |
| 37 } else { |
| 38 // Make sure not to accidentally collide with own pid. |
| 39 do { |
| 40 pid = rand(); |
| 41 } while (pid == static_cast<int>(base::GetCurrentProcId())); |
| 42 } |
| 43 |
| 44 base::win::RegKey key(HKEY_CURRENT_USER, kRegistryPath, KEY_WRITE); |
| 45 |
| 46 // Make up a unique key, starting with the given pid. |
| 47 base::string16 key_name(base::StringPrintf(L"%d-%d", pid, rand())); |
| 48 |
| 49 // Write the exit code to registry. |
| 50 ULONG result = key.WriteValue(key_name.c_str(), exit_code); |
| 51 ASSERT_EQ(result, ERROR_SUCCESS); |
| 52 } |
| 53 |
| 54 size_t ExitCodeRegistryPathValueCount() { |
| 55 base::win::RegKey key(HKEY_CURRENT_USER, kRegistryPath, KEY_READ); |
| 56 return key.GetValueCount(); |
| 57 } |
| 58 |
| 59 protected: |
| 60 registry_util::RegistryOverrideManager override_manager_; |
| 61 base::HistogramTester histogram_tester_; |
| 62 }; |
| 63 |
| 64 } // namespace |
| 65 |
| 66 TEST_F(WatcherMetricsProviderWinTest, RecordsStabilityHistogram) { |
| 67 // Record multiple success exits. |
| 68 for (size_t i = 0; i < 11; ++i) |
| 69 AddProcessExitCode(false, 0); |
| 70 |
| 71 // Record a single failure. |
| 72 AddProcessExitCode(false, 100); |
| 73 |
| 74 WatcherMetricsProviderWin provider(kRegistryPath); |
| 75 |
| 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); |
| 83 |
| 84 // Verify that the reported values are gone. |
| 85 EXPECT_EQ(ExitCodeRegistryPathValueCount(), 0); |
| 86 } |
| 87 |
| 88 TEST_F(WatcherMetricsProviderWinTest, DoesNotReportOwnProcessId) { |
| 89 // Record multiple success exits. |
| 90 for (size_t i = 0; i < 11; ++i) |
| 91 AddProcessExitCode(i, 0); |
| 92 |
| 93 // Record own process as STILL_ACTIVE. |
| 94 AddProcessExitCode(true, STILL_ACTIVE); |
| 95 |
| 96 WatcherMetricsProviderWin provider(kRegistryPath); |
| 97 |
| 98 provider.ProvideStabilityMetrics(NULL); |
| 99 histogram_tester_.ExpectUniqueSample( |
| 100 WatcherMetricsProviderWin::kBrowserExitCodeHistogramName, 0, 11); |
| 101 |
| 102 // Verify that the reported values are gone. |
| 103 EXPECT_EQ(ExitCodeRegistryPathValueCount(), 1); |
| 104 } |
| 105 |
| 106 } // namespace browser_watcher |
OLD | NEW |