Commit febed4e2 authored by Alex Ilin's avatar Alex Ilin Committed by Commit Bot

[Signin] Live signin test for cancel sync with existing account

This CL adds a new LiveSigninTest::CancelSyncWithWebAccount to the suite
of live signin tests.

The test does the following:
- Signs in on the web
- Goes to the settings page and starts the enable Sync flow
- Cancels the sync confirmation dialog
- Checks that the account is still signed in but Sync is disabled

This CL adds a new method to cancel the sync confirmation dialog to
login_ui_test_utils.

The CL also fixes a bug in the javascript injected to the sync
confirmation dialog from
SigninViewControllerTestUtil::TryDismissSyncConfirmationDialog.
"document.querySelector('sync-confirmation-app')" may evaluate in null
that causes the subsequent attempt to access "shadowRoot" to throw an
exception. content::ExecuteScriptAndExtractString(), however, stays
blocked because it waits for a message to be sent through
"window.domAutomationController.send()". All of this makes the test
timeout.

Bug: 1008891
Change-Id: I6793e40d400ca62f97ea1d60b99b628aabefb369
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1944389Reviewed-by: default avatarDavid Roger <droger@chromium.org>
Reviewed-by: default avatarMihai Sardarescu <msarda@chromium.org>
Commit-Queue: Alex Ilin <alexilin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#720444}
parent 3129a652
......@@ -141,7 +141,7 @@
on-click="onConfirm_" consent-confirmation>
$i18n{syncConfirmationConfirmLabel}
</cr-button>
<cr-button on-click="onUndo_">
<cr-button id="cancelButton" on-click="onUndo_">
$i18n{syncConfirmationUndoLabel}
</cr-button>
<cr-button id="settingsButton" on-click="onGoToSettings_"
......
......@@ -823,7 +823,7 @@ IN_PROC_BROWSER_TEST_F(DiceBrowserTest, EnableSyncAfterToken) {
ntp_url_observer.Wait();
// Dismiss the Sync confirmation UI.
EXPECT_TRUE(login_ui_test_utils::DismissSyncConfirmationDialog(
EXPECT_TRUE(login_ui_test_utils::ConfirmSyncConfirmationDialog(
browser(), base::TimeDelta::FromSeconds(30)));
}
......@@ -877,7 +877,7 @@ IN_PROC_BROWSER_TEST_F(DiceBrowserTest, EnableSyncBeforeToken) {
ntp_url_observer.Wait();
// Dismiss the Sync confirmation UI.
EXPECT_TRUE(login_ui_test_utils::DismissSyncConfirmationDialog(
EXPECT_TRUE(login_ui_test_utils::ConfirmSyncConfirmationDialog(
browser(), base::TimeDelta::FromSeconds(30)));
}
......
......@@ -3,6 +3,7 @@
// found in the LICENSE file.
#include "base/run_loop.h"
#include "base/strings/stringprintf.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "chrome/browser/signin/e2e_tests/live_test.h"
......@@ -124,7 +125,7 @@ class LiveSignInTest : public signin::test::LiveTest {
base::RunLoop primary_account_set_loop;
observer.SetOnPrimaryAccountSetCallback(
primary_account_set_loop.QuitClosure());
login_ui_test_utils::DismissSyncConfirmationDialog(
login_ui_test_utils::ConfirmSyncConfirmationDialog(
browser(), base::TimeDelta::FromSeconds(3));
primary_account_set_loop.Run();
}
......@@ -307,5 +308,35 @@ IN_PROC_BROWSER_TEST_F(LiveSignInTest, MAYBE_TurnOffSync) {
EXPECT_FALSE(identity_manager()->HasPrimaryAccount());
}
// Signs in an account on the web. Goes to the Chrome settings to enable Sync
// but cancels the sync confirmation dialog. Checks that the account is still
// signed in on the web but Sync is disabled.
IN_PROC_BROWSER_TEST_F(LiveSignInTest, CancelSyncWithWebAccount) {
TestAccount test_account;
CHECK(GetTestAccountsUtil()->GetAccount("TEST_ACCOUNT_1", test_account));
SignInFromWeb(test_account);
GURL settings_url("chrome://settings");
AddTabAtIndex(0, settings_url, ui::PageTransition::PAGE_TRANSITION_TYPED);
auto* settings_tab = browser()->tab_strip_model()->GetActiveWebContents();
EXPECT_TRUE(content::ExecuteScript(
settings_tab,
base::StringPrintf("settings.SyncBrowserProxyImpl.getInstance()."
"startSyncingWithEmail(\"%s\", true)",
test_account.user.c_str())));
login_ui_test_utils::CancelSyncConfirmationDialog(
browser(), base::TimeDelta::FromSeconds(3));
const AccountsInCookieJarInfo& accounts_in_cookie_jar =
identity_manager()->GetAccountsInCookieJar();
EXPECT_TRUE(accounts_in_cookie_jar.accounts_are_fresh);
ASSERT_EQ(1u, accounts_in_cookie_jar.signed_in_accounts.size());
const gaia::ListedAccount& account =
accounts_in_cookie_jar.signed_in_accounts[0];
EXPECT_TRUE(gaia::AreEmailsSame(test_account.user, account.email));
EXPECT_TRUE(identity_manager()->HasAccountWithRefreshToken(account.id));
EXPECT_FALSE(identity_manager()->HasPrimaryAccount());
}
} // namespace test
} // namespace signin
......@@ -370,7 +370,7 @@ bool ProfileSyncServiceHarness::SetupSyncImpl(
FinishSyncSetup();
if ((signin_type_ == SigninType::UI_SIGNIN) &&
!login_ui_test_utils::DismissSyncConfirmationDialog(
!login_ui_test_utils::ConfirmSyncConfirmationDialog(
chrome::FindBrowserWithProfile(profile_),
base::TimeDelta::FromSeconds(30))) {
LOG(ERROR) << "Failed to dismiss sync confirmation dialog.";
......
......@@ -950,7 +950,7 @@ PROFILE_MENU_CLICK_TEST(kActionableItems_WithUnconsentedPrimaryAccount,
// The sync confirmation dialog was opened after clicking the signin button
// in the profile menu. It needs to be manually dismissed to not cause any
// crashes during shutdown.
EXPECT_TRUE(login_ui_test_utils::DismissSyncConfirmationDialog(
EXPECT_TRUE(login_ui_test_utils::ConfirmSyncConfirmationDialog(
browser(), base::TimeDelta::FromSeconds(30)));
}
}
......
......@@ -175,12 +175,28 @@ void WaitUntilAnyElementExistsInSigninFrame(
"Could not find elements in the signin frame");
}
enum class SyncConfirmationDialogAction { kConfirm, kCancel };
#if !defined(OS_CHROMEOS)
std::string GetButtonIdForSyncConfirmationDialogAction(
SyncConfirmationDialogAction action) {
switch (action) {
case SyncConfirmationDialogAction::kConfirm:
return "confirmButton";
case SyncConfirmationDialogAction::kCancel:
return "cancelButton";
}
}
#endif // !defined(OS_CHROMEOS)
} // namespace
namespace login_ui_test_utils {
class SigninViewControllerTestUtil {
public:
static bool TryDismissSyncConfirmationDialog(Browser* browser) {
static bool TryDismissSyncConfirmationDialog(
Browser* browser,
SyncConfirmationDialogAction action) {
#if defined(OS_CHROMEOS)
NOTREACHED();
return false;
......@@ -193,15 +209,18 @@ class SigninViewControllerTestUtil {
content::WebContents* dialog_web_contents =
signin_view_controller->GetModalDialogWebContentsForTesting();
DCHECK_NE(dialog_web_contents, nullptr);
std::string confirm_button_selector =
std::string button_id = GetButtonIdForSyncConfirmationDialogAction(action);
std::string button_selector =
"(document.querySelector('sync-confirmation-app') == null ? null :"
"document.querySelector('sync-confirmation-app').shadowRoot."
"querySelector('#confirmButton')";
"querySelector('#" +
button_id + "'))";
std::string message;
std::string find_button_js =
"if (document.readyState != 'complete') {"
" window.domAutomationController.send('DocumentNotReady');"
"} else if (" +
confirm_button_selector +
button_selector +
" == null) {"
" window.domAutomationController.send('NotFound');"
"} else {"
......@@ -215,7 +234,7 @@ class SigninViewControllerTestUtil {
// This cannot be a synchronous call, because it closes the window as a side
// effect, which may cause the javascript execution to never finish.
content::ExecuteScriptAsync(dialog_web_contents,
confirm_button_selector + ".click();");
button_selector + ".click();");
return true;
#endif
}
......@@ -319,7 +338,9 @@ bool SignInWithUI(Browser* browser,
#endif
}
bool DismissSyncConfirmationDialog(Browser* browser, base::TimeDelta timeout) {
bool DismissSyncConfirmationDialog(Browser* browser,
base::TimeDelta timeout,
SyncConfirmationDialogAction action) {
SyncConfirmationClosedObserver confirmation_closed_observer;
ScopedObserver<LoginUIService, LoginUIService::Observer>
scoped_confirmation_closed_observer(&confirmation_closed_observer);
......@@ -329,7 +350,7 @@ bool DismissSyncConfirmationDialog(Browser* browser, base::TimeDelta timeout) {
const base::Time expire_time = base::Time::Now() + timeout;
while (base::Time::Now() <= expire_time) {
if (SigninViewControllerTestUtil::TryDismissSyncConfirmationDialog(
browser)) {
browser, action)) {
confirmation_closed_observer.WaitForConfirmationClosed();
return true;
}
......@@ -338,4 +359,14 @@ bool DismissSyncConfirmationDialog(Browser* browser, base::TimeDelta timeout) {
return false;
}
bool ConfirmSyncConfirmationDialog(Browser* browser, base::TimeDelta timeout) {
return DismissSyncConfirmationDialog(browser, timeout,
SyncConfirmationDialogAction::kConfirm);
}
bool CancelSyncConfirmationDialog(Browser* browser, base::TimeDelta timeout) {
return DismissSyncConfirmationDialog(browser, timeout,
SyncConfirmationDialogAction::kCancel);
}
} // namespace login_ui_test_utils
......@@ -44,7 +44,12 @@ bool SignInWithUI(Browser* browser,
// Waits for sync confirmation dialog to get displayed, then executes javascript
// to click on confirm button. Returns false if dialog wasn't dismissed before
// |timeout|.
bool DismissSyncConfirmationDialog(Browser* browser, base::TimeDelta timeout);
bool ConfirmSyncConfirmationDialog(Browser* browser, base::TimeDelta timeout);
// Waits for sync confirmation dialog to get displayed, then executes javascript
// to click on cancel button. Returns false if dialog wasn't dismissed before
// |timeout|.
bool CancelSyncConfirmationDialog(Browser* browser, base::TimeDelta timeout);
} // namespace login_ui_test_utils
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment