OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/command_line.h" | 6 #include "base/command_line.h" |
7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
8 #include "base/memory/weak_ptr.h" | 8 #include "base/memory/weak_ptr.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "chrome/browser/extensions/app_notify_channel_setup.h" | 10 #include "chrome/browser/extensions/app_notify_channel_setup.h" |
| 11 #include "chrome/browser/extensions/app_notify_channel_ui.h" |
| 12 #include "chrome/browser/net/gaia/token_service.h" |
| 13 #include "chrome/browser/net/gaia/token_service_unittest.h" |
11 #include "chrome/common/chrome_switches.h" | 14 #include "chrome/common/chrome_switches.h" |
| 15 #include "chrome/common/net/gaia/gaia_urls.h" |
12 #include "chrome/common/pref_names.h" | 16 #include "chrome/common/pref_names.h" |
13 #include "chrome/test/base/testing_pref_service.h" | 17 #include "chrome/test/base/testing_pref_service.h" |
14 #include "chrome/test/base/testing_profile.h" | 18 #include "chrome/test/base/testing_profile.h" |
15 #include "content/test/test_browser_thread.h" | 19 #include "content/test/test_browser_thread.h" |
16 #include "content/test/test_url_fetcher_factory.h" | 20 #include "content/test/test_url_fetcher_factory.h" |
17 #include "googleurl/src/gurl.h" | 21 #include "googleurl/src/gurl.h" |
| 22 #include "testing/gmock/include/gmock/gmock.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
19 | 24 |
20 using content::BrowserThread; | 25 using content::BrowserThread; |
| 26 using testing::_; |
| 27 using testing::Return; |
21 | 28 |
22 namespace { | 29 namespace { |
23 | 30 |
24 const int kRouteId = 4; | 31 const int kRouteId = 4; |
25 const int kCallbackId = 5; | 32 const int kCallbackId = 5; |
26 const char* kFakeExtensionId = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; | 33 const char* kFakeExtensionId = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; |
27 | 34 |
| 35 class MockTokenService : public TokenService { |
| 36 public: |
| 37 MockTokenService() { } |
| 38 virtual ~MockTokenService() { } |
| 39 |
| 40 bool AreCredentialsValid() const OVERRIDE { |
| 41 return true; |
| 42 } |
| 43 |
| 44 MOCK_CONST_METHOD1(HasTokenForService, bool(const char*)); |
| 45 }; |
| 46 |
| 47 class TestProfile : public TestingProfile { |
| 48 public: |
| 49 TestProfile() : TestingProfile() { } |
| 50 virtual ~TestProfile() { } |
| 51 |
| 52 virtual TokenService* GetTokenService() OVERRIDE { |
| 53 return &token_service_; |
| 54 } |
| 55 |
| 56 void SetTokenServiceHasTokenResult(bool result) { |
| 57 EXPECT_CALL(token_service_, HasTokenForService(_)) |
| 58 .WillRepeatedly(Return(result)); |
| 59 } |
| 60 |
| 61 private: |
| 62 MockTokenService token_service_; |
| 63 }; |
| 64 |
28 class TestDelegate : public AppNotifyChannelSetup::Delegate, | 65 class TestDelegate : public AppNotifyChannelSetup::Delegate, |
29 public base::SupportsWeakPtr<TestDelegate> { | 66 public base::SupportsWeakPtr<TestDelegate> { |
30 public: | 67 public: |
31 TestDelegate() : was_called_(false) {} | 68 TestDelegate() : was_called_(false) {} |
32 virtual ~TestDelegate() {} | 69 virtual ~TestDelegate() {} |
33 | 70 |
34 virtual void AppNotifyChannelSetupComplete( | 71 virtual void AppNotifyChannelSetupComplete( |
35 const std::string& channel_id, | 72 const std::string& channel_id, |
36 const std::string& error, | 73 const std::string& error, |
37 int route_id, | 74 int route_id, |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 | 157 |
121 virtual void SetLoggedInUser(const std::string username) { | 158 virtual void SetLoggedInUser(const std::string username) { |
122 TestingPrefService* prefs = profile_.GetTestingPrefService(); | 159 TestingPrefService* prefs = profile_.GetTestingPrefService(); |
123 prefs->SetUserPref(prefs::kGoogleServicesUsername, | 160 prefs->SetUserPref(prefs::kGoogleServicesUsername, |
124 new StringValue(username)); | 161 new StringValue(username)); |
125 } | 162 } |
126 | 163 |
127 virtual AppNotifyChannelSetup* CreateInstance() { | 164 virtual AppNotifyChannelSetup* CreateInstance() { |
128 GURL page_url("http://www.google.com"); | 165 GURL page_url("http://www.google.com"); |
129 return new AppNotifyChannelSetup(&profile_, | 166 return new AppNotifyChannelSetup(&profile_, |
130 kFakeExtensionId, | 167 kFakeExtensionId, |
131 "1234", | 168 "1234", |
132 page_url, | 169 page_url, |
133 kRouteId, | 170 kRouteId, |
134 kCallbackId, | 171 kCallbackId, |
135 ui_.release(), | 172 ui_.release(), |
136 delegate_.AsWeakPtr()); | 173 delegate_.AsWeakPtr()); |
137 } | 174 } |
138 | 175 |
139 virtual void SetupLogin(bool should_succeed) { | 176 virtual void SetupLogin(bool should_succeed) { |
140 if (should_succeed) | 177 if (should_succeed) |
141 SetLoggedInUser("user@gmail.com"); | 178 SetLoggedInUser("user@gmail.com"); |
142 else | 179 else |
143 ui_->SetSyncSetupResult(false); | 180 ui_->SetSyncSetupResult(false); |
144 } | 181 } |
145 | 182 |
| 183 virtual void SetupFetchTokens(bool should_have_tokens, bool should_succeed) { |
| 184 profile_.SetTokenServiceHasTokenResult(should_have_tokens); |
| 185 if (!should_have_tokens) { |
| 186 factory_.SetFakeResponse( |
| 187 GaiaUrls::GetInstance()->issue_auth_token_url(), |
| 188 "some_token", |
| 189 should_succeed); |
| 190 } |
| 191 } |
| 192 |
146 virtual void SetupRecordGrant(bool should_succeed) { | 193 virtual void SetupRecordGrant(bool should_succeed) { |
147 factory_.SetFakeResponse( | 194 factory_.SetFakeResponse( |
148 AppNotifyChannelSetup::GetOAuth2IssueTokenURL().spec(), | 195 AppNotifyChannelSetup::GetOAuth2IssueTokenURL().spec(), |
149 "whatever", | 196 "whatever", |
150 should_succeed); | 197 should_succeed); |
151 } | 198 } |
152 | 199 |
153 virtual void SetupGetChannelId(bool should_succeed) { | 200 virtual void SetupGetChannelId(bool should_succeed) { |
154 factory_.SetFakeResponse( | 201 factory_.SetFakeResponse( |
155 AppNotifyChannelSetup::GetCWSChannelServiceURL().spec(), | 202 AppNotifyChannelSetup::GetCWSChannelServiceURL().spec(), |
156 "{\"id\": \"dummy_do_not_use\"}", | 203 "{\"id\": \"dummy_do_not_use\"}", |
157 should_succeed); | 204 should_succeed); |
158 } | 205 } |
159 | 206 |
160 | |
161 virtual void RunServerTest(AppNotifyChannelSetup* setup, | 207 virtual void RunServerTest(AppNotifyChannelSetup* setup, |
162 const std::string& expected_code, | 208 const std::string& expected_code, |
163 const std::string& expected_error) { | 209 const std::string& expected_error) { |
164 setup->Start(); | 210 setup->Start(); |
165 message_loop_.Run(); | 211 message_loop_.Run(); |
166 delegate_.ExpectWasCalled(expected_code, expected_error); | 212 delegate_.ExpectWasCalled(expected_code, expected_error); |
167 } | 213 } |
168 | 214 |
169 protected: | 215 protected: |
170 MessageLoop message_loop_; | 216 MessageLoop message_loop_; |
171 content::TestBrowserThread ui_thread_; | 217 content::TestBrowserThread ui_thread_; |
172 TestingProfile profile_; | 218 TestProfile profile_; |
173 TestDelegate delegate_; | 219 TestDelegate delegate_; |
174 scoped_ptr<TestUI> ui_; | 220 scoped_ptr<TestUI> ui_; |
175 FakeURLFetcherFactory factory_; | 221 FakeURLFetcherFactory factory_; |
176 }; | 222 }; |
177 | 223 |
178 TEST_F(AppNotifyChannelSetupTest, LoginFailure) { | 224 TEST_F(AppNotifyChannelSetupTest, LoginFailure) { |
179 // Set login to fail. | |
180 SetupLogin(false); | 225 SetupLogin(false); |
181 | 226 |
182 scoped_refptr<AppNotifyChannelSetup> setup = CreateInstance(); | 227 scoped_refptr<AppNotifyChannelSetup> setup = CreateInstance(); |
183 RunServerTest(setup, "", "canceled_by_user"); | 228 RunServerTest(setup, "", "canceled_by_user"); |
184 } | 229 } |
185 | 230 |
186 TEST_F(AppNotifyChannelSetupTest, RecordGrantFailure) { | 231 TEST_F(AppNotifyChannelSetupTest, FetchTokensFailure) { |
187 SetupLogin(true); | 232 SetupLogin(true); |
| 233 SetupFetchTokens(false, false); |
| 234 |
| 235 scoped_refptr<AppNotifyChannelSetup> setup = CreateInstance(); |
| 236 RunServerTest(setup, "", "internal_error"); |
| 237 } |
| 238 |
| 239 TEST_F(AppNotifyChannelSetupTest, TokensAvailableRecordGrantFailure) { |
| 240 SetupLogin(true); |
| 241 SetupFetchTokens(true, false); |
| 242 SetupRecordGrant(false); |
| 243 |
| 244 scoped_refptr<AppNotifyChannelSetup> setup = CreateInstance(); |
| 245 RunServerTest(setup, "", "internal_error"); |
| 246 } |
| 247 |
| 248 TEST_F(AppNotifyChannelSetupTest, TokenFetchSuccessAndRecordGrantFailure) { |
| 249 SetupLogin(true); |
| 250 SetupFetchTokens(false , true); |
188 SetupRecordGrant(false); | 251 SetupRecordGrant(false); |
189 | 252 |
190 scoped_refptr<AppNotifyChannelSetup> setup = CreateInstance(); | 253 scoped_refptr<AppNotifyChannelSetup> setup = CreateInstance(); |
191 RunServerTest(setup, "", "internal_error"); | 254 RunServerTest(setup, "", "internal_error"); |
192 } | 255 } |
193 | 256 |
194 TEST_F(AppNotifyChannelSetupTest, GetChannelIdFailure) { | 257 TEST_F(AppNotifyChannelSetupTest, GetChannelIdFailure) { |
195 SetupLogin(true); | 258 SetupLogin(true); |
| 259 SetupFetchTokens(true, false); |
196 SetupRecordGrant(true); | 260 SetupRecordGrant(true); |
197 SetupGetChannelId(false); | 261 SetupGetChannelId(false); |
198 | 262 |
199 scoped_refptr<AppNotifyChannelSetup> setup = CreateInstance(); | 263 scoped_refptr<AppNotifyChannelSetup> setup = CreateInstance(); |
200 RunServerTest(setup, "", "internal_error"); | 264 RunServerTest(setup, "", "internal_error"); |
201 } | 265 } |
202 | 266 |
203 TEST_F(AppNotifyChannelSetupTest, ServerSuccess) { | 267 TEST_F(AppNotifyChannelSetupTest, ServerSuccess) { |
204 SetupLogin(true); | 268 SetupLogin(true); |
| 269 SetupFetchTokens(true, false); |
205 SetupRecordGrant(true); | 270 SetupRecordGrant(true); |
206 SetupGetChannelId(true); | 271 SetupGetChannelId(true); |
207 | 272 |
208 scoped_refptr<AppNotifyChannelSetup> setup = CreateInstance(); | 273 scoped_refptr<AppNotifyChannelSetup> setup = CreateInstance(); |
209 RunServerTest(setup, "dummy_do_not_use", ""); | 274 RunServerTest(setup, "dummy_do_not_use", ""); |
210 } | 275 } |
OLD | NEW |