Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(559)

Side by Side Diff: chrome/browser/services/gcm/gcm_profile_service_unittest.cc

Issue 286213003: Make GCMProfileService own GCMDriver, instead of deriving from it (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Sync Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 "chrome/browser/services/gcm/gcm_profile_service.h" 5 #include "chrome/browser/services/gcm/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/run_loop.h" 13 #include "base/run_loop.h"
14 #include "chrome/browser/services/gcm/fake_gcm_client.h" 14 #include "chrome/browser/services/gcm/fake_gcm_client.h"
15 #include "chrome/browser/services/gcm/fake_gcm_client_factory.h" 15 #include "chrome/browser/services/gcm/fake_gcm_client_factory.h"
16 #include "chrome/browser/services/gcm/fake_signin_manager.h" 16 #include "chrome/browser/services/gcm/fake_signin_manager.h"
17 #include "chrome/browser/services/gcm/gcm_driver.h"
17 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" 18 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h"
18 #include "chrome/browser/signin/signin_manager_factory.h" 19 #include "chrome/browser/signin/signin_manager_factory.h"
19 #include "chrome/test/base/testing_profile.h" 20 #include "chrome/test/base/testing_profile.h"
20 #include "components/gcm_driver/gcm_client_factory.h" 21 #include "components/gcm_driver/gcm_client_factory.h"
21 #include "components/pref_registry/pref_registry_syncable.h" 22 #include "components/pref_registry/pref_registry_syncable.h"
22 #include "content/public/browser/browser_context.h" 23 #include "content/public/browser/browser_context.h"
23 #include "content/public/test/test_browser_thread_bundle.h" 24 #include "content/public/test/test_browser_thread_bundle.h"
24 #include "google_apis/gcm/gcm_client.h" 25 #include "google_apis/gcm/gcm_client.h"
25 #include "testing/gtest/include/gtest/gtest.h" 26 #include "testing/gtest/include/gtest/gtest.h"
26 27
27 namespace gcm { 28 namespace gcm {
28 29
29 namespace { 30 namespace {
30 31
31 const char kTestAccountID[] = "user@example.com"; 32 const char kTestAccountID[] = "user@example.com";
32 const char kTestAppID[] = "TestApp"; 33 const char kTestAppID[] = "TestApp";
33 const char kUserID[] = "user"; 34 const char kUserID[] = "user";
34 35
35 KeyedService* BuildGCMProfileService(content::BrowserContext* context) { 36 KeyedService* BuildGCMProfileService(content::BrowserContext* context) {
36 return new GCMProfileService(Profile::FromBrowserContext(context)); 37 return new GCMProfileService(
38 Profile::FromBrowserContext(context),
39 scoped_ptr<GCMClientFactory>(
40 new FakeGCMClientFactory(FakeGCMClient::NO_DELAY_START)));
37 } 41 }
38 42
39 } // namespace 43 } // namespace
40 44
41 class GCMProfileServiceTest : public testing::Test { 45 class GCMProfileServiceTest : public testing::Test {
42 protected: 46 protected:
43 GCMProfileServiceTest(); 47 GCMProfileServiceTest();
44 virtual ~GCMProfileServiceTest(); 48 virtual ~GCMProfileServiceTest();
45 49
46 // testing::Test: 50 // testing::Test:
47 virtual void SetUp() OVERRIDE; 51 virtual void SetUp() OVERRIDE;
48 52
49 FakeGCMClient* GetGCMClient() const; 53 FakeGCMClient* GetGCMClient() const;
50 54
51 void RegisterAndWaitForCompletion(const std::vector<std::string>& sender_ids); 55 void RegisterAndWaitForCompletion(const std::vector<std::string>& sender_ids);
56 void UnregisterAndWaitForCompletion();
52 void SendAndWaitForCompletion(const GCMClient::OutgoingMessage& message); 57 void SendAndWaitForCompletion(const GCMClient::OutgoingMessage& message);
53 58
54 void RegisterCompleted(const base::Closure& callback, 59 void RegisterCompleted(const base::Closure& callback,
55 const std::string& registration_id, 60 const std::string& registration_id,
56 GCMClient::Result result); 61 GCMClient::Result result);
62 void UnregisterCompleted(const base::Closure& callback,
63 GCMClient::Result result);
57 void SendCompleted(const base::Closure& callback, 64 void SendCompleted(const base::Closure& callback,
58 const std::string& message_id, 65 const std::string& message_id,
59 GCMClient::Result result); 66 GCMClient::Result result);
60 67
68 GCMDriver* driver() const { return gcm_profile_service_->driver(); }
69 std::string registration_id() const { return registration_id_; }
70 GCMClient::Result registration_result() const { return registration_result_; }
71 GCMClient::Result unregistration_result() const {
72 return unregistration_result_;
73 }
74 std::string send_message_id() const { return send_message_id_; }
75 GCMClient::Result send_result() const { return send_result_; }
76
77 private:
61 content::TestBrowserThreadBundle thread_bundle_; 78 content::TestBrowserThreadBundle thread_bundle_;
62 scoped_ptr<TestingProfile> profile_; 79 scoped_ptr<TestingProfile> profile_;
63 GCMProfileService* gcm_profile_service_; 80 GCMProfileService* gcm_profile_service_;
64 81
65 std::string registration_id_; 82 std::string registration_id_;
66 GCMClient::Result registration_result_; 83 GCMClient::Result registration_result_;
84 GCMClient::Result unregistration_result_;
67 std::string send_message_id_; 85 std::string send_message_id_;
68 GCMClient::Result send_result_; 86 GCMClient::Result send_result_;
69 87
70 private:
71 DISALLOW_COPY_AND_ASSIGN(GCMProfileServiceTest); 88 DISALLOW_COPY_AND_ASSIGN(GCMProfileServiceTest);
72 }; 89 };
73 90
74 GCMProfileServiceTest::GCMProfileServiceTest() 91 GCMProfileServiceTest::GCMProfileServiceTest()
75 : gcm_profile_service_(NULL), 92 : gcm_profile_service_(NULL),
76 registration_result_(GCMClient::UNKNOWN_ERROR), 93 registration_result_(GCMClient::UNKNOWN_ERROR),
77 send_result_(GCMClient::UNKNOWN_ERROR) { 94 send_result_(GCMClient::UNKNOWN_ERROR) {
78 } 95 }
79 96
80 GCMProfileServiceTest::~GCMProfileServiceTest() { 97 GCMProfileServiceTest::~GCMProfileServiceTest() {
81 } 98 }
82 99
83 FakeGCMClient* GCMProfileServiceTest::GetGCMClient() const { 100 FakeGCMClient* GCMProfileServiceTest::GetGCMClient() const {
84 return static_cast<FakeGCMClient*>( 101 return static_cast<FakeGCMClient*>(
85 gcm_profile_service_->GetGCMClientForTesting()); 102 gcm_profile_service_->driver()->GetGCMClientForTesting());
86 } 103 }
87 104
88 void GCMProfileServiceTest::SetUp() { 105 void GCMProfileServiceTest::SetUp() {
89 TestingProfile::Builder builder; 106 TestingProfile::Builder builder;
90 builder.AddTestingFactory(SigninManagerFactory::GetInstance(), 107 builder.AddTestingFactory(SigninManagerFactory::GetInstance(),
91 FakeSigninManager::Build); 108 FakeSigninManager::Build);
92 profile_ = builder.Build(); 109 profile_ = builder.Build();
93 110
94 gcm_profile_service_ = static_cast<GCMProfileService*>( 111 gcm_profile_service_ = static_cast<GCMProfileService*>(
95 GCMProfileServiceFactory::GetInstance()->SetTestingFactoryAndUse( 112 GCMProfileServiceFactory::GetInstance()->SetTestingFactoryAndUse(
96 profile_.get(), 113 profile_.get(),
97 &BuildGCMProfileService)); 114 &BuildGCMProfileService));
98 gcm_profile_service_->Initialize(scoped_ptr<GCMClientFactory>(
99 new FakeGCMClientFactory(FakeGCMClient::NO_DELAY_START)));
100 115
101 FakeSigninManager* signin_manager = static_cast<FakeSigninManager*>( 116 FakeSigninManager* signin_manager = static_cast<FakeSigninManager*>(
102 SigninManagerFactory::GetInstance()->GetForProfile(profile_.get())); 117 SigninManagerFactory::GetInstance()->GetForProfile(profile_.get()));
103 signin_manager->SignIn(kTestAccountID); 118 signin_manager->SignIn(kTestAccountID);
104 base::RunLoop().RunUntilIdle(); 119 base::RunLoop().RunUntilIdle();
105 } 120 }
106 121
107 void GCMProfileServiceTest::RegisterAndWaitForCompletion( 122 void GCMProfileServiceTest::RegisterAndWaitForCompletion(
108 const std::vector<std::string>& sender_ids) { 123 const std::vector<std::string>& sender_ids) {
109 base::RunLoop run_loop; 124 base::RunLoop run_loop;
110 gcm_profile_service_->Register( 125 gcm_profile_service_->driver()->Register(
111 kTestAppID, 126 kTestAppID,
112 sender_ids, 127 sender_ids,
113 base::Bind(&GCMProfileServiceTest::RegisterCompleted, 128 base::Bind(&GCMProfileServiceTest::RegisterCompleted,
114 base::Unretained(this), 129 base::Unretained(this),
115 run_loop.QuitClosure())); 130 run_loop.QuitClosure()));
116 run_loop.Run(); 131 run_loop.Run();
117 } 132 }
118 133
134 void GCMProfileServiceTest::UnregisterAndWaitForCompletion() {
135 base::RunLoop run_loop;
136 gcm_profile_service_->driver()->Unregister(
137 kTestAppID,
138 base::Bind(&GCMProfileServiceTest::UnregisterCompleted,
139 base::Unretained(this),
140 run_loop.QuitClosure()));
141 run_loop.Run();
142 }
143
119 void GCMProfileServiceTest::SendAndWaitForCompletion( 144 void GCMProfileServiceTest::SendAndWaitForCompletion(
120 const GCMClient::OutgoingMessage& message) { 145 const GCMClient::OutgoingMessage& message) {
121 base::RunLoop run_loop; 146 base::RunLoop run_loop;
122 gcm_profile_service_->Send(kTestAppID, 147 gcm_profile_service_->driver()->Send(
123 kUserID, 148 kTestAppID,
124 message, 149 kUserID,
125 base::Bind(&GCMProfileServiceTest::SendCompleted, 150 message,
126 base::Unretained(this), 151 base::Bind(&GCMProfileServiceTest::SendCompleted,
127 run_loop.QuitClosure())); 152 base::Unretained(this),
153 run_loop.QuitClosure()));
128 run_loop.Run(); 154 run_loop.Run();
129 } 155 }
130 156
131 void GCMProfileServiceTest::RegisterCompleted( 157 void GCMProfileServiceTest::RegisterCompleted(
132 const base::Closure& callback, 158 const base::Closure& callback,
133 const std::string& registration_id, 159 const std::string& registration_id,
134 GCMClient::Result result) { 160 GCMClient::Result result) {
135 registration_id_ = registration_id; 161 registration_id_ = registration_id;
136 registration_result_ = result; 162 registration_result_ = result;
137 callback.Run(); 163 callback.Run();
138 } 164 }
139 165
166 void GCMProfileServiceTest::UnregisterCompleted(
167 const base::Closure& callback,
168 GCMClient::Result result) {
169 unregistration_result_ = result;
170 callback.Run();
171 }
172
140 void GCMProfileServiceTest::SendCompleted( 173 void GCMProfileServiceTest::SendCompleted(
141 const base::Closure& callback, 174 const base::Closure& callback,
142 const std::string& message_id, 175 const std::string& message_id,
143 GCMClient::Result result) { 176 GCMClient::Result result) {
144 send_message_id_ = message_id; 177 send_message_id_ = message_id;
145 send_result_ = result; 178 send_result_ = result;
146 callback.Run(); 179 callback.Run();
147 } 180 }
148 181
149 TEST_F(GCMProfileServiceTest, RegisterUnderNeutralChannelSignal) { 182 TEST_F(GCMProfileServiceTest, RegisterAndUnregister) {
150 // GCMClient should not be checked in.
151 EXPECT_FALSE(gcm_profile_service_->IsGCMClientReady());
152 EXPECT_EQ(FakeGCMClient::UNINITIALIZED, GetGCMClient()->status());
153
154 // Invoking register will make GCMClient checked in.
155 std::vector<std::string> sender_ids; 183 std::vector<std::string> sender_ids;
156 sender_ids.push_back("sender"); 184 sender_ids.push_back("sender");
157 RegisterAndWaitForCompletion(sender_ids); 185 RegisterAndWaitForCompletion(sender_ids);
158 186
159 // GCMClient should be checked in.
160 EXPECT_TRUE(gcm_profile_service_->IsGCMClientReady());
161 EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status());
162
163 // Registration should succeed.
164 std::string expected_registration_id = 187 std::string expected_registration_id =
165 FakeGCMClient::GetRegistrationIdFromSenderIds(sender_ids); 188 FakeGCMClient::GetRegistrationIdFromSenderIds(sender_ids);
166 EXPECT_EQ(expected_registration_id, registration_id_); 189 EXPECT_EQ(expected_registration_id, registration_id());
167 EXPECT_EQ(GCMClient::SUCCESS, registration_result_); 190 EXPECT_EQ(GCMClient::SUCCESS, registration_result());
191
192 UnregisterAndWaitForCompletion();
193 EXPECT_EQ(GCMClient::SUCCESS, unregistration_result());
168 } 194 }
169 195
170 TEST_F(GCMProfileServiceTest, SendUnderNeutralChannelSignal) { 196 TEST_F(GCMProfileServiceTest, Send) {
171 // GCMClient should not be checked in.
172 EXPECT_FALSE(gcm_profile_service_->IsGCMClientReady());
173 EXPECT_EQ(FakeGCMClient::UNINITIALIZED, GetGCMClient()->status());
174
175 // Invoking send will make GCMClient checked in.
176 GCMClient::OutgoingMessage message; 197 GCMClient::OutgoingMessage message;
177 message.id = "1"; 198 message.id = "1";
178 message.data["key1"] = "value1"; 199 message.data["key1"] = "value1";
179 SendAndWaitForCompletion( message); 200 SendAndWaitForCompletion( message);
180 201
181 // GCMClient should be checked in. 202 EXPECT_EQ(message.id, send_message_id());
182 EXPECT_TRUE(gcm_profile_service_->IsGCMClientReady()); 203 EXPECT_EQ(GCMClient::SUCCESS, send_result());
183 EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status());
184
185 // Sending should succeed.
186 EXPECT_EQ(message.id, send_message_id_);
187 EXPECT_EQ(GCMClient::SUCCESS, send_result_);
188 } 204 }
189 205
190 } // namespace gcm 206 } // namespace gcm
OLDNEW
« no previous file with comments | « chrome/browser/services/gcm/gcm_profile_service_factory.cc ('k') | chrome/browser/sync/profile_sync_service.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698