Commit a0f3c3d9 authored by Joon Ahn's avatar Joon Ahn Committed by Chromium LUCI CQ

nearby: use performance.now() over Date() object

Similar to using base::TimeTicks over base::Time, performance.now() only ticks forward and allows us to not worry about system time changes.
It also allows for easier mock in tests.

Bug: 1156229
Test: browser_tests --gtest_filter=*NearbySh*
Change-Id: Id1cf98d77b5f3557aaa95daf8a3114714ef97008
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2587613
Commit-Queue: Joon Ahn <joonbug@chromium.org>
Reviewed-by: default avatarJames Vecore <vecore@google.com>
Reviewed-by: default avatarKyle Horimoto <khorimoto@chromium.org>
Cr-Commit-Position: refs/heads/master@{#836769}
parent 8a11bb59
...@@ -23,8 +23,8 @@ Polymer({ ...@@ -23,8 +23,8 @@ Polymer({
}, },
/** /**
* Timestamp in milliseconds since unix epoch of when high visibility will * DOMHighResTimeStamp in milliseconds of when high visibility will be
* be turned off. * turned off.
* @type {number} * @type {number}
*/ */
shutoffTimestamp: { shutoffTimestamp: {
...@@ -64,8 +64,7 @@ Polymer({ ...@@ -64,8 +64,7 @@ Polymer({
/** @private */ /** @private */
calculateRemainingTime_() { calculateRemainingTime_() {
const now = new Date().getTime(); const now = performance.now();
const remainingTimeInMs = const remainingTimeInMs =
this.shutoffTimestamp > now ? this.shutoffTimestamp - now : 0; this.shutoffTimestamp > now ? this.shutoffTimestamp - now : 0;
this.remainingTimeInSeconds_ = Math.trunc(remainingTimeInMs / 1000); this.remainingTimeInSeconds_ = Math.trunc(remainingTimeInMs / 1000);
......
...@@ -130,7 +130,8 @@ Polymer({ ...@@ -130,7 +130,8 @@ Polymer({
* @param {boolean} inHighVisibility * @param {boolean} inHighVisibility
*/ */
onHighVisibilityChanged(inHighVisibility) { onHighVisibilityChanged(inHighVisibility) {
const now = new Date().getTime(); const now = performance.now();
if (inHighVisibility === false && if (inHighVisibility === false &&
now < this.highVisibilityShutoffTimestamp_) { now < this.highVisibilityShutoffTimestamp_) {
// TODO(crbug/1134745): Exiting high visibility can happen for multiple // TODO(crbug/1134745): Exiting high visibility can happen for multiple
...@@ -254,9 +255,9 @@ Polymer({ ...@@ -254,9 +255,9 @@ Polymer({
return; return;
} }
// Date().getTime() returns current time in milliseconds since unix epoch. // performance.now() returns DOMHighResTimeStamp in milliseconds.
this.highVisibilityShutoffTimestamp_ = this.highVisibilityShutoffTimestamp_ =
new Date().getTime() + (shutoffTimeoutInSeconds * 1000); performance.now() + (shutoffTimeoutInSeconds * 1000);
// Register a receive surface to enter high visibility and show the page. // Register a receive surface to enter high visibility and show the page.
this.receiveManager_.registerForegroundReceiveSurface(); this.receiveManager_.registerForegroundReceiveSurface();
......
...@@ -254,6 +254,12 @@ suite('NearbyShare', function() { ...@@ -254,6 +254,12 @@ suite('NearbyShare', function() {
}); });
test('show high visibility dialog', function() { test('show high visibility dialog', function() {
// Mock performance.now to return a constant 0 for testing.
const originalNow = performance.now;
performance.now = () => {
return 0;
};
const params = new URLSearchParams; const params = new URLSearchParams;
params.append('receive', '1'); params.append('receive', '1');
params.append('timeout', '600'); // 10 minutes params.append('timeout', '600'); // 10 minutes
...@@ -266,6 +272,9 @@ suite('NearbyShare', function() { ...@@ -266,6 +272,9 @@ suite('NearbyShare', function() {
const highVisibilityDialog = dialog.$$('nearby-share-high-visibility-page'); const highVisibilityDialog = dialog.$$('nearby-share-high-visibility-page');
assertTrue(test_util.isVisible(highVisibilityDialog)); assertTrue(test_util.isVisible(highVisibilityDialog));
assertGT(highVisibilityDialog.shutoffTimestamp, new Date().getTime()); assertEquals(highVisibilityDialog.shutoffTimestamp, 600 * 1000);
// Restore mock
performance.now = originalNow;
}); });
}); });
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