OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/ui/sync/one_click_signin_sync_observer.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/callback.h" |
| 9 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
| 10 #include "chrome/browser/signin/signin_manager.h" |
| 11 #include "chrome/browser/signin/signin_manager_factory.h" |
| 12 #include "chrome/browser/signin/signin_promo.h" |
| 13 #include "chrome/browser/sync/profile_sync_service_factory.h" |
| 14 #include "chrome/browser/sync/startup_controller.h" |
| 15 #include "chrome/browser/sync/test_profile_sync_service.h" |
| 16 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 17 #include "chrome/test/base/testing_profile.h" |
| 18 #include "content/public/browser/render_view_host.h" |
| 19 #include "content/public/browser/web_contents.h" |
| 20 #include "testing/gmock/include/gmock/gmock.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" |
| 22 |
| 23 using testing::_; |
| 24 |
| 25 namespace { |
| 26 |
| 27 const char kContinueUrl[] = "https://www.example.com/"; |
| 28 |
| 29 class MockWebContentsObserver : public content::WebContentsObserver { |
| 30 public: |
| 31 explicit MockWebContentsObserver(content::WebContents* web_contents) |
| 32 : content::WebContentsObserver(web_contents) {} |
| 33 virtual ~MockWebContentsObserver() {} |
| 34 |
| 35 // A hook to verify that the OneClickSigninSyncObserver initiated a redirect |
| 36 // to the continue URL. Navigations in unit_tests never complete, but a |
| 37 // navigation start is a sufficient signal for the purposes of this test. |
| 38 // Listening for this call also has the advantage of being synchronous. |
| 39 MOCK_METHOD1(AboutToNavigateRenderView, void(content::RenderViewHost*)); |
| 40 }; |
| 41 |
| 42 class OneClickTestProfileSyncService : public TestProfileSyncService { |
| 43 public: |
| 44 virtual ~OneClickTestProfileSyncService() {} |
| 45 |
| 46 // Helper routine to be used in conjunction with |
| 47 // BrowserContextKeyedServiceFactory::SetTestingFactory(). |
| 48 static KeyedService* Build(content::BrowserContext* profile) { |
| 49 return new OneClickTestProfileSyncService(static_cast<Profile*>(profile)); |
| 50 } |
| 51 |
| 52 virtual bool FirstSetupInProgress() const OVERRIDE { |
| 53 return first_setup_in_progress_; |
| 54 } |
| 55 |
| 56 virtual bool sync_initialized() const OVERRIDE { return sync_initialized_; } |
| 57 |
| 58 void set_first_setup_in_progress(bool in_progress) { |
| 59 first_setup_in_progress_ = in_progress; |
| 60 } |
| 61 void set_sync_initialized(bool initialized) { |
| 62 sync_initialized_ = initialized; |
| 63 } |
| 64 |
| 65 private: |
| 66 explicit OneClickTestProfileSyncService(Profile* profile) |
| 67 : TestProfileSyncService( |
| 68 NULL, |
| 69 profile, |
| 70 SigninManagerFactory::GetForProfile(profile), |
| 71 ProfileOAuth2TokenServiceFactory::GetForProfile(profile), |
| 72 browser_sync::MANUAL_START), |
| 73 first_setup_in_progress_(false), |
| 74 sync_initialized_(false) {} |
| 75 |
| 76 bool first_setup_in_progress_; |
| 77 bool sync_initialized_; |
| 78 }; |
| 79 |
| 80 class TestOneClickSigninSyncObserver : public OneClickSigninSyncObserver { |
| 81 public: |
| 82 typedef base::Callback<void(TestOneClickSigninSyncObserver*)> |
| 83 DestructionCallback; |
| 84 |
| 85 TestOneClickSigninSyncObserver(content::WebContents* web_contents, |
| 86 const GURL& continue_url, |
| 87 const DestructionCallback& callback) |
| 88 : OneClickSigninSyncObserver(web_contents, continue_url), |
| 89 destruction_callback_(callback) {} |
| 90 virtual ~TestOneClickSigninSyncObserver() { destruction_callback_.Run(this); } |
| 91 |
| 92 private: |
| 93 DestructionCallback destruction_callback_; |
| 94 |
| 95 DISALLOW_COPY_AND_ASSIGN(TestOneClickSigninSyncObserver); |
| 96 }; |
| 97 |
| 98 // A trivial factory to build a null service. |
| 99 KeyedService* BuildNullService(content::BrowserContext* context) { |
| 100 return NULL; |
| 101 } |
| 102 |
| 103 } // namespace |
| 104 |
| 105 class OneClickSigninSyncObserverTest : public ChromeRenderViewHostTestHarness { |
| 106 public: |
| 107 OneClickSigninSyncObserverTest() |
| 108 : sync_service_(NULL), |
| 109 sync_observer_(NULL), |
| 110 sync_observer_destroyed_(true) {} |
| 111 |
| 112 virtual void SetUp() OVERRIDE { |
| 113 ChromeRenderViewHostTestHarness::SetUp(); |
| 114 web_contents_observer_.reset(new MockWebContentsObserver(web_contents())); |
| 115 sync_service_ = |
| 116 static_cast<OneClickTestProfileSyncService*>( |
| 117 ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse( |
| 118 profile(), OneClickTestProfileSyncService::Build)); |
| 119 } |
| 120 |
| 121 virtual void TearDown() OVERRIDE { |
| 122 // Verify that the |sync_observer_| unregistered as an observer from the |
| 123 // sync service and freed its memory. |
| 124 EXPECT_TRUE(sync_observer_destroyed_); |
| 125 EXPECT_FALSE(sync_service_->HasObserver(sync_observer_)); |
| 126 ChromeRenderViewHostTestHarness::TearDown(); |
| 127 } |
| 128 |
| 129 protected: |
| 130 void CreateSyncObserver(const std::string& url) { |
| 131 sync_observer_ = new TestOneClickSigninSyncObserver( |
| 132 web_contents(), GURL(url), |
| 133 base::Bind(&OneClickSigninSyncObserverTest::OnSyncObserverDestroyed, |
| 134 base::Unretained(this))); |
| 135 EXPECT_TRUE(sync_service_->HasObserver(sync_observer_)); |
| 136 EXPECT_TRUE(sync_observer_destroyed_); |
| 137 sync_observer_destroyed_ = false; |
| 138 } |
| 139 |
| 140 OneClickTestProfileSyncService* sync_service_; |
| 141 scoped_ptr<MockWebContentsObserver> web_contents_observer_; |
| 142 |
| 143 private: |
| 144 void OnSyncObserverDestroyed(TestOneClickSigninSyncObserver* observer) { |
| 145 EXPECT_EQ(sync_observer_, observer); |
| 146 EXPECT_FALSE(sync_observer_destroyed_); |
| 147 sync_observer_destroyed_ = true; |
| 148 } |
| 149 |
| 150 TestOneClickSigninSyncObserver* sync_observer_; |
| 151 bool sync_observer_destroyed_; |
| 152 }; |
| 153 |
| 154 // Verify that if no Sync service is present, e.g. because Sync is disabled, the |
| 155 // observer immediately loads the continue URL. |
| 156 TEST_F(OneClickSigninSyncObserverTest, NoSyncService_RedirectsImmediately) { |
| 157 // Simulate disabling Sync. |
| 158 ProfileSyncServiceFactory::GetInstance()->SetTestingFactory( |
| 159 profile(), BuildNullService); |
| 160 |
| 161 // The observer should immediately redirect to the continue URL. |
| 162 // Note that since the OneClickSigninSyncObserver calls |delete this| from the |
| 163 // constructor in this case, we can't use a TestOneClickSigninSyncObserver |
| 164 // here. Otherwise, the base class would be destroyed before the test subclass |
| 165 // is even constructed, which leads to bad times. |
| 166 EXPECT_CALL(*web_contents_observer_, AboutToNavigateRenderView(_)); |
| 167 new OneClickSigninSyncObserver(web_contents(), GURL(kContinueUrl)); |
| 168 EXPECT_EQ(GURL(kContinueUrl), web_contents()->GetVisibleURL()); |
| 169 } |
| 170 |
| 171 // Verify that when the WebContents is destroyed without any Sync notifications |
| 172 // firing, the observer cleans up its memory without loading the continue URL. |
| 173 TEST_F(OneClickSigninSyncObserverTest, WebContentsDestroyed) { |
| 174 EXPECT_CALL(*web_contents_observer_, AboutToNavigateRenderView(_)).Times(0); |
| 175 CreateSyncObserver(kContinueUrl); |
| 176 SetContents(NULL); |
| 177 } |
| 178 |
| 179 // Verify that when Sync is configured successfully, the observer loads the |
| 180 // continue URL and cleans up after itself. |
| 181 TEST_F(OneClickSigninSyncObserverTest, |
| 182 OnSyncStateChanged_SyncConfiguredSuccessfully) { |
| 183 CreateSyncObserver(kContinueUrl); |
| 184 sync_service_->set_first_setup_in_progress(false); |
| 185 sync_service_->set_sync_initialized(true); |
| 186 |
| 187 EXPECT_CALL(*web_contents_observer_, AboutToNavigateRenderView(_)); |
| 188 sync_service_->NotifyObservers(); |
| 189 EXPECT_EQ(GURL(kContinueUrl), web_contents()->GetVisibleURL()); |
| 190 } |
| 191 |
| 192 // Verify that when Sync configuration fails, the observer does not load the |
| 193 // continue URL, but still cleans up after itself. |
| 194 TEST_F(OneClickSigninSyncObserverTest, |
| 195 OnSyncStateChanged_SyncConfigurationFailed) { |
| 196 CreateSyncObserver(kContinueUrl); |
| 197 sync_service_->set_first_setup_in_progress(false); |
| 198 sync_service_->set_sync_initialized(false); |
| 199 |
| 200 EXPECT_CALL(*web_contents_observer_, AboutToNavigateRenderView(_)).Times(0); |
| 201 sync_service_->NotifyObservers(); |
| 202 EXPECT_NE(GURL(kContinueUrl), web_contents()->GetVisibleURL()); |
| 203 } |
| 204 |
| 205 // Verify that when Sync sends a notification while setup is not yet complete, |
| 206 // the observer does not load the continue URL, and continues to wait. |
| 207 TEST_F(OneClickSigninSyncObserverTest, |
| 208 OnSyncStateChanged_SyncConfigurationInProgress) { |
| 209 CreateSyncObserver(kContinueUrl); |
| 210 sync_service_->set_first_setup_in_progress(true); |
| 211 sync_service_->set_sync_initialized(false); |
| 212 |
| 213 EXPECT_CALL(*web_contents_observer_, AboutToNavigateRenderView(_)).Times(0); |
| 214 sync_service_->NotifyObservers(); |
| 215 EXPECT_NE(GURL(kContinueUrl), web_contents()->GetVisibleURL()); |
| 216 |
| 217 // Trigger an event to force state to be cleaned up. |
| 218 SetContents(NULL); |
| 219 } |
| 220 |
| 221 // Verify that if the continue_url is to the settings page, no navigation is |
| 222 // triggered, since it would be redundant. |
| 223 TEST_F(OneClickSigninSyncObserverTest, |
| 224 OnSyncStateChanged_SyncConfiguredSuccessfully_SourceIsSettings) { |
| 225 GURL continue_url = signin::GetPromoURL(signin::SOURCE_SETTINGS, false); |
| 226 CreateSyncObserver(continue_url.spec()); |
| 227 sync_service_->set_first_setup_in_progress(false); |
| 228 sync_service_->set_sync_initialized(true); |
| 229 |
| 230 EXPECT_CALL(*web_contents_observer_, AboutToNavigateRenderView(_)).Times(0); |
| 231 sync_service_->NotifyObservers(); |
| 232 EXPECT_NE(GURL(kContinueUrl), web_contents()->GetVisibleURL()); |
| 233 } |
OLD | NEW |