important_file_writer_unittest.cc revision eb525c5499e34cc9c4b825d6d9e75bb07cc06ace
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 "base/files/important_file_writer.h"
6
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/time.h"
16#include "testing/gtest/include/gtest/gtest.h"
17
18namespace base {
19
20namespace {
21
22std::string GetFileContent(const FilePath& path) {
23  std::string content;
24  if (!file_util::ReadFileToString(path, &content)) {
25    NOTREACHED();
26  }
27  return content;
28}
29
30class DataSerializer : public ImportantFileWriter::DataSerializer {
31 public:
32  explicit DataSerializer(const std::string& data) : data_(data) {
33  }
34
35  virtual bool SerializeData(std::string* output) OVERRIDE {
36    output->assign(data_);
37    return true;
38  }
39
40 private:
41  const std::string data_;
42};
43
44}  // namespace
45
46class 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");
52  }
53
54 protected:
55  FilePath file_;
56  MessageLoop loop_;
57
58 private:
59  ScopedTempDir temp_dir_;
60};
61
62TEST_F(ImportantFileWriterTest, Basic) {
63  ImportantFileWriter writer(file_, MessageLoopProxy::current().get());
64  EXPECT_FALSE(file_util::PathExists(writer.path()));
65  writer.WriteNow("foo");
66  RunLoop().RunUntilIdle();
67
68  ASSERT_TRUE(file_util::PathExists(writer.path()));
69  EXPECT_EQ("foo", GetFileContent(writer.path()));
70}
71
72TEST_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(
80      FROM_HERE,
81      MessageLoop::QuitWhenIdleClosure(),
82      TimeDelta::FromMilliseconds(100));
83  MessageLoop::current()->Run();
84  EXPECT_FALSE(writer.HasPendingWrite());
85  ASSERT_TRUE(file_util::PathExists(writer.path()));
86  EXPECT_EQ("foo", GetFileContent(writer.path()));
87}
88
89TEST_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(
97      FROM_HERE,
98      MessageLoop::QuitWhenIdleClosure(),
99      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
106TEST_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(
114      FROM_HERE,
115      MessageLoop::QuitWhenIdleClosure(),
116      TimeDelta::FromMilliseconds(100));
117  MessageLoop::current()->Run();
118  ASSERT_TRUE(file_util::PathExists(writer.path()));
119  EXPECT_EQ("baz", GetFileContent(writer.path()));
120}
121
122}  // namespace base
123