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