OLD | NEW |
| (Empty) |
1 // Copyright 2013 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/browser/chromeos/drive/webkit_file_stream_reader_impl.h" | |
6 | |
7 #include <string> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/bind_helpers.h" | |
11 #include "base/memory/scoped_ptr.h" | |
12 #include "base/threading/thread.h" | |
13 #include "base/time/time.h" | |
14 #include "chrome/browser/chromeos/drive/fake_file_system.h" | |
15 #include "chrome/browser/chromeos/drive/file_system_interface.h" | |
16 #include "chrome/browser/chromeos/drive/file_system_util.h" | |
17 #include "chrome/browser/chromeos/drive/test_util.h" | |
18 #include "chrome/browser/drive/fake_drive_service.h" | |
19 #include "content/public/test/test_browser_thread_bundle.h" | |
20 #include "google_apis/drive/time_util.h" | |
21 #include "net/base/io_buffer.h" | |
22 #include "net/base/net_errors.h" | |
23 #include "net/base/test_completion_callback.h" | |
24 #include "testing/gtest/include/gtest/gtest.h" | |
25 | |
26 namespace drive { | |
27 namespace internal { | |
28 | |
29 class WebkitFileStreamReaderImplTest : public ::testing::Test { | |
30 protected: | |
31 // Because the testee should live on IO thread, the main thread is | |
32 // reused as IO thread, and UI thread will be run on background. | |
33 WebkitFileStreamReaderImplTest() | |
34 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { | |
35 } | |
36 | |
37 virtual void SetUp() OVERRIDE { | |
38 worker_thread_.reset(new base::Thread("WebkitFileStreamReaderImplTest")); | |
39 ASSERT_TRUE(worker_thread_->Start()); | |
40 | |
41 // Initialize FakeDriveService. | |
42 fake_drive_service_.reset(new FakeDriveService); | |
43 ASSERT_TRUE(fake_drive_service_->LoadResourceListForWapi( | |
44 "gdata/root_feed.json")); | |
45 ASSERT_TRUE(fake_drive_service_->LoadAccountMetadataForWapi( | |
46 "gdata/account_metadata.json")); | |
47 | |
48 // Create a testee instance. | |
49 fake_file_system_.reset( | |
50 new test_util::FakeFileSystem(fake_drive_service_.get())); | |
51 } | |
52 | |
53 FileSystemInterface* GetFileSystem() { | |
54 return fake_file_system_.get(); | |
55 } | |
56 | |
57 DriveFileStreamReader::FileSystemGetter GetFileSystemGetter() { | |
58 return base::Bind(&WebkitFileStreamReaderImplTest::GetFileSystem, | |
59 base::Unretained(this)); | |
60 } | |
61 | |
62 content::TestBrowserThreadBundle thread_bundle_; | |
63 | |
64 scoped_ptr<base::Thread> worker_thread_; | |
65 | |
66 scoped_ptr<FakeDriveService> fake_drive_service_; | |
67 scoped_ptr<test_util::FakeFileSystem> fake_file_system_; | |
68 }; | |
69 | |
70 TEST_F(WebkitFileStreamReaderImplTest, ReadThenGetLength) { | |
71 const base::FilePath kDriveFile = | |
72 util::GetDriveMyDriveRootPath().AppendASCII("File 1.txt"); | |
73 | |
74 scoped_ptr<WebkitFileStreamReaderImpl> reader(new WebkitFileStreamReaderImpl( | |
75 GetFileSystemGetter(), | |
76 worker_thread_->message_loop_proxy().get(), | |
77 kDriveFile, | |
78 0, // offset | |
79 base::Time())); // expected modification time | |
80 | |
81 std::string content; | |
82 ASSERT_EQ(net::OK, test_util::ReadAllData(reader.get(), &content)); | |
83 | |
84 net::TestInt64CompletionCallback callback; | |
85 int64 length = reader->GetLength(callback.callback()); | |
86 length = callback.GetResult(length); | |
87 EXPECT_EQ(content.size(), static_cast<size_t>(length)); | |
88 } | |
89 | |
90 TEST_F(WebkitFileStreamReaderImplTest, GetLengthThenRead) { | |
91 const base::FilePath kDriveFile = | |
92 util::GetDriveMyDriveRootPath().AppendASCII("File 1.txt"); | |
93 | |
94 scoped_ptr<WebkitFileStreamReaderImpl> reader(new WebkitFileStreamReaderImpl( | |
95 GetFileSystemGetter(), | |
96 worker_thread_->message_loop_proxy().get(), | |
97 kDriveFile, | |
98 0, // offset | |
99 base::Time())); // expected modification time | |
100 | |
101 net::TestInt64CompletionCallback callback; | |
102 int64 length = reader->GetLength(callback.callback()); | |
103 length = callback.GetResult(length); | |
104 | |
105 std::string content; | |
106 ASSERT_EQ(net::OK, test_util::ReadAllData(reader.get(), &content)); | |
107 EXPECT_EQ(content.size(), static_cast<size_t>(length)); | |
108 } | |
109 | |
110 TEST_F(WebkitFileStreamReaderImplTest, ReadWithOffset) { | |
111 const base::FilePath kDriveFile = | |
112 util::GetDriveMyDriveRootPath().AppendASCII("File 1.txt"); | |
113 const int kOffset = 5; | |
114 | |
115 scoped_ptr<WebkitFileStreamReaderImpl> reader(new WebkitFileStreamReaderImpl( | |
116 GetFileSystemGetter(), | |
117 worker_thread_->message_loop_proxy().get(), | |
118 kDriveFile, | |
119 kOffset, | |
120 base::Time())); // expected modification time | |
121 | |
122 std::string content; | |
123 ASSERT_EQ(net::OK, test_util::ReadAllData(reader.get(), &content)); | |
124 | |
125 net::TestInt64CompletionCallback callback; | |
126 int64 length = reader->GetLength(callback.callback()); | |
127 length = callback.GetResult(length); | |
128 EXPECT_EQ(content.size() + kOffset, static_cast<size_t>(length)); | |
129 } | |
130 | |
131 TEST_F(WebkitFileStreamReaderImplTest, ReadError) { | |
132 const base::FilePath kDriveFile = | |
133 util::GetDriveMyDriveRootPath().AppendASCII("non-existing.txt"); | |
134 | |
135 scoped_ptr<WebkitFileStreamReaderImpl> reader(new WebkitFileStreamReaderImpl( | |
136 GetFileSystemGetter(), | |
137 worker_thread_->message_loop_proxy().get(), | |
138 kDriveFile, | |
139 0, // offset | |
140 base::Time())); // expected modification time | |
141 | |
142 const int kBufferSize = 10; | |
143 scoped_refptr<net::IOBuffer> io_buffer(new net::IOBuffer(kBufferSize)); | |
144 net::TestCompletionCallback callback; | |
145 int result = reader->Read(io_buffer.get(), kBufferSize, callback.callback()); | |
146 result = callback.GetResult(result); | |
147 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, result); | |
148 } | |
149 | |
150 TEST_F(WebkitFileStreamReaderImplTest, GetLengthError) { | |
151 const base::FilePath kDriveFile = | |
152 util::GetDriveMyDriveRootPath().AppendASCII("non-existing.txt"); | |
153 | |
154 scoped_ptr<WebkitFileStreamReaderImpl> reader(new WebkitFileStreamReaderImpl( | |
155 GetFileSystemGetter(), | |
156 worker_thread_->message_loop_proxy().get(), | |
157 kDriveFile, | |
158 0, // offset | |
159 base::Time())); // expected modification time | |
160 | |
161 net::TestInt64CompletionCallback callback; | |
162 int64 result = reader->GetLength(callback.callback()); | |
163 result = callback.GetResult(result); | |
164 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, result); | |
165 } | |
166 | |
167 TEST_F(WebkitFileStreamReaderImplTest, LastModification) { | |
168 const base::FilePath kDriveFile = | |
169 util::GetDriveMyDriveRootPath().AppendASCII("File 1.txt"); | |
170 | |
171 base::Time expected_modification_time; | |
172 ASSERT_TRUE(google_apis::util::GetTimeFromString( | |
173 "2011-12-14T00:40:47.330Z", &expected_modification_time)); | |
174 scoped_ptr<WebkitFileStreamReaderImpl> reader( | |
175 new WebkitFileStreamReaderImpl(GetFileSystemGetter(), | |
176 worker_thread_->message_loop_proxy().get(), | |
177 kDriveFile, | |
178 0, // offset | |
179 expected_modification_time)); | |
180 | |
181 net::TestInt64CompletionCallback callback; | |
182 int64 result = reader->GetLength(callback.callback()); | |
183 result = callback.GetResult(result); | |
184 | |
185 std::string content; | |
186 ASSERT_EQ(net::OK, test_util::ReadAllData(reader.get(), &content)); | |
187 EXPECT_GE(content.size(), static_cast<size_t>(result)); | |
188 } | |
189 | |
190 TEST_F(WebkitFileStreamReaderImplTest, LastModificationError) { | |
191 const base::FilePath kDriveFile = | |
192 util::GetDriveMyDriveRootPath().AppendASCII("File 1.txt"); | |
193 | |
194 scoped_ptr<WebkitFileStreamReaderImpl> reader( | |
195 new WebkitFileStreamReaderImpl(GetFileSystemGetter(), | |
196 worker_thread_->message_loop_proxy().get(), | |
197 kDriveFile, | |
198 0, // offset | |
199 base::Time::FromInternalValue(1))); | |
200 | |
201 net::TestInt64CompletionCallback callback; | |
202 int64 result = reader->GetLength(callback.callback()); | |
203 result = callback.GetResult(result); | |
204 EXPECT_EQ(net::ERR_UPLOAD_FILE_CHANGED, result); | |
205 } | |
206 | |
207 } // namespace internal | |
208 } // namespace drive | |
OLD | NEW |