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/macros.h" | 12 #include "base/macros.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/message_loop/message_loop.h" |
| 15 #include "base/prefs/testing_pref_service.h" |
14 #include "base/run_loop.h" | 16 #include "base/run_loop.h" |
| 17 #include "base/single_thread_task_runner.h" |
| 18 #include "base/test/test_simple_task_runner.h" |
15 #include "build/build_config.h" | 19 #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) | 20 #if defined(OS_CHROMEOS) |
23 #include "chromeos/dbus/dbus_thread_manager.h" | 21 #include "chromeos/dbus/dbus_thread_manager.h" |
24 #endif | 22 #endif |
25 #include "components/gcm_driver/fake_gcm_app_handler.h" | 23 #include "components/gcm_driver/fake_gcm_app_handler.h" |
26 #include "components/gcm_driver/fake_gcm_client.h" | 24 #include "components/gcm_driver/fake_gcm_client.h" |
27 #include "components/gcm_driver/fake_gcm_client_factory.h" | 25 #include "components/gcm_driver/fake_gcm_client_factory.h" |
| 26 #include "components/gcm_driver/gcm_channel_status_syncer.h" |
28 #include "components/gcm_driver/gcm_client.h" | 27 #include "components/gcm_driver/gcm_client.h" |
29 #include "components/gcm_driver/gcm_client_factory.h" | 28 #include "components/gcm_driver/gcm_client_factory.h" |
30 #include "components/gcm_driver/gcm_driver.h" | 29 #include "components/gcm_driver/gcm_driver.h" |
31 #include "components/pref_registry/pref_registry_syncable.h" | 30 #include "components/signin/core/browser/account_tracker_service.h" |
| 31 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h" |
| 32 #include "components/signin/core/browser/fake_signin_manager.h" |
32 #include "components/signin/core/browser/signin_manager.h" | 33 #include "components/signin/core/browser/signin_manager.h" |
33 #include "content/public/browser/browser_context.h" | 34 #include "components/signin/core/browser/test_signin_client.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" | 35 #include "testing/gtest/include/gtest/gtest.h" |
37 | 36 |
38 namespace gcm { | 37 namespace gcm { |
39 | 38 |
40 namespace { | 39 namespace { |
41 | 40 |
42 const char kTestAppID[] = "TestApp"; | 41 const char kTestAppID[] = "TestApp"; |
43 const char kUserID[] = "user"; | 42 const char kUserID[] = "user"; |
44 | 43 |
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 | 44 } // namespace |
74 | 45 |
75 class GCMProfileServiceTest : public testing::Test { | 46 class GCMProfileServiceTest : public testing::Test { |
76 protected: | 47 protected: |
77 GCMProfileServiceTest(); | 48 GCMProfileServiceTest(); |
78 ~GCMProfileServiceTest() override; | 49 ~GCMProfileServiceTest() override; |
79 | 50 |
80 // testing::Test: | 51 // testing::Test: |
81 void SetUp() override; | 52 void SetUp() override; |
82 void TearDown() override; | 53 void TearDown() override; |
(...skipping 18 matching lines...) Expand all Loading... |
101 GCMDriver* driver() const { return gcm_profile_service_->driver(); } | 72 GCMDriver* driver() const { return gcm_profile_service_->driver(); } |
102 std::string registration_id() const { return registration_id_; } | 73 std::string registration_id() const { return registration_id_; } |
103 GCMClient::Result registration_result() const { return registration_result_; } | 74 GCMClient::Result registration_result() const { return registration_result_; } |
104 GCMClient::Result unregistration_result() const { | 75 GCMClient::Result unregistration_result() const { |
105 return unregistration_result_; | 76 return unregistration_result_; |
106 } | 77 } |
107 std::string send_message_id() const { return send_message_id_; } | 78 std::string send_message_id() const { return send_message_id_; } |
108 GCMClient::Result send_result() const { return send_result_; } | 79 GCMClient::Result send_result() const { return send_result_; } |
109 | 80 |
110 private: | 81 private: |
111 content::TestBrowserThreadBundle thread_bundle_; | 82 base::ScopedTempDir temp_dir_; |
112 scoped_ptr<TestingProfile> profile_; | 83 base::MessageLoop message_loop_; |
113 GCMProfileService* gcm_profile_service_; | 84 TestingPrefServiceSimple prefs_; |
| 85 scoped_ptr<TestSigninClient> signin_client_; |
| 86 scoped_ptr<AccountTrackerService> tracker_; |
| 87 scoped_ptr<GCMProfileService> gcm_profile_service_; |
114 scoped_ptr<FakeGCMAppHandler> gcm_app_handler_; | 88 scoped_ptr<FakeGCMAppHandler> gcm_app_handler_; |
115 | 89 |
116 std::string registration_id_; | 90 std::string registration_id_; |
117 GCMClient::Result registration_result_; | 91 GCMClient::Result registration_result_; |
118 GCMClient::Result unregistration_result_; | 92 GCMClient::Result unregistration_result_; |
119 std::string send_message_id_; | 93 std::string send_message_id_; |
120 GCMClient::Result send_result_; | 94 GCMClient::Result send_result_; |
121 | 95 |
122 DISALLOW_COPY_AND_ASSIGN(GCMProfileServiceTest); | 96 DISALLOW_COPY_AND_ASSIGN(GCMProfileServiceTest); |
123 }; | 97 }; |
124 | 98 |
125 GCMProfileServiceTest::GCMProfileServiceTest() | 99 GCMProfileServiceTest::GCMProfileServiceTest() |
126 : gcm_profile_service_(NULL), | 100 : gcm_app_handler_(new FakeGCMAppHandler), |
127 gcm_app_handler_(new FakeGCMAppHandler), | |
128 registration_result_(GCMClient::UNKNOWN_ERROR), | 101 registration_result_(GCMClient::UNKNOWN_ERROR), |
129 send_result_(GCMClient::UNKNOWN_ERROR) { | 102 send_result_(GCMClient::UNKNOWN_ERROR) { |
130 } | 103 } |
131 | 104 |
132 GCMProfileServiceTest::~GCMProfileServiceTest() { | 105 GCMProfileServiceTest::~GCMProfileServiceTest() { |
133 } | 106 } |
134 | 107 |
135 FakeGCMClient* GCMProfileServiceTest::GetGCMClient() const { | 108 FakeGCMClient* GCMProfileServiceTest::GetGCMClient() const { |
136 return static_cast<FakeGCMClient*>( | 109 return static_cast<FakeGCMClient*>( |
137 gcm_profile_service_->driver()->GetGCMClientForTesting()); | 110 gcm_profile_service_->driver()->GetGCMClientForTesting()); |
138 } | 111 } |
139 | 112 |
140 void GCMProfileServiceTest::SetUp() { | 113 void GCMProfileServiceTest::SetUp() { |
| 114 GCMChannelStatusSyncer::RegisterPrefs(prefs_.registry()); |
| 115 signin_client_.reset(new TestSigninClient(&prefs_)); |
| 116 tracker_.reset(new AccountTrackerService()); |
| 117 |
| 118 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
141 #if defined(OS_CHROMEOS) | 119 #if defined(OS_CHROMEOS) |
142 // Create a DBus thread manager setter for its side effect. | 120 // Create a DBus thread manager setter for its side effect. |
143 // Ignore the return value. | 121 // Ignore the return value. |
144 chromeos::DBusThreadManager::GetSetterForTesting(); | 122 chromeos::DBusThreadManager::GetSetterForTesting(); |
145 #endif | 123 #endif |
146 TestingProfile::Builder builder; | |
147 profile_ = builder.Build(); | |
148 } | 124 } |
149 | 125 |
150 void GCMProfileServiceTest::TearDown() { | 126 void GCMProfileServiceTest::TearDown() { |
151 gcm_profile_service_->driver()->RemoveAppHandler(kTestAppID); | 127 gcm_profile_service_->driver()->RemoveAppHandler(kTestAppID); |
152 } | 128 } |
153 | 129 |
154 void GCMProfileServiceTest::CreateGCMProfileService() { | 130 void GCMProfileServiceTest::CreateGCMProfileService() { |
155 gcm_profile_service_ = static_cast<GCMProfileService*>( | 131 scoped_refptr<base::SingleThreadTaskRunner> task_runner = |
156 GCMProfileServiceFactory::GetInstance()->SetTestingFactoryAndUse( | 132 message_loop_.task_runner(); |
157 profile_.get(), | 133 scoped_refptr<net::URLRequestContextGetter> request_context = |
158 &BuildGCMProfileService)); | 134 new net::TestURLRequestContextGetter(task_runner); |
159 gcm_profile_service_->driver()->AddAppHandler( | 135 gcm_profile_service_.reset(new gcm::GCMProfileService( |
160 kTestAppID, gcm_app_handler_.get()); | 136 &prefs_, temp_dir_.path(), request_context.get(), |
| 137 version_info::Channel::UNKNOWN, |
| 138 scoped_ptr<ProfileIdentityProvider>( |
| 139 new ProfileIdentityProvider( |
| 140 new FakeSigninManagerBase(signin_client_.get(), tracker_.get()), |
| 141 new FakeProfileOAuth2TokenService(), base::Closure())) |
| 142 .Pass(), |
| 143 scoped_ptr<gcm::GCMClientFactory>( |
| 144 new gcm::FakeGCMClientFactory(base::ThreadTaskRunnerHandle::Get(), |
| 145 task_runner)) |
| 146 .Pass(), |
| 147 base::ThreadTaskRunnerHandle::Get(), task_runner, task_runner)); |
| 148 gcm_profile_service_->driver()->AddAppHandler(kTestAppID, |
| 149 gcm_app_handler_.get()); |
161 } | 150 } |
162 | 151 |
163 void GCMProfileServiceTest::RegisterAndWaitForCompletion( | 152 void GCMProfileServiceTest::RegisterAndWaitForCompletion( |
164 const std::vector<std::string>& sender_ids) { | 153 const std::vector<std::string>& sender_ids) { |
165 base::RunLoop run_loop; | 154 base::RunLoop run_loop; |
166 gcm_profile_service_->driver()->Register( | 155 gcm_profile_service_->driver()->Register( |
167 kTestAppID, | 156 kTestAppID, |
168 sender_ids, | 157 sender_ids, |
169 base::Bind(&GCMProfileServiceTest::RegisterCompleted, | 158 base::Bind(&GCMProfileServiceTest::RegisterCompleted, |
170 base::Unretained(this), | 159 base::Unretained(this), |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
242 OutgoingMessage message; | 231 OutgoingMessage message; |
243 message.id = "1"; | 232 message.id = "1"; |
244 message.data["key1"] = "value1"; | 233 message.data["key1"] = "value1"; |
245 SendAndWaitForCompletion(message); | 234 SendAndWaitForCompletion(message); |
246 | 235 |
247 EXPECT_EQ(message.id, send_message_id()); | 236 EXPECT_EQ(message.id, send_message_id()); |
248 EXPECT_EQ(GCMClient::SUCCESS, send_result()); | 237 EXPECT_EQ(GCMClient::SUCCESS, send_result()); |
249 } | 238 } |
250 | 239 |
251 } // namespace gcm | 240 } // namespace gcm |
OLD | NEW |