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