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 "components/drive/drive_test_util.h"
18 #include "components/drive/file_system_core_util.h"
19 #include "components/drive/service/fake_drive_service.h"
20 #include "components/drive/service/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(), worker_thread_
->task_runner().get(), kDriveFile
,
77 base::Time())); // expected modification time
80 ASSERT_EQ(net::OK
, test_util::ReadAllData(reader
.get(), &content
));
82 net::TestInt64CompletionCallback callback
;
83 int64 length
= reader
->GetLength(callback
.callback());
84 length
= callback
.GetResult(length
);
85 EXPECT_EQ(content
.size(), static_cast<size_t>(length
));
88 TEST_F(WebkitFileStreamReaderImplTest
, GetLengthThenRead
) {
89 const base::FilePath kDriveFile
=
90 util::GetDriveMyDriveRootPath().AppendASCII("File 1.txt");
92 scoped_ptr
<WebkitFileStreamReaderImpl
> reader(new WebkitFileStreamReaderImpl(
93 GetFileSystemGetter(), worker_thread_
->task_runner().get(), kDriveFile
,
95 base::Time())); // expected modification time
97 net::TestInt64CompletionCallback callback
;
98 int64 length
= reader
->GetLength(callback
.callback());
99 length
= callback
.GetResult(length
);
102 ASSERT_EQ(net::OK
, test_util::ReadAllData(reader
.get(), &content
));
103 EXPECT_EQ(content
.size(), static_cast<size_t>(length
));
106 TEST_F(WebkitFileStreamReaderImplTest
, ReadWithOffset
) {
107 const base::FilePath kDriveFile
=
108 util::GetDriveMyDriveRootPath().AppendASCII("File 1.txt");
109 const int kOffset
= 5;
111 scoped_ptr
<WebkitFileStreamReaderImpl
> reader(new WebkitFileStreamReaderImpl(
112 GetFileSystemGetter(), worker_thread_
->task_runner().get(), kDriveFile
,
114 base::Time())); // expected modification time
117 ASSERT_EQ(net::OK
, test_util::ReadAllData(reader
.get(), &content
));
119 net::TestInt64CompletionCallback callback
;
120 int64 length
= reader
->GetLength(callback
.callback());
121 length
= callback
.GetResult(length
);
122 EXPECT_EQ(content
.size() + kOffset
, static_cast<size_t>(length
));
125 TEST_F(WebkitFileStreamReaderImplTest
, ReadError
) {
126 const base::FilePath kDriveFile
=
127 util::GetDriveMyDriveRootPath().AppendASCII("non-existing.txt");
129 scoped_ptr
<WebkitFileStreamReaderImpl
> reader(new WebkitFileStreamReaderImpl(
130 GetFileSystemGetter(), worker_thread_
->task_runner().get(), kDriveFile
,
132 base::Time())); // expected modification time
134 const int kBufferSize
= 10;
135 scoped_refptr
<net::IOBuffer
> io_buffer(new net::IOBuffer(kBufferSize
));
136 net::TestCompletionCallback callback
;
137 int result
= reader
->Read(io_buffer
.get(), kBufferSize
, callback
.callback());
138 result
= callback
.GetResult(result
);
139 EXPECT_EQ(net::ERR_FILE_NOT_FOUND
, result
);
142 TEST_F(WebkitFileStreamReaderImplTest
, GetLengthError
) {
143 const base::FilePath kDriveFile
=
144 util::GetDriveMyDriveRootPath().AppendASCII("non-existing.txt");
146 scoped_ptr
<WebkitFileStreamReaderImpl
> reader(new WebkitFileStreamReaderImpl(
147 GetFileSystemGetter(), worker_thread_
->task_runner().get(), kDriveFile
,
149 base::Time())); // expected modification time
151 net::TestInt64CompletionCallback callback
;
152 int64 result
= reader
->GetLength(callback
.callback());
153 result
= callback
.GetResult(result
);
154 EXPECT_EQ(net::ERR_FILE_NOT_FOUND
, result
);
157 TEST_F(WebkitFileStreamReaderImplTest
, LastModification
) {
158 const base::FilePath kDriveFile
=
159 util::GetDriveMyDriveRootPath().AppendASCII("File 1.txt");
161 base::Time expected_modification_time
;
162 ASSERT_TRUE(google_apis::util::GetTimeFromString(
163 "2011-12-14T00:40:47.330Z", &expected_modification_time
));
165 FileError error
= FILE_ERROR_FAILED
;
166 scoped_ptr
<ResourceEntry
> entry
;
167 fake_file_system_
->GetResourceEntry(
169 google_apis::test_util::CreateCopyResultCallback(&error
, &entry
));
170 base::RunLoop().RunUntilIdle();
171 EXPECT_EQ(FILE_ERROR_OK
, error
);
174 google_apis::DriveApiErrorCode status
= google_apis::DRIVE_OTHER_ERROR
;
175 scoped_ptr
<google_apis::FileResource
> server_entry
;
176 fake_drive_service_
->UpdateResource(
177 entry
->resource_id(),
178 std::string(), // parent_resource_id
179 std::string(), // title
180 expected_modification_time
, base::Time(),
181 google_apis::drive::Properties(),
182 google_apis::test_util::CreateCopyResultCallback(&status
, &server_entry
));
183 base::RunLoop().RunUntilIdle();
184 EXPECT_EQ(google_apis::HTTP_SUCCESS
, status
);
186 scoped_ptr
<WebkitFileStreamReaderImpl
> reader(new WebkitFileStreamReaderImpl(
187 GetFileSystemGetter(), worker_thread_
->task_runner().get(), kDriveFile
,
189 expected_modification_time
));
191 net::TestInt64CompletionCallback callback
;
192 int64 result
= reader
->GetLength(callback
.callback());
193 result
= callback
.GetResult(result
);
196 ASSERT_EQ(net::OK
, test_util::ReadAllData(reader
.get(), &content
));
197 EXPECT_GE(content
.size(), static_cast<size_t>(result
));
200 // TODO(hashimoto): Enable this test. crbug.com/346625
201 TEST_F(WebkitFileStreamReaderImplTest
, DISABLED_LastModificationError
) {
202 const base::FilePath kDriveFile
=
203 util::GetDriveMyDriveRootPath().AppendASCII("File 1.txt");
205 scoped_ptr
<WebkitFileStreamReaderImpl
> reader(new WebkitFileStreamReaderImpl(
206 GetFileSystemGetter(), worker_thread_
->task_runner().get(), kDriveFile
,
208 base::Time::FromInternalValue(1)));
210 net::TestInt64CompletionCallback callback
;
211 int64 result
= reader
->GetLength(callback
.callback());
212 result
= callback
.GetResult(result
);
213 EXPECT_EQ(net::ERR_UPLOAD_FILE_CHANGED
, result
);
216 } // namespace internal