Chromium Code Reviews| 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; | |
|
Dan Beam
2016/01/26 19:54:20
why are you copying this?
anthonyvd
2016/01/26 22:00:39
It's the value I expect the image dimensions to ha
Dan Beam
2016/01/26 22:40:51
i think we generally discourage change detection t
| |
| 28 | |
| 29 class TestingSyncConfirmationHandler : public SyncConfirmationHandler { | |
| 30 public: | |
| 31 explicit TestingSyncConfirmationHandler(content::WebUI* web_ui) { | |
| 32 set_web_ui(web_ui); | |
| 33 } | |
| 34 | |
| 35 void HandleConfirm(const base::ListValue* args) override { | |
| 36 SyncConfirmationHandler::HandleConfirm(args); | |
| 37 } | |
| 38 | |
| 39 void HandleUndo(const base::ListValue* args) override { | |
| 40 SyncConfirmationHandler::HandleUndo(args); | |
| 41 } | |
| 42 | |
| 43 void HandleInitialized(const base::ListValue* args) override { | |
| 44 SyncConfirmationHandler::HandleInitialized(args); | |
| 45 } | |
| 46 | |
| 47 void HandleGoToSettings(const base::ListValue* args) override { | |
| 48 SyncConfirmationHandler::HandleGoToSettings(args); | |
| 49 } | |
| 50 | |
| 51 void SetUserImageURL(const std::string& url) override { | |
| 52 SyncConfirmationHandler::SetUserImageURL(url); | |
| 53 } | |
|
Dan Beam
2016/01/26 19:54:20
what are any of these doing? is this just to incr
anthonyvd
2016/01/26 22:00:39
Neat, didn't think about that. Thanks!
| |
| 54 }; | |
| 55 | |
| 56 class SyncConfirmationHandlerTest : public BrowserWithTestWindowTest { | |
|
Dan Beam
2016/01/26 19:54:20
this is actually a browser test (i.e. _unittest.cc
anthonyvd
2016/01/26 22:00:39
I think it's actually a BrowserTest. All subclasse
Dan Beam
2016/01/26 22:40:51
i assume you mean "actually a testing::Test" or "U
| |
| 57 public: | |
| 58 SyncConfirmationHandlerTest() : web_ui_(new content::TestWebUI) {} | |
|
Dan Beam
2016/01/26 19:54:20
why do you need to heap allocate |web_ui_|?
anthonyvd
2016/01/26 22:00:39
I need it to be destroyed in TearDown before Brows
| |
| 59 | |
| 60 void SetUp() override { | |
| 61 BrowserWithTestWindowTest::SetUp(); | |
| 62 chrome::NewTab(browser()); | |
| 63 web_ui_->set_web_contents( | |
| 64 browser()->tab_strip_model()->GetActiveWebContents()); | |
| 65 handler_.reset(new TestingSyncConfirmationHandler(web_ui_)); | |
| 66 sync_confirmation_ui_.reset( | |
| 67 new SyncConfirmationUI(web_ui_, handler_.get())); | |
| 68 | |
| 69 // This dialog assumes the signin flow was completed, which kicks off the | |
| 70 // SigninManager. | |
| 71 new OneClickSigninSyncStarter( | |
|
Dan Beam
2016/01/26 19:54:20
how is this deleted?
anthonyvd
2016/01/26 22:00:39
Instances of this class delete themselves when the
| |
| 72 profile(), | |
| 73 browser(), | |
| 74 "gaia", | |
| 75 "foo@example.com", | |
| 76 "password", | |
| 77 "refresh_token", | |
| 78 OneClickSigninSyncStarter::SYNC_WITH_DEFAULT_SETTINGS, | |
| 79 nullptr, | |
| 80 OneClickSigninSyncStarter::NO_CONFIRMATION, | |
| 81 GURL(), | |
| 82 GURL(), | |
| 83 OneClickSigninSyncStarter::Callback()); | |
| 84 } | |
| 85 | |
| 86 void TearDown() override { | |
| 87 handler_.reset(); | |
| 88 sync_confirmation_ui_.reset(); | |
| 89 signin_manager()->ForceSignOut(); | |
| 90 BrowserWithTestWindowTest::TearDown(); | |
| 91 } | |
| 92 | |
| 93 TestingSyncConfirmationHandler* handler() { | |
| 94 return handler_.get(); | |
| 95 } | |
| 96 | |
| 97 content::TestWebUI* web_ui() { | |
| 98 return web_ui_; | |
| 99 } | |
| 100 | |
| 101 FakeAccountFetcherService* account_fetcher_service() { | |
| 102 return static_cast<FakeAccountFetcherService*>( | |
| 103 AccountFetcherServiceFactory::GetForProfile(profile())); | |
| 104 } | |
| 105 | |
| 106 FakeSigninManager* signin_manager() { | |
| 107 return static_cast<FakeSigninManager*>( | |
| 108 SigninManagerFactory::GetForProfile(profile())); | |
| 109 } | |
| 110 | |
| 111 ProfileSyncService* sync() { | |
| 112 return ProfileSyncServiceFactory::GetForProfile(profile()); | |
| 113 } | |
| 114 | |
| 115 // BrowserWithTestWindowTest | |
| 116 BrowserWindow* CreateBrowserWindow() override { | |
| 117 return new DialogTestBrowserWindow; | |
|
Dan Beam
2016/01/26 19:54:20
who owns this?
anthonyvd
2016/01/26 22:00:39
The test class owns it and BrowserWithTestWindowTe
| |
| 118 } | |
| 119 | |
| 120 TestingProfile* CreateProfile() override { | |
| 121 TestingProfile::Builder builder; | |
| 122 builder.AddTestingFactory(AccountFetcherServiceFactory::GetInstance(), | |
| 123 FakeAccountFetcherServiceBuilder::BuildForTests); | |
| 124 builder.AddTestingFactory( | |
| 125 SigninManagerFactory::GetInstance(), BuildFakeSigninManagerBase); | |
| 126 return builder.Build().release(); | |
| 127 } | |
| 128 | |
| 129 private: | |
| 130 scoped_ptr<SyncConfirmationUI> sync_confirmation_ui_; | |
| 131 scoped_ptr<TestingSyncConfirmationHandler> handler_; | |
| 132 content::TestWebUI* web_ui_; | |
| 133 }; | |
| 134 | |
| 135 TEST_F(SyncConfirmationHandlerTest, TestSetImageIfPrimaryAccountReady) { | |
| 136 account_fetcher_service()->FakeUserInfoFetchSuccess( | |
| 137 "gaia", | |
| 138 "foo@example.com", | |
| 139 "gaia", | |
| 140 "", | |
| 141 "full_name", | |
| 142 "given_name", | |
| 143 "locale", | |
| 144 "http://picture.example.com/picture.jpg"); | |
| 145 | |
| 146 handler()->HandleInitialized(nullptr); | |
| 147 EXPECT_EQ(1UL, web_ui()->call_data().size()); | |
|
Dan Beam
2016/01/26 19:54:20
can you drop the L from all these if it's not nece
anthonyvd
2016/01/26 22:00:39
Done.
| |
| 148 EXPECT_EQ("sync.confirmation.setUserImageURL", | |
| 149 web_ui()->call_data()[0]->function_name()); | |
| 150 EXPECT_TRUE( | |
| 151 web_ui()->call_data()[0]->arg1()->IsType(base::Value::TYPE_STRING)); | |
| 152 std::string passed_picture_url; | |
| 153 EXPECT_TRUE( | |
| 154 web_ui()->call_data()[0]->arg1()->GetAsString(&passed_picture_url)); | |
| 155 | |
| 156 std::string original_picture_url = | |
| 157 AccountTrackerServiceFactory::GetForProfile(profile())-> | |
| 158 GetAccountInfo("gaia").picture_url; | |
| 159 GURL picture_url_with_size; | |
| 160 EXPECT_TRUE(profiles::GetImageURLWithThumbnailSize(GURL(original_picture_url), | |
| 161 kExpectedProfileImageSize, | |
| 162 &picture_url_with_size)); | |
| 163 EXPECT_EQ(picture_url_with_size.spec(), passed_picture_url); | |
| 164 handler()->HandleUndo(nullptr); | |
| 165 } | |
| 166 | |
| 167 TEST_F(SyncConfirmationHandlerTest, TestSetImageIfPrimaryAccountReadyLater) { | |
| 168 handler()->HandleInitialized(nullptr); | |
| 169 EXPECT_EQ(0UL, web_ui()->call_data().size()); | |
| 170 | |
| 171 account_fetcher_service()->FakeUserInfoFetchSuccess( | |
| 172 "gaia", | |
| 173 "foo@example.com", | |
| 174 "gaia", | |
| 175 "", | |
| 176 "full_name", | |
| 177 "given_name", | |
| 178 "locale", | |
| 179 "http://picture.example.com/picture.jpg"); | |
| 180 | |
| 181 EXPECT_EQ(1UL, web_ui()->call_data().size()); | |
| 182 EXPECT_EQ("sync.confirmation.setUserImageURL", | |
| 183 web_ui()->call_data()[0]->function_name()); | |
| 184 EXPECT_TRUE( | |
| 185 web_ui()->call_data()[0]->arg1()->IsType(base::Value::TYPE_STRING)); | |
| 186 std::string passed_picture_url; | |
| 187 EXPECT_TRUE( | |
| 188 web_ui()->call_data()[0]->arg1()->GetAsString(&passed_picture_url)); | |
| 189 | |
| 190 std::string original_picture_url = | |
| 191 AccountTrackerServiceFactory::GetForProfile(profile())-> | |
| 192 GetAccountInfo("gaia").picture_url; | |
| 193 GURL picture_url_with_size; | |
| 194 EXPECT_TRUE(profiles::GetImageURLWithThumbnailSize(GURL(original_picture_url), | |
| 195 kExpectedProfileImageSize, | |
| 196 &picture_url_with_size)); | |
| 197 EXPECT_EQ(picture_url_with_size.spec(), passed_picture_url); | |
| 198 handler()->HandleUndo(nullptr); | |
| 199 } | |
| 200 | |
| 201 TEST_F(SyncConfirmationHandlerTest, TestHandleUndo) { | |
| 202 EXPECT_FALSE(sync()->IsFirstSetupComplete()); | |
| 203 EXPECT_TRUE(sync()->IsFirstSetupInProgress()); | |
| 204 | |
| 205 handler()->HandleUndo(nullptr); | |
| 206 | |
| 207 EXPECT_FALSE(sync()->IsFirstSetupInProgress()); | |
| 208 EXPECT_FALSE(sync()->IsFirstSetupComplete()); | |
| 209 EXPECT_FALSE( | |
| 210 SigninManagerFactory::GetForProfile(profile())->IsAuthenticated()); | |
| 211 } | |
| 212 | |
| 213 TEST_F(SyncConfirmationHandlerTest, TestHandleConfirm) { | |
| 214 EXPECT_FALSE(sync()->IsFirstSetupComplete()); | |
| 215 EXPECT_TRUE(sync()->IsFirstSetupInProgress()); | |
| 216 | |
| 217 handler()->HandleConfirm(nullptr); | |
| 218 | |
| 219 EXPECT_FALSE(sync()->IsFirstSetupInProgress()); | |
| 220 EXPECT_TRUE(sync()->IsFirstSetupComplete()); | |
| 221 EXPECT_TRUE( | |
| 222 SigninManagerFactory::GetForProfile(profile())->IsAuthenticated()); | |
| 223 } | |
| OLD | NEW |