Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(578)

Side by Side Diff: chrome/common/important_file_writer_unittest.cc

Issue 342068: Third patch in getting rid of caching MessageLoop pointers and always using C... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/common/important_file_writer.cc ('k') | chrome/common/pref_member_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/common/important_file_writer.h" 5 #include "chrome/common/important_file_writer.h"
6 6
7 #include "base/compiler_specific.h" 7 #include "base/compiler_specific.h"
8 #include "base/file_path.h" 8 #include "base/file_path.h"
9 #include "base/file_util.h" 9 #include "base/file_util.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/message_loop.h" 11 #include "base/message_loop.h"
12 #include "base/scoped_temp_dir.h" 12 #include "base/scoped_temp_dir.h"
13 #include "base/thread.h" 13 #include "base/thread.h"
14 #include "base/time.h" 14 #include "base/time.h"
15 #include "chrome/browser/chrome_thread.h"
15 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
16 17
17 namespace { 18 namespace {
18 19
19 std::string GetFileContent(const FilePath& path) { 20 std::string GetFileContent(const FilePath& path) {
20 std::string content; 21 std::string content;
21 if (!file_util::ReadFileToString(path, &content)) { 22 if (!file_util::ReadFileToString(path, &content)) {
22 NOTREACHED(); 23 NOTREACHED();
23 } 24 }
24 return content; 25 return content;
(...skipping 10 matching lines...) Expand all
35 } 36 }
36 37
37 private: 38 private:
38 const std::string data_; 39 const std::string data_;
39 }; 40 };
40 41
41 } // namespace 42 } // namespace
42 43
43 class ImportantFileWriterTest : public testing::Test { 44 class ImportantFileWriterTest : public testing::Test {
44 public: 45 public:
46 ImportantFileWriterTest() : file_thread_(ChromeThread::FILE, &loop_) { }
45 virtual void SetUp() { 47 virtual void SetUp() {
46 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 48 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
47 file_ = temp_dir_.path().AppendASCII("test-file"); 49 file_ = temp_dir_.path().AppendASCII("test-file");
48 } 50 }
49 51
50 protected: 52 protected:
51 FilePath file_; 53 FilePath file_;
54 MessageLoop loop_;
52 55
53 private: 56 private:
54 MessageLoop loop_; 57 ChromeThread file_thread_;
55 ScopedTempDir temp_dir_; 58 ScopedTempDir temp_dir_;
56 }; 59 };
57 60
58 TEST_F(ImportantFileWriterTest, WithoutBackendThread) { 61 TEST_F(ImportantFileWriterTest, Basic) {
59 ImportantFileWriter writer(file_, NULL); 62 ImportantFileWriter writer(file_);
60 EXPECT_FALSE(file_util::PathExists(writer.path())); 63 EXPECT_FALSE(file_util::PathExists(writer.path()));
61 writer.WriteNow("foo"); 64 writer.WriteNow("foo");
62 ASSERT_TRUE(file_util::PathExists(writer.path())); 65 loop_.RunAllPending();
63 EXPECT_EQ("foo", GetFileContent(writer.path()));
64 }
65
66 TEST_F(ImportantFileWriterTest, WithBackendThread) {
67 base::Thread thread("file_writer_test");
68 ASSERT_TRUE(thread.Start());
69
70 ImportantFileWriter writer(file_, &thread);
71 EXPECT_FALSE(file_util::PathExists(writer.path()));
72 writer.WriteNow("foo");
73 thread.Stop(); // Blocks until all tasks are executed.
74 66
75 ASSERT_TRUE(file_util::PathExists(writer.path())); 67 ASSERT_TRUE(file_util::PathExists(writer.path()));
76 EXPECT_EQ("foo", GetFileContent(writer.path())); 68 EXPECT_EQ("foo", GetFileContent(writer.path()));
77 } 69 }
78 70
79 TEST_F(ImportantFileWriterTest, ScheduleWrite) { 71 TEST_F(ImportantFileWriterTest, ScheduleWrite) {
80 ImportantFileWriter writer(file_, NULL); 72 ImportantFileWriter writer(file_);
81 writer.set_commit_interval(base::TimeDelta::FromMilliseconds(25)); 73 writer.set_commit_interval(base::TimeDelta::FromMilliseconds(25));
82 EXPECT_FALSE(writer.HasPendingWrite()); 74 EXPECT_FALSE(writer.HasPendingWrite());
83 DataSerializer serializer("foo"); 75 DataSerializer serializer("foo");
84 writer.ScheduleWrite(&serializer); 76 writer.ScheduleWrite(&serializer);
85 EXPECT_TRUE(writer.HasPendingWrite()); 77 EXPECT_TRUE(writer.HasPendingWrite());
86 MessageLoop::current()->PostDelayedTask(FROM_HERE, 78 MessageLoop::current()->PostDelayedTask(FROM_HERE,
87 new MessageLoop::QuitTask(), 100); 79 new MessageLoop::QuitTask(), 100);
88 MessageLoop::current()->Run(); 80 MessageLoop::current()->Run();
89 EXPECT_FALSE(writer.HasPendingWrite()); 81 EXPECT_FALSE(writer.HasPendingWrite());
90 ASSERT_TRUE(file_util::PathExists(writer.path())); 82 ASSERT_TRUE(file_util::PathExists(writer.path()));
91 EXPECT_EQ("foo", GetFileContent(writer.path())); 83 EXPECT_EQ("foo", GetFileContent(writer.path()));
92 } 84 }
93 85
94 TEST_F(ImportantFileWriterTest, DoScheduledWrite) { 86 TEST_F(ImportantFileWriterTest, DoScheduledWrite) {
95 ImportantFileWriter writer(file_, NULL); 87 ImportantFileWriter writer(file_);
96 EXPECT_FALSE(writer.HasPendingWrite()); 88 EXPECT_FALSE(writer.HasPendingWrite());
97 DataSerializer serializer("foo"); 89 DataSerializer serializer("foo");
98 writer.ScheduleWrite(&serializer); 90 writer.ScheduleWrite(&serializer);
99 EXPECT_TRUE(writer.HasPendingWrite()); 91 EXPECT_TRUE(writer.HasPendingWrite());
100 writer.DoScheduledWrite(); 92 writer.DoScheduledWrite();
93 MessageLoop::current()->PostDelayedTask(FROM_HERE,
94 new MessageLoop::QuitTask(), 100);
95 MessageLoop::current()->Run();
101 EXPECT_FALSE(writer.HasPendingWrite()); 96 EXPECT_FALSE(writer.HasPendingWrite());
102 ASSERT_TRUE(file_util::PathExists(writer.path())); 97 ASSERT_TRUE(file_util::PathExists(writer.path()));
103 EXPECT_EQ("foo", GetFileContent(writer.path())); 98 EXPECT_EQ("foo", GetFileContent(writer.path()));
104 } 99 }
105 100
106 TEST_F(ImportantFileWriterTest, BatchingWrites) { 101 TEST_F(ImportantFileWriterTest, BatchingWrites) {
107 ImportantFileWriter writer(file_, NULL); 102 ImportantFileWriter writer(file_);
108 writer.set_commit_interval(base::TimeDelta::FromMilliseconds(25)); 103 writer.set_commit_interval(base::TimeDelta::FromMilliseconds(25));
109 DataSerializer foo("foo"), bar("bar"), baz("baz"); 104 DataSerializer foo("foo"), bar("bar"), baz("baz");
110 writer.ScheduleWrite(&foo); 105 writer.ScheduleWrite(&foo);
111 writer.ScheduleWrite(&bar); 106 writer.ScheduleWrite(&bar);
112 writer.ScheduleWrite(&baz); 107 writer.ScheduleWrite(&baz);
113 MessageLoop::current()->PostDelayedTask(FROM_HERE, 108 MessageLoop::current()->PostDelayedTask(FROM_HERE,
114 new MessageLoop::QuitTask(), 100); 109 new MessageLoop::QuitTask(), 100);
115 MessageLoop::current()->Run(); 110 MessageLoop::current()->Run();
116 ASSERT_TRUE(file_util::PathExists(writer.path())); 111 ASSERT_TRUE(file_util::PathExists(writer.path()));
117 EXPECT_EQ("baz", GetFileContent(writer.path())); 112 EXPECT_EQ("baz", GetFileContent(writer.path()));
118 } 113 }
OLDNEW
« no previous file with comments | « chrome/common/important_file_writer.cc ('k') | chrome/common/pref_member_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698