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/message_loop.h"
13 #include "base/run_loop.h"
14 #include "base/threading/thread.h"
15 #include "base/time/time.h"
16 #include "testing/gtest/include/gtest/gtest.h"
22 std::string
GetFileContent(const FilePath
& path
) {
24 if (!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_
, MessageLoopProxy::current().get());
64 EXPECT_FALSE(PathExists(writer
.path()));
65 writer
.WriteNow("foo");
66 RunLoop().RunUntilIdle();
68 ASSERT_TRUE(PathExists(writer
.path()));
69 EXPECT_EQ("foo", GetFileContent(writer
.path()));
72 TEST_F(ImportantFileWriterTest
, ScheduleWrite
) {
73 ImportantFileWriter
writer(file_
, MessageLoopProxy::current().get());
74 writer
.set_commit_interval(TimeDelta::FromMilliseconds(25));
75 EXPECT_FALSE(writer
.HasPendingWrite());
76 DataSerializer
serializer("foo");
77 writer
.ScheduleWrite(&serializer
);
78 EXPECT_TRUE(writer
.HasPendingWrite());
79 MessageLoop::current()->PostDelayedTask(
81 MessageLoop::QuitWhenIdleClosure(),
82 TimeDelta::FromMilliseconds(100));
83 MessageLoop::current()->Run();
84 EXPECT_FALSE(writer
.HasPendingWrite());
85 ASSERT_TRUE(PathExists(writer
.path()));
86 EXPECT_EQ("foo", GetFileContent(writer
.path()));
89 TEST_F(ImportantFileWriterTest
, DoScheduledWrite
) {
90 ImportantFileWriter
writer(file_
, MessageLoopProxy::current().get());
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(
98 MessageLoop::QuitWhenIdleClosure(),
99 TimeDelta::FromMilliseconds(100));
100 MessageLoop::current()->Run();
101 EXPECT_FALSE(writer
.HasPendingWrite());
102 ASSERT_TRUE(PathExists(writer
.path()));
103 EXPECT_EQ("foo", GetFileContent(writer
.path()));
106 TEST_F(ImportantFileWriterTest
, BatchingWrites
) {
107 ImportantFileWriter
writer(file_
, MessageLoopProxy::current().get());
108 writer
.set_commit_interval(TimeDelta::FromMilliseconds(25));
109 DataSerializer
foo("foo"), bar("bar"), baz("baz");
110 writer
.ScheduleWrite(&foo
);
111 writer
.ScheduleWrite(&bar
);
112 writer
.ScheduleWrite(&baz
);
113 MessageLoop::current()->PostDelayedTask(
115 MessageLoop::QuitWhenIdleClosure(),
116 TimeDelta::FromMilliseconds(100));
117 MessageLoop::current()->Run();
118 ASSERT_TRUE(PathExists(writer
.path()));
119 EXPECT_EQ("baz", GetFileContent(writer
.path()));