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

Side by Side Diff: chrome/browser/chromeos/drive/drive_file_stream_reader_unittest.cc

Issue 14197014: Add TestBrowserThreadBundle into RenderViewHostTestHarness. Kill some unnecessary real threads. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: merged ToT Created 7 years, 6 months 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
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 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/browser/chromeos/drive/drive_file_stream_reader.h" 5 #include "chrome/browser/chromeos/drive/drive_file_stream_reader.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/files/file_path.h" 10 #include "base/files/file_path.h"
11 #include "base/files/scoped_temp_dir.h" 11 #include "base/files/scoped_temp_dir.h"
12 #include "base/message_loop.h" 12 #include "base/message_loop.h"
13 #include "base/rand_util.h" 13 #include "base/rand_util.h"
14 #include "base/sequenced_task_runner.h" 14 #include "base/sequenced_task_runner.h"
15 #include "base/threading/thread.h" 15 #include "base/threading/thread.h"
16 #include "chrome/browser/chromeos/drive/fake_file_system.h" 16 #include "chrome/browser/chromeos/drive/fake_file_system.h"
17 #include "chrome/browser/chromeos/drive/file_system_util.h" 17 #include "chrome/browser/chromeos/drive/file_system_util.h"
18 #include "chrome/browser/chromeos/drive/local_file_reader.h" 18 #include "chrome/browser/chromeos/drive/local_file_reader.h"
19 #include "chrome/browser/chromeos/drive/test_util.h" 19 #include "chrome/browser/chromeos/drive/test_util.h"
20 #include "chrome/browser/google_apis/fake_drive_service.h" 20 #include "chrome/browser/google_apis/fake_drive_service.h"
21 #include "chrome/browser/google_apis/task_util.h" 21 #include "chrome/browser/google_apis/task_util.h"
22 #include "chrome/browser/google_apis/test_util.h" 22 #include "chrome/browser/google_apis/test_util.h"
23 #include "content/public/test/test_browser_thread.h" 23 #include "content/public/test/test_browser_thread_bundle.h"
24 #include "net/base/io_buffer.h" 24 #include "net/base/io_buffer.h"
25 #include "net/base/net_errors.h" 25 #include "net/base/net_errors.h"
26 #include "net/base/test_completion_callback.h" 26 #include "net/base/test_completion_callback.h"
27 #include "net/http/http_byte_range.h" 27 #include "net/http/http_byte_range.h"
28 #include "testing/gtest/include/gtest/gtest.h" 28 #include "testing/gtest/include/gtest/gtest.h"
29 29
30 using content::BrowserThread;
31
32 namespace drive { 30 namespace drive {
33 namespace internal { 31 namespace internal {
34 namespace { 32 namespace {
35 33
36 // Increments the |num_called|, when this method is invoked. 34 // Increments the |num_called|, when this method is invoked.
37 void IncrementCallback(int* num_called) { 35 void IncrementCallback(int* num_called) {
38 DCHECK(num_called); 36 DCHECK(num_called);
39 ++*num_called; 37 ++*num_called;
40 } 38 }
41 39
42 } // namespace 40 } // namespace
43 41
44 class LocalReaderProxyTest : public ::testing::Test { 42 class LocalReaderProxyTest : public ::testing::Test {
45 protected: 43 protected:
46 LocalReaderProxyTest() : io_thread_(BrowserThread::IO, &message_loop_) { 44 LocalReaderProxyTest()
45 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {
47 } 46 }
48 47
49 virtual void SetUp() OVERRIDE { 48 virtual void SetUp() OVERRIDE {
50 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 49 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
51 ASSERT_TRUE(google_apis::test_util::CreateFileOfSpecifiedSize( 50 ASSERT_TRUE(google_apis::test_util::CreateFileOfSpecifiedSize(
52 temp_dir_.path(), 1024, &file_path_, &file_content_)); 51 temp_dir_.path(), 1024, &file_path_, &file_content_));
53 52
54 worker_thread_.reset(new base::Thread("ReaderProxyTest")); 53 worker_thread_.reset(new base::Thread("ReaderProxyTest"));
55 ASSERT_TRUE(worker_thread_->Start()); 54 ASSERT_TRUE(worker_thread_->Start());
56 } 55 }
57 56
58 virtual void TearDown() OVERRIDE { 57 virtual void TearDown() OVERRIDE {
59 worker_thread_.reset(); 58 worker_thread_.reset();
60 } 59 }
61 60
62 base::MessageLoopForIO message_loop_; 61 content::TestBrowserThreadBundle thread_bundle_;
63 content::TestBrowserThread io_thread_;
64 62
65 base::ScopedTempDir temp_dir_; 63 base::ScopedTempDir temp_dir_;
66 base::FilePath file_path_; 64 base::FilePath file_path_;
67 std::string file_content_; 65 std::string file_content_;
68 66
69 scoped_ptr<base::Thread> worker_thread_; 67 scoped_ptr<base::Thread> worker_thread_;
70 }; 68 };
71 69
72 TEST_F(LocalReaderProxyTest, Read) { 70 TEST_F(LocalReaderProxyTest, Read) {
73 // Open the file first. 71 // Open the file first.
(...skipping 28 matching lines...) Expand all
102 LocalReaderProxy proxy(file_reader.Pass(), expected_content.size()); 100 LocalReaderProxy proxy(file_reader.Pass(), expected_content.size());
103 101
104 // Make sure the read contant is as same as the file. 102 // Make sure the read contant is as same as the file.
105 std::string content; 103 std::string content;
106 ASSERT_EQ(net::OK, test_util::ReadAllData(&proxy, &content)); 104 ASSERT_EQ(net::OK, test_util::ReadAllData(&proxy, &content));
107 EXPECT_EQ(expected_content, content); 105 EXPECT_EQ(expected_content, content);
108 } 106 }
109 107
110 class NetworkReaderProxyTest : public ::testing::Test { 108 class NetworkReaderProxyTest : public ::testing::Test {
111 protected: 109 protected:
112 NetworkReaderProxyTest() : io_thread_(BrowserThread::IO, &message_loop_) { 110 NetworkReaderProxyTest()
111 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {
113 } 112 }
114 113
115 base::MessageLoopForIO message_loop_; 114 content::TestBrowserThreadBundle thread_bundle_;
116 content::TestBrowserThread io_thread_;
117 }; 115 };
118 116
119 TEST_F(NetworkReaderProxyTest, EmptyFile) { 117 TEST_F(NetworkReaderProxyTest, EmptyFile) {
120 NetworkReaderProxy proxy(0, 0, base::Bind(&base::DoNothing)); 118 NetworkReaderProxy proxy(0, 0, base::Bind(&base::DoNothing));
121 119
122 net::TestCompletionCallback callback; 120 net::TestCompletionCallback callback;
123 const int kBufferSize = 10; 121 const int kBufferSize = 10;
124 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kBufferSize)); 122 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kBufferSize));
125 int result = proxy.Read(buffer.get(), kBufferSize, callback.callback()); 123 int result = proxy.Read(buffer.get(), kBufferSize, callback.callback());
126 124
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
282 // The cancelling callback should be called. 280 // The cancelling callback should be called.
283 } 281 }
284 EXPECT_EQ(1, num_called); 282 EXPECT_EQ(1, num_called);
285 } 283 }
286 284
287 } // namespace internal 285 } // namespace internal
288 286
289 class DriveFileStreamReaderTest : public ::testing::Test { 287 class DriveFileStreamReaderTest : public ::testing::Test {
290 protected: 288 protected:
291 DriveFileStreamReaderTest() 289 DriveFileStreamReaderTest()
292 : ui_thread_(BrowserThread::UI), 290 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {
293 io_thread_(BrowserThread::IO, &message_loop_) {
294 } 291 }
295 292
296 virtual void SetUp() OVERRIDE { 293 virtual void SetUp() OVERRIDE {
297 ui_thread_.Start();
298
299 worker_thread_.reset(new base::Thread("DriveFileStreamReaderTest")); 294 worker_thread_.reset(new base::Thread("DriveFileStreamReaderTest"));
300 ASSERT_TRUE(worker_thread_->Start()); 295 ASSERT_TRUE(worker_thread_->Start());
301 296
302 BrowserThread::PostTaskAndReply(
303 BrowserThread::UI,
304 FROM_HERE,
305 base::Bind(&DriveFileStreamReaderTest::SetUpOnUIThread,
306 base::Unretained(this)),
307 base::MessageLoop::QuitClosure());
308 message_loop_.Run();
309 }
310
311 virtual void TearDown() OVERRIDE {
312 BrowserThread::PostTaskAndReply(
313 BrowserThread::UI,
314 FROM_HERE,
315 base::Bind(&DriveFileStreamReaderTest::TearDownOnUIThread,
316 base::Unretained(this)),
317 base::MessageLoop::QuitClosure());
318 message_loop_.Run();
319
320 worker_thread_.reset();
321 }
322
323 void SetUpOnUIThread() {
324 // Initialize FakeDriveService. 297 // Initialize FakeDriveService.
325 fake_drive_service_.reset(new google_apis::FakeDriveService); 298 fake_drive_service_.reset(new google_apis::FakeDriveService);
326 fake_drive_service_->LoadResourceListForWapi( 299 fake_drive_service_->LoadResourceListForWapi(
327 "chromeos/gdata/root_feed.json"); 300 "chromeos/gdata/root_feed.json");
328 fake_drive_service_->LoadAccountMetadataForWapi( 301 fake_drive_service_->LoadAccountMetadataForWapi(
329 "chromeos/gdata/account_metadata.json"); 302 "chromeos/gdata/account_metadata.json");
330 303
331 // Create a testee instance. 304 // Create a testee instance.
332 fake_file_system_.reset( 305 fake_file_system_.reset(
333 new test_util::FakeFileSystem(fake_drive_service_.get())); 306 new test_util::FakeFileSystem(fake_drive_service_.get()));
334 fake_file_system_->Initialize(); 307 fake_file_system_->Initialize();
335 } 308 }
336 309
337 void TearDownOnUIThread() { 310 virtual void TearDown() OVERRIDE {
338 fake_file_system_.reset(); 311 fake_file_system_.reset();
339 fake_drive_service_.reset(); 312 fake_drive_service_.reset();
313
314 worker_thread_.reset();
340 } 315 }
341 316
342 FileSystemInterface* GetFileSystem() { 317 FileSystemInterface* GetFileSystem() {
343 return fake_file_system_.get(); 318 return fake_file_system_.get();
344 } 319 }
345 320
346 DriveFileStreamReader::FileSystemGetter GetFileSystemGetter() { 321 DriveFileStreamReader::FileSystemGetter GetFileSystemGetter() {
347 return base::Bind(&DriveFileStreamReaderTest::GetFileSystem, 322 return base::Bind(&DriveFileStreamReaderTest::GetFileSystem,
348 base::Unretained(this)); 323 base::Unretained(this));
349 } 324 }
350 325
351 base::MessageLoopForIO message_loop_; 326 content::TestBrowserThreadBundle thread_bundle_;
352 content::TestBrowserThread ui_thread_;
353 content::TestBrowserThread io_thread_;
354 327
355 scoped_ptr<base::Thread> worker_thread_; 328 scoped_ptr<base::Thread> worker_thread_;
356 329
357 scoped_ptr<google_apis::FakeDriveService> fake_drive_service_; 330 scoped_ptr<google_apis::FakeDriveService> fake_drive_service_;
358 scoped_ptr<test_util::FakeFileSystem> fake_file_system_; 331 scoped_ptr<test_util::FakeFileSystem> fake_file_system_;
359 }; 332 };
360 333
361 TEST_F(DriveFileStreamReaderTest, Read) { 334 TEST_F(DriveFileStreamReaderTest, Read) {
362 const base::FilePath kDriveFile = 335 const base::FilePath kDriveFile =
363 util::GetDriveMyDriveRootPath().AppendASCII("File 1.txt"); 336 util::GetDriveMyDriveRootPath().AppendASCII("File 1.txt");
364 // Create the reader, and initialize it. 337 // Create the reader, and initialize it.
365 // In this case, the file is not yet locally cached. 338 // In this case, the file is not yet locally cached.
366 scoped_ptr<DriveFileStreamReader> reader(new DriveFileStreamReader( 339 scoped_ptr<DriveFileStreamReader> reader(new DriveFileStreamReader(
367 GetFileSystemGetter(), 340 GetFileSystemGetter(),
368 worker_thread_->message_loop_proxy())); 341 worker_thread_->message_loop_proxy()));
369 EXPECT_FALSE(reader->IsInitialized()); 342 EXPECT_FALSE(reader->IsInitialized());
370 343
371 int error = net::ERR_FAILED; 344 int error = net::ERR_FAILED;
372 scoped_ptr<ResourceEntry> entry; 345 scoped_ptr<ResourceEntry> entry;
373 reader->Initialize( 346 reader->Initialize(
374 kDriveFile, 347 kDriveFile,
375 net::HttpByteRange(), 348 net::HttpByteRange(),
376 google_apis::CreateComposedCallback( 349 google_apis::CreateComposedCallback(
377 base::Bind(&google_apis::test_util::RunAndQuit), 350 base::Bind(&google_apis::test_util::RunAndQuit),
378 google_apis::test_util::CreateCopyResultCallback( 351 google_apis::test_util::CreateCopyResultCallback(
379 &error, &entry))); 352 &error, &entry)));
380 message_loop_.Run(); 353 base::MessageLoop::current()->Run();
381 EXPECT_EQ(net::OK, error); 354 EXPECT_EQ(net::OK, error);
382 ASSERT_TRUE(entry); 355 ASSERT_TRUE(entry);
383 EXPECT_TRUE(reader->IsInitialized()); 356 EXPECT_TRUE(reader->IsInitialized());
384 size_t content_size = entry->file_info().size(); 357 size_t content_size = entry->file_info().size();
385 358
386 // Read data from the reader. 359 // Read data from the reader.
387 std::string first_content; 360 std::string first_content;
388 ASSERT_EQ(net::OK, test_util::ReadAllData(reader.get(), &first_content)); 361 ASSERT_EQ(net::OK, test_util::ReadAllData(reader.get(), &first_content));
389 EXPECT_EQ(content_size, first_content.size()); 362 EXPECT_EQ(content_size, first_content.size());
390 363
391 // Create second instance and initialize it. 364 // Create second instance and initialize it.
392 // In this case, the file should be cached one. 365 // In this case, the file should be cached one.
393 reader.reset( 366 reader.reset(
394 new DriveFileStreamReader(GetFileSystemGetter(), 367 new DriveFileStreamReader(GetFileSystemGetter(),
395 worker_thread_->message_loop_proxy())); 368 worker_thread_->message_loop_proxy()));
396 EXPECT_FALSE(reader->IsInitialized()); 369 EXPECT_FALSE(reader->IsInitialized());
397 370
398 error = net::ERR_FAILED; 371 error = net::ERR_FAILED;
399 entry.reset(); 372 entry.reset();
400 reader->Initialize( 373 reader->Initialize(
401 kDriveFile, 374 kDriveFile,
402 net::HttpByteRange(), 375 net::HttpByteRange(),
403 google_apis::CreateComposedCallback( 376 google_apis::CreateComposedCallback(
404 base::Bind(&google_apis::test_util::RunAndQuit), 377 base::Bind(&google_apis::test_util::RunAndQuit),
405 google_apis::test_util::CreateCopyResultCallback( 378 google_apis::test_util::CreateCopyResultCallback(
406 &error, &entry))); 379 &error, &entry)));
407 message_loop_.Run(); 380 base::MessageLoop::current()->Run();
408 EXPECT_EQ(net::OK, error); 381 EXPECT_EQ(net::OK, error);
409 ASSERT_TRUE(entry); 382 ASSERT_TRUE(entry);
410 EXPECT_TRUE(reader->IsInitialized()); 383 EXPECT_TRUE(reader->IsInitialized());
411 384
412 // The size should be same. 385 // The size should be same.
413 EXPECT_EQ(content_size, static_cast<size_t>(entry->file_info().size())); 386 EXPECT_EQ(content_size, static_cast<size_t>(entry->file_info().size()));
414 387
415 // Read data from the reader, again. 388 // Read data from the reader, again.
416 std::string second_content; 389 std::string second_content;
417 ASSERT_EQ(net::OK, test_util::ReadAllData(reader.get(), &second_content)); 390 ASSERT_EQ(net::OK, test_util::ReadAllData(reader.get(), &second_content));
(...skipping 22 matching lines...) Expand all
440 byte_range.set_first_byte_position(kRangeOffset); 413 byte_range.set_first_byte_position(kRangeOffset);
441 // Last byte position is inclusive. 414 // Last byte position is inclusive.
442 byte_range.set_last_byte_position(kRangeOffset + kRangeLength - 1); 415 byte_range.set_last_byte_position(kRangeOffset + kRangeLength - 1);
443 reader->Initialize( 416 reader->Initialize(
444 kDriveFile, 417 kDriveFile,
445 byte_range, 418 byte_range,
446 google_apis::CreateComposedCallback( 419 google_apis::CreateComposedCallback(
447 base::Bind(&google_apis::test_util::RunAndQuit), 420 base::Bind(&google_apis::test_util::RunAndQuit),
448 google_apis::test_util::CreateCopyResultCallback( 421 google_apis::test_util::CreateCopyResultCallback(
449 &error, &entry))); 422 &error, &entry)));
450 message_loop_.Run(); 423 base::MessageLoop::current()->Run();
451 EXPECT_EQ(net::OK, error); 424 EXPECT_EQ(net::OK, error);
452 ASSERT_TRUE(entry); 425 ASSERT_TRUE(entry);
453 EXPECT_TRUE(reader->IsInitialized()); 426 EXPECT_TRUE(reader->IsInitialized());
454 427
455 // Read data from the reader. 428 // Read data from the reader.
456 std::string first_content; 429 std::string first_content;
457 ASSERT_EQ(net::OK, test_util::ReadAllData(reader.get(), &first_content)); 430 ASSERT_EQ(net::OK, test_util::ReadAllData(reader.get(), &first_content));
458 431
459 // The length should be equal to range length. 432 // The length should be equal to range length.
460 EXPECT_EQ(kRangeLength, static_cast<int64>(first_content.size())); 433 EXPECT_EQ(kRangeLength, static_cast<int64>(first_content.size()));
461 434
462 // Create second instance and initialize it. 435 // Create second instance and initialize it.
463 // In this case, the file should be cached one. 436 // In this case, the file should be cached one.
464 reader.reset( 437 reader.reset(
465 new DriveFileStreamReader(GetFileSystemGetter(), 438 new DriveFileStreamReader(GetFileSystemGetter(),
466 worker_thread_->message_loop_proxy())); 439 worker_thread_->message_loop_proxy()));
467 EXPECT_FALSE(reader->IsInitialized()); 440 EXPECT_FALSE(reader->IsInitialized());
468 441
469 error = net::ERR_FAILED; 442 error = net::ERR_FAILED;
470 entry.reset(); 443 entry.reset();
471 reader->Initialize( 444 reader->Initialize(
472 kDriveFile, 445 kDriveFile,
473 byte_range, 446 byte_range,
474 google_apis::CreateComposedCallback( 447 google_apis::CreateComposedCallback(
475 base::Bind(&google_apis::test_util::RunAndQuit), 448 base::Bind(&google_apis::test_util::RunAndQuit),
476 google_apis::test_util::CreateCopyResultCallback( 449 google_apis::test_util::CreateCopyResultCallback(
477 &error, &entry))); 450 &error, &entry)));
478 message_loop_.Run(); 451 base::MessageLoop::current()->Run();
479 EXPECT_EQ(net::OK, error); 452 EXPECT_EQ(net::OK, error);
480 ASSERT_TRUE(entry); 453 ASSERT_TRUE(entry);
481 EXPECT_TRUE(reader->IsInitialized()); 454 EXPECT_TRUE(reader->IsInitialized());
482 455
483 // Read data from the reader, again. 456 // Read data from the reader, again.
484 std::string second_content; 457 std::string second_content;
485 ASSERT_EQ(net::OK, test_util::ReadAllData(reader.get(), &second_content)); 458 ASSERT_EQ(net::OK, test_util::ReadAllData(reader.get(), &second_content));
486 459
487 // The same content is expected. 460 // The same content is expected.
488 EXPECT_EQ(first_content, second_content); 461 EXPECT_EQ(first_content, second_content);
(...skipping 18 matching lines...) Expand all
507 byte_range.set_first_byte_position(kRangeOffset); 480 byte_range.set_first_byte_position(kRangeOffset);
508 // Last byte position is inclusive. 481 // Last byte position is inclusive.
509 byte_range.set_last_byte_position(kRangeOffset + kRangeLength - 1); 482 byte_range.set_last_byte_position(kRangeOffset + kRangeLength - 1);
510 reader->Initialize( 483 reader->Initialize(
511 kDriveFile, 484 kDriveFile,
512 byte_range, 485 byte_range,
513 google_apis::CreateComposedCallback( 486 google_apis::CreateComposedCallback(
514 base::Bind(&google_apis::test_util::RunAndQuit), 487 base::Bind(&google_apis::test_util::RunAndQuit),
515 google_apis::test_util::CreateCopyResultCallback( 488 google_apis::test_util::CreateCopyResultCallback(
516 &error, &entry))); 489 &error, &entry)));
517 message_loop_.Run(); 490 base::MessageLoop::current()->Run();
518 EXPECT_EQ(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE, error); 491 EXPECT_EQ(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE, error);
519 EXPECT_FALSE(entry); 492 EXPECT_FALSE(entry);
520 } 493 }
521 494
522 } // namespace drive 495 } // namespace drive
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698