blob: a284d38709ea79c09dd96949269f280d950a3254 [file] [log] [blame]
[email protected]64bb4cb32012-01-05 19:17:161// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]6faa0e0d2009-04-28 06:50:362// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]43486252012-10-24 16:33:365#include "base/files/important_file_writer.h"
[email protected]6faa0e0d2009-04-28 06:50:366
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"
[email protected]e0785902011-05-19 23:34:1712#include "base/scoped_temp_dir.h"
[email protected]34b99632011-01-01 01:01:0613#include "base/threading/thread.h"
[email protected]6faa0e0d2009-04-28 06:50:3614#include "base/time.h"
15#include "testing/gtest/include/gtest/gtest.h"
16
[email protected]43486252012-10-24 16:33:3617namespace base {
18
[email protected]6faa0e0d2009-04-28 06:50:3619namespace {
20
21std::string GetFileContent(const FilePath& path) {
22 std::string content;
23 if (!file_util::ReadFileToString(path, &content)) {
24 NOTREACHED();
25 }
26 return content;
27}
28
[email protected]6c1164042009-05-08 14:41:0829class DataSerializer : public ImportantFileWriter::DataSerializer {
30 public:
31 explicit DataSerializer(const std::string& data) : data_(data) {
32 }
33
34 virtual bool SerializeData(std::string* output) {
35 output->assign(data_);
36 return true;
37 }
38
39 private:
40 const std::string data_;
41};
42
[email protected]6faa0e0d2009-04-28 06:50:3643} // namespace
44
45class ImportantFileWriterTest : public testing::Test {
46 public:
[email protected]6658ca82010-05-20 18:20:2947 ImportantFileWriterTest() { }
[email protected]6faa0e0d2009-04-28 06:50:3648 virtual void SetUp() {
49 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
50 file_ = temp_dir_.path().AppendASCII("test-file");
51 }
52
53 protected:
54 FilePath file_;
[email protected]6fad2632009-11-02 05:59:3755 MessageLoop loop_;
[email protected]6faa0e0d2009-04-28 06:50:3656
57 private:
[email protected]6faa0e0d2009-04-28 06:50:3658 ScopedTempDir temp_dir_;
59};
60
[email protected]6fad2632009-11-02 05:59:3761TEST_F(ImportantFileWriterTest, Basic) {
[email protected]6658ca82010-05-20 18:20:2962 ImportantFileWriter writer(file_,
[email protected]43486252012-10-24 16:33:3663 MessageLoopProxy::current());
[email protected]6faa0e0d2009-04-28 06:50:3664 EXPECT_FALSE(file_util::PathExists(writer.path()));
65 writer.WriteNow("foo");
[email protected]1f18bfe2012-10-30 01:02:5566 loop_.RunUntilIdle();
[email protected]6faa0e0d2009-04-28 06:50:3667
68 ASSERT_TRUE(file_util::PathExists(writer.path()));
69 EXPECT_EQ("foo", GetFileContent(writer.path()));
70}
71
72TEST_F(ImportantFileWriterTest, ScheduleWrite) {
[email protected]6658ca82010-05-20 18:20:2973 ImportantFileWriter writer(file_,
[email protected]43486252012-10-24 16:33:3674 MessageLoopProxy::current());
75 writer.set_commit_interval(TimeDelta::FromMilliseconds(25));
[email protected]6c1164042009-05-08 14:41:0876 EXPECT_FALSE(writer.HasPendingWrite());
77 DataSerializer serializer("foo");
78 writer.ScheduleWrite(&serializer);
79 EXPECT_TRUE(writer.HasPendingWrite());
[email protected]02798a982012-01-27 00:45:3380 MessageLoop::current()->PostDelayedTask(
81 FROM_HERE,
82 MessageLoop::QuitClosure(),
[email protected]43486252012-10-24 16:33:3683 TimeDelta::FromMilliseconds(100));
[email protected]6faa0e0d2009-04-28 06:50:3684 MessageLoop::current()->Run();
[email protected]6c1164042009-05-08 14:41:0885 EXPECT_FALSE(writer.HasPendingWrite());
86 ASSERT_TRUE(file_util::PathExists(writer.path()));
87 EXPECT_EQ("foo", GetFileContent(writer.path()));
88}
89
90TEST_F(ImportantFileWriterTest, DoScheduledWrite) {
[email protected]6658ca82010-05-20 18:20:2991 ImportantFileWriter writer(file_,
[email protected]43486252012-10-24 16:33:3692 MessageLoopProxy::current());
[email protected]6c1164042009-05-08 14:41:0893 EXPECT_FALSE(writer.HasPendingWrite());
94 DataSerializer serializer("foo");
95 writer.ScheduleWrite(&serializer);
96 EXPECT_TRUE(writer.HasPendingWrite());
97 writer.DoScheduledWrite();
[email protected]02798a982012-01-27 00:45:3398 MessageLoop::current()->PostDelayedTask(
99 FROM_HERE,
100 MessageLoop::QuitClosure(),
[email protected]43486252012-10-24 16:33:36101 TimeDelta::FromMilliseconds(100));
[email protected]6fad2632009-11-02 05:59:37102 MessageLoop::current()->Run();
[email protected]6c1164042009-05-08 14:41:08103 EXPECT_FALSE(writer.HasPendingWrite());
[email protected]6faa0e0d2009-04-28 06:50:36104 ASSERT_TRUE(file_util::PathExists(writer.path()));
105 EXPECT_EQ("foo", GetFileContent(writer.path()));
106}
107
[email protected]b9a8ee4d2012-11-08 04:29:12108TEST_F(ImportantFileWriterTest, BatchingWrites) {
[email protected]6658ca82010-05-20 18:20:29109 ImportantFileWriter writer(file_,
[email protected]43486252012-10-24 16:33:36110 MessageLoopProxy::current());
111 writer.set_commit_interval(TimeDelta::FromMilliseconds(25));
[email protected]6c1164042009-05-08 14:41:08112 DataSerializer foo("foo"), bar("bar"), baz("baz");
113 writer.ScheduleWrite(&foo);
114 writer.ScheduleWrite(&bar);
115 writer.ScheduleWrite(&baz);
[email protected]02798a982012-01-27 00:45:33116 MessageLoop::current()->PostDelayedTask(
117 FROM_HERE,
118 MessageLoop::QuitClosure(),
[email protected]43486252012-10-24 16:33:36119 TimeDelta::FromMilliseconds(100));
[email protected]6faa0e0d2009-04-28 06:50:36120 MessageLoop::current()->Run();
121 ASSERT_TRUE(file_util::PathExists(writer.path()));
122 EXPECT_EQ("baz", GetFileContent(writer.path()));
123}
[email protected]43486252012-10-24 16:33:36124
125} // namespace base