summaryrefslogtreecommitdiffstats
path: root/chrome/common/important_file_writer_unittest.cc
diff options
context:
space:
mode:
authorphajdan.jr@chromium.org <phajdan.jr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-04-28 06:50:36 +0000
committerphajdan.jr@chromium.org <phajdan.jr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-04-28 06:50:36 +0000
commit6faa0e0d23ca6fc27ae603063ce23eb018a670cd (patch)
treece8256337ad0632cd3cdb797394ffeb1500e9280 /chrome/common/important_file_writer_unittest.cc
parent7b441021461106603e4b7769305b8fce7db17294 (diff)
downloadchromium_src-6faa0e0d23ca6fc27ae603063ce23eb018a670cd.zip
chromium_src-6faa0e0d23ca6fc27ae603063ce23eb018a670cd.tar.gz
chromium_src-6faa0e0d23ca6fc27ae603063ce23eb018a670cd.tar.bz2
ImportantFileWriter
Introducing a class for writing important files, preventing their corruption during writing. Switched PrefService to use it. Other classes will be switched in future changesets. TEST=This may affect things using preferences. Make sure that changes in preferences don't get lost, and that you don't get excessive disk activity when changing preferences. http://crbug.com/10618 Review URL: http://codereview.chromium.org/83001 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@14717 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/common/important_file_writer_unittest.cc')
-rw-r--r--chrome/common/important_file_writer_unittest.cc96
1 files changed, 96 insertions, 0 deletions
diff --git a/chrome/common/important_file_writer_unittest.cc b/chrome/common/important_file_writer_unittest.cc
new file mode 100644
index 0000000..09703a5
--- /dev/null
+++ b/chrome/common/important_file_writer_unittest.cc
@@ -0,0 +1,96 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/common/important_file_writer.h"
+
+#include "base/compiler_specific.h"
+#include "base/file_path.h"
+#include "base/file_util.h"
+#include "base/logging.h"
+#include "base/message_loop.h"
+#include "base/scoped_temp_dir.h"
+#include "base/thread.h"
+#include "base/time.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace {
+
+std::string GetFileContent(const FilePath& path) {
+ std::string content;
+ if (!file_util::ReadFileToString(path, &content)) {
+ NOTREACHED();
+ }
+ return content;
+}
+
+} // namespace
+
+class ImportantFileWriterTest : public testing::Test {
+ public:
+ virtual void SetUp() {
+ ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
+ file_ = temp_dir_.path().AppendASCII("test-file");
+ }
+
+ protected:
+ FilePath file_;
+
+ private:
+ MessageLoop loop_;
+ ScopedTempDir temp_dir_;
+};
+
+TEST_F(ImportantFileWriterTest, WithoutBackendThread) {
+ ImportantFileWriter writer(file_, NULL);
+ EXPECT_FALSE(file_util::PathExists(writer.path()));
+ writer.WriteNow("foo");
+ ASSERT_TRUE(file_util::PathExists(writer.path()));
+ EXPECT_EQ("foo", GetFileContent(writer.path()));
+}
+
+TEST_F(ImportantFileWriterTest, WithBackendThread) {
+ base::Thread thread("file_writer_test");
+ ASSERT_TRUE(thread.Start());
+
+ ImportantFileWriter writer(file_, &thread);
+ EXPECT_FALSE(file_util::PathExists(writer.path()));
+ writer.WriteNow("foo");
+ thread.Stop(); // Blocks until all tasks are executed.
+
+ ASSERT_TRUE(file_util::PathExists(writer.path()));
+ EXPECT_EQ("foo", GetFileContent(writer.path()));
+}
+
+TEST_F(ImportantFileWriterTest, ScheduleWrite) {
+ ImportantFileWriter writer(file_, NULL);
+ writer.set_commit_interval(base::TimeDelta::FromMilliseconds(25));
+ writer.ScheduleWrite("foo");
+ MessageLoop::current()->PostDelayedTask(FROM_HERE,
+ new MessageLoop::QuitTask(), 100);
+ MessageLoop::current()->Run();
+ ASSERT_TRUE(file_util::PathExists(writer.path()));
+ EXPECT_EQ("foo", GetFileContent(writer.path()));
+}
+
+TEST_F(ImportantFileWriterTest, BatchingWrites) {
+ ImportantFileWriter writer(file_, NULL);
+ writer.set_commit_interval(base::TimeDelta::FromMilliseconds(25));
+ writer.ScheduleWrite("foo");
+ writer.ScheduleWrite("bar");
+ writer.ScheduleWrite("baz");
+ MessageLoop::current()->PostDelayedTask(FROM_HERE,
+ new MessageLoop::QuitTask(), 100);
+ MessageLoop::current()->Run();
+ ASSERT_TRUE(file_util::PathExists(writer.path()));
+ EXPECT_EQ("baz", GetFileContent(writer.path()));
+}
+
+TEST_F(ImportantFileWriterTest, WriteOnDestruction) {
+ {
+ ImportantFileWriter writer(file_, NULL);
+ writer.ScheduleWrite("foo");
+ }
+ ASSERT_TRUE(file_util::PathExists(file_));
+ EXPECT_EQ("foo", GetFileContent(file_));
+}