| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "components/gcm_driver/gcm_profile_service.h" | |
| 6 | |
| 7 #include <vector> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/bind_helpers.h" | |
| 11 #include "base/callback.h" | |
| 12 #include "base/memory/scoped_ptr.h" | |
| 13 #include "base/run_loop.h" | |
| 14 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" | |
| 15 #include "chrome/browser/signin/profile_oauth2_token_service_factory.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) | |
| 21 #include "chromeos/dbus/dbus_thread_manager.h" | |
| 22 #endif | |
| 23 #include "components/gcm_driver/fake_gcm_app_handler.h" | |
| 24 #include "components/gcm_driver/fake_gcm_client.h" | |
| 25 #include "components/gcm_driver/fake_gcm_client_factory.h" | |
| 26 #include "components/gcm_driver/gcm_client.h" | |
| 27 #include "components/gcm_driver/gcm_client_factory.h" | |
| 28 #include "components/gcm_driver/gcm_driver.h" | |
| 29 #include "components/pref_registry/pref_registry_syncable.h" | |
| 30 #include "components/signin/core/browser/signin_manager.h" | |
| 31 #include "content/public/browser/browser_context.h" | |
| 32 #include "content/public/browser/browser_thread.h" | |
| 33 #include "content/public/test/test_browser_thread_bundle.h" | |
| 34 #include "testing/gtest/include/gtest/gtest.h" | |
| 35 | |
| 36 namespace gcm { | |
| 37 | |
| 38 namespace { | |
| 39 | |
| 40 const char kTestAppID[] = "TestApp"; | |
| 41 const char kUserID[] = "user"; | |
| 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 | |
| 72 | |
| 73 class GCMProfileServiceTest : public testing::Test { | |
| 74 protected: | |
| 75 GCMProfileServiceTest(); | |
| 76 ~GCMProfileServiceTest() override; | |
| 77 | |
| 78 // testing::Test: | |
| 79 void SetUp() override; | |
| 80 void TearDown() override; | |
| 81 | |
| 82 FakeGCMClient* GetGCMClient() const; | |
| 83 | |
| 84 void CreateGCMProfileService(); | |
| 85 | |
| 86 void RegisterAndWaitForCompletion(const std::vector<std::string>& sender_ids); | |
| 87 void UnregisterAndWaitForCompletion(); | |
| 88 void SendAndWaitForCompletion(const OutgoingMessage& message); | |
| 89 | |
| 90 void RegisterCompleted(const base::Closure& callback, | |
| 91 const std::string& registration_id, | |
| 92 GCMClient::Result result); | |
| 93 void UnregisterCompleted(const base::Closure& callback, | |
| 94 GCMClient::Result result); | |
| 95 void SendCompleted(const base::Closure& callback, | |
| 96 const std::string& message_id, | |
| 97 GCMClient::Result result); | |
| 98 | |
| 99 GCMDriver* driver() const { return gcm_profile_service_->driver(); } | |
| 100 std::string registration_id() const { return registration_id_; } | |
| 101 GCMClient::Result registration_result() const { return registration_result_; } | |
| 102 GCMClient::Result unregistration_result() const { | |
| 103 return unregistration_result_; | |
| 104 } | |
| 105 std::string send_message_id() const { return send_message_id_; } | |
| 106 GCMClient::Result send_result() const { return send_result_; } | |
| 107 | |
| 108 private: | |
| 109 content::TestBrowserThreadBundle thread_bundle_; | |
| 110 scoped_ptr<TestingProfile> profile_; | |
| 111 GCMProfileService* gcm_profile_service_; | |
| 112 scoped_ptr<FakeGCMAppHandler> gcm_app_handler_; | |
| 113 | |
| 114 std::string registration_id_; | |
| 115 GCMClient::Result registration_result_; | |
| 116 GCMClient::Result unregistration_result_; | |
| 117 std::string send_message_id_; | |
| 118 GCMClient::Result send_result_; | |
| 119 | |
| 120 DISALLOW_COPY_AND_ASSIGN(GCMProfileServiceTest); | |
| 121 }; | |
| 122 | |
| 123 GCMProfileServiceTest::GCMProfileServiceTest() | |
| 124 : gcm_profile_service_(NULL), | |
| 125 gcm_app_handler_(new FakeGCMAppHandler), | |
| 126 registration_result_(GCMClient::UNKNOWN_ERROR), | |
| 127 send_result_(GCMClient::UNKNOWN_ERROR) { | |
| 128 } | |
| 129 | |
| 130 GCMProfileServiceTest::~GCMProfileServiceTest() { | |
| 131 } | |
| 132 | |
| 133 FakeGCMClient* GCMProfileServiceTest::GetGCMClient() const { | |
| 134 return static_cast<FakeGCMClient*>( | |
| 135 gcm_profile_service_->driver()->GetGCMClientForTesting()); | |
| 136 } | |
| 137 | |
| 138 void GCMProfileServiceTest::SetUp() { | |
| 139 #if defined(OS_CHROMEOS) | |
| 140 // Create a DBus thread manager setter for its side effect. | |
| 141 // Ignore the return value. | |
| 142 chromeos::DBusThreadManager::GetSetterForTesting(); | |
| 143 #endif | |
| 144 TestingProfile::Builder builder; | |
| 145 profile_ = builder.Build(); | |
| 146 } | |
| 147 | |
| 148 void GCMProfileServiceTest::TearDown() { | |
| 149 gcm_profile_service_->driver()->RemoveAppHandler(kTestAppID); | |
| 150 } | |
| 151 | |
| 152 void GCMProfileServiceTest::CreateGCMProfileService() { | |
| 153 gcm_profile_service_ = static_cast<GCMProfileService*>( | |
| 154 GCMProfileServiceFactory::GetInstance()->SetTestingFactoryAndUse( | |
| 155 profile_.get(), | |
| 156 &BuildGCMProfileService)); | |
| 157 gcm_profile_service_->driver()->AddAppHandler( | |
| 158 kTestAppID, gcm_app_handler_.get()); | |
| 159 } | |
| 160 | |
| 161 void GCMProfileServiceTest::RegisterAndWaitForCompletion( | |
| 162 const std::vector<std::string>& sender_ids) { | |
| 163 base::RunLoop run_loop; | |
| 164 gcm_profile_service_->driver()->Register( | |
| 165 kTestAppID, | |
| 166 sender_ids, | |
| 167 base::Bind(&GCMProfileServiceTest::RegisterCompleted, | |
| 168 base::Unretained(this), | |
| 169 run_loop.QuitClosure())); | |
| 170 run_loop.Run(); | |
| 171 } | |
| 172 | |
| 173 void GCMProfileServiceTest::UnregisterAndWaitForCompletion() { | |
| 174 base::RunLoop run_loop; | |
| 175 gcm_profile_service_->driver()->Unregister( | |
| 176 kTestAppID, | |
| 177 base::Bind(&GCMProfileServiceTest::UnregisterCompleted, | |
| 178 base::Unretained(this), | |
| 179 run_loop.QuitClosure())); | |
| 180 run_loop.Run(); | |
| 181 } | |
| 182 | |
| 183 void GCMProfileServiceTest::SendAndWaitForCompletion( | |
| 184 const OutgoingMessage& message) { | |
| 185 base::RunLoop run_loop; | |
| 186 gcm_profile_service_->driver()->Send( | |
| 187 kTestAppID, | |
| 188 kUserID, | |
| 189 message, | |
| 190 base::Bind(&GCMProfileServiceTest::SendCompleted, | |
| 191 base::Unretained(this), | |
| 192 run_loop.QuitClosure())); | |
| 193 run_loop.Run(); | |
| 194 } | |
| 195 | |
| 196 void GCMProfileServiceTest::RegisterCompleted( | |
| 197 const base::Closure& callback, | |
| 198 const std::string& registration_id, | |
| 199 GCMClient::Result result) { | |
| 200 registration_id_ = registration_id; | |
| 201 registration_result_ = result; | |
| 202 callback.Run(); | |
| 203 } | |
| 204 | |
| 205 void GCMProfileServiceTest::UnregisterCompleted( | |
| 206 const base::Closure& callback, | |
| 207 GCMClient::Result result) { | |
| 208 unregistration_result_ = result; | |
| 209 callback.Run(); | |
| 210 } | |
| 211 | |
| 212 void GCMProfileServiceTest::SendCompleted( | |
| 213 const base::Closure& callback, | |
| 214 const std::string& message_id, | |
| 215 GCMClient::Result result) { | |
| 216 send_message_id_ = message_id; | |
| 217 send_result_ = result; | |
| 218 callback.Run(); | |
| 219 } | |
| 220 | |
| 221 TEST_F(GCMProfileServiceTest, RegisterAndUnregister) { | |
| 222 CreateGCMProfileService(); | |
| 223 | |
| 224 std::vector<std::string> sender_ids; | |
| 225 sender_ids.push_back("sender"); | |
| 226 RegisterAndWaitForCompletion(sender_ids); | |
| 227 | |
| 228 std::string expected_registration_id = | |
| 229 FakeGCMClient::GenerateGCMRegistrationID(sender_ids); | |
| 230 EXPECT_EQ(expected_registration_id, registration_id()); | |
| 231 EXPECT_EQ(GCMClient::SUCCESS, registration_result()); | |
| 232 | |
| 233 UnregisterAndWaitForCompletion(); | |
| 234 EXPECT_EQ(GCMClient::SUCCESS, unregistration_result()); | |
| 235 } | |
| 236 | |
| 237 TEST_F(GCMProfileServiceTest, Send) { | |
| 238 CreateGCMProfileService(); | |
| 239 | |
| 240 OutgoingMessage message; | |
| 241 message.id = "1"; | |
| 242 message.data["key1"] = "value1"; | |
| 243 SendAndWaitForCompletion(message); | |
| 244 | |
| 245 EXPECT_EQ(message.id, send_message_id()); | |
| 246 EXPECT_EQ(GCMClient::SUCCESS, send_result()); | |
| 247 } | |
| 248 | |
| 249 } // namespace gcm | |
| OLD | NEW |