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.
5 #include "base/files/important_file_writer.h"
7 #include "base/compiler_specific.h"
8 #include "base/file_util.h"
9 #include "base/files/file_path.h"
10 #include "base/files/scoped_temp_dir.h"
11 #include "base/logging.h"
12 #include "base/message_loop.h"
13 #include "base/run_loop.h"
14 #include "base/threading/thread.h"
15 #include "base/time.h"
16 #include "testing/gtest/include/gtest/gtest.h"
22 std::string
GetFileContent(const FilePath
& path
) {
24 if (!file_util::ReadFileToString(path
, &content
)) {
30 class DataSerializer
: public ImportantFileWriter::DataSerializer
{
32 explicit DataSerializer(const std::string
& data
) : data_(data
) {
35 virtual bool SerializeData(std::string
* output
) OVERRIDE
{
36 output
->assign(data_
);
41 const std::string data_
;
46 class ImportantFileWriterTest
: public testing::Test
{
48 ImportantFileWriterTest() { }
49 virtual void SetUp() {
50 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
51 file_
= temp_dir_
.path().AppendASCII("test-file");
59 ScopedTempDir temp_dir_
;
62 TEST_F(ImportantFileWriterTest
, Basic
) {
63 ImportantFileWriter
writer(file_
,
64 MessageLoopProxy::current());
65 EXPECT_FALSE(file_util::PathExists(writer
.path()));
66 writer
.WriteNow("foo");
67 RunLoop().RunUntilIdle();
69 ASSERT_TRUE(file_util::PathExists(writer
.path()));
70 EXPECT_EQ("foo", GetFileContent(writer
.path()));
73 TEST_F(ImportantFileWriterTest
, ScheduleWrite
) {
74 ImportantFileWriter
writer(file_
,
75 MessageLoopProxy::current());
76 writer
.set_commit_interval(TimeDelta::FromMilliseconds(25));
77 EXPECT_FALSE(writer
.HasPendingWrite());
78 DataSerializer
serializer("foo");
79 writer
.ScheduleWrite(&serializer
);
80 EXPECT_TRUE(writer
.HasPendingWrite());
81 MessageLoop::current()->PostDelayedTask(
83 MessageLoop::QuitWhenIdleClosure(),
84 TimeDelta::FromMilliseconds(100));
85 MessageLoop::current()->Run();
86 EXPECT_FALSE(writer
.HasPendingWrite());
87 ASSERT_TRUE(file_util::PathExists(writer
.path()));
88 EXPECT_EQ("foo", GetFileContent(writer
.path()));
91 TEST_F(ImportantFileWriterTest
, DoScheduledWrite
) {
92 ImportantFileWriter
writer(file_
,
93 MessageLoopProxy::current());
94 EXPECT_FALSE(writer
.HasPendingWrite());
95 DataSerializer
serializer("foo");
96 writer
.ScheduleWrite(&serializer
);
97 EXPECT_TRUE(writer
.HasPendingWrite());
98 writer
.DoScheduledWrite();
99 MessageLoop::current()->PostDelayedTask(
101 MessageLoop::QuitWhenIdleClosure(),
102 TimeDelta::FromMilliseconds(100));
103 MessageLoop::current()->Run();
104 EXPECT_FALSE(writer
.HasPendingWrite());
105 ASSERT_TRUE(file_util::PathExists(writer
.path()));
106 EXPECT_EQ("foo", GetFileContent(writer
.path()));
109 TEST_F(ImportantFileWriterTest
, BatchingWrites
) {
110 ImportantFileWriter
writer(file_
,
111 MessageLoopProxy::current());
112 writer
.set_commit_interval(TimeDelta::FromMilliseconds(25));
113 DataSerializer
foo("foo"), bar("bar"), baz("baz");
114 writer
.ScheduleWrite(&foo
);
115 writer
.ScheduleWrite(&bar
);
116 writer
.ScheduleWrite(&baz
);
117 MessageLoop::current()->PostDelayedTask(
119 MessageLoop::QuitWhenIdleClosure(),
120 TimeDelta::FromMilliseconds(100));
121 MessageLoop::current()->Run();
122 ASSERT_TRUE(file_util::PathExists(writer
.path()));
123 EXPECT_EQ("baz", GetFileContent(writer
.path()));