Commit 653f97ce authored by dpapad's avatar dpapad Committed by Commit Bot

Migrate CrOS chrome://set-time to Polymer3.

Bug: 965770
Test: No more HTML Import warnings in DevTools
Change-Id: I0abdba8172166dee7bd8f45121440d1bc01b7a0d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1863951Reviewed-by: default avatarJames Cook <jamescook@chromium.org>
Commit-Queue: Demetrios Papadopoulos <dpapad@chromium.org>
Cr-Commit-Position: refs/heads/master@{#706659}
parent 05dc8a43
...@@ -5349,6 +5349,7 @@ grit("resources") { ...@@ -5349,6 +5349,7 @@ grit("resources") {
if (is_chromeos) { if (is_chromeos) {
deps += [ deps += [
"//chrome/browser/resources/chromeos/crostini_installer:polymer3_elements", "//chrome/browser/resources/chromeos/crostini_installer:polymer3_elements",
"//chrome/browser/resources/chromeos/set_time_dialog:polymer3_elements",
"//chrome/browser/ui/webui/chromeos/add_supervision:mojo_bindings_js", "//chrome/browser/ui/webui/chromeos/add_supervision:mojo_bindings_js",
"//chrome/browser/ui/webui/chromeos/crostini_installer:mojo_bindings_js", "//chrome/browser/ui/webui/chromeos/crostini_installer:mojo_bindings_js",
"//chrome/browser/ui/webui/chromeos/machine_learning:mojo_bindings_js", "//chrome/browser/ui/webui/chromeos/machine_learning:mojo_bindings_js",
......
...@@ -514,9 +514,8 @@ ...@@ -514,9 +514,8 @@
<include name="IDR_DEVICE_EMULATOR_SHARED_STYLES_HTML" file="resources\chromeos\emulator\shared_styles.html" type="BINDATA" /> <include name="IDR_DEVICE_EMULATOR_SHARED_STYLES_HTML" file="resources\chromeos\emulator\shared_styles.html" type="BINDATA" />
</if> </if>
<if expr="chromeos"> <if expr="chromeos">
<include name="IDR_SET_TIME_DIALOG_HTML" file="resources\chromeos\set_time_dialog\set_time_dialog.html" type="BINDATA" compress="gzip" /> <include name="IDR_SET_TIME_HTML" file="resources\chromeos\set_time_dialog\set_time.html" type="BINDATA" compress="gzip" />
<include name="IDR_SET_TIME_DIALOG_JS" file="resources\chromeos\set_time_dialog\set_time_dialog.js" type="BINDATA" compress="gzip" /> <include name="IDR_SET_TIME_DIALOG_JS" file="${root_gen_dir}\chrome\browser\resources\chromeos\set_time_dialog\set_time_dialog.js" use_base_dir="false" type="BINDATA" compress="gzip" />
<include name="IDR_SET_TIME_BROWSER_PROXY_HTML" file="resources\chromeos\set_time_dialog\set_time_browser_proxy.html" type="BINDATA" compress="gzip" />
<include name="IDR_SET_TIME_BROWSER_PROXY_JS" file="resources\chromeos\set_time_dialog\set_time_browser_proxy.js" type="BINDATA" compress="gzip" /> <include name="IDR_SET_TIME_BROWSER_PROXY_JS" file="resources\chromeos\set_time_dialog\set_time_browser_proxy.js" type="BINDATA" compress="gzip" />
</if> </if>
<if expr="chromeos"> <if expr="chromeos">
......
...@@ -3,8 +3,10 @@ ...@@ -3,8 +3,10 @@
# found in the LICENSE file. # found in the LICENSE file.
import("//third_party/closure_compiler/compile_js.gni") import("//third_party/closure_compiler/compile_js.gni")
import("//tools/polymer/polymer.gni")
js_type_check("closure_compile") { js_type_check("closure_compile") {
is_polymer3 = true
deps = [ deps = [
":set_time_browser_proxy", ":set_time_browser_proxy",
":set_time_dialog", ":set_time_dialog",
...@@ -14,17 +16,29 @@ js_type_check("closure_compile") { ...@@ -14,17 +16,29 @@ js_type_check("closure_compile") {
js_library("set_time_dialog") { js_library("set_time_dialog") {
deps = [ deps = [
":set_time_browser_proxy", ":set_time_browser_proxy",
"//ui/webui/resources/cr_elements/cr_dialog:cr_dialog", "//ui/webui/resources/cr_elements/cr_dialog:cr_dialog.m",
"//ui/webui/resources/js:assert", "//ui/webui/resources/js:assert.m",
"//ui/webui/resources/js:cr", "//ui/webui/resources/js:cr.m",
"//ui/webui/resources/js:load_time_data", "//ui/webui/resources/js:load_time_data.m",
"//ui/webui/resources/js:web_ui_listener_behavior", "//ui/webui/resources/js:web_ui_listener_behavior.m",
] ]
} }
js_library("set_time_browser_proxy") { js_library("set_time_browser_proxy") {
deps = [ deps = [
"//ui/webui/resources/js:cr", "//ui/webui/resources/js:cr.m",
] ]
externs_list = [ "$externs_path/chrome_send.js" ] externs_list = [ "$externs_path/chrome_send.js" ]
} }
polymer_modulizer("set_time_dialog") {
js_file = "set_time_dialog.js"
html_file = "set_time_dialog.html"
html_type = "v3-ready"
}
group("polymer3_elements") {
deps = [
":set_time_dialog_module",
]
}
<!doctype html>
<html dir="$i18n{textdirection}" lang="$i18n{language}">
<head>
<meta charset="utf-8">
<title>$i18n{setTimeTitle}</title>
<link rel="stylesheet" href="chrome://resources/css/text_defaults_md.css">
</head>
<body>
<set-time-dialog></set-time-dialog>
<script type="module" src="set_time_dialog.js"></script>
</body>
</html>
<link rel="import" href="chrome://resources/html/cr.html">
<script src="set_time_browser_proxy.js"></script>
...@@ -3,61 +3,57 @@ ...@@ -3,61 +3,57 @@
// found in the LICENSE file. // found in the LICENSE file.
/** @fileoverview A helper object used by the "Set Time" dialog. */ /** @fileoverview A helper object used by the "Set Time" dialog. */
cr.define('settime', function() {
/** @interface */ import {addSingletonGetter} from 'chrome://resources/js/cr.m.js';
class SetTimeBrowserProxy {
/** Notifies C++ code that it's safe to call JS functions. */ /** @interface */
sendPageReady() {} export class SetTimeBrowserProxy {
/** Notifies C++ code that it's safe to call JS functions. */
/** @param {number} timeInSeconds */ sendPageReady() {}
setTimeInSeconds(timeInSeconds) {}
/** @param {number} timeInSeconds */
/** @param {string} timezone */ setTimeInSeconds(timeInSeconds) {}
setTimezone(timezone) {}
/** @param {string} timezone */
/** Closes the dialog. */ setTimezone(timezone) {}
dialogClose() {}
/** Closes the dialog. */
/** dialogClose() {}
* Notifies C++ code that done button was clicked.
* @param {number} timeInSeconds Seconds since epoch representing the date /**
* on the dialog inputs. * Notifies C++ code that done button was clicked.
*/ * @param {number} timeInSeconds Seconds since epoch representing the date
doneClicked(timeInSeconds) {} * on the dialog inputs.
*/
doneClicked(timeInSeconds) {}
}
/** @implements {SetTimeBrowserProxy} */
export class SetTimeBrowserProxyImpl {
/** @override */
sendPageReady() {
chrome.send('setTimePageReady');
}
/** @override */
setTimeInSeconds(timeInSeconds) {
chrome.send('setTimeInSeconds', [timeInSeconds]);
} }
/** @implements {settime.SetTimeBrowserProxy} */ /** @override */
class SetTimeBrowserProxyImpl { setTimezone(timezone) {
/** @override */ chrome.send('setTimezone', [timezone]);
sendPageReady() {
chrome.send('setTimePageReady');
}
/** @override */
setTimeInSeconds(timeInSeconds) {
chrome.send('setTimeInSeconds', [timeInSeconds]);
}
/** @override */
setTimezone(timezone) {
chrome.send('setTimezone', [timezone]);
}
/** @override */
dialogClose() {
chrome.send('dialogClose');
}
/** @override */
doneClicked(timeInSeconds) {
chrome.send('doneClicked', [timeInSeconds]);
}
} }
cr.addSingletonGetter(SetTimeBrowserProxyImpl); /** @override */
dialogClose() {
chrome.send('dialogClose');
}
/** @override */
doneClicked(timeInSeconds) {
chrome.send('doneClicked', [timeInSeconds]);
}
}
return { addSingletonGetter(SetTimeBrowserProxyImpl);
SetTimeBrowserProxy: SetTimeBrowserProxy,
SetTimeBrowserProxyImpl: SetTimeBrowserProxyImpl,
};
});
<!doctype html>
<html dir="$i18n{textdirection}" lang="$i18n{language}">
<head>
<meta charset="utf-8">
<title>$i18n{setTimeTitle}</title>
<link rel="import" href="chrome://resources/html/polymer.html">
<link rel="import" href="chrome://resources/cr_elements/cr_button/cr_button.html">
<link rel="import" href="chrome://resources/cr_elements/cr_dialog/cr_dialog.html">
<link rel="import" href="chrome://resources/cr_elements/shared_style_css.html">
<link rel="import" href="chrome://resources/html/assert.html">
<link rel="import" href="chrome://resources/html/load_time_data.html">
<link rel="import" href="chrome://resources/cr_elements/cr_page_host_style_css.html">
<link rel="import" href="chrome://resources/cr_elements/md_select_css.html">
<link rel="import" href="chrome://resources/html/web_ui_listener_behavior.html">
<link rel="import" href="set_time_browser_proxy.html">
<link rel="stylesheet" href="chrome://resources/css/text_defaults_md.css">
<script src="strings.js"></script>
</head>
<body>
<dom-module id="set-time-dialog">
<template>
<style include="cr-page-host-style cr-shared-style md-select"> <style include="cr-page-host-style cr-shared-style md-select">
:host { :host {
user-select: none; user-select: none;
...@@ -143,10 +116,3 @@ ...@@ -143,10 +116,3 @@
</div> </div>
</cr-dialog> </cr-dialog>
</template> </template>
<script src="set_time_dialog.js"></script>
</dom-module>
<set-time-dialog></set-time-dialog>
</body>
</html>
...@@ -12,8 +12,19 @@ ...@@ -12,8 +12,19 @@
* when the user changes the time or timezone. * when the user changes the time or timezone.
*/ */
(function() { import 'chrome://resources/cr_elements/cr_button/cr_button.m.js';
'use strict'; import 'chrome://resources/cr_elements/cr_dialog/cr_dialog.m.js';
import 'chrome://resources/cr_elements/cr_page_host_style_css.m.js';
import 'chrome://resources/cr_elements/md_select_css.m.js';
import 'chrome://resources/cr_elements/shared_style_css.m.js';
import './strings.m.js';
import {assert} from 'chrome://resources/js/assert.m.js';
import {loadTimeData} from 'chrome://resources/js/load_time_data.m.js';
import {WebUIListenerBehavior} from 'chrome://resources/js/web_ui_listener_behavior.m.js';
import {html, Polymer} from 'chrome://resources/polymer/v3_0/polymer/polymer_bundled.min.js';
import {SetTimeBrowserProxy, SetTimeBrowserProxyImpl} from './set_time_browser_proxy.js';
/** /**
* @return {!Array<!{id: string, name: string, selected: Boolean}>} Items for * @return {!Array<!{id: string, name: string, selected: Boolean}>} Items for
...@@ -103,6 +114,8 @@ function getTimezoneDelta(firstTimezoneId, secondsTimezoneId) { ...@@ -103,6 +114,8 @@ function getTimezoneDelta(firstTimezoneId, secondsTimezoneId) {
Polymer({ Polymer({
is: 'set-time-dialog', is: 'set-time-dialog',
_template: html`{__html_template__}`,
// Remove listeners on detach. // Remove listeners on detach.
behaviors: [WebUIListenerBehavior], behaviors: [WebUIListenerBehavior],
...@@ -171,12 +184,12 @@ Polymer({ ...@@ -171,12 +184,12 @@ Polymer({
*/ */
timeTimeoutId_: null, timeTimeoutId_: null,
/** @private {?settime.SetTimeBrowserProxy} */ /** @private {?SetTimeBrowserProxy} */
browserProxy_: null, browserProxy_: null,
/** @override */ /** @override */
created: function() { created: function() {
this.browserProxy_ = settime.SetTimeBrowserProxyImpl.getInstance(); this.browserProxy_ = SetTimeBrowserProxyImpl.getInstance();
}, },
/** @override */ /** @override */
...@@ -339,4 +352,3 @@ Polymer({ ...@@ -339,4 +352,3 @@ Polymer({
this.browserProxy_.dialogClose(); this.browserProxy_.dialogClose();
}, },
}); });
})();
...@@ -33,6 +33,7 @@ ...@@ -33,6 +33,7 @@
#include "content/public/browser/web_ui.h" #include "content/public/browser/web_ui.h"
#include "content/public/browser/web_ui_data_source.h" #include "content/public/browser/web_ui_data_source.h"
#include "content/public/browser/web_ui_message_handler.h" #include "content/public/browser/web_ui_message_handler.h"
#include "ui/resources/grit/webui_resources.h"
namespace chromeos { namespace chromeos {
...@@ -171,6 +172,8 @@ SetTimeUI::SetTimeUI(content::WebUI* web_ui) : WebDialogUI(web_ui) { ...@@ -171,6 +172,8 @@ SetTimeUI::SetTimeUI(content::WebUI* web_ui) : WebDialogUI(web_ui) {
// Set up the chrome://set-time source. // Set up the chrome://set-time source.
content::WebUIDataSource* source = content::WebUIDataSource* source =
content::WebUIDataSource::Create(chrome::kChromeUISetTimeHost); content::WebUIDataSource::Create(chrome::kChromeUISetTimeHost);
source->OverrideContentSecurityPolicyScriptSrc(
"script-src chrome://resources chrome://test 'self';");
static constexpr LocalizedString kStrings[] = { static constexpr LocalizedString kStrings[] = {
{"setTimeTitle", IDS_SET_TIME_TITLE}, {"setTimeTitle", IDS_SET_TIME_TITLE},
...@@ -196,13 +199,15 @@ SetTimeUI::SetTimeUI(content::WebUI* web_ui) : WebDialogUI(web_ui) { ...@@ -196,13 +199,15 @@ SetTimeUI::SetTimeUI(content::WebUI* web_ui) : WebDialogUI(web_ui) {
source->AddLocalizedStrings(values); source->AddLocalizedStrings(values);
source->UseStringsJs(); source->UseStringsJs();
source->EnableReplaceI18nInJS();
source->AddResourcePath("set_time_browser_proxy.html",
IDR_SET_TIME_BROWSER_PROXY_HTML);
source->AddResourcePath("set_time_browser_proxy.js", source->AddResourcePath("set_time_browser_proxy.js",
IDR_SET_TIME_BROWSER_PROXY_JS); IDR_SET_TIME_BROWSER_PROXY_JS);
source->AddResourcePath("set_time_dialog.js", IDR_SET_TIME_DIALOG_JS); source->AddResourcePath("set_time_dialog.js", IDR_SET_TIME_DIALOG_JS);
source->SetDefaultResource(IDR_SET_TIME_DIALOG_HTML); source->SetDefaultResource(IDR_SET_TIME_HTML);
source->AddResourcePath("test_loader.js", IDR_WEBUI_JS_TEST_LOADER);
source->AddResourcePath("test_loader.html", IDR_WEBUI_HTML_TEST_LOADER);
content::WebUIDataSource::Add(Profile::FromWebUI(web_ui), source); content::WebUIDataSource::Add(Profile::FromWebUI(web_ui), source);
} }
......
...@@ -3,244 +3,22 @@ ...@@ -3,244 +3,22 @@
// found in the LICENSE file. // found in the LICENSE file.
GEN_INCLUDE(['//chrome/test/data/webui/polymer_browser_test_base.js']); GEN_INCLUDE(['//chrome/test/data/webui/polymer_browser_test_base.js']);
GEN('#include "services/network/public/cpp/features.h"');
/**
* SetTimeDialogBrowserTest tests the "Set Time" web UI dialog. // SetTimeDialogBrowserTest tests the "Set Time" web UI dialog.
* @constructor // eslint-disable-next-line no-var
* @extends {PolymerTest} var SetTimeDialogBrowserTest = class extends PolymerTest {
*/ /** @override */
function SetTimeDialogBrowserTest() {} get browsePreload() {
return 'chrome://set-time/test_loader.html?module=set_time_dialog_test.js';
SetTimeDialogBrowserTest.prototype = { }
__proto__: PolymerTest.prototype,
/** @override */
browsePreload: 'chrome://set-time/', get featureList() {
return {enabled: ['network::features::kOutOfBlinkCors']};
extraLibraries: [ }
...PolymerTest.prototype.extraLibraries,
'//chrome/test/data/webui/test_browser_proxy.js',
],
}; };
TEST_F('SetTimeDialogBrowserTest', 'All', function() { TEST_F('SetTimeDialogBrowserTest', 'All', function() {
suite('SetTimeDialog', function() {
let setTimeElement = null;
let testBrowserProxy = null;
/** @implements {settime.SetTimeBrowserProxy} */
class TestSetTimeBrowserProxy extends TestBrowserProxy {
constructor() {
super([
'sendPageReady', 'setTimeInSeconds', 'setTimezone', 'dialogClose',
'doneClicked'
]);
}
/** @override */
sendPageReady() {
this.methodCalled('sendPageReady');
}
/** @override */
setTimeInSeconds(timeInSeconds) {
this.methodCalled('setTimeInSeconds', timeInSeconds);
}
/** @override */
setTimezone(timezone) {
this.methodCalled('setTimezone', timezone);
}
/** @override */
dialogClose() {
this.methodCalled('dialogClose');
}
/** @override */
doneClicked() {
this.methodCalled('doneClicked');
cr.webUIListenerCallback('validation-complete');
}
}
suiteSetup(function() {
// Must use existing timezones in the test.
loadTimeData.overrideValues({
currentTimezoneId: 'America/Sao_Paulo',
timezoneList: [
[
'America/Los_Angeles',
'(GMT-7:00) Pacific Daylight Time (Los Angeles)'
],
[
'America/Sao_Paulo', '(GMT-3:00) Brasilis Standard Time (Sao Paulo)'
],
['Asia/Seoul', '(GMT+9:00) Korean Standard Time (Seoul)'],
],
});
});
setup(function() {
testBrowserProxy = new TestSetTimeBrowserProxy();
settime.SetTimeBrowserProxyImpl.instance_ = testBrowserProxy;
PolymerTest.clearBody();
setTimeElement = document.createElement('set-time-dialog');
document.body.appendChild(setTimeElement);
Polymer.dom.flush();
});
teardown(function() {
setTimeElement.remove();
});
test('PageReady', () => {
// Verify the page sends the ready message.
assertEquals(1, testBrowserProxy.getCallCount('sendPageReady'));
});
test('DateRangeContainsNow', () => {
const dateInput = setTimeElement.$$('#dateInput');
// Input element attributes min and max are strings like '2019-03-01'.
const minDate = new Date(dateInput.min);
const maxDate = new Date(dateInput.max);
const now = new Date();
// Verify min <= now <= max.
assertLE(minDate, now);
assertLE(now, maxDate);
});
test('SetDate', () => {
const dateInput = setTimeElement.$$('#dateInput');
assertTrue(!!dateInput);
// Simulate the user changing the date picker forward by a week.
const today = dateInput.valueAsDate;
const nextWeek = new Date(today.getTime() + 7 * 24 * 60 * 60 * 1000);
dateInput.focus();
dateInput.valueAsDate = nextWeek;
setTimeElement.$$('#doneButton').click();
// Verify the page sends a request to move time forward.
return testBrowserProxy.whenCalled('setTimeInSeconds')
.then(timeInSeconds => {
const todaySeconds = today.getTime() / 1000;
// The exact value isn't important (it depends on the current time).
assertGT(timeInSeconds, todaySeconds);
});
});
test('Revert invalid date on blur', () => {
const dateInput = setTimeElement.$$('#dateInput');
dateInput.focus();
dateInput.value = '9999-99-99';
dateInput.blur();
// The exact value isn't important (it depends on the current date, and
// the date could change in the middle of the test).
assertNotEquals('9999-99-99', dateInput.value);
});
test('SystemTimezoneChanged', () => {
const timezoneSelect = setTimeElement.$$('#timezoneSelect');
assertTrue(!!timezoneSelect);
expectEquals('America/Sao_Paulo', timezoneSelect.value);
cr.webUIListenerCallback(
'system-timezone-changed', 'America/Los_Angeles');
expectEquals('America/Los_Angeles', timezoneSelect.value);
cr.webUIListenerCallback('system-timezone-changed', 'Asia/Seoul');
expectEquals('Asia/Seoul', timezoneSelect.value);
});
test('SetDateAndTimezone', () => {
const dateInput = setTimeElement.$$('#dateInput');
assertTrue(!!dateInput);
const timeInput = setTimeElement.$$('#timeInput');
assertTrue(!!timeInput);
const timezoneSelect = setTimeElement.$$('#timezoneSelect');
assertTrue(!!timezoneSelect);
expectEquals('America/Sao_Paulo', timezoneSelect.value);
// Simulate the user changing the time by forwarding it 15 minutes.
const originalTime = dateInput.valueAsDate;
originalTime.setMilliseconds(timeInput.valueAsNumber);
const updatedTime = new Date(originalTime.getTime() + 15 * 60 * 1000);
dateInput.focus();
dateInput.valueAsDate = updatedTime;
setTimeElement.$$('#doneButton').click();
// Simulate timezone change.
cr.webUIListenerCallback(
'system-timezone-changed', 'America/Los_Angeles');
expectEquals('America/Los_Angeles', timezoneSelect.value);
// Make sure that time on input field was updated.
const updatedTimeAndTimezone = dateInput.valueAsDate;
updatedTimeAndTimezone.setMilliseconds(timeInput.valueAsNumber);
// updatedTimeAndTimezone reflects the new timezone so it should be
// smaller, because it is more to the west than the original
// one, therefore even with the 15 minutes forwarded it should be smaller.
expectGT(updatedTime.getTime(), updatedTimeAndTimezone.getTime());
assertEquals(1, testBrowserProxy.getCallCount('setTimezone'));
return testBrowserProxy.whenCalled('setTimeInSeconds')
.then(timeInSeconds => {
const todaySeconds = originalTime.getTime() / 1000;
// The exact value isn't important (it depends on the current time).
// timeInSeconds should be bigger, because this timestamp is seconds
// since epoch and it does not hold any information regarding the
// current timezone.
assertGT(timeInSeconds, todaySeconds);
});
});
suite('NullTimezone', () => {
suiteSetup(() => {
loadTimeData.overrideValues({
currentTimezoneId: '',
timezoneList: [],
});
});
test('SetDateNullTimezone', () => {
const dateInput = setTimeElement.$$('#dateInput');
assertTrue(!!dateInput);
assertEquals(null, setTimeElement.$$('#timezoneSelect'));
// Simulates the user changing the date picker backward by two days. We
// are changing the date to make the test simpler. Changing the time
// would require timezone manipulation and handling corner cases over
// midnight. valuesAsDate return the time in UTC, therefore the amount
// of days here must be bigger than one to avoid situations where the
// new time and old time are in the same day.
const today = dateInput.valueAsDate;
const twoDaysAgo = new Date(today.getTime() - 2 * 24 * 60 * 60 * 1000);
dateInput.focus();
dateInput.valueAsDate = twoDaysAgo;
setTimeElement.$$('#doneButton').click();
assertEquals(0, testBrowserProxy.getCallCount('setTimezone'));
// Verify the page sends a request to move time backward.
return testBrowserProxy.whenCalled('setTimeInSeconds')
.then(newTimeSeconds => {
const todaySeconds = today.getTime() / 1000;
// Check that the current time is bigger than the new time, which
// is supposed to be two days ago. The exact value isn't
// important, checking it is difficult because it depends on the
// current time, which is constantly updated, therefore we only
// assert that one is bigger than the other.
assertGT(todaySeconds, newTimeSeconds);
});
});
});
});
mocha.run(); mocha.run();
}); });
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'chrome://set-time/set_time_dialog.js';
import {loadTimeData} from 'chrome://resources/js/load_time_data.m.js';
import {flush} from 'chrome://resources/polymer/v3_0/polymer/polymer_bundled.min.js';
import {SetTimeBrowserProxyImpl} from 'chrome://set-time/set_time_browser_proxy.js';
import {TestBrowserProxy} from './test_browser_proxy.m.js';
suite('SetTimeDialog', function() {
let setTimeElement = null;
let testBrowserProxy = null;
/** @implements {SetTimeBrowserProxy} */
class TestSetTimeBrowserProxy extends TestBrowserProxy {
constructor() {
super([
'sendPageReady', 'setTimeInSeconds', 'setTimezone', 'dialogClose',
'doneClicked'
]);
}
/** @override */
sendPageReady() {
this.methodCalled('sendPageReady');
}
/** @override */
setTimeInSeconds(timeInSeconds) {
this.methodCalled('setTimeInSeconds', timeInSeconds);
}
/** @override */
setTimezone(timezone) {
this.methodCalled('setTimezone', timezone);
}
/** @override */
dialogClose() {
this.methodCalled('dialogClose');
}
/** @override */
doneClicked() {
this.methodCalled('doneClicked');
cr.webUIListenerCallback('validation-complete');
}
}
suiteSetup(function() {
// Must use existing timezones in the test.
loadTimeData.overrideValues({
currentTimezoneId: 'America/Sao_Paulo',
timezoneList: [
[
'America/Los_Angeles',
'(GMT-7:00) Pacific Daylight Time (Los Angeles)'
],
['America/Sao_Paulo', '(GMT-3:00) Brasilis Standard Time (Sao Paulo)'],
['Asia/Seoul', '(GMT+9:00) Korean Standard Time (Seoul)'],
],
});
});
setup(function() {
testBrowserProxy = new TestSetTimeBrowserProxy();
SetTimeBrowserProxyImpl.instance_ = testBrowserProxy;
PolymerTest.clearBody();
setTimeElement = document.createElement('set-time-dialog');
document.body.appendChild(setTimeElement);
flush();
});
teardown(function() {
setTimeElement.remove();
});
test('PageReady', () => {
// Verify the page sends the ready message.
assertEquals(1, testBrowserProxy.getCallCount('sendPageReady'));
});
test('DateRangeContainsNow', () => {
const dateInput = setTimeElement.$$('#dateInput');
// Input element attributes min and max are strings like '2019-03-01'.
const minDate = new Date(dateInput.min);
const maxDate = new Date(dateInput.max);
const now = new Date();
// Verify min <= now <= max.
assertLE(minDate, now);
assertLE(now, maxDate);
});
test('SetDate', () => {
const dateInput = setTimeElement.$$('#dateInput');
assertTrue(!!dateInput);
// Simulate the user changing the date picker forward by a week.
const today = dateInput.valueAsDate;
const nextWeek = new Date(today.getTime() + 7 * 24 * 60 * 60 * 1000);
dateInput.focus();
dateInput.valueAsDate = nextWeek;
setTimeElement.$$('#doneButton').click();
// Verify the page sends a request to move time forward.
return testBrowserProxy.whenCalled('setTimeInSeconds')
.then(timeInSeconds => {
const todaySeconds = today.getTime() / 1000;
// The exact value isn't important (it depends on the current time).
assertGT(timeInSeconds, todaySeconds);
});
});
test('Revert invalid date on blur', () => {
const dateInput = setTimeElement.$$('#dateInput');
dateInput.focus();
dateInput.value = '9999-99-99';
dateInput.blur();
// The exact value isn't important (it depends on the current date, and
// the date could change in the middle of the test).
assertNotEquals('9999-99-99', dateInput.value);
});
test('SystemTimezoneChanged', () => {
const timezoneSelect = setTimeElement.$$('#timezoneSelect');
assertTrue(!!timezoneSelect);
expectEquals('America/Sao_Paulo', timezoneSelect.value);
cr.webUIListenerCallback('system-timezone-changed', 'America/Los_Angeles');
expectEquals('America/Los_Angeles', timezoneSelect.value);
cr.webUIListenerCallback('system-timezone-changed', 'Asia/Seoul');
expectEquals('Asia/Seoul', timezoneSelect.value);
});
test('SetDateAndTimezone', () => {
const dateInput = setTimeElement.$$('#dateInput');
assertTrue(!!dateInput);
const timeInput = setTimeElement.$$('#timeInput');
assertTrue(!!timeInput);
const timezoneSelect = setTimeElement.$$('#timezoneSelect');
assertTrue(!!timezoneSelect);
expectEquals('America/Sao_Paulo', timezoneSelect.value);
// Simulate the user changing the time by forwarding it 15 minutes.
const originalTime = dateInput.valueAsDate;
originalTime.setMilliseconds(timeInput.valueAsNumber);
const updatedTime = new Date(originalTime.getTime() + 15 * 60 * 1000);
dateInput.focus();
dateInput.valueAsDate = updatedTime;
setTimeElement.$$('#doneButton').click();
// Simulate timezone change.
cr.webUIListenerCallback('system-timezone-changed', 'America/Los_Angeles');
expectEquals('America/Los_Angeles', timezoneSelect.value);
// Make sure that time on input field was updated.
const updatedTimeAndTimezone = dateInput.valueAsDate;
updatedTimeAndTimezone.setMilliseconds(timeInput.valueAsNumber);
// updatedTimeAndTimezone reflects the new timezone so it should be
// smaller, because it is more to the west than the original
// one, therefore even with the 15 minutes forwarded it should be smaller.
expectGT(updatedTime.getTime(), updatedTimeAndTimezone.getTime());
assertEquals(1, testBrowserProxy.getCallCount('setTimezone'));
return testBrowserProxy.whenCalled('setTimeInSeconds')
.then(timeInSeconds => {
const todaySeconds = originalTime.getTime() / 1000;
// The exact value isn't important (it depends on the current time).
// timeInSeconds should be bigger, because this timestamp is seconds
// since epoch and it does not hold any information regarding the
// current timezone.
assertGT(timeInSeconds, todaySeconds);
});
});
suite('NullTimezone', () => {
suiteSetup(() => {
loadTimeData.overrideValues({
currentTimezoneId: '',
timezoneList: [],
});
});
test('SetDateNullTimezone', () => {
const dateInput = setTimeElement.$$('#dateInput');
assertTrue(!!dateInput);
assertEquals(null, setTimeElement.$$('#timezoneSelect'));
// Simulates the user changing the date picker backward by two days. We
// are changing the date to make the test simpler. Changing the time
// would require timezone manipulation and handling corner cases over
// midnight. valuesAsDate return the time in UTC, therefore the amount
// of days here must be bigger than one to avoid situations where the
// new time and old time are in the same day.
const today = dateInput.valueAsDate;
const twoDaysAgo = new Date(today.getTime() - 2 * 24 * 60 * 60 * 1000);
dateInput.focus();
dateInput.valueAsDate = twoDaysAgo;
setTimeElement.$$('#doneButton').click();
assertEquals(0, testBrowserProxy.getCallCount('setTimezone'));
// Verify the page sends a request to move time backward.
return testBrowserProxy.whenCalled('setTimeInSeconds')
.then(newTimeSeconds => {
const todaySeconds = today.getTime() / 1000;
// Check that the current time is bigger than the new time, which
// is supposed to be two days ago. The exact value isn't
// important, checking it is difficult because it depends on the
// current time, which is constantly updated, therefore we only
// assert that one is bigger than the other.
assertGT(todaySeconds, newTimeSeconds);
});
});
});
});
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