OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 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 "chrome/common/important_file_writer.h" |
| 6 |
| 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" |
| 12 #include "base/scoped_temp_dir.h" |
| 13 #include "base/thread.h" |
| 14 #include "base/time.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 |
| 17 namespace { |
| 18 |
| 19 std::string GetFileContent(const FilePath& path) { |
| 20 std::string content; |
| 21 if (!file_util::ReadFileToString(path, &content)) { |
| 22 NOTREACHED(); |
| 23 } |
| 24 return content; |
| 25 } |
| 26 |
| 27 } // namespace |
| 28 |
| 29 class ImportantFileWriterTest : public testing::Test { |
| 30 public: |
| 31 virtual void SetUp() { |
| 32 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 33 file_ = temp_dir_.path().AppendASCII("test-file"); |
| 34 } |
| 35 |
| 36 protected: |
| 37 FilePath file_; |
| 38 |
| 39 private: |
| 40 MessageLoop loop_; |
| 41 ScopedTempDir temp_dir_; |
| 42 }; |
| 43 |
| 44 TEST_F(ImportantFileWriterTest, WithoutBackendThread) { |
| 45 ImportantFileWriter writer(file_, NULL); |
| 46 EXPECT_FALSE(file_util::PathExists(writer.path())); |
| 47 writer.WriteNow("foo"); |
| 48 ASSERT_TRUE(file_util::PathExists(writer.path())); |
| 49 EXPECT_EQ("foo", GetFileContent(writer.path())); |
| 50 } |
| 51 |
| 52 TEST_F(ImportantFileWriterTest, WithBackendThread) { |
| 53 base::Thread thread("file_writer_test"); |
| 54 ASSERT_TRUE(thread.Start()); |
| 55 |
| 56 ImportantFileWriter writer(file_, &thread); |
| 57 EXPECT_FALSE(file_util::PathExists(writer.path())); |
| 58 writer.WriteNow("foo"); |
| 59 thread.Stop(); // Blocks until all tasks are executed. |
| 60 |
| 61 ASSERT_TRUE(file_util::PathExists(writer.path())); |
| 62 EXPECT_EQ("foo", GetFileContent(writer.path())); |
| 63 } |
| 64 |
| 65 TEST_F(ImportantFileWriterTest, ScheduleWrite) { |
| 66 ImportantFileWriter writer(file_, NULL); |
| 67 writer.set_commit_interval(base::TimeDelta::FromMilliseconds(25)); |
| 68 writer.ScheduleWrite("foo"); |
| 69 MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 70 new MessageLoop::QuitTask(), 100); |
| 71 MessageLoop::current()->Run(); |
| 72 ASSERT_TRUE(file_util::PathExists(writer.path())); |
| 73 EXPECT_EQ("foo", GetFileContent(writer.path())); |
| 74 } |
| 75 |
| 76 TEST_F(ImportantFileWriterTest, BatchingWrites) { |
| 77 ImportantFileWriter writer(file_, NULL); |
| 78 writer.set_commit_interval(base::TimeDelta::FromMilliseconds(25)); |
| 79 writer.ScheduleWrite("foo"); |
| 80 writer.ScheduleWrite("bar"); |
| 81 writer.ScheduleWrite("baz"); |
| 82 MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 83 new MessageLoop::QuitTask(), 100); |
| 84 MessageLoop::current()->Run(); |
| 85 ASSERT_TRUE(file_util::PathExists(writer.path())); |
| 86 EXPECT_EQ("baz", GetFileContent(writer.path())); |
| 87 } |
| 88 |
| 89 TEST_F(ImportantFileWriterTest, WriteOnDestruction) { |
| 90 { |
| 91 ImportantFileWriter writer(file_, NULL); |
| 92 writer.ScheduleWrite("foo"); |
| 93 } |
| 94 ASSERT_TRUE(file_util::PathExists(file_)); |
| 95 EXPECT_EQ("foo", GetFileContent(file_)); |
| 96 } |
OLD | NEW |