summaryrefslogtreecommitdiffstats
path: root/components/browser_watcher/crash_reporting_metrics_win_unittest.cc
blob: 6d9fd2410282617ac9cf645f9c4727a91469854a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright 2015 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/crash_reporting_metrics_win.h"

#include "base/macros.h"
#include "base/strings/string16.h"
#include "base/win/registry.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace browser_watcher {

namespace {

const base::char16 test_root[] = L"TmpTmp";

class CrashReportingMetricsTest : public testing::Test {
 protected:
  CrashReportingMetricsTest() {}

  void SetUp() override {
    // Create a temporary key for testing
    base::win::RegKey key(HKEY_CURRENT_USER, L"", KEY_ALL_ACCESS);
    key.DeleteKey(test_root);
    ASSERT_NE(ERROR_SUCCESS, key.Open(HKEY_CURRENT_USER, test_root, KEY_READ));
    ASSERT_EQ(ERROR_SUCCESS, key.Create(HKEY_CURRENT_USER, test_root,
                                        KEY_READ));
  }

  void TearDown() override {
    // Clean up the temporary key
    base::win::RegKey key(HKEY_CURRENT_USER, L"", KEY_ALL_ACCESS);
    ASSERT_EQ(ERROR_SUCCESS, key.DeleteKey(test_root));
  }

  base::string16 registry_path() {
    return base::string16(test_root) + L"\\CrashReportingMetricsTest";
  };

 private:
  DISALLOW_COPY_AND_ASSIGN(CrashReportingMetricsTest);
};

}  // namespace

TEST_F(CrashReportingMetricsTest, BasicTest) {
  CrashReportingMetrics::Values values =
      CrashReportingMetrics(registry_path()).RetrieveAndResetMetrics();
  EXPECT_EQ(0, values.crash_dump_attempts);
  EXPECT_EQ(0, values.successful_crash_dumps);
  EXPECT_EQ(0, values.failed_crash_dumps);
  EXPECT_EQ(0, values.dump_without_crash_attempts);
  EXPECT_EQ(0, values.successful_dumps_without_crash);
  EXPECT_EQ(0, values.failed_dumps_without_crash);

  CrashReportingMetrics writer(registry_path());
  writer.RecordCrashDumpAttempt();
  writer.RecordCrashDumpAttempt();
  writer.RecordCrashDumpAttempt();
  writer.RecordDumpWithoutCrashAttempt();
  writer.RecordDumpWithoutCrashAttempt();
  writer.RecordDumpWithoutCrashAttempt();
  writer.RecordDumpWithoutCrashAttempt();

  writer.RecordCrashDumpAttemptResult(true);
  writer.RecordCrashDumpAttemptResult(false);
  writer.RecordCrashDumpAttemptResult(false);
  writer.RecordDumpWithoutCrashAttemptResult(true);
  writer.RecordDumpWithoutCrashAttemptResult(true);
  writer.RecordDumpWithoutCrashAttemptResult(true);
  writer.RecordDumpWithoutCrashAttemptResult(false);

  values = CrashReportingMetrics(registry_path()).RetrieveAndResetMetrics();
  EXPECT_EQ(3, values.crash_dump_attempts);
  EXPECT_EQ(1, values.successful_crash_dumps);
  EXPECT_EQ(2, values.failed_crash_dumps);
  EXPECT_EQ(4, values.dump_without_crash_attempts);
  EXPECT_EQ(3, values.successful_dumps_without_crash);
  EXPECT_EQ(1, values.failed_dumps_without_crash);

  values = CrashReportingMetrics(registry_path()).RetrieveAndResetMetrics();
  EXPECT_EQ(0, values.crash_dump_attempts);
  EXPECT_EQ(0, values.successful_crash_dumps);
  EXPECT_EQ(0, values.failed_crash_dumps);
  EXPECT_EQ(0, values.dump_without_crash_attempts);
  EXPECT_EQ(0, values.successful_dumps_without_crash);
  EXPECT_EQ(0, values.failed_dumps_without_crash);

  writer.RecordCrashDumpAttempt();
  writer.RecordCrashDumpAttempt();
  writer.RecordDumpWithoutCrashAttempt();
  writer.RecordDumpWithoutCrashAttempt();
  writer.RecordDumpWithoutCrashAttempt();

  writer.RecordCrashDumpAttemptResult(true);
  writer.RecordCrashDumpAttemptResult(true);
  writer.RecordDumpWithoutCrashAttemptResult(true);
  writer.RecordDumpWithoutCrashAttemptResult(false);
  writer.RecordDumpWithoutCrashAttemptResult(false);

  values = CrashReportingMetrics(registry_path()).RetrieveAndResetMetrics();
  EXPECT_EQ(2, values.crash_dump_attempts);
  EXPECT_EQ(2, values.successful_crash_dumps);
  EXPECT_EQ(0, values.failed_crash_dumps);
  EXPECT_EQ(3, values.dump_without_crash_attempts);
  EXPECT_EQ(1, values.successful_dumps_without_crash);
  EXPECT_EQ(2, values.failed_dumps_without_crash);
}

} // namespace browser_watcher