Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / base / files / important_file_writer_unittest.cc
blob81337823febcafd2fa979f0cfdc7ad41084a8d64
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"
18 namespace base {
20 namespace {
22 std::string GetFileContent(const FilePath& path) {
23 std::string content;
24 if (!file_util::ReadFileToString(path, &content)) {
25 NOTREACHED();
27 return content;
30 class DataSerializer : public ImportantFileWriter::DataSerializer {
31 public:
32 explicit DataSerializer(const std::string& data) : data_(data) {
35 virtual bool SerializeData(std::string* output) OVERRIDE {
36 output->assign(data_);
37 return true;
40 private:
41 const std::string data_;
44 } // namespace
46 class ImportantFileWriterTest : public testing::Test {
47 public:
48 ImportantFileWriterTest() { }
49 virtual void SetUp() {
50 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
51 file_ = temp_dir_.path().AppendASCII("test-file");
54 protected:
55 FilePath file_;
56 MessageLoop loop_;
58 private:
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(
82 FROM_HERE,
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(
100 FROM_HERE,
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(
118 FROM_HERE,
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()));
126 } // namespace base