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

Side by Side Diff: unclean_shutdown_collector_test.cc

Issue 3644007: Crash reporter: collect suspend and resume info from power manager (Closed) Base URL: http://git.chromium.org/git/crash-reporter.git
Patch Set: Reorganized deletions, added unit tests Created 10 years 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 unified diff | Download patch | Annotate | Revision Log
« unclean_shutdown_collector.cc ('K') | « unclean_shutdown_collector.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. 1 // Copyright (c) 2010 The Chromium OS 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 <unistd.h> 5 #include <unistd.h>
6 6
7 #include "base/file_util.h" 7 #include "base/file_util.h"
8 #include "base/string_util.h" 8 #include "base/string_util.h"
9 #include "crash-reporter/unclean_shutdown_collector.h" 9 #include "crash-reporter/unclean_shutdown_collector.h"
10 #include "crash-reporter/system_logging_mock.h" 10 #include "crash-reporter/system_logging_mock.h"
11 #include "gflags/gflags.h" 11 #include "gflags/gflags.h"
12 #include "gtest/gtest.h" 12 #include "gtest/gtest.h"
13 13
14 static int s_crashes = 0; 14 static int s_crashes = 0;
15 static bool s_metrics = false; 15 static bool s_metrics = false;
16 16
17 static const char kTestUnclean[] = "test/unclean"; 17 static const char kTestUnclean[] = "test/unclean";
18 static const char kTestSuspended[] = "test/suspended";
19 static const char kTestLowBattery[] = "test/low_battery";
kmixter1 2010/11/23 23:18:01 nit: abc order unless there's a specific reason no
18 20
19 void CountCrash() { 21 void CountCrash() {
20 ++s_crashes; 22 ++s_crashes;
21 } 23 }
22 24
23 bool IsMetrics() { 25 bool IsMetrics() {
24 return s_metrics; 26 return s_metrics;
25 } 27 }
26 28
27 class UncleanShutdownCollectorTest : public ::testing::Test { 29 class UncleanShutdownCollectorTest : public ::testing::Test {
28 void SetUp() { 30 void SetUp() {
29 s_crashes = 0; 31 s_crashes = 0;
30 collector_.Initialize(CountCrash, 32 collector_.Initialize(CountCrash,
31 IsMetrics, 33 IsMetrics,
32 &logging_); 34 &logging_);
33 rmdir("test"); 35 rmdir("test");
34 test_unclean_ = FilePath(kTestUnclean); 36 test_unclean_ = FilePath(kTestUnclean);
35 collector_.unclean_shutdown_file_ = kTestUnclean; 37 collector_.unclean_shutdown_file_ = kTestUnclean;
36 file_util::Delete(test_unclean_, true); 38 file_util::Delete(test_unclean_, true);
39 // Set up alternate power manager tracing files as well
40 collector_.powerd_suspended_file_ = FilePath(kTestSuspended);
41 collector_.powerd_low_battery_file_ = FilePath(kTestLowBattery);
37 } 42 }
38 protected: 43 protected:
39 void WriteStringToFile(const FilePath &file_path, 44 void WriteStringToFile(const FilePath &file_path,
40 const char *data) { 45 const char *data) {
41 ASSERT_EQ(strlen(data), 46 ASSERT_EQ(strlen(data),
42 file_util::WriteFile(file_path, data, strlen(data))); 47 file_util::WriteFile(file_path, data, strlen(data)));
43 } 48 }
44 49
45 SystemLoggingMock logging_; 50 SystemLoggingMock logging_;
46 UncleanShutdownCollector collector_; 51 UncleanShutdownCollector collector_;
(...skipping 24 matching lines...) Expand all
71 ASSERT_TRUE(collector_.Collect()); 76 ASSERT_TRUE(collector_.Collect());
72 ASSERT_FALSE(file_util::PathExists(test_unclean_)); 77 ASSERT_FALSE(file_util::PathExists(test_unclean_));
73 ASSERT_NE(std::string::npos, 78 ASSERT_NE(std::string::npos,
74 logging_.log().find("Last shutdown was not clean")); 79 logging_.log().find("Last shutdown was not clean"));
75 } 80 }
76 81
77 TEST_F(UncleanShutdownCollectorTest, CollectFalse) { 82 TEST_F(UncleanShutdownCollectorTest, CollectFalse) {
78 ASSERT_FALSE(collector_.Collect()); 83 ASSERT_FALSE(collector_.Collect());
79 } 84 }
80 85
86 TEST_F(UncleanShutdownCollectorTest, CollectDeadBatteryRunningLow) {
87 ASSERT_TRUE(collector_.Enable());
88 ASSERT_TRUE(file_util::PathExists(test_unclean_));
89 file_util::WriteFile(collector_.powerd_low_battery_file_, "", 0);
90 ASSERT_TRUE(collector_.Collect());
kmixter1 2010/11/23 23:18:01 Should return false.
91 ASSERT_FALSE(file_util::PathExists(test_unclean_));
92 ASSERT_FALSE(file_util::PathExists(collector_.powerd_low_battery_file_));
93 ASSERT_NE(std::string::npos,
94 logging_.log().find("Unclean shutdown occurred while running with "
95 "battery critically low"));
kmixter1 2010/11/23 23:18:01 nit: search for ending '.' like you do in the othe
96 }
97
98 TEST_F(UncleanShutdownCollectorTest, CollectDeadBatterySuspended) {
99 ASSERT_TRUE(collector_.Enable());
100 ASSERT_TRUE(file_util::PathExists(test_unclean_));
101 file_util::WriteFile(collector_.powerd_suspended_file_, "", 0);
102 ASSERT_TRUE(collector_.Collect());
kmixter1 2010/11/23 23:18:01 Should return false.
103 ASSERT_FALSE(file_util::PathExists(test_unclean_));
104 ASSERT_FALSE(file_util::PathExists(collector_.powerd_suspended_file_));
105 ASSERT_NE(std::string::npos,
106 logging_.log().find("Unclean shutdown occurred while suspended."));
kmixter1 2010/11/23 23:18:01 Could you add a check here, above and in CollectFa
107 }
108
81 TEST_F(UncleanShutdownCollectorTest, Disable) { 109 TEST_F(UncleanShutdownCollectorTest, Disable) {
82 ASSERT_TRUE(collector_.Enable()); 110 ASSERT_TRUE(collector_.Enable());
83 ASSERT_TRUE(file_util::PathExists(test_unclean_)); 111 ASSERT_TRUE(file_util::PathExists(test_unclean_));
84 ASSERT_TRUE(collector_.Disable()); 112 ASSERT_TRUE(collector_.Disable());
85 ASSERT_FALSE(file_util::PathExists(test_unclean_)); 113 ASSERT_FALSE(file_util::PathExists(test_unclean_));
86 ASSERT_FALSE(collector_.Collect()); 114 ASSERT_FALSE(collector_.Collect());
87 } 115 }
88 116
89 TEST_F(UncleanShutdownCollectorTest, DisableWhenNotEnabled) { 117 TEST_F(UncleanShutdownCollectorTest, DisableWhenNotEnabled) {
90 ASSERT_TRUE(collector_.Disable()); 118 ASSERT_TRUE(collector_.Disable());
91 } 119 }
92 120
93 TEST_F(UncleanShutdownCollectorTest, CantDisable) { 121 TEST_F(UncleanShutdownCollectorTest, CantDisable) {
94 mkdir(kTestUnclean, 0700); 122 mkdir(kTestUnclean, 0700);
95 file_util::WriteFile(test_unclean_.Append("foo"), "", 0); 123 file_util::WriteFile(test_unclean_.Append("foo"), "", 0);
96 ASSERT_FALSE(collector_.Disable()); 124 ASSERT_FALSE(collector_.Disable());
97 rmdir(kTestUnclean); 125 rmdir(kTestUnclean);
98 } 126 }
99 127
100 int main(int argc, char **argv) { 128 int main(int argc, char **argv) {
101 ::testing::InitGoogleTest(&argc, argv); 129 ::testing::InitGoogleTest(&argc, argv);
102 return RUN_ALL_TESTS(); 130 return RUN_ALL_TESTS();
103 } 131 }
OLDNEW
« unclean_shutdown_collector.cc ('K') | « unclean_shutdown_collector.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698