| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/chrome_to_mobile_service.h" | 5 #include "chrome/browser/chrome_to_mobile_service.h" |
| 6 | 6 |
| 7 #include "chrome/browser/signin/token_service.h" | 7 #include "chrome/browser/signin/token_service.h" |
| 8 #include "chrome/common/chrome_notification_types.h" | 8 #include "chrome/common/chrome_notification_types.h" |
| 9 #include "chrome/common/net/gaia/gaia_constants.h" | 9 #include "chrome/common/net/gaia/gaia_constants.h" |
| 10 #include "content/public/browser/notification_details.h" | 10 #include "content/public/browser/notification_details.h" |
| 11 #include "content/public/browser/notification_source.h" | 11 #include "content/public/browser/notification_source.h" |
| 12 #include "testing/gmock/include/gmock/gmock.h" | 12 #include "testing/gmock/include/gmock/gmock.h" |
| 13 | 13 |
| 14 namespace { | 14 namespace { |
| 15 | 15 |
| 16 const char kDummyString[] = "dummy"; | 16 const char kDummyString[] = "dummy"; |
| 17 | 17 |
| 18 class DummyNotificationSource {}; | 18 class DummyNotificationSource {}; |
| 19 | 19 |
| 20 // A mock ChromeToMobileService with a mocked out RequestAccessToken method. | |
| 21 class MockChromeToMobileService : public ChromeToMobileService { | 20 class MockChromeToMobileService : public ChromeToMobileService { |
| 22 public: | 21 public: |
| 23 MockChromeToMobileService(); | 22 MockChromeToMobileService(); |
| 24 ~MockChromeToMobileService(); | 23 ~MockChromeToMobileService(); |
| 25 | 24 |
| 26 MOCK_METHOD0(RequestAccessToken, void()); | 25 MOCK_METHOD0(RequestAccessToken, void()); |
| 27 | 26 |
| 28 private: | 27 private: |
| 29 DISALLOW_COPY_AND_ASSIGN(MockChromeToMobileService); | 28 DISALLOW_COPY_AND_ASSIGN(MockChromeToMobileService); |
| 30 }; | 29 }; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 43 MockChromeToMobileService service_; | 42 MockChromeToMobileService service_; |
| 44 | 43 |
| 45 private: | 44 private: |
| 46 DISALLOW_COPY_AND_ASSIGN(ChromeToMobileServiceTest); | 45 DISALLOW_COPY_AND_ASSIGN(ChromeToMobileServiceTest); |
| 47 }; | 46 }; |
| 48 | 47 |
| 49 ChromeToMobileServiceTest::ChromeToMobileServiceTest() {} | 48 ChromeToMobileServiceTest::ChromeToMobileServiceTest() {} |
| 50 | 49 |
| 51 ChromeToMobileServiceTest::~ChromeToMobileServiceTest() {} | 50 ChromeToMobileServiceTest::~ChromeToMobileServiceTest() {} |
| 52 | 51 |
| 53 // Ensure that RequestAccessToken is not called for irrelevant notifications. | 52 // Ensure that irrelevant notifications do not invalidate the access token. |
| 54 TEST_F(ChromeToMobileServiceTest, IgnoreIrrelevantNotifications) { | 53 TEST_F(ChromeToMobileServiceTest, IgnoreIrrelevantNotifications) { |
| 55 EXPECT_CALL(service_, RequestAccessToken()).Times(0); | 54 EXPECT_CALL(service_, RequestAccessToken()).Times(0); |
| 56 | 55 |
| 57 // Send dummy service/token details (should not refresh token). | 56 service_.SetAccessTokenForTest(kDummyString); |
| 57 ASSERT_FALSE(service_.GetAccessTokenForTest().empty()); |
| 58 |
| 59 // Send dummy service/token details (should not request token). |
| 58 DummyNotificationSource dummy_source; | 60 DummyNotificationSource dummy_source; |
| 59 TokenService::TokenAvailableDetails dummy_details(kDummyString, kDummyString); | 61 TokenService::TokenAvailableDetails dummy_details(kDummyString, kDummyString); |
| 60 service_.Observe(chrome::NOTIFICATION_TOKEN_AVAILABLE, | 62 service_.Observe(chrome::NOTIFICATION_TOKEN_AVAILABLE, |
| 61 content::Source<DummyNotificationSource>(&dummy_source), | 63 content::Source<DummyNotificationSource>(&dummy_source), |
| 62 content::Details<TokenService::TokenAvailableDetails>(&dummy_details)); | 64 content::Details<TokenService::TokenAvailableDetails>(&dummy_details)); |
| 65 EXPECT_FALSE(service_.GetAccessTokenForTest().empty()); |
| 63 } | 66 } |
| 64 | 67 |
| 65 // Ensure that RequestAccessToken is called on the proper notification. | 68 // Ensure that proper notifications invalidate the access token. |
| 66 TEST_F(ChromeToMobileServiceTest, AuthenticateOnTokenAvailable) { | 69 TEST_F(ChromeToMobileServiceTest, AuthenticateOnTokenAvailable) { |
| 67 EXPECT_CALL(service_, RequestAccessToken()).Times(1); | 70 EXPECT_CALL(service_, RequestAccessToken()).Times(0); |
| 68 | 71 |
| 69 // Send a Gaia OAuth2 Login service dummy token (should refresh token). | 72 service_.SetAccessTokenForTest(kDummyString); |
| 73 ASSERT_FALSE(service_.GetAccessTokenForTest().empty()); |
| 74 |
| 75 // Send a Gaia OAuth2 Login service dummy token (should request token). |
| 70 DummyNotificationSource dummy_source; | 76 DummyNotificationSource dummy_source; |
| 71 TokenService::TokenAvailableDetails login_details( | 77 TokenService::TokenAvailableDetails login_details( |
| 72 GaiaConstants::kGaiaOAuth2LoginRefreshToken, kDummyString); | 78 GaiaConstants::kGaiaOAuth2LoginRefreshToken, kDummyString); |
| 73 service_.Observe(chrome::NOTIFICATION_TOKEN_AVAILABLE, | 79 service_.Observe(chrome::NOTIFICATION_TOKEN_AVAILABLE, |
| 74 content::Source<DummyNotificationSource>(&dummy_source), | 80 content::Source<DummyNotificationSource>(&dummy_source), |
| 75 content::Details<TokenService::TokenAvailableDetails>(&login_details)); | 81 content::Details<TokenService::TokenAvailableDetails>(&login_details)); |
| 82 EXPECT_TRUE(service_.GetAccessTokenForTest().empty()); |
| 76 } | 83 } |
| 77 | 84 |
| 78 } // namespace | 85 } // namespace |
| OLD | NEW |