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/test/test_simple_task_runner.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) | 17 #if defined(OS_CHROMEOS) |
21 #include "chromeos/dbus/dbus_thread_manager.h" | 18 #include "chromeos/dbus/dbus_thread_manager.h" |
22 #endif | 19 #endif |
23 #include "components/gcm_driver/fake_gcm_app_handler.h" | 20 #include "components/gcm_driver/fake_gcm_app_handler.h" |
24 #include "components/gcm_driver/fake_gcm_client.h" | 21 #include "components/gcm_driver/fake_gcm_client.h" |
25 #include "components/gcm_driver/fake_gcm_client_factory.h" | 22 #include "components/gcm_driver/fake_gcm_client_factory.h" |
23 #include "components/gcm_driver/gcm_channel_status_syncer.h" | |
26 #include "components/gcm_driver/gcm_client.h" | 24 #include "components/gcm_driver/gcm_client.h" |
27 #include "components/gcm_driver/gcm_client_factory.h" | 25 #include "components/gcm_driver/gcm_client_factory.h" |
28 #include "components/gcm_driver/gcm_driver.h" | 26 #include "components/gcm_driver/gcm_driver.h" |
29 #include "components/pref_registry/pref_registry_syncable.h" | 27 #include "components/pref_registry/pref_registry_syncable.h" |
28 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h" | |
29 #include "components/signin/core/browser/fake_signin_manager.h" | |
30 #include "components/signin/core/browser/signin_manager.h" | 30 #include "components/signin/core/browser/signin_manager.h" |
31 #include "content/public/browser/browser_context.h" | 31 #include "components/signin/core/browser/test_signin_client.h" |
32 #include "components/signin/core/common/signin_pref_names.h" | |
32 #include "content/public/browser/browser_thread.h" | 33 #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" | 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 scoped_ptr<TestSigninClient> client; | |
122 client.reset(new TestSigninClient(&prefs_)); | |
droger
2015/12/14 13:54:34
If you do this, the client will be destroyed at th
Jitu( very slow this week)
2015/12/14 14:27:54
Done.
| |
123 AccountTrackerService tracker; | |
droger
2015/12/14 13:54:34
Same for AccountTrackerService, you probably need
Jitu( very slow this week)
2015/12/14 14:27:54
Done.
| |
124 tracker.Initialize(client.get()); | |
125 signin_manager_.reset(new FakeSigninManagerBase(client.get(), &tracker)); | |
126 signin_manager_->Initialize(&prefs_); | |
127 | |
128 fake_token_service_.reset(new FakeProfileOAuth2TokenService()); | |
129 | |
139 #if defined(OS_CHROMEOS) | 130 #if defined(OS_CHROMEOS) |
140 // Create a DBus thread manager setter for its side effect. | 131 // Create a DBus thread manager setter for its side effect. |
141 // Ignore the return value. | 132 // Ignore the return value. |
142 chromeos::DBusThreadManager::GetSetterForTesting(); | 133 chromeos::DBusThreadManager::GetSetterForTesting(); |
143 #endif | 134 #endif |
144 TestingProfile::Builder builder; | |
145 profile_ = builder.Build(); | |
146 } | 135 } |
147 | 136 |
148 void GCMProfileServiceTest::TearDown() { | 137 void GCMProfileServiceTest::TearDown() { |
149 gcm_profile_service_->driver()->RemoveAppHandler(kTestAppID); | 138 gcm_profile_service_->driver()->RemoveAppHandler(kTestAppID); |
150 } | 139 } |
151 | 140 |
152 void GCMProfileServiceTest::CreateGCMProfileService() { | 141 void GCMProfileServiceTest::CreateGCMProfileService() { |
153 gcm_profile_service_ = static_cast<GCMProfileService*>( | 142 net::URLRequestContextGetter* request_context = |
154 GCMProfileServiceFactory::GetInstance()->SetTestingFactoryAndUse( | 143 new net::TestURLRequestContextGetter(message_loop_.task_runner()); |
155 profile_.get(), | 144 |
156 &BuildGCMProfileService)); | 145 base::SequencedWorkerPool* worker_pool = |
157 gcm_profile_service_->driver()->AddAppHandler( | 146 content::BrowserThread::GetBlockingPool(); |
158 kTestAppID, gcm_app_handler_.get()); | 147 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner( |
148 worker_pool->GetSequencedTaskRunnerWithShutdownBehavior( | |
149 worker_pool->GetSequenceToken(), | |
150 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)); | |
151 gcm_profile_service_ = new gcm::GCMProfileService( | |
152 &prefs_, temp_dir_.path(), request_context, | |
153 version_info::Channel::UNKNOWN, | |
154 scoped_ptr<ProfileIdentityProvider>( | |
155 new ProfileIdentityProvider(signin_manager_.get(), | |
156 fake_token_service_.get(), | |
157 base::Closure())) | |
158 .Pass(), | |
159 scoped_ptr<gcm::GCMClientFactory>( | |
160 new gcm::FakeGCMClientFactory(base::ThreadTaskRunnerHandle::Get(), | |
161 message_loop_.task_runner())) | |
162 .Pass(), | |
163 base::ThreadTaskRunnerHandle::Get(), message_loop_.task_runner(), | |
droger
2015/12/14 13:54:34
Nit: I think that message_loop_.task_runner() and
Jitu( very slow this week)
2015/12/14 14:27:54
message_loop_.task_runner() and
base::ThreadTaskRu
droger
2015/12/14 14:31:47
SingleThreadTaskRunner inherits from SequencedTask
| |
164 blocking_task_runner); | |
droger
2015/12/14 13:54:34
Does it fail if you use message_loop_.task_runner(
Jitu( very slow this week)
2015/12/14 14:27:54
Getting below Compilation error
../../components/
droger
2015/12/14 14:31:47
Same here, the conversion should work.
| |
165 gcm_profile_service_->driver()->AddAppHandler(kTestAppID, | |
166 gcm_app_handler_.get()); | |
159 } | 167 } |
160 | 168 |
161 void GCMProfileServiceTest::RegisterAndWaitForCompletion( | 169 void GCMProfileServiceTest::RegisterAndWaitForCompletion( |
162 const std::vector<std::string>& sender_ids) { | 170 const std::vector<std::string>& sender_ids) { |
163 base::RunLoop run_loop; | 171 base::RunLoop run_loop; |
164 gcm_profile_service_->driver()->Register( | 172 gcm_profile_service_->driver()->Register( |
165 kTestAppID, | 173 kTestAppID, |
166 sender_ids, | 174 sender_ids, |
167 base::Bind(&GCMProfileServiceTest::RegisterCompleted, | 175 base::Bind(&GCMProfileServiceTest::RegisterCompleted, |
168 base::Unretained(this), | 176 base::Unretained(this), |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
240 OutgoingMessage message; | 248 OutgoingMessage message; |
241 message.id = "1"; | 249 message.id = "1"; |
242 message.data["key1"] = "value1"; | 250 message.data["key1"] = "value1"; |
243 SendAndWaitForCompletion(message); | 251 SendAndWaitForCompletion(message); |
244 | 252 |
245 EXPECT_EQ(message.id, send_message_id()); | 253 EXPECT_EQ(message.id, send_message_id()); |
246 EXPECT_EQ(GCMClient::SUCCESS, send_result()); | 254 EXPECT_EQ(GCMClient::SUCCESS, send_result()); |
247 } | 255 } |
248 | 256 |
249 } // namespace gcm | 257 } // namespace gcm |
OLD | NEW |