Chromium Code Reviews| 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 "testing/gtest/include/gtest/gtest.h" | |
| 12 | |
| 13 namespace browser_watcher { | |
| 14 | |
| 15 namespace { | |
| 16 | |
| 17 const wchar_t kRegistryPath[] = L"Software\\WatcherMetricsProviderWinTest"; | |
| 18 | |
| 19 class WatcherMetricsProviderWinTest : public testing::Test { | |
| 20 public: | |
| 21 typedef testing::Test Super; | |
| 22 | |
| 23 virtual void SetUp() override { | |
| 24 Super::SetUp(); | |
| 25 | |
| 26 override_manager_.OverrideRegistry(HKEY_CURRENT_USER); | |
| 27 } | |
| 28 | |
| 29 virtual void TearDown() override { | |
|
erikwright (departed)
2014/11/18 21:13:36
Remove?
Sigurður Ásgeirsson
2014/11/18 21:52:37
Done.
| |
| 30 Super::TearDown(); | |
| 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(); | |
|
erikwright (departed)
2014/11/18 21:13:36
include whatever is required for rand()
Sigurður Ásgeirsson
2014/11/18 21:52:37
Done.
| |
| 41 } while (pid == static_cast<int>(base::GetCurrentProcId())); | |
| 42 } | |
| 43 | |
| 44 base::win::RegKey key(HKEY_CURRENT_USER, kRegistryPath, KEY_WRITE); | |
|
erikwright (departed)
2014/11/18 21:13:36
include for RegKey
Sigurður Ásgeirsson
2014/11/18 21:52:37
Done.
| |
| 45 | |
| 46 // Make up a unique key, starting with the given pid. | |
| 47 base::string16 key_name(base::StringPrintf(L"%d-%d", pid, rand())); | |
|
erikwright (departed)
2014/11/18 21:13:36
include string16
Sigurður Ásgeirsson
2014/11/18 21:52:37
Done.
| |
| 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 |