1 // Copyright 2014 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.
5 #include "chrome/browser/chromeos/drive/fileapi/webkit_file_stream_reader_impl.h"
10 #include "base/bind_helpers.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/run_loop.h"
13 #include "base/threading/thread.h"
14 #include "base/time/time.h"
15 #include "chrome/browser/chromeos/drive/fake_file_system.h"
16 #include "chrome/browser/chromeos/drive/file_system_interface.h"
17 #include "chrome/browser/chromeos/drive/file_system_util.h"
18 #include "chrome/browser/chromeos/drive/test_util.h"
19 #include "chrome/browser/drive/fake_drive_service.h"
20 #include "chrome/browser/drive/test_util.h"
21 #include "content/public/test/test_browser_thread_bundle.h"
22 #include "google_apis/drive/drive_api_parser.h"
23 #include "google_apis/drive/time_util.h"
24 #include "net/base/io_buffer.h"
25 #include "net/base/net_errors.h"
26 #include "net/base/test_completion_callback.h"
27 #include "testing/gtest/include/gtest/gtest.h"
32 class WebkitFileStreamReaderImplTest
: public ::testing::Test
{
34 // Because the testee should live on IO thread, the main thread is
35 // reused as IO thread, and UI thread will be run on background.
36 WebkitFileStreamReaderImplTest()
37 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP
) {
40 void SetUp() override
{
41 worker_thread_
.reset(new base::Thread("WebkitFileStreamReaderImplTest"));
42 ASSERT_TRUE(worker_thread_
->Start());
44 // Initialize FakeDriveService.
45 fake_drive_service_
.reset(new FakeDriveService
);
46 ASSERT_TRUE(test_util::SetUpTestEntries(fake_drive_service_
.get()));
48 // Create a testee instance.
49 fake_file_system_
.reset(
50 new test_util::FakeFileSystem(fake_drive_service_
.get()));
53 FileSystemInterface
* GetFileSystem() {
54 return fake_file_system_
.get();
57 DriveFileStreamReader::FileSystemGetter
GetFileSystemGetter() {
58 return base::Bind(&WebkitFileStreamReaderImplTest::GetFileSystem
,
59 base::Unretained(this));
62 content::TestBrowserThreadBundle thread_bundle_
;
64 scoped_ptr
<base::Thread
> worker_thread_
;
66 scoped_ptr
<FakeDriveService
> fake_drive_service_
;
67 scoped_ptr
<test_util::FakeFileSystem
> fake_file_system_
;
70 TEST_F(WebkitFileStreamReaderImplTest
, ReadThenGetLength
) {
71 const base::FilePath kDriveFile
=
72 util::GetDriveMyDriveRootPath().AppendASCII("File 1.txt");
74 scoped_ptr
<WebkitFileStreamReaderImpl
> reader(new WebkitFileStreamReaderImpl(
75 GetFileSystemGetter(),
76 worker_thread_
->message_loop_proxy().get(),
79 base::Time())); // expected modification time
82 ASSERT_EQ(net::OK
, test_util::ReadAllData(reader
.get(), &content
));
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
));
90 TEST_F(WebkitFileStreamReaderImplTest
, GetLengthThenRead
) {
91 const base::FilePath kDriveFile
=
92 util::GetDriveMyDriveRootPath().AppendASCII("File 1.txt");
94 scoped_ptr
<WebkitFileStreamReaderImpl
> reader(new WebkitFileStreamReaderImpl(
95 GetFileSystemGetter(),
96 worker_thread_
->message_loop_proxy().get(),
99 base::Time())); // expected modification time
101 net::TestInt64CompletionCallback callback
;
102 int64 length
= reader
->GetLength(callback
.callback());
103 length
= callback
.GetResult(length
);
106 ASSERT_EQ(net::OK
, test_util::ReadAllData(reader
.get(), &content
));
107 EXPECT_EQ(content
.size(), static_cast<size_t>(length
));
110 TEST_F(WebkitFileStreamReaderImplTest
, ReadWithOffset
) {
111 const base::FilePath kDriveFile
=
112 util::GetDriveMyDriveRootPath().AppendASCII("File 1.txt");
113 const int kOffset
= 5;
115 scoped_ptr
<WebkitFileStreamReaderImpl
> reader(new WebkitFileStreamReaderImpl(
116 GetFileSystemGetter(),
117 worker_thread_
->message_loop_proxy().get(),
120 base::Time())); // expected modification time
123 ASSERT_EQ(net::OK
, test_util::ReadAllData(reader
.get(), &content
));
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
));
131 TEST_F(WebkitFileStreamReaderImplTest
, ReadError
) {
132 const base::FilePath kDriveFile
=
133 util::GetDriveMyDriveRootPath().AppendASCII("non-existing.txt");
135 scoped_ptr
<WebkitFileStreamReaderImpl
> reader(new WebkitFileStreamReaderImpl(
136 GetFileSystemGetter(),
137 worker_thread_
->message_loop_proxy().get(),
140 base::Time())); // expected modification time
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
);
150 TEST_F(WebkitFileStreamReaderImplTest
, GetLengthError
) {
151 const base::FilePath kDriveFile
=
152 util::GetDriveMyDriveRootPath().AppendASCII("non-existing.txt");
154 scoped_ptr
<WebkitFileStreamReaderImpl
> reader(new WebkitFileStreamReaderImpl(
155 GetFileSystemGetter(),
156 worker_thread_
->message_loop_proxy().get(),
159 base::Time())); // expected modification time
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
);
167 TEST_F(WebkitFileStreamReaderImplTest
, LastModification
) {
168 const base::FilePath kDriveFile
=
169 util::GetDriveMyDriveRootPath().AppendASCII("File 1.txt");
171 base::Time expected_modification_time
;
172 ASSERT_TRUE(google_apis::util::GetTimeFromString(
173 "2011-12-14T00:40:47.330Z", &expected_modification_time
));
175 FileError error
= FILE_ERROR_FAILED
;
176 scoped_ptr
<ResourceEntry
> entry
;
177 fake_file_system_
->GetResourceEntry(
179 google_apis::test_util::CreateCopyResultCallback(&error
, &entry
));
180 base::RunLoop().RunUntilIdle();
181 EXPECT_EQ(FILE_ERROR_OK
, error
);
184 google_apis::DriveApiErrorCode status
= google_apis::DRIVE_OTHER_ERROR
;
185 scoped_ptr
<google_apis::FileResource
> server_entry
;
186 fake_drive_service_
->UpdateResource(
187 entry
->resource_id(),
188 std::string(), // parent_resource_id
189 std::string(), // title
190 expected_modification_time
, base::Time(),
191 google_apis::drive::Properties(),
192 google_apis::test_util::CreateCopyResultCallback(&status
, &server_entry
));
193 base::RunLoop().RunUntilIdle();
194 EXPECT_EQ(google_apis::HTTP_SUCCESS
, status
);
196 scoped_ptr
<WebkitFileStreamReaderImpl
> reader(
197 new WebkitFileStreamReaderImpl(GetFileSystemGetter(),
198 worker_thread_
->message_loop_proxy().get(),
201 expected_modification_time
));
203 net::TestInt64CompletionCallback callback
;
204 int64 result
= reader
->GetLength(callback
.callback());
205 result
= callback
.GetResult(result
);
208 ASSERT_EQ(net::OK
, test_util::ReadAllData(reader
.get(), &content
));
209 EXPECT_GE(content
.size(), static_cast<size_t>(result
));
212 // TODO(hashimoto): Enable this test. crbug.com/346625
213 TEST_F(WebkitFileStreamReaderImplTest
, DISABLED_LastModificationError
) {
214 const base::FilePath kDriveFile
=
215 util::GetDriveMyDriveRootPath().AppendASCII("File 1.txt");
217 scoped_ptr
<WebkitFileStreamReaderImpl
> reader(
218 new WebkitFileStreamReaderImpl(GetFileSystemGetter(),
219 worker_thread_
->message_loop_proxy().get(),
222 base::Time::FromInternalValue(1)));
224 net::TestInt64CompletionCallback callback
;
225 int64 result
= reader
->GetLength(callback
.callback());
226 result
= callback
.GetResult(result
);
227 EXPECT_EQ(net::ERR_UPLOAD_FILE_CHANGED
, result
);
230 } // namespace internal