| 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 "chrome/browser/ui/sync/one_click_signin_sync_observer.h" | 5 #include "chrome/browser/ui/sync/one_click_signin_sync_observer.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | 9 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
| 10 #include "chrome/browser/signin/signin_manager_factory.h" | 10 #include "chrome/browser/signin/signin_manager_factory.h" |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 class MockWebContentsObserver : public content::WebContentsObserver { | 31 class MockWebContentsObserver : public content::WebContentsObserver { |
| 32 public: | 32 public: |
| 33 explicit MockWebContentsObserver(content::WebContents* web_contents) | 33 explicit MockWebContentsObserver(content::WebContents* web_contents) |
| 34 : content::WebContentsObserver(web_contents) {} | 34 : content::WebContentsObserver(web_contents) {} |
| 35 virtual ~MockWebContentsObserver() {} | 35 virtual ~MockWebContentsObserver() {} |
| 36 | 36 |
| 37 // A hook to verify that the OneClickSigninSyncObserver initiated a redirect | 37 // A hook to verify that the OneClickSigninSyncObserver initiated a redirect |
| 38 // to the continue URL. Navigations in unit_tests never complete, but a | 38 // to the continue URL. Navigations in unit_tests never complete, but a |
| 39 // navigation start is a sufficient signal for the purposes of this test. | 39 // navigation start is a sufficient signal for the purposes of this test. |
| 40 // Listening for this call also has the advantage of being synchronous. | 40 // Listening for this call also has the advantage of being synchronous. |
| 41 MOCK_METHOD1(AboutToNavigateRenderFrame, void(content::RenderFrameHost*)); | 41 MOCK_METHOD2(AboutToNavigateRenderFrame, void(content::RenderFrameHost*, |
| 42 content::RenderFrameHost*)); |
| 42 }; | 43 }; |
| 43 | 44 |
| 44 class OneClickTestProfileSyncService : public TestProfileSyncService { | 45 class OneClickTestProfileSyncService : public TestProfileSyncService { |
| 45 public: | 46 public: |
| 46 ~OneClickTestProfileSyncService() override {} | 47 ~OneClickTestProfileSyncService() override {} |
| 47 | 48 |
| 48 // Helper routine to be used in conjunction with | 49 // Helper routine to be used in conjunction with |
| 49 // BrowserContextKeyedServiceFactory::SetTestingFactory(). | 50 // BrowserContextKeyedServiceFactory::SetTestingFactory(). |
| 50 static KeyedService* Build(content::BrowserContext* profile) { | 51 static KeyedService* Build(content::BrowserContext* profile) { |
| 51 return new OneClickTestProfileSyncService(static_cast<Profile*>(profile)); | 52 return new OneClickTestProfileSyncService(static_cast<Profile*>(profile)); |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 // Verify that if no Sync service is present, e.g. because Sync is disabled, the | 163 // Verify that if no Sync service is present, e.g. because Sync is disabled, the |
| 163 // observer immediately loads the continue URL. | 164 // observer immediately loads the continue URL. |
| 164 TEST_F(OneClickSigninSyncObserverTest, NoSyncService_RedirectsImmediately) { | 165 TEST_F(OneClickSigninSyncObserverTest, NoSyncService_RedirectsImmediately) { |
| 165 // Simulate disabling Sync. | 166 // Simulate disabling Sync. |
| 166 sync_service_ = | 167 sync_service_ = |
| 167 static_cast<OneClickTestProfileSyncService*>( | 168 static_cast<OneClickTestProfileSyncService*>( |
| 168 ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse( | 169 ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse( |
| 169 profile(), BuildNullService)); | 170 profile(), BuildNullService)); |
| 170 | 171 |
| 171 // The observer should immediately redirect to the continue URL. | 172 // The observer should immediately redirect to the continue URL. |
| 172 EXPECT_CALL(*web_contents_observer_, AboutToNavigateRenderFrame(_)); | 173 EXPECT_CALL(*web_contents_observer_, AboutToNavigateRenderFrame(_, _)); |
| 173 CreateSyncObserver(kContinueUrl); | 174 CreateSyncObserver(kContinueUrl); |
| 174 EXPECT_EQ(GURL(kContinueUrl), web_contents()->GetVisibleURL()); | 175 EXPECT_EQ(GURL(kContinueUrl), web_contents()->GetVisibleURL()); |
| 175 | 176 |
| 176 // The |sync_observer_| will be destroyed asynchronously, so manually pump | 177 // The |sync_observer_| will be destroyed asynchronously, so manually pump |
| 177 // the message loop to wait for the destruction. | 178 // the message loop to wait for the destruction. |
| 178 content::RunAllPendingInMessageLoop(); | 179 content::RunAllPendingInMessageLoop(); |
| 179 } | 180 } |
| 180 | 181 |
| 181 // Verify that when the WebContents is destroyed without any Sync notifications | 182 // Verify that when the WebContents is destroyed without any Sync notifications |
| 182 // firing, the observer cleans up its memory without loading the continue URL. | 183 // firing, the observer cleans up its memory without loading the continue URL. |
| 183 TEST_F(OneClickSigninSyncObserverTest, WebContentsDestroyed) { | 184 TEST_F(OneClickSigninSyncObserverTest, WebContentsDestroyed) { |
| 184 EXPECT_CALL(*web_contents_observer_, AboutToNavigateRenderFrame(_)).Times(0); | 185 EXPECT_CALL(*web_contents_observer_, |
| 186 AboutToNavigateRenderFrame(_, _)).Times(0); |
| 185 CreateSyncObserver(kContinueUrl); | 187 CreateSyncObserver(kContinueUrl); |
| 186 SetContents(NULL); | 188 SetContents(NULL); |
| 187 } | 189 } |
| 188 | 190 |
| 189 // Verify that when Sync is configured successfully, the observer loads the | 191 // Verify that when Sync is configured successfully, the observer loads the |
| 190 // continue URL and cleans up after itself. | 192 // continue URL and cleans up after itself. |
| 191 TEST_F(OneClickSigninSyncObserverTest, | 193 TEST_F(OneClickSigninSyncObserverTest, |
| 192 OnSyncStateChanged_SyncConfiguredSuccessfully) { | 194 OnSyncStateChanged_SyncConfiguredSuccessfully) { |
| 193 CreateSyncObserver(kContinueUrl); | 195 CreateSyncObserver(kContinueUrl); |
| 194 sync_service_->set_first_setup_in_progress(false); | 196 sync_service_->set_first_setup_in_progress(false); |
| 195 sync_service_->set_sync_active(true); | 197 sync_service_->set_sync_active(true); |
| 196 | 198 |
| 197 EXPECT_CALL(*web_contents_observer_, AboutToNavigateRenderFrame(_)); | 199 EXPECT_CALL(*web_contents_observer_, AboutToNavigateRenderFrame(_, _)); |
| 198 sync_service_->NotifyObservers(); | 200 sync_service_->NotifyObservers(); |
| 199 EXPECT_EQ(GURL(kContinueUrl), web_contents()->GetVisibleURL()); | 201 EXPECT_EQ(GURL(kContinueUrl), web_contents()->GetVisibleURL()); |
| 200 } | 202 } |
| 201 | 203 |
| 202 // Verify that when Sync configuration fails, the observer does not load the | 204 // Verify that when Sync configuration fails, the observer does not load the |
| 203 // continue URL, but still cleans up after itself. | 205 // continue URL, but still cleans up after itself. |
| 204 TEST_F(OneClickSigninSyncObserverTest, | 206 TEST_F(OneClickSigninSyncObserverTest, |
| 205 OnSyncStateChanged_SyncConfigurationFailed) { | 207 OnSyncStateChanged_SyncConfigurationFailed) { |
| 206 CreateSyncObserver(kContinueUrl); | 208 CreateSyncObserver(kContinueUrl); |
| 207 sync_service_->set_first_setup_in_progress(false); | 209 sync_service_->set_first_setup_in_progress(false); |
| 208 sync_service_->set_sync_active(false); | 210 sync_service_->set_sync_active(false); |
| 209 | 211 |
| 210 EXPECT_CALL(*web_contents_observer_, AboutToNavigateRenderFrame(_)).Times(0); | 212 EXPECT_CALL(*web_contents_observer_, |
| 213 AboutToNavigateRenderFrame(_, _)).Times(0); |
| 211 sync_service_->NotifyObservers(); | 214 sync_service_->NotifyObservers(); |
| 212 EXPECT_NE(GURL(kContinueUrl), web_contents()->GetVisibleURL()); | 215 EXPECT_NE(GURL(kContinueUrl), web_contents()->GetVisibleURL()); |
| 213 } | 216 } |
| 214 | 217 |
| 215 // Verify that when Sync sends a notification while setup is not yet complete, | 218 // Verify that when Sync sends a notification while setup is not yet complete, |
| 216 // the observer does not load the continue URL, and continues to wait. | 219 // the observer does not load the continue URL, and continues to wait. |
| 217 TEST_F(OneClickSigninSyncObserverTest, | 220 TEST_F(OneClickSigninSyncObserverTest, |
| 218 OnSyncStateChanged_SyncConfigurationInProgress) { | 221 OnSyncStateChanged_SyncConfigurationInProgress) { |
| 219 CreateSyncObserver(kContinueUrl); | 222 CreateSyncObserver(kContinueUrl); |
| 220 sync_service_->set_first_setup_in_progress(true); | 223 sync_service_->set_first_setup_in_progress(true); |
| 221 sync_service_->set_sync_active(false); | 224 sync_service_->set_sync_active(false); |
| 222 | 225 |
| 223 EXPECT_CALL(*web_contents_observer_, AboutToNavigateRenderFrame(_)).Times(0); | 226 EXPECT_CALL(*web_contents_observer_, |
| 227 AboutToNavigateRenderFrame(_, _)).Times(0); |
| 224 sync_service_->NotifyObservers(); | 228 sync_service_->NotifyObservers(); |
| 225 EXPECT_NE(GURL(kContinueUrl), web_contents()->GetVisibleURL()); | 229 EXPECT_NE(GURL(kContinueUrl), web_contents()->GetVisibleURL()); |
| 226 | 230 |
| 227 // Trigger an event to force state to be cleaned up. | 231 // Trigger an event to force state to be cleaned up. |
| 228 SetContents(NULL); | 232 SetContents(NULL); |
| 229 } | 233 } |
| 230 | 234 |
| 231 // Verify that if the continue_url is to the settings page, no navigation is | 235 // Verify that if the continue_url is to the settings page, no navigation is |
| 232 // triggered, since it would be redundant. | 236 // triggered, since it would be redundant. |
| 233 TEST_F(OneClickSigninSyncObserverTest, | 237 TEST_F(OneClickSigninSyncObserverTest, |
| 234 OnSyncStateChanged_SyncConfiguredSuccessfully_SourceIsSettings) { | 238 OnSyncStateChanged_SyncConfiguredSuccessfully_SourceIsSettings) { |
| 235 GURL continue_url = signin::GetPromoURL( | 239 GURL continue_url = signin::GetPromoURL( |
| 236 signin_metrics::SOURCE_SETTINGS, false); | 240 signin_metrics::SOURCE_SETTINGS, false); |
| 237 CreateSyncObserver(continue_url.spec()); | 241 CreateSyncObserver(continue_url.spec()); |
| 238 sync_service_->set_first_setup_in_progress(false); | 242 sync_service_->set_first_setup_in_progress(false); |
| 239 sync_service_->set_sync_active(true); | 243 sync_service_->set_sync_active(true); |
| 240 | 244 |
| 241 EXPECT_CALL(*web_contents_observer_, AboutToNavigateRenderFrame(_)).Times(0); | 245 EXPECT_CALL(*web_contents_observer_, |
| 246 AboutToNavigateRenderFrame(_, _)).Times(0); |
| 242 sync_service_->NotifyObservers(); | 247 sync_service_->NotifyObservers(); |
| 243 EXPECT_NE(GURL(kContinueUrl), web_contents()->GetVisibleURL()); | 248 EXPECT_NE(GURL(kContinueUrl), web_contents()->GetVisibleURL()); |
| 244 } | 249 } |
| OLD | NEW |