| 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/json/json_reader.h" | |
| 12 #include "base/prefs/pref_service.h" | 11 #include "base/prefs/pref_service.h" |
| 13 #include "base/threading/sequenced_worker_pool.h" | 12 #include "base/threading/sequenced_worker_pool.h" |
| 14 #include "chrome/browser/chromeos/drive/drive_test_util.h" | 13 #include "chrome/browser/chromeos/drive/drive_test_util.h" |
| 15 #include "chrome/browser/google_apis/drive_api_parser.h" | 14 #include "chrome/browser/google_apis/drive_api_parser.h" |
| 16 #include "chrome/browser/google_apis/fake_drive_service.h" | 15 #include "chrome/browser/google_apis/fake_drive_service.h" |
| 17 #include "chrome/browser/google_apis/gdata_wapi_parser.h" | 16 #include "chrome/browser/google_apis/gdata_wapi_parser.h" |
| 18 #include "chrome/common/pref_names.h" | 17 #include "chrome/common/pref_names.h" |
| 19 #include "chrome/test/base/testing_profile.h" | 18 #include "chrome/test/base/testing_profile.h" |
| 20 #include "content/public/test/test_browser_thread.h" | 19 #include "content/public/test/test_browser_thread.h" |
| 21 #include "testing/gmock/include/gmock/gmock.h" | |
| 22 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 23 | 21 |
| 24 using ::testing::AnyNumber; | |
| 25 using ::testing::DoAll; | |
| 26 using ::testing::Eq; | |
| 27 using ::testing::Return; | |
| 28 using ::testing::StrictMock; | |
| 29 using ::testing::_; | |
| 30 | |
| 31 namespace drive { | 22 namespace drive { |
| 32 | 23 |
| 33 namespace { | 24 namespace { |
| 34 | 25 |
| 35 class MockNetworkChangeNotifier : public net::NetworkChangeNotifier { | 26 class FakeNetworkChangeNotifier : public net::NetworkChangeNotifier { |
| 36 public: | 27 public: |
| 37 MOCK_CONST_METHOD0(GetCurrentConnectionType, | 28 FakeNetworkChangeNotifier() : type_(CONNECTION_NONE) {} |
| 38 net::NetworkChangeNotifier::ConnectionType()); | 29 |
| 30 void set_connection_type(ConnectionType type) { type_ = type; } |
| 31 |
| 32 virtual ConnectionType GetCurrentConnectionType() const OVERRIDE { |
| 33 return type_; |
| 34 } |
| 35 |
| 36 private: |
| 37 net::NetworkChangeNotifier::ConnectionType type_; |
| 39 }; | 38 }; |
| 40 | 39 |
| 41 void CopyResourceIdFromGetResourceEntryCallback( | 40 void CopyResourceIdFromGetResourceEntryCallback( |
| 42 std::vector<std::string>* id_list_out, | 41 std::vector<std::string>* id_list_out, |
| 43 const std::string& requested_id, | 42 const std::string& requested_id, |
| 44 google_apis::GDataErrorCode error_in, | 43 google_apis::GDataErrorCode error_in, |
| 45 scoped_ptr<google_apis::ResourceEntry> resource_entry_in) { | 44 scoped_ptr<google_apis::ResourceEntry> resource_entry_in) { |
| 46 id_list_out->push_back(requested_id); | 45 id_list_out->push_back(requested_id); |
| 47 } | 46 } |
| 48 | 47 |
| 49 } // namespace | 48 } // namespace |
| 50 | 49 |
| 51 class DriveSchedulerTest : public testing::Test { | 50 class DriveSchedulerTest : public testing::Test { |
| 52 public: | 51 public: |
| 53 DriveSchedulerTest() | 52 DriveSchedulerTest() |
| 54 : ui_thread_(content::BrowserThread::UI, &message_loop_), | 53 : ui_thread_(content::BrowserThread::UI, &message_loop_), |
| 55 profile_(new TestingProfile) { | 54 profile_(new TestingProfile) { |
| 56 } | 55 } |
| 57 | 56 |
| 58 virtual void SetUp() OVERRIDE { | 57 virtual void SetUp() OVERRIDE { |
| 59 mock_network_change_notifier_.reset(new MockNetworkChangeNotifier); | 58 fake_network_change_notifier_.reset(new FakeNetworkChangeNotifier); |
| 60 | 59 |
| 61 fake_drive_service_.reset(new google_apis::FakeDriveService()); | 60 fake_drive_service_.reset(new google_apis::FakeDriveService()); |
| 62 fake_drive_service_->LoadResourceListForWapi( | 61 fake_drive_service_->LoadResourceListForWapi( |
| 63 "chromeos/gdata/root_feed.json"); | 62 "chromeos/gdata/root_feed.json"); |
| 64 fake_drive_service_->LoadAccountMetadataForWapi( | 63 fake_drive_service_->LoadAccountMetadataForWapi( |
| 65 "chromeos/gdata/account_metadata.json"); | 64 "chromeos/gdata/account_metadata.json"); |
| 66 fake_drive_service_->LoadAppListForDriveApi( | 65 fake_drive_service_->LoadAppListForDriveApi( |
| 67 "chromeos/drive/applist.json"); | 66 "chromeos/drive/applist.json"); |
| 68 | 67 |
| 69 scheduler_.reset(new DriveScheduler(profile_.get(), | 68 scheduler_.reset(new DriveScheduler(profile_.get(), |
| 70 fake_drive_service_.get())); | 69 fake_drive_service_.get())); |
| 71 | 70 |
| 72 scheduler_->Initialize(); | 71 scheduler_->Initialize(); |
| 73 scheduler_->SetDisableThrottling(true); | 72 scheduler_->SetDisableThrottling(true); |
| 74 } | 73 } |
| 75 | 74 |
| 76 virtual void TearDown() OVERRIDE { | 75 virtual void TearDown() OVERRIDE { |
| 77 // The scheduler should be deleted before NetworkLibrary, as it | 76 // The scheduler should be deleted before NetworkLibrary, as it |
| 78 // registers itself as observer of NetworkLibrary. | 77 // registers itself as observer of NetworkLibrary. |
| 79 scheduler_.reset(); | 78 scheduler_.reset(); |
| 80 google_apis::test_util::RunBlockingPoolTask(); | 79 google_apis::test_util::RunBlockingPoolTask(); |
| 81 fake_drive_service_.reset(); | 80 fake_drive_service_.reset(); |
| 82 mock_network_change_notifier_.reset(); | 81 fake_network_change_notifier_.reset(); |
| 83 } | 82 } |
| 84 | 83 |
| 85 // Sets up MockNetworkChangeNotifier as if it's connected to a network with | 84 protected: |
| 85 // Sets up FakeNetworkChangeNotifier as if it's connected to a network with |
| 86 // the specified connection type. | 86 // the specified connection type. |
| 87 void ChangeConnectionType(net::NetworkChangeNotifier::ConnectionType type) { | 87 void ChangeConnectionType(net::NetworkChangeNotifier::ConnectionType type) { |
| 88 EXPECT_CALL(*mock_network_change_notifier_, GetCurrentConnectionType()) | 88 fake_network_change_notifier_->set_connection_type(type); |
| 89 .WillRepeatedly(Return(type)); | |
| 90 // Notify the sync client that the network is changed. This is done via | 89 // Notify the sync client that the network is changed. This is done via |
| 91 // NetworkChangeNotifier in production, but here, we simulate the behavior | 90 // NetworkChangeNotifier in production, but here, we simulate the behavior |
| 92 // by directly calling OnConnectionTypeChanged(). | 91 // by directly calling OnConnectionTypeChanged(). |
| 93 scheduler_->OnConnectionTypeChanged(type); | 92 scheduler_->OnConnectionTypeChanged(type); |
| 94 } | 93 } |
| 95 | 94 |
| 96 // Sets up MockNetworkChangeNotifier as if it's connected to wifi network. | 95 // Sets up FakeNetworkChangeNotifier as if it's connected to wifi network. |
| 97 void ConnectToWifi() { | 96 void ConnectToWifi() { |
| 98 ChangeConnectionType(net::NetworkChangeNotifier::CONNECTION_WIFI); | 97 ChangeConnectionType(net::NetworkChangeNotifier::CONNECTION_WIFI); |
| 99 } | 98 } |
| 100 | 99 |
| 101 // Sets up MockNetworkChangeNotifier as if it's connected to cellular network. | 100 // Sets up FakeNetworkChangeNotifier as if it's connected to cellular network. |
| 102 void ConnectToCellular() { | 101 void ConnectToCellular() { |
| 103 ChangeConnectionType(net::NetworkChangeNotifier::CONNECTION_2G); | 102 ChangeConnectionType(net::NetworkChangeNotifier::CONNECTION_2G); |
| 104 } | 103 } |
| 105 | 104 |
| 106 // Sets up MockNetworkChangeNotifier as if it's connected to wimax network. | 105 // Sets up FakeNetworkChangeNotifier as if it's connected to wimax network. |
| 107 void ConnectToWimax() { | 106 void ConnectToWimax() { |
| 108 ChangeConnectionType(net::NetworkChangeNotifier::CONNECTION_4G); | 107 ChangeConnectionType(net::NetworkChangeNotifier::CONNECTION_4G); |
| 109 } | 108 } |
| 110 | 109 |
| 111 // Sets up MockNetworkChangeNotifier as if it's disconnected. | 110 // Sets up FakeNetworkChangeNotifier as if it's disconnected. |
| 112 void ConnectToNone() { | 111 void ConnectToNone() { |
| 113 ChangeConnectionType(net::NetworkChangeNotifier::CONNECTION_NONE); | 112 ChangeConnectionType(net::NetworkChangeNotifier::CONNECTION_NONE); |
| 114 } | 113 } |
| 115 | 114 |
| 116 protected: | |
| 117 MessageLoopForUI message_loop_; | 115 MessageLoopForUI message_loop_; |
| 118 content::TestBrowserThread ui_thread_; | 116 content::TestBrowserThread ui_thread_; |
| 119 scoped_ptr<TestingProfile> profile_; | 117 scoped_ptr<TestingProfile> profile_; |
| 120 scoped_ptr<DriveScheduler> scheduler_; | 118 scoped_ptr<DriveScheduler> scheduler_; |
| 121 scoped_ptr<MockNetworkChangeNotifier> mock_network_change_notifier_; | 119 scoped_ptr<FakeNetworkChangeNotifier> fake_network_change_notifier_; |
| 122 scoped_ptr<google_apis::FakeDriveService> fake_drive_service_; | 120 scoped_ptr<google_apis::FakeDriveService> fake_drive_service_; |
| 123 }; | 121 }; |
| 124 | 122 |
| 125 TEST_F(DriveSchedulerTest, GetAboutResource) { | 123 TEST_F(DriveSchedulerTest, GetAboutResource) { |
| 126 ConnectToWifi(); | 124 ConnectToWifi(); |
| 127 | 125 |
| 128 google_apis::GDataErrorCode error = google_apis::GDATA_OTHER_ERROR; | 126 google_apis::GDataErrorCode error = google_apis::GDATA_OTHER_ERROR; |
| 129 scoped_ptr<google_apis::AboutResource> about_resource; | 127 scoped_ptr<google_apis::AboutResource> about_resource; |
| 130 scheduler_->GetAboutResource( | 128 scheduler_->GetAboutResource( |
| 131 google_apis::test_util::CreateCopyResultCallback( | 129 google_apis::test_util::CreateCopyResultCallback( |
| (...skipping 590 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 722 | 720 |
| 723 // Run the background downloading job as well. | 721 // Run the background downloading job as well. |
| 724 ConnectToWifi(); | 722 ConnectToWifi(); |
| 725 google_apis::test_util::RunBlockingPoolTask(); | 723 google_apis::test_util::RunBlockingPoolTask(); |
| 726 | 724 |
| 727 // All jobs should have finished. | 725 // All jobs should have finished. |
| 728 EXPECT_EQ(0U, scheduler_->GetJobInfoList().size()); | 726 EXPECT_EQ(0U, scheduler_->GetJobInfoList().size()); |
| 729 } | 727 } |
| 730 | 728 |
| 731 } // namespace drive | 729 } // namespace drive |
| OLD | NEW |