OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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/webui/signin/sync_confirmation_handler.h" |
| 6 |
| 7 #include "base/values.h" |
| 8 #include "chrome/browser/profiles/profile_avatar_icon_util.h" |
| 9 #include "chrome/browser/signin/account_fetcher_service_factory.h" |
| 10 #include "chrome/browser/signin/account_tracker_service_factory.h" |
| 11 #include "chrome/browser/signin/fake_account_fetcher_service_builder.h" |
| 12 #include "chrome/browser/signin/fake_signin_manager_builder.h" |
| 13 #include "chrome/browser/signin/signin_manager_factory.h" |
| 14 #include "chrome/browser/sync/profile_sync_service_factory.h" |
| 15 #include "chrome/browser/ui/browser_commands.h" |
| 16 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 17 #include "chrome/browser/ui/webui/signin/sync_confirmation_ui.h" |
| 18 #include "chrome/test/base/browser_with_test_window_test.h" |
| 19 #include "chrome/test/base/dialog_test_browser_window.h" |
| 20 #include "chrome/test/base/testing_profile.h" |
| 21 #include "components/browser_sync/browser/profile_sync_service.h" |
| 22 #include "components/signin/core/browser/account_fetcher_service.h" |
| 23 #include "components/signin/core/browser/fake_account_fetcher_service.h" |
| 24 #include "components/signin/core/browser/fake_signin_manager.h" |
| 25 #include "content/public/test/test_web_ui.h" |
| 26 |
| 27 const int kExpectedProfileImageSize = 128; |
| 28 |
| 29 class TestingSyncConfirmationHandler : public SyncConfirmationHandler { |
| 30 public: |
| 31 explicit TestingSyncConfirmationHandler(content::WebUI* web_ui) { |
| 32 set_web_ui(web_ui); |
| 33 } |
| 34 |
| 35 using SyncConfirmationHandler::HandleConfirm; |
| 36 using SyncConfirmationHandler::HandleUndo; |
| 37 using SyncConfirmationHandler::HandleInitialized; |
| 38 using SyncConfirmationHandler::HandleGoToSettings; |
| 39 using SyncConfirmationHandler::SetUserImageURL; |
| 40 }; |
| 41 |
| 42 class SyncConfirmationHandlerTest : public BrowserWithTestWindowTest { |
| 43 public: |
| 44 SyncConfirmationHandlerTest() : web_ui_(new content::TestWebUI) {} |
| 45 void SetUp() override { |
| 46 BrowserWithTestWindowTest::SetUp(); |
| 47 chrome::NewTab(browser()); |
| 48 web_ui()->set_web_contents( |
| 49 browser()->tab_strip_model()->GetActiveWebContents()); |
| 50 |
| 51 // WebUI owns the handlers. |
| 52 handler_ = new TestingSyncConfirmationHandler(web_ui()); |
| 53 sync_confirmation_ui_.reset( |
| 54 new SyncConfirmationUI(web_ui(), handler_)); |
| 55 |
| 56 // This dialog assumes the signin flow was completed, which kicks off the |
| 57 // SigninManager. |
| 58 new OneClickSigninSyncStarter( |
| 59 profile(), |
| 60 browser(), |
| 61 "gaia", |
| 62 "foo@example.com", |
| 63 "password", |
| 64 "refresh_token", |
| 65 OneClickSigninSyncStarter::SYNC_WITH_DEFAULT_SETTINGS, |
| 66 nullptr, |
| 67 OneClickSigninSyncStarter::NO_CONFIRMATION, |
| 68 GURL(), |
| 69 GURL(), |
| 70 OneClickSigninSyncStarter::Callback()); |
| 71 } |
| 72 |
| 73 void TearDown() override { |
| 74 sync_confirmation_ui_.reset(); |
| 75 web_ui_.reset(); |
| 76 BrowserWithTestWindowTest::TearDown(); |
| 77 } |
| 78 |
| 79 TestingSyncConfirmationHandler* handler() { |
| 80 return handler_; |
| 81 } |
| 82 |
| 83 content::TestWebUI* web_ui() { |
| 84 return web_ui_.get(); |
| 85 } |
| 86 |
| 87 FakeAccountFetcherService* account_fetcher_service() { |
| 88 return static_cast<FakeAccountFetcherService*>( |
| 89 AccountFetcherServiceFactory::GetForProfile(profile())); |
| 90 } |
| 91 |
| 92 FakeSigninManager* signin_manager() { |
| 93 return static_cast<FakeSigninManager*>( |
| 94 SigninManagerFactory::GetForProfile(profile())); |
| 95 } |
| 96 |
| 97 ProfileSyncService* sync() { |
| 98 return ProfileSyncServiceFactory::GetForProfile(profile()); |
| 99 } |
| 100 |
| 101 // BrowserWithTestWindowTest |
| 102 BrowserWindow* CreateBrowserWindow() override { |
| 103 return new DialogTestBrowserWindow; |
| 104 } |
| 105 |
| 106 TestingProfile* CreateProfile() override { |
| 107 TestingProfile::Builder builder; |
| 108 builder.AddTestingFactory(AccountFetcherServiceFactory::GetInstance(), |
| 109 FakeAccountFetcherServiceBuilder::BuildForTests); |
| 110 builder.AddTestingFactory( |
| 111 SigninManagerFactory::GetInstance(), BuildFakeSigninManagerBase); |
| 112 return builder.Build().release(); |
| 113 } |
| 114 |
| 115 private: |
| 116 scoped_ptr<content::TestWebUI> web_ui_; |
| 117 scoped_ptr<SyncConfirmationUI> sync_confirmation_ui_; |
| 118 TestingSyncConfirmationHandler* handler_; // Not owned. |
| 119 }; |
| 120 |
| 121 TEST_F(SyncConfirmationHandlerTest, TestSetImageIfPrimaryAccountReady) { |
| 122 account_fetcher_service()->FakeUserInfoFetchSuccess( |
| 123 "gaia", |
| 124 "foo@example.com", |
| 125 "gaia", |
| 126 "", |
| 127 "full_name", |
| 128 "given_name", |
| 129 "locale", |
| 130 "http://picture.example.com/picture.jpg"); |
| 131 |
| 132 handler()->HandleInitialized(nullptr); |
| 133 EXPECT_EQ(1U, web_ui()->call_data().size()); |
| 134 EXPECT_EQ("sync.confirmation.setUserImageURL", |
| 135 web_ui()->call_data()[0]->function_name()); |
| 136 EXPECT_TRUE( |
| 137 web_ui()->call_data()[0]->arg1()->IsType(base::Value::TYPE_STRING)); |
| 138 std::string passed_picture_url; |
| 139 EXPECT_TRUE( |
| 140 web_ui()->call_data()[0]->arg1()->GetAsString(&passed_picture_url)); |
| 141 |
| 142 std::string original_picture_url = |
| 143 AccountTrackerServiceFactory::GetForProfile(profile())-> |
| 144 GetAccountInfo("gaia").picture_url; |
| 145 GURL picture_url_with_size; |
| 146 EXPECT_TRUE(profiles::GetImageURLWithThumbnailSize(GURL(original_picture_url), |
| 147 kExpectedProfileImageSize, |
| 148 &picture_url_with_size)); |
| 149 EXPECT_EQ(picture_url_with_size.spec(), passed_picture_url); |
| 150 handler()->HandleUndo(nullptr); |
| 151 } |
| 152 |
| 153 TEST_F(SyncConfirmationHandlerTest, TestSetImageIfPrimaryAccountReadyLater) { |
| 154 handler()->HandleInitialized(nullptr); |
| 155 EXPECT_EQ(0U, web_ui()->call_data().size()); |
| 156 |
| 157 account_fetcher_service()->FakeUserInfoFetchSuccess( |
| 158 "gaia", |
| 159 "foo@example.com", |
| 160 "gaia", |
| 161 "", |
| 162 "full_name", |
| 163 "given_name", |
| 164 "locale", |
| 165 "http://picture.example.com/picture.jpg"); |
| 166 |
| 167 EXPECT_EQ(1U, web_ui()->call_data().size()); |
| 168 EXPECT_EQ("sync.confirmation.setUserImageURL", |
| 169 web_ui()->call_data()[0]->function_name()); |
| 170 EXPECT_TRUE( |
| 171 web_ui()->call_data()[0]->arg1()->IsType(base::Value::TYPE_STRING)); |
| 172 std::string passed_picture_url; |
| 173 EXPECT_TRUE( |
| 174 web_ui()->call_data()[0]->arg1()->GetAsString(&passed_picture_url)); |
| 175 |
| 176 std::string original_picture_url = |
| 177 AccountTrackerServiceFactory::GetForProfile(profile())-> |
| 178 GetAccountInfo("gaia").picture_url; |
| 179 GURL picture_url_with_size; |
| 180 EXPECT_TRUE(profiles::GetImageURLWithThumbnailSize(GURL(original_picture_url), |
| 181 kExpectedProfileImageSize, |
| 182 &picture_url_with_size)); |
| 183 EXPECT_EQ(picture_url_with_size.spec(), passed_picture_url); |
| 184 handler()->HandleUndo(nullptr); |
| 185 } |
| 186 |
| 187 TEST_F(SyncConfirmationHandlerTest, TestHandleUndo) { |
| 188 EXPECT_FALSE(sync()->IsFirstSetupComplete()); |
| 189 EXPECT_TRUE(sync()->IsFirstSetupInProgress()); |
| 190 |
| 191 handler()->HandleUndo(nullptr); |
| 192 |
| 193 EXPECT_FALSE(sync()->IsFirstSetupInProgress()); |
| 194 EXPECT_FALSE(sync()->IsFirstSetupComplete()); |
| 195 EXPECT_FALSE( |
| 196 SigninManagerFactory::GetForProfile(profile())->IsAuthenticated()); |
| 197 } |
| 198 |
| 199 TEST_F(SyncConfirmationHandlerTest, TestHandleConfirm) { |
| 200 EXPECT_FALSE(sync()->IsFirstSetupComplete()); |
| 201 EXPECT_TRUE(sync()->IsFirstSetupInProgress()); |
| 202 |
| 203 handler()->HandleConfirm(nullptr); |
| 204 |
| 205 EXPECT_FALSE(sync()->IsFirstSetupInProgress()); |
| 206 EXPECT_TRUE(sync()->IsFirstSetupComplete()); |
| 207 EXPECT_TRUE( |
| 208 SigninManagerFactory::GetForProfile(profile())->IsAuthenticated()); |
| 209 } |
OLD | NEW |