blob: 84d307a1182a001f33317c816fe6ce6c2c5e47f2 [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]6658ca82010-05-20 18:20:295#include "chrome/common/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
17namespace {
18
19std::string GetFileContent(const FilePath& path) {
20 std::string content;
21 if (!file_util::ReadFileToString(path, &content)) {
22 NOTREACHED();
23 }
24 return content;
25}
26
[email protected]6c1164042009-05-08 14:41:0827class DataSerializer : public ImportantFileWriter::DataSerializer {
28 public:
29 explicit DataSerializer(const std::string& data) : data_(data) {
30 }
31
32 virtual bool SerializeData(std::string* output) {
33 output->assign(data_);
34 return true;
35 }
36
37 private:
38 const std::string data_;
39};
40
[email protected]6faa0e0d2009-04-28 06:50:3641} // namespace
42
43class ImportantFileWriterTest : public testing::Test {
44 public:
[email protected]6658ca82010-05-20 18:20:2945 ImportantFileWriterTest() { }
[email protected]6faa0e0d2009-04-28 06:50:3646 virtual void SetUp() {
47 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
48 file_ = temp_dir_.path().AppendASCII("test-file");
49 }
50
51 protected:
52 FilePath file_;
[email protected]6fad2632009-11-02 05:59:3753 MessageLoop loop_;
[email protected]6faa0e0d2009-04-28 06:50:3654
55 private:
[email protected]6faa0e0d2009-04-28 06:50:3656 ScopedTempDir temp_dir_;
57};
58
[email protected]6fad2632009-11-02 05:59:3759TEST_F(ImportantFileWriterTest, Basic) {
[email protected]6658ca82010-05-20 18:20:2960 ImportantFileWriter writer(file_,
[email protected]edd685f2011-08-15 20:33:4661 base::MessageLoopProxy::current());
[email protected]6faa0e0d2009-04-28 06:50:3662 EXPECT_FALSE(file_util::PathExists(writer.path()));
63 writer.WriteNow("foo");
[email protected]6fad2632009-11-02 05:59:3764 loop_.RunAllPending();
[email protected]6faa0e0d2009-04-28 06:50:3665
66 ASSERT_TRUE(file_util::PathExists(writer.path()));
67 EXPECT_EQ("foo", GetFileContent(writer.path()));
68}
69
70TEST_F(ImportantFileWriterTest, ScheduleWrite) {
[email protected]6658ca82010-05-20 18:20:2971 ImportantFileWriter writer(file_,
[email protected]edd685f2011-08-15 20:33:4672 base::MessageLoopProxy::current());
[email protected]6faa0e0d2009-04-28 06:50:3673 writer.set_commit_interval(base::TimeDelta::FromMilliseconds(25));
[email protected]6c1164042009-05-08 14:41:0874 EXPECT_FALSE(writer.HasPendingWrite());
75 DataSerializer serializer("foo");
76 writer.ScheduleWrite(&serializer);
77 EXPECT_TRUE(writer.HasPendingWrite());
[email protected]6faa0e0d2009-04-28 06:50:3678 MessageLoop::current()->PostDelayedTask(FROM_HERE,
[email protected]a778709f2011-12-10 00:28:1779 MessageLoop::QuitClosure(), 100);
[email protected]6faa0e0d2009-04-28 06:50:3680 MessageLoop::current()->Run();
[email protected]6c1164042009-05-08 14:41:0881 EXPECT_FALSE(writer.HasPendingWrite());
82 ASSERT_TRUE(file_util::PathExists(writer.path()));
83 EXPECT_EQ("foo", GetFileContent(writer.path()));
84}
85
86TEST_F(ImportantFileWriterTest, DoScheduledWrite) {
[email protected]6658ca82010-05-20 18:20:2987 ImportantFileWriter writer(file_,
[email protected]edd685f2011-08-15 20:33:4688 base::MessageLoopProxy::current());
[email protected]6c1164042009-05-08 14:41:0889 EXPECT_FALSE(writer.HasPendingWrite());
90 DataSerializer serializer("foo");
91 writer.ScheduleWrite(&serializer);
92 EXPECT_TRUE(writer.HasPendingWrite());
93 writer.DoScheduledWrite();
[email protected]6fad2632009-11-02 05:59:3794 MessageLoop::current()->PostDelayedTask(FROM_HERE,
[email protected]a778709f2011-12-10 00:28:1795 MessageLoop::QuitClosure(), 100);
[email protected]6fad2632009-11-02 05:59:3796 MessageLoop::current()->Run();
[email protected]6c1164042009-05-08 14:41:0897 EXPECT_FALSE(writer.HasPendingWrite());
[email protected]6faa0e0d2009-04-28 06:50:3698 ASSERT_TRUE(file_util::PathExists(writer.path()));
99 EXPECT_EQ("foo", GetFileContent(writer.path()));
100}
101
[email protected]64bb4cb32012-01-05 19:17:16102// Flaky - http://crbug.com/109292
103TEST_F(ImportantFileWriterTest, FLAKY_BatchingWrites) {
[email protected]6658ca82010-05-20 18:20:29104 ImportantFileWriter writer(file_,
[email protected]edd685f2011-08-15 20:33:46105 base::MessageLoopProxy::current());
[email protected]6faa0e0d2009-04-28 06:50:36106 writer.set_commit_interval(base::TimeDelta::FromMilliseconds(25));
[email protected]6c1164042009-05-08 14:41:08107 DataSerializer foo("foo"), bar("bar"), baz("baz");
108 writer.ScheduleWrite(&foo);
109 writer.ScheduleWrite(&bar);
110 writer.ScheduleWrite(&baz);
[email protected]6faa0e0d2009-04-28 06:50:36111 MessageLoop::current()->PostDelayedTask(FROM_HERE,
[email protected]a778709f2011-12-10 00:28:17112 MessageLoop::QuitClosure(), 100);
[email protected]6faa0e0d2009-04-28 06:50:36113 MessageLoop::current()->Run();
114 ASSERT_TRUE(file_util::PathExists(writer.path()));
115 EXPECT_EQ("baz", GetFileContent(writer.path()));
116}