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" | 12 #include "base/location.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/memory/ptr_util.h" |
15 #include "base/run_loop.h" | 16 #include "base/run_loop.h" |
16 #include "base/single_thread_task_runner.h" | 17 #include "base/single_thread_task_runner.h" |
17 #include "base/thread_task_runner_handle.h" | 18 #include "base/thread_task_runner_handle.h" |
18 #include "base/threading/thread.h" | 19 #include "base/threading/thread.h" |
19 #include "base/time/time.h" | 20 #include "base/time/time.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
21 | 22 |
22 namespace base { | 23 namespace base { |
23 | 24 |
24 namespace { | 25 namespace { |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 MessageLoop loop_; | 97 MessageLoop loop_; |
97 | 98 |
98 private: | 99 private: |
99 ScopedTempDir temp_dir_; | 100 ScopedTempDir temp_dir_; |
100 }; | 101 }; |
101 | 102 |
102 TEST_F(ImportantFileWriterTest, Basic) { | 103 TEST_F(ImportantFileWriterTest, Basic) { |
103 ImportantFileWriter writer(file_, ThreadTaskRunnerHandle::Get()); | 104 ImportantFileWriter writer(file_, ThreadTaskRunnerHandle::Get()); |
104 EXPECT_FALSE(PathExists(writer.path())); | 105 EXPECT_FALSE(PathExists(writer.path())); |
105 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); | 106 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); |
106 writer.WriteNow(make_scoped_ptr(new std::string("foo"))); | 107 writer.WriteNow(WrapUnique(new std::string("foo"))); |
107 RunLoop().RunUntilIdle(); | 108 RunLoop().RunUntilIdle(); |
108 | 109 |
109 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); | 110 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); |
110 ASSERT_TRUE(PathExists(writer.path())); | 111 ASSERT_TRUE(PathExists(writer.path())); |
111 EXPECT_EQ("foo", GetFileContent(writer.path())); | 112 EXPECT_EQ("foo", GetFileContent(writer.path())); |
112 } | 113 } |
113 | 114 |
114 TEST_F(ImportantFileWriterTest, BasicWithSuccessfulWriteObserver) { | 115 TEST_F(ImportantFileWriterTest, BasicWithSuccessfulWriteObserver) { |
115 ImportantFileWriter writer(file_, ThreadTaskRunnerHandle::Get()); | 116 ImportantFileWriter writer(file_, ThreadTaskRunnerHandle::Get()); |
116 EXPECT_FALSE(PathExists(writer.path())); | 117 EXPECT_FALSE(PathExists(writer.path())); |
117 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); | 118 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); |
118 successful_write_observer_.ObserveNextSuccessfulWrite(&writer); | 119 successful_write_observer_.ObserveNextSuccessfulWrite(&writer); |
119 writer.WriteNow(make_scoped_ptr(new std::string("foo"))); | 120 writer.WriteNow(WrapUnique(new std::string("foo"))); |
120 RunLoop().RunUntilIdle(); | 121 RunLoop().RunUntilIdle(); |
121 | 122 |
122 // Confirm that the observer is invoked. | 123 // Confirm that the observer is invoked. |
123 EXPECT_TRUE(successful_write_observer_.GetAndResetObservationState()); | 124 EXPECT_TRUE(successful_write_observer_.GetAndResetObservationState()); |
124 ASSERT_TRUE(PathExists(writer.path())); | 125 ASSERT_TRUE(PathExists(writer.path())); |
125 EXPECT_EQ("foo", GetFileContent(writer.path())); | 126 EXPECT_EQ("foo", GetFileContent(writer.path())); |
126 | 127 |
127 // Confirm that re-installing the observer works for another write. | 128 // Confirm that re-installing the observer works for another write. |
128 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); | 129 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); |
129 successful_write_observer_.ObserveNextSuccessfulWrite(&writer); | 130 successful_write_observer_.ObserveNextSuccessfulWrite(&writer); |
130 writer.WriteNow(make_scoped_ptr(new std::string("bar"))); | 131 writer.WriteNow(WrapUnique(new std::string("bar"))); |
131 RunLoop().RunUntilIdle(); | 132 RunLoop().RunUntilIdle(); |
132 | 133 |
133 EXPECT_TRUE(successful_write_observer_.GetAndResetObservationState()); | 134 EXPECT_TRUE(successful_write_observer_.GetAndResetObservationState()); |
134 ASSERT_TRUE(PathExists(writer.path())); | 135 ASSERT_TRUE(PathExists(writer.path())); |
135 EXPECT_EQ("bar", GetFileContent(writer.path())); | 136 EXPECT_EQ("bar", GetFileContent(writer.path())); |
136 | 137 |
137 // Confirm that writing again without re-installing the observer doesn't | 138 // Confirm that writing again without re-installing the observer doesn't |
138 // result in a notification. | 139 // result in a notification. |
139 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); | 140 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); |
140 writer.WriteNow(make_scoped_ptr(new std::string("baz"))); | 141 writer.WriteNow(WrapUnique(new std::string("baz"))); |
141 RunLoop().RunUntilIdle(); | 142 RunLoop().RunUntilIdle(); |
142 | 143 |
143 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); | 144 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); |
144 ASSERT_TRUE(PathExists(writer.path())); | 145 ASSERT_TRUE(PathExists(writer.path())); |
145 EXPECT_EQ("baz", GetFileContent(writer.path())); | 146 EXPECT_EQ("baz", GetFileContent(writer.path())); |
146 } | 147 } |
147 | 148 |
148 TEST_F(ImportantFileWriterTest, ScheduleWrite) { | 149 TEST_F(ImportantFileWriterTest, ScheduleWrite) { |
149 ImportantFileWriter writer(file_, | 150 ImportantFileWriter writer(file_, |
150 ThreadTaskRunnerHandle::Get(), | 151 ThreadTaskRunnerHandle::Get(), |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 writer.ScheduleWrite(&baz); | 189 writer.ScheduleWrite(&baz); |
189 ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 190 ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
190 FROM_HERE, MessageLoop::QuitWhenIdleClosure(), | 191 FROM_HERE, MessageLoop::QuitWhenIdleClosure(), |
191 TimeDelta::FromMilliseconds(100)); | 192 TimeDelta::FromMilliseconds(100)); |
192 MessageLoop::current()->Run(); | 193 MessageLoop::current()->Run(); |
193 ASSERT_TRUE(PathExists(writer.path())); | 194 ASSERT_TRUE(PathExists(writer.path())); |
194 EXPECT_EQ("baz", GetFileContent(writer.path())); | 195 EXPECT_EQ("baz", GetFileContent(writer.path())); |
195 } | 196 } |
196 | 197 |
197 } // namespace base | 198 } // namespace base |
OLD | NEW |