OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/common/important_file_writer.h" | 5 #include "base/files/important_file_writer.h" |
6 | 6 |
7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
8 #include "base/file_path.h" | 8 #include "base/file_path.h" |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
12 #include "base/scoped_temp_dir.h" | 12 #include "base/scoped_temp_dir.h" |
13 #include "base/threading/thread.h" | 13 #include "base/threading/thread.h" |
14 #include "base/time.h" | 14 #include "base/time.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
16 | 16 |
| 17 namespace base { |
| 18 |
17 namespace { | 19 namespace { |
18 | 20 |
19 std::string GetFileContent(const FilePath& path) { | 21 std::string GetFileContent(const FilePath& path) { |
20 std::string content; | 22 std::string content; |
21 if (!file_util::ReadFileToString(path, &content)) { | 23 if (!file_util::ReadFileToString(path, &content)) { |
22 NOTREACHED(); | 24 NOTREACHED(); |
23 } | 25 } |
24 return content; | 26 return content; |
25 } | 27 } |
26 | 28 |
(...skipping 24 matching lines...) Expand all Loading... |
51 protected: | 53 protected: |
52 FilePath file_; | 54 FilePath file_; |
53 MessageLoop loop_; | 55 MessageLoop loop_; |
54 | 56 |
55 private: | 57 private: |
56 ScopedTempDir temp_dir_; | 58 ScopedTempDir temp_dir_; |
57 }; | 59 }; |
58 | 60 |
59 TEST_F(ImportantFileWriterTest, Basic) { | 61 TEST_F(ImportantFileWriterTest, Basic) { |
60 ImportantFileWriter writer(file_, | 62 ImportantFileWriter writer(file_, |
61 base::MessageLoopProxy::current()); | 63 MessageLoopProxy::current()); |
62 EXPECT_FALSE(file_util::PathExists(writer.path())); | 64 EXPECT_FALSE(file_util::PathExists(writer.path())); |
63 writer.WriteNow("foo"); | 65 writer.WriteNow("foo"); |
64 loop_.RunAllPending(); | 66 loop_.RunAllPending(); |
65 | 67 |
66 ASSERT_TRUE(file_util::PathExists(writer.path())); | 68 ASSERT_TRUE(file_util::PathExists(writer.path())); |
67 EXPECT_EQ("foo", GetFileContent(writer.path())); | 69 EXPECT_EQ("foo", GetFileContent(writer.path())); |
68 } | 70 } |
69 | 71 |
70 TEST_F(ImportantFileWriterTest, ScheduleWrite) { | 72 TEST_F(ImportantFileWriterTest, ScheduleWrite) { |
71 ImportantFileWriter writer(file_, | 73 ImportantFileWriter writer(file_, |
72 base::MessageLoopProxy::current()); | 74 MessageLoopProxy::current()); |
73 writer.set_commit_interval(base::TimeDelta::FromMilliseconds(25)); | 75 writer.set_commit_interval(TimeDelta::FromMilliseconds(25)); |
74 EXPECT_FALSE(writer.HasPendingWrite()); | 76 EXPECT_FALSE(writer.HasPendingWrite()); |
75 DataSerializer serializer("foo"); | 77 DataSerializer serializer("foo"); |
76 writer.ScheduleWrite(&serializer); | 78 writer.ScheduleWrite(&serializer); |
77 EXPECT_TRUE(writer.HasPendingWrite()); | 79 EXPECT_TRUE(writer.HasPendingWrite()); |
78 MessageLoop::current()->PostDelayedTask( | 80 MessageLoop::current()->PostDelayedTask( |
79 FROM_HERE, | 81 FROM_HERE, |
80 MessageLoop::QuitClosure(), | 82 MessageLoop::QuitClosure(), |
81 base::TimeDelta::FromMilliseconds(100)); | 83 TimeDelta::FromMilliseconds(100)); |
82 MessageLoop::current()->Run(); | 84 MessageLoop::current()->Run(); |
83 EXPECT_FALSE(writer.HasPendingWrite()); | 85 EXPECT_FALSE(writer.HasPendingWrite()); |
84 ASSERT_TRUE(file_util::PathExists(writer.path())); | 86 ASSERT_TRUE(file_util::PathExists(writer.path())); |
85 EXPECT_EQ("foo", GetFileContent(writer.path())); | 87 EXPECT_EQ("foo", GetFileContent(writer.path())); |
86 } | 88 } |
87 | 89 |
88 TEST_F(ImportantFileWriterTest, DoScheduledWrite) { | 90 TEST_F(ImportantFileWriterTest, DoScheduledWrite) { |
89 ImportantFileWriter writer(file_, | 91 ImportantFileWriter writer(file_, |
90 base::MessageLoopProxy::current()); | 92 MessageLoopProxy::current()); |
91 EXPECT_FALSE(writer.HasPendingWrite()); | 93 EXPECT_FALSE(writer.HasPendingWrite()); |
92 DataSerializer serializer("foo"); | 94 DataSerializer serializer("foo"); |
93 writer.ScheduleWrite(&serializer); | 95 writer.ScheduleWrite(&serializer); |
94 EXPECT_TRUE(writer.HasPendingWrite()); | 96 EXPECT_TRUE(writer.HasPendingWrite()); |
95 writer.DoScheduledWrite(); | 97 writer.DoScheduledWrite(); |
96 MessageLoop::current()->PostDelayedTask( | 98 MessageLoop::current()->PostDelayedTask( |
97 FROM_HERE, | 99 FROM_HERE, |
98 MessageLoop::QuitClosure(), | 100 MessageLoop::QuitClosure(), |
99 base::TimeDelta::FromMilliseconds(100)); | 101 TimeDelta::FromMilliseconds(100)); |
100 MessageLoop::current()->Run(); | 102 MessageLoop::current()->Run(); |
101 EXPECT_FALSE(writer.HasPendingWrite()); | 103 EXPECT_FALSE(writer.HasPendingWrite()); |
102 ASSERT_TRUE(file_util::PathExists(writer.path())); | 104 ASSERT_TRUE(file_util::PathExists(writer.path())); |
103 EXPECT_EQ("foo", GetFileContent(writer.path())); | 105 EXPECT_EQ("foo", GetFileContent(writer.path())); |
104 } | 106 } |
105 | 107 |
106 // Flaky - http://crbug.com/109292 | 108 // Flaky - http://crbug.com/109292 |
107 TEST_F(ImportantFileWriterTest, DISABLED_BatchingWrites) { | 109 TEST_F(ImportantFileWriterTest, DISABLED_BatchingWrites) { |
108 ImportantFileWriter writer(file_, | 110 ImportantFileWriter writer(file_, |
109 base::MessageLoopProxy::current()); | 111 MessageLoopProxy::current()); |
110 writer.set_commit_interval(base::TimeDelta::FromMilliseconds(25)); | 112 writer.set_commit_interval(TimeDelta::FromMilliseconds(25)); |
111 DataSerializer foo("foo"), bar("bar"), baz("baz"); | 113 DataSerializer foo("foo"), bar("bar"), baz("baz"); |
112 writer.ScheduleWrite(&foo); | 114 writer.ScheduleWrite(&foo); |
113 writer.ScheduleWrite(&bar); | 115 writer.ScheduleWrite(&bar); |
114 writer.ScheduleWrite(&baz); | 116 writer.ScheduleWrite(&baz); |
115 MessageLoop::current()->PostDelayedTask( | 117 MessageLoop::current()->PostDelayedTask( |
116 FROM_HERE, | 118 FROM_HERE, |
117 MessageLoop::QuitClosure(), | 119 MessageLoop::QuitClosure(), |
118 base::TimeDelta::FromMilliseconds(100)); | 120 TimeDelta::FromMilliseconds(100)); |
119 MessageLoop::current()->Run(); | 121 MessageLoop::current()->Run(); |
120 ASSERT_TRUE(file_util::PathExists(writer.path())); | 122 ASSERT_TRUE(file_util::PathExists(writer.path())); |
121 EXPECT_EQ("baz", GetFileContent(writer.path())); | 123 EXPECT_EQ("baz", GetFileContent(writer.path())); |
122 } | 124 } |
| 125 |
| 126 } // namespace base |
OLD | NEW |