OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/common/important_file_writer.h" | |
6 | |
7 #include "base/compiler_specific.h" | |
8 #include "base/file_path.h" | |
9 #include "base/file_util.h" | |
10 #include "base/logging.h" | |
11 #include "base/message_loop.h" | |
12 #include "base/scoped_temp_dir.h" | |
13 #include "base/threading/thread.h" | |
14 #include "base/time.h" | |
15 #include "testing/gtest/include/gtest/gtest.h" | |
16 | |
17 namespace { | |
18 | |
19 std::string GetFileContent(const FilePath& path) { | |
20 std::string content; | |
21 if (!file_util::ReadFileToString(path, &content)) { | |
22 NOTREACHED(); | |
23 } | |
24 return content; | |
25 } | |
26 | |
27 class DataSerializer : public ImportantFileWriter::DataSerializer { | |
28 public: | |
29 explicit DataSerializer(const std::string& data) : data_(data) { | |
30 } | |
31 | |
32 virtual bool SerializeData(std::string* output) { | |
33 output->assign(data_); | |
34 return true; | |
35 } | |
36 | |
37 private: | |
38 const std::string data_; | |
39 }; | |
40 | |
41 } // namespace | |
42 | |
43 class ImportantFileWriterTest : public testing::Test { | |
44 public: | |
45 ImportantFileWriterTest() { } | |
46 virtual void SetUp() { | |
47 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
48 file_ = temp_dir_.path().AppendASCII("test-file"); | |
49 } | |
50 | |
51 protected: | |
52 FilePath file_; | |
53 MessageLoop loop_; | |
54 | |
55 private: | |
56 ScopedTempDir temp_dir_; | |
57 }; | |
58 | |
59 TEST_F(ImportantFileWriterTest, Basic) { | |
60 ImportantFileWriter writer(file_, | |
61 base::MessageLoopProxy::current()); | |
62 EXPECT_FALSE(file_util::PathExists(writer.path())); | |
63 writer.WriteNow("foo"); | |
64 loop_.RunAllPending(); | |
65 | |
66 ASSERT_TRUE(file_util::PathExists(writer.path())); | |
67 EXPECT_EQ("foo", GetFileContent(writer.path())); | |
68 } | |
69 | |
70 TEST_F(ImportantFileWriterTest, ScheduleWrite) { | |
71 ImportantFileWriter writer(file_, | |
72 base::MessageLoopProxy::current()); | |
73 writer.set_commit_interval(base::TimeDelta::FromMilliseconds(25)); | |
74 EXPECT_FALSE(writer.HasPendingWrite()); | |
75 DataSerializer serializer("foo"); | |
76 writer.ScheduleWrite(&serializer); | |
77 EXPECT_TRUE(writer.HasPendingWrite()); | |
78 MessageLoop::current()->PostDelayedTask( | |
79 FROM_HERE, | |
80 MessageLoop::QuitClosure(), | |
81 base::TimeDelta::FromMilliseconds(100)); | |
82 MessageLoop::current()->Run(); | |
83 EXPECT_FALSE(writer.HasPendingWrite()); | |
84 ASSERT_TRUE(file_util::PathExists(writer.path())); | |
85 EXPECT_EQ("foo", GetFileContent(writer.path())); | |
86 } | |
87 | |
88 TEST_F(ImportantFileWriterTest, DoScheduledWrite) { | |
89 ImportantFileWriter writer(file_, | |
90 base::MessageLoopProxy::current()); | |
91 EXPECT_FALSE(writer.HasPendingWrite()); | |
92 DataSerializer serializer("foo"); | |
93 writer.ScheduleWrite(&serializer); | |
94 EXPECT_TRUE(writer.HasPendingWrite()); | |
95 writer.DoScheduledWrite(); | |
96 MessageLoop::current()->PostDelayedTask( | |
97 FROM_HERE, | |
98 MessageLoop::QuitClosure(), | |
99 base::TimeDelta::FromMilliseconds(100)); | |
100 MessageLoop::current()->Run(); | |
101 EXPECT_FALSE(writer.HasPendingWrite()); | |
102 ASSERT_TRUE(file_util::PathExists(writer.path())); | |
103 EXPECT_EQ("foo", GetFileContent(writer.path())); | |
104 } | |
105 | |
106 // Flaky - http://crbug.com/109292 | |
107 TEST_F(ImportantFileWriterTest, DISABLED_BatchingWrites) { | |
108 ImportantFileWriter writer(file_, | |
109 base::MessageLoopProxy::current()); | |
110 writer.set_commit_interval(base::TimeDelta::FromMilliseconds(25)); | |
111 DataSerializer foo("foo"), bar("bar"), baz("baz"); | |
112 writer.ScheduleWrite(&foo); | |
113 writer.ScheduleWrite(&bar); | |
114 writer.ScheduleWrite(&baz); | |
115 MessageLoop::current()->PostDelayedTask( | |
116 FROM_HERE, | |
117 MessageLoop::QuitClosure(), | |
118 base::TimeDelta::FromMilliseconds(100)); | |
119 MessageLoop::current()->Run(); | |
120 ASSERT_TRUE(file_util::PathExists(writer.path())); | |
121 EXPECT_EQ("baz", GetFileContent(writer.path())); | |
122 } | |
OLD | NEW |