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/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
10 #include "base/files/scoped_temp_dir.h" | 10 #include "base/files/scoped_temp_dir.h" |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 protected: | 54 protected: |
55 FilePath file_; | 55 FilePath file_; |
56 MessageLoop loop_; | 56 MessageLoop loop_; |
57 | 57 |
58 private: | 58 private: |
59 ScopedTempDir temp_dir_; | 59 ScopedTempDir temp_dir_; |
60 }; | 60 }; |
61 | 61 |
62 TEST_F(ImportantFileWriterTest, Basic) { | 62 TEST_F(ImportantFileWriterTest, Basic) { |
63 ImportantFileWriter writer(file_, MessageLoopProxy::current().get()); | 63 ImportantFileWriter writer(file_, MessageLoopProxy::current().get()); |
64 EXPECT_FALSE(file_util::PathExists(writer.path())); | 64 EXPECT_FALSE(PathExists(writer.path())); |
65 writer.WriteNow("foo"); | 65 writer.WriteNow("foo"); |
66 RunLoop().RunUntilIdle(); | 66 RunLoop().RunUntilIdle(); |
67 | 67 |
68 ASSERT_TRUE(file_util::PathExists(writer.path())); | 68 ASSERT_TRUE(PathExists(writer.path())); |
69 EXPECT_EQ("foo", GetFileContent(writer.path())); | 69 EXPECT_EQ("foo", GetFileContent(writer.path())); |
70 } | 70 } |
71 | 71 |
72 TEST_F(ImportantFileWriterTest, ScheduleWrite) { | 72 TEST_F(ImportantFileWriterTest, ScheduleWrite) { |
73 ImportantFileWriter writer(file_, MessageLoopProxy::current().get()); | 73 ImportantFileWriter writer(file_, MessageLoopProxy::current().get()); |
74 writer.set_commit_interval(TimeDelta::FromMilliseconds(25)); | 74 writer.set_commit_interval(TimeDelta::FromMilliseconds(25)); |
75 EXPECT_FALSE(writer.HasPendingWrite()); | 75 EXPECT_FALSE(writer.HasPendingWrite()); |
76 DataSerializer serializer("foo"); | 76 DataSerializer serializer("foo"); |
77 writer.ScheduleWrite(&serializer); | 77 writer.ScheduleWrite(&serializer); |
78 EXPECT_TRUE(writer.HasPendingWrite()); | 78 EXPECT_TRUE(writer.HasPendingWrite()); |
79 MessageLoop::current()->PostDelayedTask( | 79 MessageLoop::current()->PostDelayedTask( |
80 FROM_HERE, | 80 FROM_HERE, |
81 MessageLoop::QuitWhenIdleClosure(), | 81 MessageLoop::QuitWhenIdleClosure(), |
82 TimeDelta::FromMilliseconds(100)); | 82 TimeDelta::FromMilliseconds(100)); |
83 MessageLoop::current()->Run(); | 83 MessageLoop::current()->Run(); |
84 EXPECT_FALSE(writer.HasPendingWrite()); | 84 EXPECT_FALSE(writer.HasPendingWrite()); |
85 ASSERT_TRUE(file_util::PathExists(writer.path())); | 85 ASSERT_TRUE(PathExists(writer.path())); |
86 EXPECT_EQ("foo", GetFileContent(writer.path())); | 86 EXPECT_EQ("foo", GetFileContent(writer.path())); |
87 } | 87 } |
88 | 88 |
89 TEST_F(ImportantFileWriterTest, DoScheduledWrite) { | 89 TEST_F(ImportantFileWriterTest, DoScheduledWrite) { |
90 ImportantFileWriter writer(file_, MessageLoopProxy::current().get()); | 90 ImportantFileWriter writer(file_, MessageLoopProxy::current().get()); |
91 EXPECT_FALSE(writer.HasPendingWrite()); | 91 EXPECT_FALSE(writer.HasPendingWrite()); |
92 DataSerializer serializer("foo"); | 92 DataSerializer serializer("foo"); |
93 writer.ScheduleWrite(&serializer); | 93 writer.ScheduleWrite(&serializer); |
94 EXPECT_TRUE(writer.HasPendingWrite()); | 94 EXPECT_TRUE(writer.HasPendingWrite()); |
95 writer.DoScheduledWrite(); | 95 writer.DoScheduledWrite(); |
96 MessageLoop::current()->PostDelayedTask( | 96 MessageLoop::current()->PostDelayedTask( |
97 FROM_HERE, | 97 FROM_HERE, |
98 MessageLoop::QuitWhenIdleClosure(), | 98 MessageLoop::QuitWhenIdleClosure(), |
99 TimeDelta::FromMilliseconds(100)); | 99 TimeDelta::FromMilliseconds(100)); |
100 MessageLoop::current()->Run(); | 100 MessageLoop::current()->Run(); |
101 EXPECT_FALSE(writer.HasPendingWrite()); | 101 EXPECT_FALSE(writer.HasPendingWrite()); |
102 ASSERT_TRUE(file_util::PathExists(writer.path())); | 102 ASSERT_TRUE(PathExists(writer.path())); |
103 EXPECT_EQ("foo", GetFileContent(writer.path())); | 103 EXPECT_EQ("foo", GetFileContent(writer.path())); |
104 } | 104 } |
105 | 105 |
106 TEST_F(ImportantFileWriterTest, BatchingWrites) { | 106 TEST_F(ImportantFileWriterTest, BatchingWrites) { |
107 ImportantFileWriter writer(file_, MessageLoopProxy::current().get()); | 107 ImportantFileWriter writer(file_, MessageLoopProxy::current().get()); |
108 writer.set_commit_interval(TimeDelta::FromMilliseconds(25)); | 108 writer.set_commit_interval(TimeDelta::FromMilliseconds(25)); |
109 DataSerializer foo("foo"), bar("bar"), baz("baz"); | 109 DataSerializer foo("foo"), bar("bar"), baz("baz"); |
110 writer.ScheduleWrite(&foo); | 110 writer.ScheduleWrite(&foo); |
111 writer.ScheduleWrite(&bar); | 111 writer.ScheduleWrite(&bar); |
112 writer.ScheduleWrite(&baz); | 112 writer.ScheduleWrite(&baz); |
113 MessageLoop::current()->PostDelayedTask( | 113 MessageLoop::current()->PostDelayedTask( |
114 FROM_HERE, | 114 FROM_HERE, |
115 MessageLoop::QuitWhenIdleClosure(), | 115 MessageLoop::QuitWhenIdleClosure(), |
116 TimeDelta::FromMilliseconds(100)); | 116 TimeDelta::FromMilliseconds(100)); |
117 MessageLoop::current()->Run(); | 117 MessageLoop::current()->Run(); |
118 ASSERT_TRUE(file_util::PathExists(writer.path())); | 118 ASSERT_TRUE(PathExists(writer.path())); |
119 EXPECT_EQ("baz", GetFileContent(writer.path())); | 119 EXPECT_EQ("baz", GetFileContent(writer.path())); |
120 } | 120 } |
121 | 121 |
122 } // namespace base | 122 } // namespace base |
OLD | NEW |