| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_scheduler.h" | 5 #include "chrome/browser/chromeos/drive/drive_scheduler.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| 11 #include "base/prefs/pref_service.h" | 11 #include "base/prefs/pref_service.h" |
| 12 #include "base/stl_util.h" |
| 12 #include "base/threading/sequenced_worker_pool.h" | 13 #include "base/threading/sequenced_worker_pool.h" |
| 13 #include "chrome/browser/chromeos/drive/drive_test_util.h" | 14 #include "chrome/browser/chromeos/drive/drive_test_util.h" |
| 14 #include "chrome/browser/google_apis/drive_api_parser.h" | 15 #include "chrome/browser/google_apis/drive_api_parser.h" |
| 15 #include "chrome/browser/google_apis/fake_drive_service.h" | 16 #include "chrome/browser/google_apis/fake_drive_service.h" |
| 16 #include "chrome/browser/google_apis/gdata_wapi_parser.h" | 17 #include "chrome/browser/google_apis/gdata_wapi_parser.h" |
| 17 #include "chrome/common/pref_names.h" | 18 #include "chrome/common/pref_names.h" |
| 18 #include "chrome/test/base/testing_profile.h" | 19 #include "chrome/test/base/testing_profile.h" |
| 19 #include "content/public/test/test_browser_thread.h" | 20 #include "content/public/test/test_browser_thread.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 21 | 22 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 38 }; | 39 }; |
| 39 | 40 |
| 40 void CopyResourceIdFromGetResourceEntryCallback( | 41 void CopyResourceIdFromGetResourceEntryCallback( |
| 41 std::vector<std::string>* id_list_out, | 42 std::vector<std::string>* id_list_out, |
| 42 const std::string& requested_id, | 43 const std::string& requested_id, |
| 43 google_apis::GDataErrorCode error_in, | 44 google_apis::GDataErrorCode error_in, |
| 44 scoped_ptr<google_apis::ResourceEntry> resource_entry_in) { | 45 scoped_ptr<google_apis::ResourceEntry> resource_entry_in) { |
| 45 id_list_out->push_back(requested_id); | 46 id_list_out->push_back(requested_id); |
| 46 } | 47 } |
| 47 | 48 |
| 49 class JobListLogger : public JobListObserver { |
| 50 public: |
| 51 enum EventType { |
| 52 ADDED, |
| 53 UPDATED, |
| 54 DONE, |
| 55 }; |
| 56 |
| 57 struct EventLog { |
| 58 EventType type; |
| 59 JobInfo info; |
| 60 |
| 61 EventLog(EventType type, const JobInfo& info) : type(type), info(info) { |
| 62 } |
| 63 }; |
| 64 |
| 65 // Checks whether the specified type of event has occurred. |
| 66 bool Has(EventType type, JobType job_type) { |
| 67 for (size_t i = 0; i < events.size(); ++i) { |
| 68 if (events[i].type == type && events[i].info.job_type == job_type) |
| 69 return true; |
| 70 } |
| 71 return false; |
| 72 } |
| 73 |
| 74 // Gets the progress event information of the specified type. |
| 75 void GetProgressInfo(JobType job_type, std::vector<int64>* progress) { |
| 76 for (size_t i = 0; i < events.size(); ++i) { |
| 77 if (events[i].type == UPDATED && events[i].info.job_type == job_type) |
| 78 progress->push_back(events[i].info.num_completed_bytes); |
| 79 } |
| 80 } |
| 81 |
| 82 // JobListObserver overrides. |
| 83 virtual void OnJobAdded(const JobInfo& info) OVERRIDE { |
| 84 events.push_back(EventLog(ADDED, info)); |
| 85 } |
| 86 |
| 87 virtual void OnJobUpdated(const JobInfo& info) OVERRIDE { |
| 88 events.push_back(EventLog(UPDATED, info)); |
| 89 } |
| 90 |
| 91 virtual void OnJobDone(const JobInfo& info, DriveFileError error) OVERRIDE { |
| 92 events.push_back(EventLog(DONE, info)); |
| 93 } |
| 94 |
| 95 private: |
| 96 std::vector<EventLog> events; |
| 97 }; |
| 98 |
| 48 } // namespace | 99 } // namespace |
| 49 | 100 |
| 50 class DriveSchedulerTest : public testing::Test { | 101 class DriveSchedulerTest : public testing::Test { |
| 51 public: | 102 public: |
| 52 DriveSchedulerTest() | 103 DriveSchedulerTest() |
| 53 : ui_thread_(content::BrowserThread::UI, &message_loop_), | 104 : ui_thread_(content::BrowserThread::UI, &message_loop_), |
| 54 profile_(new TestingProfile) { | 105 profile_(new TestingProfile) { |
| 55 } | 106 } |
| 56 | 107 |
| 57 virtual void SetUp() OVERRIDE { | 108 virtual void SetUp() OVERRIDE { |
| (...skipping 582 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 640 // Check the download | 691 // Check the download |
| 641 EXPECT_EQ(google_apis::HTTP_SUCCESS, download_error); | 692 EXPECT_EQ(google_apis::HTTP_SUCCESS, download_error); |
| 642 std::string content; | 693 std::string content; |
| 643 EXPECT_EQ(output_file_path, kOutputFilePath); | 694 EXPECT_EQ(output_file_path, kOutputFilePath); |
| 644 ASSERT_TRUE(file_util::ReadFileToString(output_file_path, &content)); | 695 ASSERT_TRUE(file_util::ReadFileToString(output_file_path, &content)); |
| 645 // The content is "x"s of the file size specified in root_feed.json. | 696 // The content is "x"s of the file size specified in root_feed.json. |
| 646 EXPECT_EQ("xxxxxxxxxx", content); | 697 EXPECT_EQ("xxxxxxxxxx", content); |
| 647 } | 698 } |
| 648 | 699 |
| 649 TEST_F(DriveSchedulerTest, JobInfo) { | 700 TEST_F(DriveSchedulerTest, JobInfo) { |
| 701 JobListLogger logger; |
| 702 scheduler_->AddObserver(&logger); |
| 703 |
| 650 // Disable background upload/download. | 704 // Disable background upload/download. |
| 651 ConnectToWimax(); | 705 ConnectToWimax(); |
| 652 profile_->GetPrefs()->SetBoolean(prefs::kDisableDriveOverCellular, true); | 706 profile_->GetPrefs()->SetBoolean(prefs::kDisableDriveOverCellular, true); |
| 653 | 707 |
| 654 base::ScopedTempDir temp_dir; | 708 base::ScopedTempDir temp_dir; |
| 655 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 709 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 656 | 710 |
| 657 google_apis::GDataErrorCode error = google_apis::GDATA_OTHER_ERROR; | 711 google_apis::GDataErrorCode error = google_apis::GDATA_OTHER_ERROR; |
| 658 scoped_ptr<google_apis::ResourceEntry> entry; | 712 scoped_ptr<google_apis::ResourceEntry> entry; |
| 659 scoped_ptr<google_apis::AccountMetadata> account_metadata; | 713 scoped_ptr<google_apis::AccountMetadata> account_metadata; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 680 scheduler_->DownloadFile( | 734 scheduler_->DownloadFile( |
| 681 base::FilePath::FromUTF8Unsafe("drive/whatever.txt"), // virtual path | 735 base::FilePath::FromUTF8Unsafe("drive/whatever.txt"), // virtual path |
| 682 temp_dir.path().AppendASCII("whatever.txt"), | 736 temp_dir.path().AppendASCII("whatever.txt"), |
| 683 GURL("https://file_content_url/"), | 737 GURL("https://file_content_url/"), |
| 684 DriveClientContext(BACKGROUND), | 738 DriveClientContext(BACKGROUND), |
| 685 google_apis::test_util::CreateCopyResultCallback(&error, &path), | 739 google_apis::test_util::CreateCopyResultCallback(&error, &path), |
| 686 google_apis::GetContentCallback()); | 740 google_apis::GetContentCallback()); |
| 687 | 741 |
| 688 // The number of jobs queued so far. | 742 // The number of jobs queued so far. |
| 689 EXPECT_EQ(4U, scheduler_->GetJobInfoList().size()); | 743 EXPECT_EQ(4U, scheduler_->GetJobInfoList().size()); |
| 744 EXPECT_TRUE(logger.Has(JobListLogger::ADDED, TYPE_ADD_NEW_DIRECTORY)); |
| 745 EXPECT_TRUE(logger.Has(JobListLogger::ADDED, TYPE_GET_ACCOUNT_METADATA)); |
| 746 EXPECT_TRUE(logger.Has(JobListLogger::ADDED, TYPE_RENAME_RESOURCE)); |
| 747 EXPECT_TRUE(logger.Has(JobListLogger::ADDED, TYPE_DOWNLOAD_FILE)); |
| 748 EXPECT_FALSE(logger.Has(JobListLogger::DONE, TYPE_ADD_NEW_DIRECTORY)); |
| 749 EXPECT_FALSE(logger.Has(JobListLogger::DONE, TYPE_GET_ACCOUNT_METADATA)); |
| 750 EXPECT_FALSE(logger.Has(JobListLogger::DONE, TYPE_RENAME_RESOURCE)); |
| 751 EXPECT_FALSE(logger.Has(JobListLogger::DONE, TYPE_DOWNLOAD_FILE)); |
| 690 | 752 |
| 691 // Add more jobs. | 753 // Add more jobs. |
| 692 expected_types.insert(TYPE_ADD_RESOURCE_TO_DIRECTORY); | 754 expected_types.insert(TYPE_ADD_RESOURCE_TO_DIRECTORY); |
| 693 scheduler_->AddResourceToDirectory( | 755 scheduler_->AddResourceToDirectory( |
| 694 "folder:1_folder_resource_id", | 756 "folder:1_folder_resource_id", |
| 695 "file:2_file_resource_id", | 757 "file:2_file_resource_id", |
| 696 google_apis::test_util::CreateCopyResultCallback(&error)); | 758 google_apis::test_util::CreateCopyResultCallback(&error)); |
| 697 expected_types.insert(TYPE_COPY_HOSTED_DOCUMENT); | 759 expected_types.insert(TYPE_COPY_HOSTED_DOCUMENT); |
| 698 scheduler_->CopyHostedDocument( | 760 scheduler_->CopyHostedDocument( |
| 699 "document:5_document_resource_id", | 761 "document:5_document_resource_id", |
| 700 "New Document", | 762 "New Document", |
| 701 google_apis::test_util::CreateCopyResultCallback(&error, &entry)); | 763 google_apis::test_util::CreateCopyResultCallback(&error, &entry)); |
| 702 | 764 |
| 703 // 6 jobs in total were queued. | 765 // 6 jobs in total were queued. |
| 704 std::vector<JobInfo> jobs = scheduler_->GetJobInfoList(); | 766 std::vector<JobInfo> jobs = scheduler_->GetJobInfoList(); |
| 705 EXPECT_EQ(6U, jobs.size()); | 767 EXPECT_EQ(6U, jobs.size()); |
| 706 std::set<JobType> actual_types; | 768 std::set<JobType> actual_types; |
| 707 for (size_t i = 0; i < jobs.size(); ++i) | 769 std::set<JobID> job_ids; |
| 770 for (size_t i = 0; i < jobs.size(); ++i) { |
| 708 actual_types.insert(jobs[i].job_type); | 771 actual_types.insert(jobs[i].job_type); |
| 772 job_ids.insert(jobs[i].job_id); |
| 773 } |
| 709 EXPECT_EQ(expected_types, actual_types); | 774 EXPECT_EQ(expected_types, actual_types); |
| 775 EXPECT_EQ(6U, job_ids.size()) << "All job IDs must be unique"; |
| 776 EXPECT_TRUE(logger.Has(JobListLogger::ADDED, TYPE_ADD_RESOURCE_TO_DIRECTORY)); |
| 777 EXPECT_TRUE(logger.Has(JobListLogger::ADDED, TYPE_COPY_HOSTED_DOCUMENT)); |
| 778 EXPECT_FALSE(logger.Has(JobListLogger::DONE, TYPE_ADD_RESOURCE_TO_DIRECTORY)); |
| 779 EXPECT_FALSE(logger.Has(JobListLogger::DONE, TYPE_COPY_HOSTED_DOCUMENT)); |
| 710 | 780 |
| 711 // Run the jobs. | 781 // Run the jobs. |
| 712 google_apis::test_util::RunBlockingPoolTask(); | 782 google_apis::test_util::RunBlockingPoolTask(); |
| 713 | 783 |
| 714 // All jobs except the BACKGROUND job should have finished. | 784 // All jobs except the BACKGROUND job should have started running (UPDATED) |
| 785 // and then finished (DONE). |
| 715 jobs = scheduler_->GetJobInfoList(); | 786 jobs = scheduler_->GetJobInfoList(); |
| 716 ASSERT_EQ(1U, jobs.size()); | 787 ASSERT_EQ(1U, jobs.size()); |
| 717 EXPECT_EQ(TYPE_DOWNLOAD_FILE, jobs[0].job_type); | 788 EXPECT_EQ(TYPE_DOWNLOAD_FILE, jobs[0].job_type); |
| 718 | 789 |
| 790 EXPECT_TRUE(logger.Has(JobListLogger::UPDATED, TYPE_ADD_NEW_DIRECTORY)); |
| 791 EXPECT_TRUE(logger.Has(JobListLogger::UPDATED, TYPE_GET_ACCOUNT_METADATA)); |
| 792 EXPECT_TRUE(logger.Has(JobListLogger::UPDATED, TYPE_RENAME_RESOURCE)); |
| 793 EXPECT_TRUE(logger.Has(JobListLogger::UPDATED, |
| 794 TYPE_ADD_RESOURCE_TO_DIRECTORY)); |
| 795 EXPECT_TRUE(logger.Has(JobListLogger::UPDATED, TYPE_COPY_HOSTED_DOCUMENT)); |
| 796 EXPECT_FALSE(logger.Has(JobListLogger::UPDATED, TYPE_DOWNLOAD_FILE)); |
| 797 |
| 798 EXPECT_TRUE(logger.Has(JobListLogger::DONE, TYPE_ADD_NEW_DIRECTORY)); |
| 799 EXPECT_TRUE(logger.Has(JobListLogger::DONE, TYPE_GET_ACCOUNT_METADATA)); |
| 800 EXPECT_TRUE(logger.Has(JobListLogger::DONE, TYPE_RENAME_RESOURCE)); |
| 801 EXPECT_TRUE(logger.Has(JobListLogger::DONE, TYPE_ADD_RESOURCE_TO_DIRECTORY)); |
| 802 EXPECT_TRUE(logger.Has(JobListLogger::DONE, TYPE_COPY_HOSTED_DOCUMENT)); |
| 803 EXPECT_FALSE(logger.Has(JobListLogger::DONE, TYPE_DOWNLOAD_FILE)); |
| 804 |
| 719 // Run the background downloading job as well. | 805 // Run the background downloading job as well. |
| 720 ConnectToWifi(); | 806 ConnectToWifi(); |
| 721 google_apis::test_util::RunBlockingPoolTask(); | 807 google_apis::test_util::RunBlockingPoolTask(); |
| 722 | 808 |
| 723 // All jobs should have finished. | 809 // All jobs should have finished. |
| 724 EXPECT_EQ(0U, scheduler_->GetJobInfoList().size()); | 810 EXPECT_EQ(0U, scheduler_->GetJobInfoList().size()); |
| 811 EXPECT_TRUE(logger.Has(JobListLogger::UPDATED, TYPE_DOWNLOAD_FILE)); |
| 812 EXPECT_TRUE(logger.Has(JobListLogger::DONE, TYPE_DOWNLOAD_FILE)); |
| 813 } |
| 814 |
| 815 |
| 816 TEST_F(DriveSchedulerTest, JobInfoProgress) { |
| 817 JobListLogger logger; |
| 818 scheduler_->AddObserver(&logger); |
| 819 |
| 820 ConnectToWifi(); |
| 821 |
| 822 base::ScopedTempDir temp_dir; |
| 823 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 824 |
| 825 google_apis::GDataErrorCode error = google_apis::GDATA_OTHER_ERROR; |
| 826 base::FilePath path; |
| 827 |
| 828 // Download job. |
| 829 scheduler_->DownloadFile( |
| 830 base::FilePath::FromUTF8Unsafe("drive/whatever.txt"), // virtual path |
| 831 temp_dir.path().AppendASCII("whatever.txt"), |
| 832 GURL("https://file_content_url/"), |
| 833 DriveClientContext(BACKGROUND), |
| 834 google_apis::test_util::CreateCopyResultCallback(&error, &path), |
| 835 google_apis::GetContentCallback()); |
| 836 google_apis::test_util::RunBlockingPoolTask(); |
| 837 |
| 838 std::vector<int64> download_progress; |
| 839 logger.GetProgressInfo(TYPE_DOWNLOAD_FILE, &download_progress); |
| 840 ASSERT_TRUE(!download_progress.empty()); |
| 841 EXPECT_TRUE(base::STLIsSorted(download_progress)); |
| 842 EXPECT_GE(download_progress.front(), 0); |
| 843 EXPECT_LE(download_progress.back(), 10); |
| 844 |
| 845 // Upload job. |
| 846 path = temp_dir.path().AppendASCII("new_file.txt"); |
| 847 file_util::WriteFile(path, "Hello", 5); |
| 848 google_apis::DriveUploadError upload_error = |
| 849 google_apis::DRIVE_UPLOAD_ERROR_ABORT; |
| 850 scoped_ptr<google_apis::ResourceEntry> entry; |
| 851 |
| 852 scheduler_->UploadNewFile( |
| 853 fake_drive_service_->GetRootResourceId(), |
| 854 base::FilePath::FromUTF8Unsafe("drive/new_file.txt"), |
| 855 path, |
| 856 "dummy title", |
| 857 "plain/plain", |
| 858 DriveClientContext(BACKGROUND), |
| 859 google_apis::test_util::CreateCopyResultCallback( |
| 860 &upload_error, &path, &path, &entry)); |
| 861 google_apis::test_util::RunBlockingPoolTask(); |
| 862 |
| 863 std::vector<int64> upload_progress; |
| 864 logger.GetProgressInfo(TYPE_UPLOAD_NEW_FILE, &upload_progress); |
| 865 ASSERT_TRUE(!upload_progress.empty()); |
| 866 EXPECT_TRUE(base::STLIsSorted(upload_progress)); |
| 867 EXPECT_GE(upload_progress.front(), 0); |
| 868 EXPECT_LE(upload_progress.back(), 5); |
| 725 } | 869 } |
| 726 | 870 |
| 727 } // namespace drive | 871 } // namespace drive |
| OLD | NEW |