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