blob: 81337823febcafd2fa979f0cfdc7ad41084a8d64 [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"
[email protected]6faa0e0d2009-04-28 06:50:368#include "base/file_util.h"
[email protected]57999812013-02-24 05:40:529#include "base/files/file_path.h"
[email protected]ea1a3f62012-11-16 20:34:2310#include "base/files/scoped_temp_dir.h"
[email protected]6faa0e0d2009-04-28 06:50:3611#include "base/logging.h"
12#include "base/message_loop.h"
[email protected]7ff48ca2013-02-06 16:56:1913#include "base/run_loop.h"
[email protected]34b99632011-01-01 01:01:0614#include "base/threading/thread.h"
[email protected]6faa0e0d2009-04-28 06:50:3615#include "base/time.h"
16#include "testing/gtest/include/gtest/gtest.h"
17
[email protected]43486252012-10-24 16:33:3618namespace base {
19
[email protected]6faa0e0d2009-04-28 06:50:3620namespace {
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
[email protected]6c1164042009-05-08 14:41:0830class DataSerializer : public ImportantFileWriter::DataSerializer {
31 public:
32 explicit DataSerializer(const std::string& data) : data_(data) {
33 }
34
[email protected]d5b69d92013-02-07 07:20:5535 virtual bool SerializeData(std::string* output) OVERRIDE {
[email protected]6c1164042009-05-08 14:41:0836 output->assign(data_);
37 return true;
38 }
39
40 private:
41 const std::string data_;
42};
43
[email protected]6faa0e0d2009-04-28 06:50:3644} // namespace
45
46class ImportantFileWriterTest : public testing::Test {
47 public:
[email protected]6658ca82010-05-20 18:20:2948 ImportantFileWriterTest() { }
[email protected]6faa0e0d2009-04-28 06:50:3649 virtual void SetUp() {
50 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
51 file_ = temp_dir_.path().AppendASCII("test-file");
52 }
53
54 protected:
55 FilePath file_;
[email protected]6fad2632009-11-02 05:59:3756 MessageLoop loop_;
[email protected]6faa0e0d2009-04-28 06:50:3657
58 private:
[email protected]6faa0e0d2009-04-28 06:50:3659 ScopedTempDir temp_dir_;
60};
61
[email protected]6fad2632009-11-02 05:59:3762TEST_F(ImportantFileWriterTest, Basic) {
[email protected]6658ca82010-05-20 18:20:2963 ImportantFileWriter writer(file_,
[email protected]43486252012-10-24 16:33:3664 MessageLoopProxy::current());
[email protected]6faa0e0d2009-04-28 06:50:3665 EXPECT_FALSE(file_util::PathExists(writer.path()));
66 writer.WriteNow("foo");
[email protected]7ff48ca2013-02-06 16:56:1967 RunLoop().RunUntilIdle();
[email protected]6faa0e0d2009-04-28 06:50:3668
69 ASSERT_TRUE(file_util::PathExists(writer.path()));
70 EXPECT_EQ("foo", GetFileContent(writer.path()));
71}
72
73TEST_F(ImportantFileWriterTest, ScheduleWrite) {
[email protected]6658ca82010-05-20 18:20:2974 ImportantFileWriter writer(file_,
[email protected]43486252012-10-24 16:33:3675 MessageLoopProxy::current());
76 writer.set_commit_interval(TimeDelta::FromMilliseconds(25));
[email protected]6c1164042009-05-08 14:41:0877 EXPECT_FALSE(writer.HasPendingWrite());
78 DataSerializer serializer("foo");
79 writer.ScheduleWrite(&serializer);
80 EXPECT_TRUE(writer.HasPendingWrite());
[email protected]02798a982012-01-27 00:45:3381 MessageLoop::current()->PostDelayedTask(
82 FROM_HERE,
[email protected]a085953f2013-02-04 23:40:0083 MessageLoop::QuitWhenIdleClosure(),
[email protected]43486252012-10-24 16:33:3684 TimeDelta::FromMilliseconds(100));
[email protected]6faa0e0d2009-04-28 06:50:3685 MessageLoop::current()->Run();
[email protected]6c1164042009-05-08 14:41:0886 EXPECT_FALSE(writer.HasPendingWrite());
87 ASSERT_TRUE(file_util::PathExists(writer.path()));
88 EXPECT_EQ("foo", GetFileContent(writer.path()));
89}
90
91TEST_F(ImportantFileWriterTest, DoScheduledWrite) {
[email protected]6658ca82010-05-20 18:20:2992 ImportantFileWriter writer(file_,
[email protected]43486252012-10-24 16:33:3693 MessageLoopProxy::current());
[email protected]6c1164042009-05-08 14:41:0894 EXPECT_FALSE(writer.HasPendingWrite());
95 DataSerializer serializer("foo");
96 writer.ScheduleWrite(&serializer);
97 EXPECT_TRUE(writer.HasPendingWrite());
98 writer.DoScheduledWrite();
[email protected]02798a982012-01-27 00:45:3399 MessageLoop::current()->PostDelayedTask(
100 FROM_HERE,
[email protected]a085953f2013-02-04 23:40:00101 MessageLoop::QuitWhenIdleClosure(),
[email protected]43486252012-10-24 16:33:36102 TimeDelta::FromMilliseconds(100));
[email protected]6fad2632009-11-02 05:59:37103 MessageLoop::current()->Run();
[email protected]6c1164042009-05-08 14:41:08104 EXPECT_FALSE(writer.HasPendingWrite());
[email protected]6faa0e0d2009-04-28 06:50:36105 ASSERT_TRUE(file_util::PathExists(writer.path()));
106 EXPECT_EQ("foo", GetFileContent(writer.path()));
107}
108
[email protected]b9a8ee4d2012-11-08 04:29:12109TEST_F(ImportantFileWriterTest, BatchingWrites) {
[email protected]6658ca82010-05-20 18:20:29110 ImportantFileWriter writer(file_,
[email protected]43486252012-10-24 16:33:36111 MessageLoopProxy::current());
112 writer.set_commit_interval(TimeDelta::FromMilliseconds(25));
[email protected]6c1164042009-05-08 14:41:08113 DataSerializer foo("foo"), bar("bar"), baz("baz");
114 writer.ScheduleWrite(&foo);
115 writer.ScheduleWrite(&bar);
116 writer.ScheduleWrite(&baz);
[email protected]02798a982012-01-27 00:45:33117 MessageLoop::current()->PostDelayedTask(
118 FROM_HERE,
[email protected]a085953f2013-02-04 23:40:00119 MessageLoop::QuitWhenIdleClosure(),
[email protected]43486252012-10-24 16:33:36120 TimeDelta::FromMilliseconds(100));
[email protected]6faa0e0d2009-04-28 06:50:36121 MessageLoop::current()->Run();
122 ASSERT_TRUE(file_util::PathExists(writer.path()));
123 EXPECT_EQ("baz", GetFileContent(writer.path()));
124}
[email protected]43486252012-10-24 16:33:36125
126} // namespace base