| 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 <memory> | 7 #include <memory> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/callback.h" | 12 #include "base/callback.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/memory/ptr_util.h" | 14 #include "base/memory/ptr_util.h" |
| 15 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | 15 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
| 16 #include "chrome/browser/signin/signin_manager_factory.h" | 16 #include "chrome/browser/signin/signin_manager_factory.h" |
| 17 #include "chrome/browser/signin/signin_promo.h" | 17 #include "chrome/browser/signin/signin_promo.h" |
| 18 #include "chrome/browser/sync/profile_sync_service_factory.h" | 18 #include "chrome/browser/sync/profile_sync_service_factory.h" |
| 19 #include "chrome/browser/sync/profile_sync_test_util.h" | 19 #include "chrome/browser/sync/profile_sync_test_util.h" |
| 20 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 20 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 21 #include "chrome/test/base/testing_profile.h" | 21 #include "chrome/test/base/testing_profile.h" |
| 22 #include "components/browser_sync/test_profile_sync_service.h" | 22 #include "components/browser_sync/test_profile_sync_service.h" |
| 23 #include "components/signin/core/browser/signin_manager.h" | 23 #include "components/signin/core/browser/signin_manager.h" |
| 24 #include "components/sync/driver/startup_controller.h" | 24 #include "components/sync/driver/startup_controller.h" |
| 25 #include "content/public/browser/reload_type.h" | 25 #include "content/public/browser/reload_type.h" |
| 26 #include "content/public/browser/render_frame_host.h" | 26 #include "content/public/browser/render_frame_host.h" |
| 27 #include "content/public/browser/web_contents.h" | 27 #include "content/public/browser/web_contents.h" |
| 28 #include "content/public/common/browser_side_navigation_policy.h" |
| 28 #include "content/public/test/test_utils.h" | 29 #include "content/public/test/test_utils.h" |
| 29 #include "testing/gmock/include/gmock/gmock.h" | 30 #include "testing/gmock/include/gmock/gmock.h" |
| 30 #include "testing/gtest/include/gtest/gtest.h" | 31 #include "testing/gtest/include/gtest/gtest.h" |
| 31 | 32 |
| 32 using testing::_; | 33 using testing::_; |
| 33 | 34 |
| 34 namespace { | 35 namespace { |
| 35 | 36 |
| 36 const char kContinueUrl[] = "https://www.example.com/"; | 37 const char kContinueUrl[] = "https://www.example.com/"; |
| 37 | 38 |
| 38 class MockWebContentsObserver : public content::WebContentsObserver { | 39 class MockWebContentsObserver : public content::WebContentsObserver { |
| 39 public: | 40 public: |
| 40 explicit MockWebContentsObserver(content::WebContents* web_contents) | 41 explicit MockWebContentsObserver(content::WebContents* web_contents) |
| 41 : content::WebContentsObserver(web_contents) {} | 42 : content::WebContentsObserver(web_contents) {} |
| 42 virtual ~MockWebContentsObserver() {} | 43 virtual ~MockWebContentsObserver() {} |
| 43 | 44 |
| 44 // A hook to verify that the OneClickSigninSyncObserver initiated a redirect | 45 // A hook to verify that the OneClickSigninSyncObserver initiated a redirect |
| 45 // to the continue URL. Navigations in unit_tests never complete, but a | 46 // to the continue URL. Navigations in unit_tests never complete, but a |
| 46 // navigation start is a sufficient signal for the purposes of this test. | 47 // navigation start is a sufficient signal for the purposes of this test. |
| 47 // Listening for this call also has the advantage of being synchronous. | 48 // Listening for this call also has the advantage of being synchronous. |
| 49 MOCK_METHOD1(DidStartNavigation, void(content::NavigationHandle*)); |
| 50 // TODO: remove this method when PlzNavigate is turned on by default. |
| 48 MOCK_METHOD2(DidStartNavigationToPendingEntry, | 51 MOCK_METHOD2(DidStartNavigationToPendingEntry, |
| 49 void(const GURL&, content::ReloadType)); | 52 void(const GURL&, content::ReloadType)); |
| 50 }; | 53 }; |
| 51 | 54 |
| 52 class OneClickTestProfileSyncService | 55 class OneClickTestProfileSyncService |
| 53 : public browser_sync::TestProfileSyncService { | 56 : public browser_sync::TestProfileSyncService { |
| 54 public: | 57 public: |
| 55 ~OneClickTestProfileSyncService() override {} | 58 ~OneClickTestProfileSyncService() override {} |
| 56 | 59 |
| 57 // Helper routine to be used in conjunction with | 60 // Helper routine to be used in conjunction with |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 170 | 173 |
| 171 // Verify that if no Sync service is present, e.g. because Sync is disabled, the | 174 // Verify that if no Sync service is present, e.g. because Sync is disabled, the |
| 172 // observer immediately loads the continue URL. | 175 // observer immediately loads the continue URL. |
| 173 TEST_F(OneClickSigninSyncObserverTest, NoSyncService_RedirectsImmediately) { | 176 TEST_F(OneClickSigninSyncObserverTest, NoSyncService_RedirectsImmediately) { |
| 174 // Simulate disabling Sync. | 177 // Simulate disabling Sync. |
| 175 sync_service_ = static_cast<OneClickTestProfileSyncService*>( | 178 sync_service_ = static_cast<OneClickTestProfileSyncService*>( |
| 176 ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse( | 179 ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse( |
| 177 profile(), BuildNullService)); | 180 profile(), BuildNullService)); |
| 178 | 181 |
| 179 // The observer should immediately redirect to the continue URL. | 182 // The observer should immediately redirect to the continue URL. |
| 180 EXPECT_CALL(*web_contents_observer_, DidStartNavigationToPendingEntry(_, _)); | 183 if (content::IsBrowserSideNavigationEnabled()) { |
| 184 EXPECT_CALL(*web_contents_observer_, DidStartNavigation(_)); |
| 185 } else { |
| 186 EXPECT_CALL(*web_contents_observer_, |
| 187 DidStartNavigationToPendingEntry(_, _)); |
| 188 } |
| 181 CreateSyncObserver(kContinueUrl); | 189 CreateSyncObserver(kContinueUrl); |
| 182 EXPECT_EQ(GURL(kContinueUrl), web_contents()->GetVisibleURL()); | 190 EXPECT_EQ(GURL(kContinueUrl), web_contents()->GetVisibleURL()); |
| 183 | 191 |
| 184 // The |sync_observer_| will be destroyed asynchronously, so manually pump | 192 // The |sync_observer_| will be destroyed asynchronously, so manually pump |
| 185 // the message loop to wait for the destruction. | 193 // the message loop to wait for the destruction. |
| 186 content::RunAllPendingInMessageLoop(); | 194 content::RunAllPendingInMessageLoop(); |
| 187 } | 195 } |
| 188 | 196 |
| 189 // Verify that when the WebContents is destroyed without any Sync notifications | 197 // Verify that when the WebContents is destroyed without any Sync notifications |
| 190 // firing, the observer cleans up its memory without loading the continue URL. | 198 // firing, the observer cleans up its memory without loading the continue URL. |
| 191 TEST_F(OneClickSigninSyncObserverTest, WebContentsDestroyed) { | 199 TEST_F(OneClickSigninSyncObserverTest, WebContentsDestroyed) { |
| 192 EXPECT_CALL(*web_contents_observer_, | 200 if (content::IsBrowserSideNavigationEnabled()) { |
| 193 DidStartNavigationToPendingEntry(_, _)).Times(0); | 201 EXPECT_CALL(*web_contents_observer_, DidStartNavigation(_)).Times(0);; |
| 194 CreateSyncObserver(kContinueUrl); | 202 CreateSyncObserver(kContinueUrl); |
| 195 SetContents(NULL); | 203 SetContents(NULL); |
| 204 } else { |
| 205 EXPECT_CALL(*web_contents_observer_, |
| 206 DidStartNavigationToPendingEntry(_, _)).Times(0); |
| 207 CreateSyncObserver(kContinueUrl); |
| 208 SetContents(NULL); |
| 209 } |
| 196 } | 210 } |
| 197 | 211 |
| 198 // Verify that when Sync is configured successfully, the observer loads the | 212 // Verify that when Sync is configured successfully, the observer loads the |
| 199 // continue URL and cleans up after itself. | 213 // continue URL and cleans up after itself. |
| 200 TEST_F(OneClickSigninSyncObserverTest, | 214 TEST_F(OneClickSigninSyncObserverTest, |
| 201 OnSyncStateChanged_SyncConfiguredSuccessfully) { | 215 OnSyncStateChanged_SyncConfiguredSuccessfully) { |
| 202 CreateSyncObserver(kContinueUrl); | 216 CreateSyncObserver(kContinueUrl); |
| 203 sync_service_->set_first_setup_in_progress(false); | 217 sync_service_->set_first_setup_in_progress(false); |
| 204 sync_service_->set_sync_active(true); | 218 sync_service_->set_sync_active(true); |
| 205 | 219 |
| 206 EXPECT_CALL(*web_contents_observer_, DidStartNavigationToPendingEntry(_, _)); | 220 if (content::IsBrowserSideNavigationEnabled()) { |
| 221 EXPECT_CALL(*web_contents_observer_, DidStartNavigation(_)); |
| 222 } else { |
| 223 EXPECT_CALL(*web_contents_observer_, |
| 224 DidStartNavigationToPendingEntry(_, _)); |
| 225 } |
| 207 sync_service_->NotifyObservers(); | 226 sync_service_->NotifyObservers(); |
| 208 EXPECT_EQ(GURL(kContinueUrl), web_contents()->GetVisibleURL()); | 227 EXPECT_EQ(GURL(kContinueUrl), web_contents()->GetVisibleURL()); |
| 209 } | 228 } |
| 210 | 229 |
| 211 // Verify that when Sync configuration fails, the observer does not load the | 230 // Verify that when Sync configuration fails, the observer does not load the |
| 212 // continue URL, but still cleans up after itself. | 231 // continue URL, but still cleans up after itself. |
| 213 TEST_F(OneClickSigninSyncObserverTest, | 232 TEST_F(OneClickSigninSyncObserverTest, |
| 214 OnSyncStateChanged_SyncConfigurationFailed) { | 233 OnSyncStateChanged_SyncConfigurationFailed) { |
| 215 CreateSyncObserver(kContinueUrl); | 234 CreateSyncObserver(kContinueUrl); |
| 216 sync_service_->set_first_setup_in_progress(false); | 235 sync_service_->set_first_setup_in_progress(false); |
| 217 sync_service_->set_sync_active(false); | 236 sync_service_->set_sync_active(false); |
| 218 | 237 |
| 219 EXPECT_CALL(*web_contents_observer_, | 238 if (content::IsBrowserSideNavigationEnabled()) { |
| 220 DidStartNavigationToPendingEntry(_, _)).Times(0); | 239 EXPECT_CALL(*web_contents_observer_, DidStartNavigation(_)).Times(0); |
| 221 sync_service_->NotifyObservers(); | 240 sync_service_->NotifyObservers(); |
| 241 } else { |
| 242 EXPECT_CALL(*web_contents_observer_, |
| 243 DidStartNavigationToPendingEntry(_, _)).Times(0); |
| 244 sync_service_->NotifyObservers(); |
| 245 } |
| 222 EXPECT_NE(GURL(kContinueUrl), web_contents()->GetVisibleURL()); | 246 EXPECT_NE(GURL(kContinueUrl), web_contents()->GetVisibleURL()); |
| 223 } | 247 } |
| 224 | 248 |
| 225 // Verify that when Sync sends a notification while setup is not yet complete, | 249 // Verify that when Sync sends a notification while setup is not yet complete, |
| 226 // the observer does not load the continue URL, and continues to wait. | 250 // the observer does not load the continue URL, and continues to wait. |
| 227 TEST_F(OneClickSigninSyncObserverTest, | 251 TEST_F(OneClickSigninSyncObserverTest, |
| 228 OnSyncStateChanged_SyncConfigurationInProgress) { | 252 OnSyncStateChanged_SyncConfigurationInProgress) { |
| 229 CreateSyncObserver(kContinueUrl); | 253 CreateSyncObserver(kContinueUrl); |
| 230 sync_service_->set_first_setup_in_progress(true); | 254 sync_service_->set_first_setup_in_progress(true); |
| 231 sync_service_->set_sync_active(false); | 255 sync_service_->set_sync_active(false); |
| 232 | 256 |
| 233 EXPECT_CALL(*web_contents_observer_, | 257 if (content::IsBrowserSideNavigationEnabled()) { |
| 234 DidStartNavigationToPendingEntry(_, _)).Times(0); | 258 EXPECT_CALL(*web_contents_observer_, DidStartNavigation(_)).Times(0);; |
| 235 sync_service_->NotifyObservers(); | 259 sync_service_->NotifyObservers(); |
| 260 } else { |
| 261 EXPECT_CALL(*web_contents_observer_, |
| 262 DidStartNavigationToPendingEntry(_, _)).Times(0);; |
| 263 sync_service_->NotifyObservers(); |
| 264 } |
| 236 EXPECT_NE(GURL(kContinueUrl), web_contents()->GetVisibleURL()); | 265 EXPECT_NE(GURL(kContinueUrl), web_contents()->GetVisibleURL()); |
| 237 | 266 |
| 238 // Trigger an event to force state to be cleaned up. | 267 // Trigger an event to force state to be cleaned up. |
| 239 SetContents(NULL); | 268 SetContents(NULL); |
| 240 } | 269 } |
| 241 | 270 |
| 242 // Verify that if the continue_url is to the settings page, no navigation is | 271 // Verify that if the continue_url is to the settings page, no navigation is |
| 243 // triggered, since it would be redundant. | 272 // triggered, since it would be redundant. |
| 244 TEST_F(OneClickSigninSyncObserverTest, | 273 TEST_F(OneClickSigninSyncObserverTest, |
| 245 OnSyncStateChanged_SyncConfiguredSuccessfully_SourceIsSettings) { | 274 OnSyncStateChanged_SyncConfiguredSuccessfully_SourceIsSettings) { |
| 246 GURL continue_url = signin::GetPromoURL( | 275 GURL continue_url = signin::GetPromoURL( |
| 247 signin_metrics::AccessPoint::ACCESS_POINT_SETTINGS, | 276 signin_metrics::AccessPoint::ACCESS_POINT_SETTINGS, |
| 248 signin_metrics::Reason::REASON_SIGNIN_PRIMARY_ACCOUNT, false); | 277 signin_metrics::Reason::REASON_SIGNIN_PRIMARY_ACCOUNT, false); |
| 249 CreateSyncObserver(continue_url.spec()); | 278 CreateSyncObserver(continue_url.spec()); |
| 250 sync_service_->set_first_setup_in_progress(false); | 279 sync_service_->set_first_setup_in_progress(false); |
| 251 sync_service_->set_sync_active(true); | 280 sync_service_->set_sync_active(true); |
| 252 | 281 |
| 253 EXPECT_CALL(*web_contents_observer_, | 282 if (content::IsBrowserSideNavigationEnabled()) { |
| 254 DidStartNavigationToPendingEntry(_, _)).Times(0); | 283 EXPECT_CALL(*web_contents_observer_, DidStartNavigation(_)).Times(0); |
| 255 sync_service_->NotifyObservers(); | 284 sync_service_->NotifyObservers(); |
| 285 } else { |
| 286 EXPECT_CALL(*web_contents_observer_, |
| 287 DidStartNavigationToPendingEntry(_, _)).Times(0); |
| 288 sync_service_->NotifyObservers(); |
| 289 } |
| 256 EXPECT_NE(GURL(kContinueUrl), web_contents()->GetVisibleURL()); | 290 EXPECT_NE(GURL(kContinueUrl), web_contents()->GetVisibleURL()); |
| 257 } | 291 } |
| OLD | NEW |