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