| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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 "base/bind.h" | |
| 6 #include "base/command_line.h" | |
| 7 #include "base/compiler_specific.h" | |
| 8 #include "base/memory/weak_ptr.h" | |
| 9 #include "base/message_loop.h" | |
| 10 #include "base/prefs/testing_pref_service.h" | |
| 11 #include "base/synchronization/waitable_event.h" | |
| 12 #include "chrome/browser/extensions/app_notify_channel_setup.h" | |
| 13 #include "chrome/browser/extensions/app_notify_channel_ui.h" | |
| 14 #include "chrome/browser/signin/token_service_factory.h" | |
| 15 #include "chrome/browser/signin/token_service_unittest.h" | |
| 16 #include "chrome/common/chrome_switches.h" | |
| 17 #include "chrome/common/pref_names.h" | |
| 18 #include "chrome/test/base/testing_profile.h" | |
| 19 #include "content/public/test/test_browser_thread.h" | |
| 20 #include "google_apis/gaia/gaia_urls.h" | |
| 21 #include "googleurl/src/gurl.h" | |
| 22 #include "net/url_request/test_url_fetcher_factory.h" | |
| 23 #include "testing/gmock/include/gmock/gmock.h" | |
| 24 #include "testing/gtest/include/gtest/gtest.h" | |
| 25 | |
| 26 using content::BrowserThread; | |
| 27 using testing::_; | |
| 28 using testing::Return; | |
| 29 | |
| 30 namespace extensions { | |
| 31 | |
| 32 namespace { | |
| 33 | |
| 34 const int kRouteId = 4; | |
| 35 const int kCallbackId = 5; | |
| 36 const char* kFakeExtensionId = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; | |
| 37 | |
| 38 static const char kValidAccessTokenResponse[] = | |
| 39 "{" | |
| 40 " \"access_token\": \"at1\"," | |
| 41 " \"expires_in\": 3600," | |
| 42 " \"token_type\": \"Bearer\"" | |
| 43 "}"; | |
| 44 | |
| 45 class MockTokenService : public TokenService { | |
| 46 public: | |
| 47 MockTokenService() : mockToken_("test_refresh_token") { } | |
| 48 virtual ~MockTokenService() { } | |
| 49 | |
| 50 bool AreCredentialsValid() const OVERRIDE { | |
| 51 return true; | |
| 52 } | |
| 53 | |
| 54 const std::string& GetOAuth2LoginRefreshToken() const OVERRIDE { | |
| 55 return mockToken_; | |
| 56 } | |
| 57 | |
| 58 std::string mockToken_; | |
| 59 | |
| 60 MOCK_CONST_METHOD0(HasOAuthLoginToken, bool()); | |
| 61 }; | |
| 62 | |
| 63 ProfileKeyedService* BuildMockTokenService(Profile* profile) { | |
| 64 return new MockTokenService; | |
| 65 } | |
| 66 | |
| 67 MockTokenService* BuildForProfile(Profile* profile) { | |
| 68 return static_cast<MockTokenService*>( | |
| 69 TokenServiceFactory::GetInstance()->SetTestingFactoryAndUse( | |
| 70 profile, BuildMockTokenService)); | |
| 71 } | |
| 72 | |
| 73 class TestProfile : public TestingProfile { | |
| 74 public: | |
| 75 TestProfile() | |
| 76 : TestingProfile(), | |
| 77 token_service_(BuildForProfile(this)) { | |
| 78 } | |
| 79 | |
| 80 virtual ~TestProfile() { } | |
| 81 | |
| 82 void SetTokenServiceHasTokenResult(bool result) { | |
| 83 EXPECT_CALL(*token_service_, HasOAuthLoginToken()) | |
| 84 .WillRepeatedly(Return(result)); | |
| 85 } | |
| 86 | |
| 87 private: | |
| 88 MockTokenService* token_service_; | |
| 89 }; | |
| 90 | |
| 91 class TestDelegate : public AppNotifyChannelSetup::Delegate, | |
| 92 public base::SupportsWeakPtr<TestDelegate> { | |
| 93 public: | |
| 94 TestDelegate() : was_called_(false) {} | |
| 95 virtual ~TestDelegate() {} | |
| 96 | |
| 97 virtual void AppNotifyChannelSetupComplete( | |
| 98 const std::string& channel_id, | |
| 99 const std::string& error, | |
| 100 const AppNotifyChannelSetup* setup) OVERRIDE { | |
| 101 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 102 EXPECT_FALSE(was_called_); | |
| 103 was_called_ = true; | |
| 104 error_ = error; | |
| 105 route_id_ = setup->return_route_id(); | |
| 106 callback_id_ = setup->callback_id(); | |
| 107 MessageLoop::current()->Quit(); | |
| 108 } | |
| 109 | |
| 110 // Called to check that we were called with the expected arguments. | |
| 111 void ExpectWasCalled(const std::string& expected_channel_id, | |
| 112 const std::string& expected_error) { | |
| 113 EXPECT_TRUE(was_called_); | |
| 114 EXPECT_EQ(expected_error, error_); | |
| 115 EXPECT_EQ(kRouteId, route_id_); | |
| 116 EXPECT_EQ(kCallbackId, callback_id_); | |
| 117 } | |
| 118 | |
| 119 private: | |
| 120 // Has our callback been called yet? | |
| 121 bool was_called_; | |
| 122 | |
| 123 // When our AppNotifyChannelSetupComplete method is called, we copy the | |
| 124 // arguments into these member variables. | |
| 125 std::string channel_id_; | |
| 126 std::string error_; | |
| 127 int route_id_; | |
| 128 int callback_id_; | |
| 129 | |
| 130 DISALLOW_COPY_AND_ASSIGN(TestDelegate); | |
| 131 }; | |
| 132 | |
| 133 class TestUI : public AppNotifyChannelUI { | |
| 134 public: | |
| 135 TestUI() : delegate_(NULL) {} | |
| 136 virtual ~TestUI() {} | |
| 137 | |
| 138 // AppNotifyChannelUI. | |
| 139 virtual void PromptSyncSetup(Delegate* delegate) OVERRIDE { | |
| 140 CHECK(!delegate_); | |
| 141 delegate_ = delegate; | |
| 142 | |
| 143 // If we have a result, post a task to call back the delegate with | |
| 144 // it. Otherwise ReportResult can be called manually at some later point. | |
| 145 if (setup_result_.get()) { | |
| 146 MessageLoop::current()->PostTask( | |
| 147 FROM_HERE, | |
| 148 base::Bind(&TestUI::ReportResult, | |
| 149 base::Unretained(this), | |
| 150 *setup_result_)); | |
| 151 } | |
| 152 } | |
| 153 | |
| 154 // This will make us automatically call back the delegate with |result| after | |
| 155 // PromptSyncSetup is called. | |
| 156 void SetSyncSetupResult(bool result) { | |
| 157 setup_result_.reset(new bool); | |
| 158 *setup_result_ = result; | |
| 159 } | |
| 160 | |
| 161 void ReportResult(bool result) { | |
| 162 CHECK(delegate_); | |
| 163 delegate_->OnSyncSetupResult(result); | |
| 164 } | |
| 165 | |
| 166 private: | |
| 167 AppNotifyChannelUI::Delegate* delegate_; | |
| 168 scoped_ptr<bool> setup_result_; | |
| 169 | |
| 170 DISALLOW_COPY_AND_ASSIGN(TestUI); | |
| 171 }; | |
| 172 | |
| 173 } // namespace | |
| 174 | |
| 175 class AppNotifyChannelSetupTest : public testing::Test { | |
| 176 public: | |
| 177 AppNotifyChannelSetupTest() : ui_thread_(BrowserThread::UI, &message_loop_), | |
| 178 db_thread_(BrowserThread::DB), | |
| 179 ui_(new TestUI()), | |
| 180 factory_(NULL) { | |
| 181 } | |
| 182 | |
| 183 virtual ~AppNotifyChannelSetupTest() {} | |
| 184 | |
| 185 virtual void SetLoggedInUser(const std::string username) { | |
| 186 profile_.GetPrefs()->SetString(prefs::kGoogleServicesUsername, username); | |
| 187 } | |
| 188 | |
| 189 virtual AppNotifyChannelSetup* CreateInstance() { | |
| 190 GURL page_url("http://www.google.com"); | |
| 191 return new AppNotifyChannelSetup(&profile_, | |
| 192 kFakeExtensionId, | |
| 193 "1234", | |
| 194 page_url, | |
| 195 kRouteId, | |
| 196 kCallbackId, | |
| 197 ui_.release(), | |
| 198 delegate_.AsWeakPtr()); | |
| 199 } | |
| 200 | |
| 201 virtual void SetupLogin(bool should_prompt, bool should_succeed) { | |
| 202 if (should_succeed) { | |
| 203 SetLoggedInUser("user@gmail.com"); | |
| 204 profile_.SetTokenServiceHasTokenResult(true); | |
| 205 } | |
| 206 if (should_prompt) | |
| 207 ui_->SetSyncSetupResult(should_succeed); | |
| 208 } | |
| 209 | |
| 210 virtual void SetupFetchAccessToken(bool should_succeed) { | |
| 211 factory_.SetFakeResponse( | |
| 212 GaiaUrls::GetInstance()->oauth2_token_url(), | |
| 213 kValidAccessTokenResponse, | |
| 214 should_succeed); | |
| 215 } | |
| 216 | |
| 217 virtual void SetupRecordGrant(bool should_succeed) { | |
| 218 factory_.SetFakeResponse( | |
| 219 AppNotifyChannelSetup::GetOAuth2IssueTokenURL().spec(), | |
| 220 "whatever", | |
| 221 should_succeed); | |
| 222 } | |
| 223 | |
| 224 virtual void SetupGetChannelId(bool should_succeed) { | |
| 225 factory_.SetFakeResponse( | |
| 226 AppNotifyChannelSetup::GetCWSChannelServiceURL().spec(), | |
| 227 "{\"id\": \"dummy_do_not_use\"}", | |
| 228 should_succeed); | |
| 229 } | |
| 230 | |
| 231 virtual void RunServerTest(AppNotifyChannelSetup* setup, | |
| 232 const std::string& expected_code, | |
| 233 const std::string& expected_error) { | |
| 234 setup->Start(); | |
| 235 message_loop_.Run(); | |
| 236 delegate_.ExpectWasCalled(expected_code, expected_error); | |
| 237 } | |
| 238 | |
| 239 virtual void SetUp() OVERRIDE { | |
| 240 db_thread_.Start(); | |
| 241 } | |
| 242 | |
| 243 virtual void TearDown() OVERRIDE { | |
| 244 // Schedule another task on the DB thread to notify us that it's safe to | |
| 245 // carry on with the test. | |
| 246 base::WaitableEvent done(false, false); | |
| 247 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, | |
| 248 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&done))); | |
| 249 done.Wait(); | |
| 250 db_thread_.Stop(); | |
| 251 } | |
| 252 | |
| 253 protected: | |
| 254 MessageLoop message_loop_; | |
| 255 content::TestBrowserThread ui_thread_; | |
| 256 content::TestBrowserThread db_thread_; | |
| 257 TestProfile profile_; | |
| 258 TestDelegate delegate_; | |
| 259 scoped_ptr<TestUI> ui_; | |
| 260 net::FakeURLFetcherFactory factory_; | |
| 261 }; | |
| 262 | |
| 263 TEST_F(AppNotifyChannelSetupTest, LoginFailure) { | |
| 264 SetupLogin(true, false); | |
| 265 | |
| 266 scoped_refptr<AppNotifyChannelSetup> setup = CreateInstance(); | |
| 267 RunServerTest(setup, "", "canceled_by_user"); | |
| 268 } | |
| 269 | |
| 270 TEST_F(AppNotifyChannelSetupTest, DoubleFetchAccessTokenFailure) { | |
| 271 SetupLogin(false, true); | |
| 272 SetupFetchAccessToken(false); | |
| 273 SetupLogin(true, true); | |
| 274 SetupFetchAccessToken(false); | |
| 275 | |
| 276 scoped_refptr<AppNotifyChannelSetup> setup = CreateInstance(); | |
| 277 RunServerTest(setup, "", "internal_error"); | |
| 278 } | |
| 279 | |
| 280 TEST_F(AppNotifyChannelSetupTest, RecordGrantFailure) { | |
| 281 SetupLogin(false, true); | |
| 282 SetupFetchAccessToken(true); | |
| 283 SetupRecordGrant(false); | |
| 284 | |
| 285 scoped_refptr<AppNotifyChannelSetup> setup = CreateInstance(); | |
| 286 RunServerTest(setup, "", "internal_error"); | |
| 287 } | |
| 288 | |
| 289 TEST_F(AppNotifyChannelSetupTest, GetChannelIdFailure) { | |
| 290 SetupLogin(false, true); | |
| 291 SetupFetchAccessToken(true); | |
| 292 SetupRecordGrant(true); | |
| 293 SetupGetChannelId(false); | |
| 294 | |
| 295 scoped_refptr<AppNotifyChannelSetup> setup = CreateInstance(); | |
| 296 RunServerTest(setup, "", "internal_error"); | |
| 297 } | |
| 298 | |
| 299 TEST_F(AppNotifyChannelSetupTest, FirstFetchAccessTokenSuccess) { | |
| 300 SetupLogin(false, true); | |
| 301 SetupFetchAccessToken(true); | |
| 302 SetupRecordGrant(true); | |
| 303 SetupGetChannelId(true); | |
| 304 | |
| 305 scoped_refptr<AppNotifyChannelSetup> setup = CreateInstance(); | |
| 306 RunServerTest(setup, "dummy_do_not_use", ""); | |
| 307 } | |
| 308 | |
| 309 TEST_F(AppNotifyChannelSetupTest, SecondFetchAccessTokenSuccess) { | |
| 310 SetupLogin(false, true); | |
| 311 SetupFetchAccessToken(false); | |
| 312 SetupLogin(true, true); | |
| 313 SetupFetchAccessToken(true); | |
| 314 SetupRecordGrant(true); | |
| 315 SetupGetChannelId(true); | |
| 316 | |
| 317 scoped_refptr<AppNotifyChannelSetup> setup = CreateInstance(); | |
| 318 RunServerTest(setup, "dummy_do_not_use", ""); | |
| 319 } | |
| 320 | |
| 321 } // namespace extensions | |
| OLD | NEW |