| OLD | NEW |
| 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/browser_watcher/watcher_metrics_provider_win.h" | 5 #include "components/browser_watcher/watcher_metrics_provider_win.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <cstdlib> | 10 #include <cstdlib> |
| 11 | 11 |
| 12 #include "base/process/process_handle.h" | 12 #include "base/process/process_handle.h" |
| 13 #include "base/strings/string16.h" | 13 #include "base/strings/string16.h" |
| 14 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
| 15 #include "base/test/histogram_tester.h" | 15 #include "base/test/histogram_tester.h" |
| 16 #include "base/test/test_reg_util_win.h" | 16 #include "base/test/test_reg_util_win.h" |
| 17 #include "base/test/test_simple_task_runner.h" | 17 #include "base/test/test_simple_task_runner.h" |
| 18 #include "base/win/registry.h" | 18 #include "base/win/registry.h" |
| 19 #include "components/browser_watcher/exit_funnel_win.h" | 19 #include "components/browser_watcher/exit_funnel_win.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 21 | 21 |
| 22 namespace browser_watcher { | 22 namespace browser_watcher { |
| 23 | 23 |
| 24 namespace { | 24 namespace { |
| 25 | 25 |
| 26 using GetExecutableDetailsCallback = |
| 27 WatcherMetricsProviderWin::GetExecutableDetailsCallback; |
| 28 |
| 26 const wchar_t kRegistryPath[] = L"Software\\WatcherMetricsProviderWinTest"; | 29 const wchar_t kRegistryPath[] = L"Software\\WatcherMetricsProviderWinTest"; |
| 27 | 30 |
| 28 class WatcherMetricsProviderWinTest : public testing::Test { | 31 class WatcherMetricsProviderWinTest : public testing::Test { |
| 29 public: | 32 public: |
| 30 typedef testing::Test Super; | 33 typedef testing::Test Super; |
| 31 | 34 |
| 32 void SetUp() override { | 35 void SetUp() override { |
| 33 Super::SetUp(); | 36 Super::SetUp(); |
| 34 | 37 |
| 35 override_manager_.OverrideRegistry(HKEY_CURRENT_USER); | 38 override_manager_.OverrideRegistry(HKEY_CURRENT_USER); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 } // namespace | 83 } // namespace |
| 81 | 84 |
| 82 TEST_F(WatcherMetricsProviderWinTest, RecordsStabilityHistogram) { | 85 TEST_F(WatcherMetricsProviderWinTest, RecordsStabilityHistogram) { |
| 83 // Record multiple success exits. | 86 // Record multiple success exits. |
| 84 for (size_t i = 0; i < 11; ++i) | 87 for (size_t i = 0; i < 11; ++i) |
| 85 AddProcessExitCode(false, 0); | 88 AddProcessExitCode(false, 0); |
| 86 | 89 |
| 87 // Record a single failure. | 90 // Record a single failure. |
| 88 AddProcessExitCode(false, 100); | 91 AddProcessExitCode(false, 100); |
| 89 | 92 |
| 90 WatcherMetricsProviderWin provider(kRegistryPath, base::FilePath(), | 93 WatcherMetricsProviderWin provider( |
| 91 base::FilePath(), test_task_runner_.get()); | 94 kRegistryPath, base::FilePath(), base::FilePath(), |
| 95 GetExecutableDetailsCallback(), test_task_runner_.get()); |
| 92 | 96 |
| 93 provider.ProvideStabilityMetrics(NULL); | 97 provider.ProvideStabilityMetrics(NULL); |
| 94 histogram_tester_.ExpectBucketCount( | 98 histogram_tester_.ExpectBucketCount( |
| 95 WatcherMetricsProviderWin::kBrowserExitCodeHistogramName, 0, 11); | 99 WatcherMetricsProviderWin::kBrowserExitCodeHistogramName, 0, 11); |
| 96 histogram_tester_.ExpectBucketCount( | 100 histogram_tester_.ExpectBucketCount( |
| 97 WatcherMetricsProviderWin::kBrowserExitCodeHistogramName, 100, 1); | 101 WatcherMetricsProviderWin::kBrowserExitCodeHistogramName, 100, 1); |
| 98 histogram_tester_.ExpectTotalCount( | 102 histogram_tester_.ExpectTotalCount( |
| 99 WatcherMetricsProviderWin::kBrowserExitCodeHistogramName, 12); | 103 WatcherMetricsProviderWin::kBrowserExitCodeHistogramName, 12); |
| 100 | 104 |
| 101 // Verify that the reported values are gone. | 105 // Verify that the reported values are gone. |
| 102 EXPECT_EQ(0u, ExitCodeRegistryPathValueCount()); | 106 EXPECT_EQ(0u, ExitCodeRegistryPathValueCount()); |
| 103 } | 107 } |
| 104 | 108 |
| 105 TEST_F(WatcherMetricsProviderWinTest, DoesNotReportOwnProcessId) { | 109 TEST_F(WatcherMetricsProviderWinTest, DoesNotReportOwnProcessId) { |
| 106 // Record multiple success exits. | 110 // Record multiple success exits. |
| 107 for (size_t i = 0; i < 11; ++i) | 111 for (size_t i = 0; i < 11; ++i) |
| 108 AddProcessExitCode(i, 0); | 112 AddProcessExitCode(i, 0); |
| 109 | 113 |
| 110 // Record own process as STILL_ACTIVE. | 114 // Record own process as STILL_ACTIVE. |
| 111 AddProcessExitCode(true, STILL_ACTIVE); | 115 AddProcessExitCode(true, STILL_ACTIVE); |
| 112 | 116 |
| 113 WatcherMetricsProviderWin provider(kRegistryPath, base::FilePath(), | 117 WatcherMetricsProviderWin provider( |
| 114 base::FilePath(), test_task_runner_.get()); | 118 kRegistryPath, base::FilePath(), base::FilePath(), |
| 119 GetExecutableDetailsCallback(), test_task_runner_.get()); |
| 115 | 120 |
| 116 provider.ProvideStabilityMetrics(NULL); | 121 provider.ProvideStabilityMetrics(NULL); |
| 117 histogram_tester_.ExpectUniqueSample( | 122 histogram_tester_.ExpectUniqueSample( |
| 118 WatcherMetricsProviderWin::kBrowserExitCodeHistogramName, 0, 11); | 123 WatcherMetricsProviderWin::kBrowserExitCodeHistogramName, 0, 11); |
| 119 | 124 |
| 120 // Verify that the reported values are gone. | 125 // Verify that the reported values are gone. |
| 121 EXPECT_EQ(1u, ExitCodeRegistryPathValueCount()); | 126 EXPECT_EQ(1u, ExitCodeRegistryPathValueCount()); |
| 122 } | 127 } |
| 123 | 128 |
| 124 TEST_F(WatcherMetricsProviderWinTest, DeletesRecordedExitFunnelEvents) { | 129 TEST_F(WatcherMetricsProviderWinTest, DeletesRecordedExitFunnelEvents) { |
| 125 // Record an exit funnel and make sure the registry is cleaned up on | 130 // Record an exit funnel and make sure the registry is cleaned up on |
| 126 // reporting, without recording any events. | 131 // reporting, without recording any events. |
| 127 AddExitFunnelEvent(100, L"One", 1000 * 1000); | 132 AddExitFunnelEvent(100, L"One", 1000 * 1000); |
| 128 AddExitFunnelEvent(101, L"Two", 1010 * 1000); | 133 AddExitFunnelEvent(101, L"Two", 1010 * 1000); |
| 129 AddExitFunnelEvent(102, L"Three", 990 * 1000); | 134 AddExitFunnelEvent(102, L"Three", 990 * 1000); |
| 130 | 135 |
| 131 base::win::RegistryKeyIterator it(HKEY_CURRENT_USER, kRegistryPath); | 136 base::win::RegistryKeyIterator it(HKEY_CURRENT_USER, kRegistryPath); |
| 132 EXPECT_EQ(3u, it.SubkeyCount()); | 137 EXPECT_EQ(3u, it.SubkeyCount()); |
| 133 | 138 |
| 134 WatcherMetricsProviderWin provider(kRegistryPath, base::FilePath(), | 139 WatcherMetricsProviderWin provider( |
| 135 base::FilePath(), test_task_runner_.get()); | 140 kRegistryPath, base::FilePath(), base::FilePath(), |
| 141 GetExecutableDetailsCallback(), test_task_runner_.get()); |
| 136 | 142 |
| 137 provider.ProvideStabilityMetrics(NULL); | 143 provider.ProvideStabilityMetrics(NULL); |
| 138 // Make sure the exit funnel events are no longer recorded in histograms. | 144 // Make sure the exit funnel events are no longer recorded in histograms. |
| 139 EXPECT_TRUE( | 145 EXPECT_TRUE( |
| 140 histogram_tester_.GetAllSamples("Stability.ExitFunnel.One").empty()); | 146 histogram_tester_.GetAllSamples("Stability.ExitFunnel.One").empty()); |
| 141 EXPECT_TRUE( | 147 EXPECT_TRUE( |
| 142 histogram_tester_.GetAllSamples("Stability.ExitFunnel.Two").empty()); | 148 histogram_tester_.GetAllSamples("Stability.ExitFunnel.Two").empty()); |
| 143 EXPECT_TRUE( | 149 EXPECT_TRUE( |
| 144 histogram_tester_.GetAllSamples("Stability.ExitFunnel.Three").empty()); | 150 histogram_tester_.GetAllSamples("Stability.ExitFunnel.Three").empty()); |
| 145 | 151 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 159 AddProcessExitCode(false, 100); | 165 AddProcessExitCode(false, 100); |
| 160 | 166 |
| 161 // Record an exit funnel. | 167 // Record an exit funnel. |
| 162 ASSERT_TRUE(funnel.InitImpl(kRegistryPath, 4, base::Time::Now())); | 168 ASSERT_TRUE(funnel.InitImpl(kRegistryPath, 4, base::Time::Now())); |
| 163 | 169 |
| 164 AddExitFunnelEvent(100, L"One", 1000 * 1000); | 170 AddExitFunnelEvent(100, L"One", 1000 * 1000); |
| 165 AddExitFunnelEvent(101, L"Two", 1010 * 1000); | 171 AddExitFunnelEvent(101, L"Two", 1010 * 1000); |
| 166 AddExitFunnelEvent(102, L"Three", 990 * 1000); | 172 AddExitFunnelEvent(102, L"Three", 990 * 1000); |
| 167 | 173 |
| 168 // Make like the user is opted out of reporting. | 174 // Make like the user is opted out of reporting. |
| 169 WatcherMetricsProviderWin provider(kRegistryPath, base::FilePath(), | 175 WatcherMetricsProviderWin provider( |
| 170 base::FilePath(), test_task_runner_.get()); | 176 kRegistryPath, base::FilePath(), base::FilePath(), |
| 177 GetExecutableDetailsCallback(), test_task_runner_.get()); |
| 171 provider.OnRecordingDisabled(); | 178 provider.OnRecordingDisabled(); |
| 172 | 179 |
| 173 base::win::RegKey key; | 180 base::win::RegKey key; |
| 174 { | 181 { |
| 175 // The deletion should be scheduled to the test_task_runner, and not happen | 182 // The deletion should be scheduled to the test_task_runner, and not happen |
| 176 // immediately. | 183 // immediately. |
| 177 ASSERT_EQ(ERROR_SUCCESS, | 184 ASSERT_EQ(ERROR_SUCCESS, |
| 178 key.Open(HKEY_CURRENT_USER, kRegistryPath, KEY_READ)); | 185 key.Open(HKEY_CURRENT_USER, kRegistryPath, KEY_READ)); |
| 179 } | 186 } |
| 180 | 187 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 191 for (size_t i = 0; i < 200; ++i) { | 198 for (size_t i = 0; i < 200; ++i) { |
| 192 AddExitFunnelEvent(i, L"One", 10); | 199 AddExitFunnelEvent(i, L"One", 10); |
| 193 AddExitFunnelEvent(i, L"Two", 10); | 200 AddExitFunnelEvent(i, L"Two", 10); |
| 194 } | 201 } |
| 195 | 202 |
| 196 base::win::RegistryKeyIterator it(HKEY_CURRENT_USER, kRegistryPath); | 203 base::win::RegistryKeyIterator it(HKEY_CURRENT_USER, kRegistryPath); |
| 197 EXPECT_EQ(200u, it.SubkeyCount()); | 204 EXPECT_EQ(200u, it.SubkeyCount()); |
| 198 | 205 |
| 199 { | 206 { |
| 200 // Make like the user is opted out of reporting. | 207 // Make like the user is opted out of reporting. |
| 201 WatcherMetricsProviderWin provider(kRegistryPath, base::FilePath(), | 208 WatcherMetricsProviderWin provider( |
| 202 base::FilePath(), | 209 kRegistryPath, base::FilePath(), base::FilePath(), |
| 203 test_task_runner_.get()); | 210 GetExecutableDetailsCallback(), test_task_runner_.get()); |
| 204 provider.OnRecordingDisabled(); | 211 provider.OnRecordingDisabled(); |
| 205 // Flush the task(s). | 212 // Flush the task(s). |
| 206 test_task_runner_->RunPendingTasks(); | 213 test_task_runner_->RunPendingTasks(); |
| 207 } | 214 } |
| 208 | 215 |
| 209 // We expect only 100 of the funnels have been scrubbed. | 216 // We expect only 100 of the funnels have been scrubbed. |
| 210 EXPECT_EQ(100u, it.SubkeyCount()); | 217 EXPECT_EQ(100u, it.SubkeyCount()); |
| 211 } | 218 } |
| 212 | 219 |
| 213 } // namespace browser_watcher | 220 } // namespace browser_watcher |
| OLD | NEW |