Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "components/gcm_driver/gcm_profile_service.h" | 5 #include "components/gcm_driver/gcm_profile_service.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| 11 #include "base/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/message_loop/message_loop.h" | |
| 14 #include "base/prefs/testing_pref_service.h" | |
| 13 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
| 14 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" | 16 #include "base/single_thread_task_runner.h" |
| 15 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | 17 #include "base/test/test_simple_task_runner.h" |
| 16 #include "chrome/browser/signin/signin_manager_factory.h" | |
| 17 #include "chrome/browser/ui/webui/signin/login_ui_service_factory.h" | |
| 18 #include "chrome/common/channel_info.h" | |
| 19 #include "chrome/test/base/testing_profile.h" | |
| 20 #if defined(OS_CHROMEOS) | 18 #if defined(OS_CHROMEOS) |
| 21 #include "chromeos/dbus/dbus_thread_manager.h" | 19 #include "chromeos/dbus/dbus_thread_manager.h" |
| 22 #endif | 20 #endif |
| 23 #include "components/gcm_driver/fake_gcm_app_handler.h" | 21 #include "components/gcm_driver/fake_gcm_app_handler.h" |
| 24 #include "components/gcm_driver/fake_gcm_client.h" | 22 #include "components/gcm_driver/fake_gcm_client.h" |
| 25 #include "components/gcm_driver/fake_gcm_client_factory.h" | 23 #include "components/gcm_driver/fake_gcm_client_factory.h" |
| 24 #include "components/gcm_driver/gcm_channel_status_syncer.h" | |
| 26 #include "components/gcm_driver/gcm_client.h" | 25 #include "components/gcm_driver/gcm_client.h" |
| 27 #include "components/gcm_driver/gcm_client_factory.h" | 26 #include "components/gcm_driver/gcm_client_factory.h" |
| 28 #include "components/gcm_driver/gcm_driver.h" | 27 #include "components/gcm_driver/gcm_driver.h" |
| 29 #include "components/pref_registry/pref_registry_syncable.h" | 28 #include "components/pref_registry/pref_registry_syncable.h" |
| 29 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h" | |
| 30 #include "components/signin/core/browser/fake_signin_manager.h" | |
| 30 #include "components/signin/core/browser/signin_manager.h" | 31 #include "components/signin/core/browser/signin_manager.h" |
| 31 #include "content/public/browser/browser_context.h" | 32 #include "components/signin/core/browser/test_signin_client.h" |
| 32 #include "content/public/browser/browser_thread.h" | 33 #include "components/signin/core/common/signin_pref_names.h" |
| 33 #include "content/public/test/test_browser_thread_bundle.h" | |
| 34 #include "testing/gtest/include/gtest/gtest.h" | 34 #include "testing/gtest/include/gtest/gtest.h" |
| 35 | 35 |
| 36 namespace gcm { | 36 namespace gcm { |
| 37 | 37 |
| 38 namespace { | 38 namespace { |
| 39 | 39 |
| 40 const char kTestAppID[] = "TestApp"; | 40 const char kTestAppID[] = "TestApp"; |
| 41 const char kUserID[] = "user"; | 41 const char kUserID[] = "user"; |
| 42 | 42 |
| 43 scoped_ptr<KeyedService> BuildGCMProfileService( | |
| 44 content::BrowserContext* context) { | |
| 45 Profile* profile = Profile::FromBrowserContext(context); | |
| 46 base::SequencedWorkerPool* worker_pool = | |
| 47 content::BrowserThread::GetBlockingPool(); | |
| 48 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner( | |
| 49 worker_pool->GetSequencedTaskRunnerWithShutdownBehavior( | |
| 50 worker_pool->GetSequenceToken(), | |
| 51 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)); | |
| 52 return make_scoped_ptr(new gcm::GCMProfileService( | |
| 53 profile->GetPrefs(), profile->GetPath(), profile->GetRequestContext(), | |
| 54 chrome::GetChannel(), | |
| 55 scoped_ptr<ProfileIdentityProvider>(new ProfileIdentityProvider( | |
| 56 SigninManagerFactory::GetForProfile(profile), | |
| 57 ProfileOAuth2TokenServiceFactory::GetForProfile(profile), | |
| 58 LoginUIServiceFactory::GetShowLoginPopupCallbackForProfile(profile))), | |
| 59 scoped_ptr<gcm::GCMClientFactory>(new gcm::FakeGCMClientFactory( | |
| 60 content::BrowserThread::GetMessageLoopProxyForThread( | |
| 61 content::BrowserThread::UI), | |
| 62 content::BrowserThread::GetMessageLoopProxyForThread( | |
| 63 content::BrowserThread::IO))), | |
| 64 content::BrowserThread::GetMessageLoopProxyForThread( | |
| 65 content::BrowserThread::UI), | |
| 66 content::BrowserThread::GetMessageLoopProxyForThread( | |
| 67 content::BrowserThread::IO), | |
| 68 blocking_task_runner)); | |
| 69 } | |
| 70 | |
| 71 } // namespace | 43 } // namespace |
| 72 | 44 |
| 73 class GCMProfileServiceTest : public testing::Test { | 45 class GCMProfileServiceTest : public testing::Test { |
| 74 protected: | 46 protected: |
| 75 GCMProfileServiceTest(); | 47 GCMProfileServiceTest(); |
| 76 ~GCMProfileServiceTest() override; | 48 ~GCMProfileServiceTest() override; |
| 77 | 49 |
| 78 // testing::Test: | 50 // testing::Test: |
| 79 void SetUp() override; | 51 void SetUp() override; |
| 80 void TearDown() override; | 52 void TearDown() override; |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 99 GCMDriver* driver() const { return gcm_profile_service_->driver(); } | 71 GCMDriver* driver() const { return gcm_profile_service_->driver(); } |
| 100 std::string registration_id() const { return registration_id_; } | 72 std::string registration_id() const { return registration_id_; } |
| 101 GCMClient::Result registration_result() const { return registration_result_; } | 73 GCMClient::Result registration_result() const { return registration_result_; } |
| 102 GCMClient::Result unregistration_result() const { | 74 GCMClient::Result unregistration_result() const { |
| 103 return unregistration_result_; | 75 return unregistration_result_; |
| 104 } | 76 } |
| 105 std::string send_message_id() const { return send_message_id_; } | 77 std::string send_message_id() const { return send_message_id_; } |
| 106 GCMClient::Result send_result() const { return send_result_; } | 78 GCMClient::Result send_result() const { return send_result_; } |
| 107 | 79 |
| 108 private: | 80 private: |
| 109 content::TestBrowserThreadBundle thread_bundle_; | 81 base::ScopedTempDir temp_dir_; |
| 110 scoped_ptr<TestingProfile> profile_; | 82 base::MessageLoop message_loop_; |
| 83 | |
| 84 TestingPrefServiceSimple prefs_; | |
| 85 scoped_ptr<FakeSigninManagerBase> signin_manager_; | |
| 86 scoped_ptr<ProfileOAuth2TokenService> fake_token_service_; | |
| 111 GCMProfileService* gcm_profile_service_; | 87 GCMProfileService* gcm_profile_service_; |
| 112 scoped_ptr<FakeGCMAppHandler> gcm_app_handler_; | 88 scoped_ptr<FakeGCMAppHandler> gcm_app_handler_; |
| 113 | 89 |
| 114 std::string registration_id_; | 90 std::string registration_id_; |
| 115 GCMClient::Result registration_result_; | 91 GCMClient::Result registration_result_; |
| 116 GCMClient::Result unregistration_result_; | 92 GCMClient::Result unregistration_result_; |
| 117 std::string send_message_id_; | 93 std::string send_message_id_; |
| 118 GCMClient::Result send_result_; | 94 GCMClient::Result send_result_; |
| 119 | 95 |
| 120 DISALLOW_COPY_AND_ASSIGN(GCMProfileServiceTest); | 96 DISALLOW_COPY_AND_ASSIGN(GCMProfileServiceTest); |
| 121 }; | 97 }; |
| 122 | 98 |
| 123 GCMProfileServiceTest::GCMProfileServiceTest() | 99 GCMProfileServiceTest::GCMProfileServiceTest() |
| 124 : gcm_profile_service_(NULL), | 100 : gcm_profile_service_(NULL), |
| 125 gcm_app_handler_(new FakeGCMAppHandler), | 101 gcm_app_handler_(new FakeGCMAppHandler), |
| 126 registration_result_(GCMClient::UNKNOWN_ERROR), | 102 registration_result_(GCMClient::UNKNOWN_ERROR), |
| 127 send_result_(GCMClient::UNKNOWN_ERROR) { | 103 send_result_(GCMClient::UNKNOWN_ERROR) {} |
| 128 } | |
| 129 | 104 |
| 130 GCMProfileServiceTest::~GCMProfileServiceTest() { | 105 GCMProfileServiceTest::~GCMProfileServiceTest() {} |
| 131 } | |
| 132 | 106 |
| 133 FakeGCMClient* GCMProfileServiceTest::GetGCMClient() const { | 107 FakeGCMClient* GCMProfileServiceTest::GetGCMClient() const { |
| 134 return static_cast<FakeGCMClient*>( | 108 return static_cast<FakeGCMClient*>( |
| 135 gcm_profile_service_->driver()->GetGCMClientForTesting()); | 109 gcm_profile_service_->driver()->GetGCMClientForTesting()); |
| 136 } | 110 } |
| 137 | 111 |
| 138 void GCMProfileServiceTest::SetUp() { | 112 void GCMProfileServiceTest::SetUp() { |
| 113 GCMChannelStatusSyncer::RegisterPrefs(prefs_.registry()); | |
| 114 | |
| 115 prefs_.registry()->RegisterListPref(AccountTrackerService::kAccountInfoPref); | |
| 116 prefs_.registry()->RegisterIntegerPref( | |
| 117 ::prefs::kAccountIdMigrationState, | |
| 118 AccountTrackerService::MIGRATION_NOT_STARTED); | |
| 119 | |
| 120 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
| 121 signin_manager_.reset(new FakeSigninManagerBase(nullptr, nullptr)); | |
| 122 fake_token_service_.reset(new FakeProfileOAuth2TokenService()); | |
| 123 | |
| 139 #if defined(OS_CHROMEOS) | 124 #if defined(OS_CHROMEOS) |
| 140 // Create a DBus thread manager setter for its side effect. | 125 // Create a DBus thread manager setter for its side effect. |
| 141 // Ignore the return value. | 126 // Ignore the return value. |
| 142 chromeos::DBusThreadManager::GetSetterForTesting(); | 127 chromeos::DBusThreadManager::GetSetterForTesting(); |
| 143 #endif | 128 #endif |
| 144 TestingProfile::Builder builder; | |
| 145 profile_ = builder.Build(); | |
| 146 } | 129 } |
| 147 | 130 |
| 148 void GCMProfileServiceTest::TearDown() { | 131 void GCMProfileServiceTest::TearDown() { |
| 149 gcm_profile_service_->driver()->RemoveAppHandler(kTestAppID); | 132 gcm_profile_service_->driver()->RemoveAppHandler(kTestAppID); |
| 150 } | 133 } |
| 151 | 134 |
| 152 void GCMProfileServiceTest::CreateGCMProfileService() { | 135 void GCMProfileServiceTest::CreateGCMProfileService() { |
| 153 gcm_profile_service_ = static_cast<GCMProfileService*>( | 136 scoped_refptr<base::SequencedTaskRunner> task_runner = |
| 154 GCMProfileServiceFactory::GetInstance()->SetTestingFactoryAndUse( | 137 message_loop_.task_runner(); |
| 155 profile_.get(), | 138 net::URLRequestContextGetter* request_context = |
| 156 &BuildGCMProfileService)); | 139 new net::TestURLRequestContextGetter(message_loop_.task_runner()); |
|
Jitu( very slow this week)
2015/12/15 07:59:49
Converting to SingleThreadTaskRunner here gives me
droger
2015/12/15 14:51:12
If you change task_runner to be a scoped_refptr<ba
Jitu( very slow this week)
2015/12/16 05:41:09
It will work here, but it will fail for the last a
droger
2015/12/17 10:18:24
I patched your CL and changed |task_runner| to be
Jitu( very slow this week)
2015/12/17 11:52:56
Thanks a lot
| |
| 157 gcm_profile_service_->driver()->AddAppHandler( | 140 gcm_profile_service_ = new gcm::GCMProfileService( |
| 158 kTestAppID, gcm_app_handler_.get()); | 141 &prefs_, temp_dir_.path(), request_context, |
| 142 version_info::Channel::UNKNOWN, | |
| 143 scoped_ptr<ProfileIdentityProvider>( | |
| 144 new ProfileIdentityProvider(signin_manager_.get(), | |
| 145 fake_token_service_.get(), | |
| 146 base::Closure())) | |
| 147 .Pass(), | |
| 148 scoped_ptr<gcm::GCMClientFactory>( | |
| 149 new gcm::FakeGCMClientFactory(base::ThreadTaskRunnerHandle::Get(), | |
| 150 task_runner)) | |
| 151 .Pass(), | |
| 152 base::ThreadTaskRunnerHandle::Get(), task_runner, | |
| 153 task_runner); | |
| 154 gcm_profile_service_->driver()->AddAppHandler(kTestAppID, | |
| 155 gcm_app_handler_.get()); | |
| 159 } | 156 } |
| 160 | 157 |
| 161 void GCMProfileServiceTest::RegisterAndWaitForCompletion( | 158 void GCMProfileServiceTest::RegisterAndWaitForCompletion( |
| 162 const std::vector<std::string>& sender_ids) { | 159 const std::vector<std::string>& sender_ids) { |
| 163 base::RunLoop run_loop; | 160 base::RunLoop run_loop; |
| 164 gcm_profile_service_->driver()->Register( | 161 gcm_profile_service_->driver()->Register( |
| 165 kTestAppID, | 162 kTestAppID, |
| 166 sender_ids, | 163 sender_ids, |
| 167 base::Bind(&GCMProfileServiceTest::RegisterCompleted, | 164 base::Bind(&GCMProfileServiceTest::RegisterCompleted, |
| 168 base::Unretained(this), | 165 base::Unretained(this), |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 240 OutgoingMessage message; | 237 OutgoingMessage message; |
| 241 message.id = "1"; | 238 message.id = "1"; |
| 242 message.data["key1"] = "value1"; | 239 message.data["key1"] = "value1"; |
| 243 SendAndWaitForCompletion(message); | 240 SendAndWaitForCompletion(message); |
| 244 | 241 |
| 245 EXPECT_EQ(message.id, send_message_id()); | 242 EXPECT_EQ(message.id, send_message_id()); |
| 246 EXPECT_EQ(GCMClient::SUCCESS, send_result()); | 243 EXPECT_EQ(GCMClient::SUCCESS, send_result()); |
| 247 } | 244 } |
| 248 | 245 |
| 249 } // namespace gcm | 246 } // namespace gcm |
| OLD | NEW |