Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(100)

Unified Diff: components/browser_watcher/watcher_metrics_provider_win_unittest.cc

Issue 742523002: Browser watcher part three. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@watcher_client
Patch Set: Work around wonky RegKey API. Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « components/browser_watcher/watcher_metrics_provider_win.cc ('k') | components/components_tests.gyp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: components/browser_watcher/watcher_metrics_provider_win_unittest.cc
diff --git a/components/browser_watcher/watcher_metrics_provider_win_unittest.cc b/components/browser_watcher/watcher_metrics_provider_win_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..9002a171536660b2033955e549ef6ff5f841b989
--- /dev/null
+++ b/components/browser_watcher/watcher_metrics_provider_win_unittest.cc
@@ -0,0 +1,106 @@
+// Copyright (c) 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "components/browser_watcher/watcher_metrics_provider_win.h"
+
+#include <cstdlib>
+
+#include "base/process/process_handle.h"
+#include "base/strings/string16.h"
+#include "base/strings/stringprintf.h"
+#include "base/test/histogram_tester.h"
+#include "base/test/test_reg_util_win.h"
+#include "base/win/registry.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace browser_watcher {
+
+namespace {
+
+const wchar_t kRegistryPath[] = L"Software\\WatcherMetricsProviderWinTest";
+
+class WatcherMetricsProviderWinTest : public testing::Test {
+ public:
+ typedef testing::Test Super;
+
+ virtual void SetUp() override {
+ Super::SetUp();
+
+ override_manager_.OverrideRegistry(HKEY_CURRENT_USER);
+ }
+
+ void AddProcessExitCode(bool use_own_pid, int exit_code) {
+ int pid = 0;
+ if (use_own_pid) {
+ pid = base::GetCurrentProcId();
+ } else {
+ // Make sure not to accidentally collide with own pid.
+ do {
+ pid = rand();
+ } while (pid == static_cast<int>(base::GetCurrentProcId()));
+ }
+
+ base::win::RegKey key(HKEY_CURRENT_USER, kRegistryPath, KEY_WRITE);
+
+ // Make up a unique key, starting with the given pid.
+ base::string16 key_name(base::StringPrintf(L"%d-%d", pid, rand()));
+
+ // Write the exit code to registry.
+ ULONG result = key.WriteValue(key_name.c_str(), exit_code);
+ ASSERT_EQ(result, ERROR_SUCCESS);
+ }
+
+ size_t ExitCodeRegistryPathValueCount() {
+ base::win::RegKey key(HKEY_CURRENT_USER, kRegistryPath, KEY_READ);
+ return key.GetValueCount();
+ }
+
+ protected:
+ registry_util::RegistryOverrideManager override_manager_;
+ base::HistogramTester histogram_tester_;
+};
+
+} // namespace
+
+TEST_F(WatcherMetricsProviderWinTest, RecordsStabilityHistogram) {
+ // Record multiple success exits.
+ for (size_t i = 0; i < 11; ++i)
+ AddProcessExitCode(false, 0);
+
+ // Record a single failure.
+ AddProcessExitCode(false, 100);
+
+ WatcherMetricsProviderWin provider(kRegistryPath);
+
+ provider.ProvideStabilityMetrics(NULL);
+ histogram_tester_.ExpectBucketCount(
+ WatcherMetricsProviderWin::kBrowserExitCodeHistogramName, 0, 11);
+ histogram_tester_.ExpectBucketCount(
+ WatcherMetricsProviderWin::kBrowserExitCodeHistogramName, 100, 1);
+ histogram_tester_.ExpectTotalCount(
+ WatcherMetricsProviderWin::kBrowserExitCodeHistogramName, 12);
+
+ // Verify that the reported values are gone.
+ EXPECT_EQ(ExitCodeRegistryPathValueCount(), 0);
+}
+
+TEST_F(WatcherMetricsProviderWinTest, DoesNotReportOwnProcessId) {
+ // Record multiple success exits.
+ for (size_t i = 0; i < 11; ++i)
+ AddProcessExitCode(i, 0);
+
+ // Record own process as STILL_ACTIVE.
+ AddProcessExitCode(true, STILL_ACTIVE);
+
+ WatcherMetricsProviderWin provider(kRegistryPath);
+
+ provider.ProvideStabilityMetrics(NULL);
+ histogram_tester_.ExpectUniqueSample(
+ WatcherMetricsProviderWin::kBrowserExitCodeHistogramName, 0, 11);
+
+ // Verify that the reported values are gone.
+ EXPECT_EQ(ExitCodeRegistryPathValueCount(), 1);
+}
+
+} // namespace browser_watcher
« no previous file with comments | « components/browser_watcher/watcher_metrics_provider_win.cc ('k') | components/components_tests.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698