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 "base/files/important_file_writer.h" | 5 #include "base/files/important_file_writer.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
11 #include "base/files/scoped_temp_dir.h" | 11 #include "base/files/scoped_temp_dir.h" |
12 #include "base/location.h" | |
13 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/message_loop/message_loop.h" |
14 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
15 #include "base/single_thread_task_runner.h" | |
16 #include "base/thread_task_runner_handle.h" | |
17 #include "base/threading/thread.h" | 15 #include "base/threading/thread.h" |
18 #include "base/time/time.h" | 16 #include "base/time/time.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
20 | 18 |
21 namespace base { | 19 namespace base { |
22 | 20 |
23 namespace { | 21 namespace { |
24 | 22 |
25 std::string GetFileContent(const FilePath& path) { | 23 std::string GetFileContent(const FilePath& path) { |
26 std::string content; | 24 std::string content; |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 protected: | 90 protected: |
93 SuccessfulWriteObserver successful_write_observer_; | 91 SuccessfulWriteObserver successful_write_observer_; |
94 FilePath file_; | 92 FilePath file_; |
95 MessageLoop loop_; | 93 MessageLoop loop_; |
96 | 94 |
97 private: | 95 private: |
98 ScopedTempDir temp_dir_; | 96 ScopedTempDir temp_dir_; |
99 }; | 97 }; |
100 | 98 |
101 TEST_F(ImportantFileWriterTest, Basic) { | 99 TEST_F(ImportantFileWriterTest, Basic) { |
102 ImportantFileWriter writer(file_, ThreadTaskRunnerHandle::Get()); | 100 ImportantFileWriter writer(file_, MessageLoopProxy::current().get()); |
103 EXPECT_FALSE(PathExists(writer.path())); | 101 EXPECT_FALSE(PathExists(writer.path())); |
104 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); | 102 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); |
105 writer.WriteNow(make_scoped_ptr(new std::string("foo"))); | 103 writer.WriteNow(make_scoped_ptr(new std::string("foo"))); |
106 RunLoop().RunUntilIdle(); | 104 RunLoop().RunUntilIdle(); |
107 | 105 |
108 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); | 106 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); |
109 ASSERT_TRUE(PathExists(writer.path())); | 107 ASSERT_TRUE(PathExists(writer.path())); |
110 EXPECT_EQ("foo", GetFileContent(writer.path())); | 108 EXPECT_EQ("foo", GetFileContent(writer.path())); |
111 } | 109 } |
112 | 110 |
113 TEST_F(ImportantFileWriterTest, BasicWithSuccessfulWriteObserver) { | 111 TEST_F(ImportantFileWriterTest, BasicWithSuccessfulWriteObserver) { |
114 ImportantFileWriter writer(file_, ThreadTaskRunnerHandle::Get()); | 112 ImportantFileWriter writer(file_, MessageLoopProxy::current().get()); |
115 EXPECT_FALSE(PathExists(writer.path())); | 113 EXPECT_FALSE(PathExists(writer.path())); |
116 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); | 114 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); |
117 successful_write_observer_.ObserveNextSuccessfulWrite(&writer); | 115 successful_write_observer_.ObserveNextSuccessfulWrite(&writer); |
118 writer.WriteNow(make_scoped_ptr(new std::string("foo"))); | 116 writer.WriteNow(make_scoped_ptr(new std::string("foo"))); |
119 RunLoop().RunUntilIdle(); | 117 RunLoop().RunUntilIdle(); |
120 | 118 |
121 // Confirm that the observer is invoked. | 119 // Confirm that the observer is invoked. |
122 EXPECT_TRUE(successful_write_observer_.GetAndResetObservationState()); | 120 EXPECT_TRUE(successful_write_observer_.GetAndResetObservationState()); |
123 ASSERT_TRUE(PathExists(writer.path())); | 121 ASSERT_TRUE(PathExists(writer.path())); |
124 EXPECT_EQ("foo", GetFileContent(writer.path())); | 122 EXPECT_EQ("foo", GetFileContent(writer.path())); |
(...skipping 13 matching lines...) Expand all Loading... |
138 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); | 136 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); |
139 writer.WriteNow(make_scoped_ptr(new std::string("baz"))); | 137 writer.WriteNow(make_scoped_ptr(new std::string("baz"))); |
140 RunLoop().RunUntilIdle(); | 138 RunLoop().RunUntilIdle(); |
141 | 139 |
142 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); | 140 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); |
143 ASSERT_TRUE(PathExists(writer.path())); | 141 ASSERT_TRUE(PathExists(writer.path())); |
144 EXPECT_EQ("baz", GetFileContent(writer.path())); | 142 EXPECT_EQ("baz", GetFileContent(writer.path())); |
145 } | 143 } |
146 | 144 |
147 TEST_F(ImportantFileWriterTest, ScheduleWrite) { | 145 TEST_F(ImportantFileWriterTest, ScheduleWrite) { |
148 ImportantFileWriter writer(file_, ThreadTaskRunnerHandle::Get()); | 146 ImportantFileWriter writer(file_, MessageLoopProxy::current().get()); |
149 writer.set_commit_interval(TimeDelta::FromMilliseconds(25)); | 147 writer.set_commit_interval(TimeDelta::FromMilliseconds(25)); |
150 EXPECT_FALSE(writer.HasPendingWrite()); | 148 EXPECT_FALSE(writer.HasPendingWrite()); |
151 DataSerializer serializer("foo"); | 149 DataSerializer serializer("foo"); |
152 writer.ScheduleWrite(&serializer); | 150 writer.ScheduleWrite(&serializer); |
153 EXPECT_TRUE(writer.HasPendingWrite()); | 151 EXPECT_TRUE(writer.HasPendingWrite()); |
154 ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 152 MessageLoop::current()->PostDelayedTask( |
155 FROM_HERE, MessageLoop::QuitWhenIdleClosure(), | 153 FROM_HERE, |
| 154 MessageLoop::QuitWhenIdleClosure(), |
156 TimeDelta::FromMilliseconds(100)); | 155 TimeDelta::FromMilliseconds(100)); |
157 MessageLoop::current()->Run(); | 156 MessageLoop::current()->Run(); |
158 EXPECT_FALSE(writer.HasPendingWrite()); | 157 EXPECT_FALSE(writer.HasPendingWrite()); |
159 ASSERT_TRUE(PathExists(writer.path())); | 158 ASSERT_TRUE(PathExists(writer.path())); |
160 EXPECT_EQ("foo", GetFileContent(writer.path())); | 159 EXPECT_EQ("foo", GetFileContent(writer.path())); |
161 } | 160 } |
162 | 161 |
163 TEST_F(ImportantFileWriterTest, DoScheduledWrite) { | 162 TEST_F(ImportantFileWriterTest, DoScheduledWrite) { |
164 ImportantFileWriter writer(file_, ThreadTaskRunnerHandle::Get()); | 163 ImportantFileWriter writer(file_, MessageLoopProxy::current().get()); |
165 EXPECT_FALSE(writer.HasPendingWrite()); | 164 EXPECT_FALSE(writer.HasPendingWrite()); |
166 DataSerializer serializer("foo"); | 165 DataSerializer serializer("foo"); |
167 writer.ScheduleWrite(&serializer); | 166 writer.ScheduleWrite(&serializer); |
168 EXPECT_TRUE(writer.HasPendingWrite()); | 167 EXPECT_TRUE(writer.HasPendingWrite()); |
169 writer.DoScheduledWrite(); | 168 writer.DoScheduledWrite(); |
170 ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 169 MessageLoop::current()->PostDelayedTask( |
171 FROM_HERE, MessageLoop::QuitWhenIdleClosure(), | 170 FROM_HERE, |
| 171 MessageLoop::QuitWhenIdleClosure(), |
172 TimeDelta::FromMilliseconds(100)); | 172 TimeDelta::FromMilliseconds(100)); |
173 MessageLoop::current()->Run(); | 173 MessageLoop::current()->Run(); |
174 EXPECT_FALSE(writer.HasPendingWrite()); | 174 EXPECT_FALSE(writer.HasPendingWrite()); |
175 ASSERT_TRUE(PathExists(writer.path())); | 175 ASSERT_TRUE(PathExists(writer.path())); |
176 EXPECT_EQ("foo", GetFileContent(writer.path())); | 176 EXPECT_EQ("foo", GetFileContent(writer.path())); |
177 } | 177 } |
178 | 178 |
179 TEST_F(ImportantFileWriterTest, BatchingWrites) { | 179 TEST_F(ImportantFileWriterTest, BatchingWrites) { |
180 ImportantFileWriter writer(file_, ThreadTaskRunnerHandle::Get()); | 180 ImportantFileWriter writer(file_, MessageLoopProxy::current().get()); |
181 writer.set_commit_interval(TimeDelta::FromMilliseconds(25)); | 181 writer.set_commit_interval(TimeDelta::FromMilliseconds(25)); |
182 DataSerializer foo("foo"), bar("bar"), baz("baz"); | 182 DataSerializer foo("foo"), bar("bar"), baz("baz"); |
183 writer.ScheduleWrite(&foo); | 183 writer.ScheduleWrite(&foo); |
184 writer.ScheduleWrite(&bar); | 184 writer.ScheduleWrite(&bar); |
185 writer.ScheduleWrite(&baz); | 185 writer.ScheduleWrite(&baz); |
186 ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 186 MessageLoop::current()->PostDelayedTask( |
187 FROM_HERE, MessageLoop::QuitWhenIdleClosure(), | 187 FROM_HERE, |
| 188 MessageLoop::QuitWhenIdleClosure(), |
188 TimeDelta::FromMilliseconds(100)); | 189 TimeDelta::FromMilliseconds(100)); |
189 MessageLoop::current()->Run(); | 190 MessageLoop::current()->Run(); |
190 ASSERT_TRUE(PathExists(writer.path())); | 191 ASSERT_TRUE(PathExists(writer.path())); |
191 EXPECT_EQ("baz", GetFileContent(writer.path())); | 192 EXPECT_EQ("baz", GetFileContent(writer.path())); |
192 } | 193 } |
193 | 194 |
194 } // namespace base | 195 } // namespace base |
OLD | NEW |