Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run Resources tests via realWin #32056

Merged
merged 2 commits into from
Jan 20, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/service/resources-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ export class ResourcesImpl {
// When user scrolling stops, run pass to check newly in-viewport elements.
// When viewport is resized, we have to re-measure everything.
this.viewport_.onChanged((event) => {
this.lastScrollTime_ = Date.now();
this.lastScrollTime_ = this.win.Date.now();
this.lastVelocity_ = event.velocity;
if (event.relayoutAll) {
this.relayoutAll_ = true;
Expand All @@ -274,14 +274,14 @@ export class ResourcesImpl {
this.schedulePass();
});
this.viewport_.onScroll(() => {
this.lastScrollTime_ = Date.now();
this.lastScrollTime_ = this.win.Date.now();
});

// When document becomes visible, e.g. from "prerender" mode, do a
// simple pass.
this.ampdoc.onVisibilityChanged(() => {
if (this.firstVisibleTime_ == -1 && this.ampdoc.isVisible()) {
this.firstVisibleTime_ = Date.now();
this.firstVisibleTime_ = this.win.Date.now();
}
this.schedulePass();
});
Expand Down Expand Up @@ -858,7 +858,7 @@ export class ResourcesImpl {
// scroll adjustment to avoid active viewport changing without user's
// action. The elements in the active viewport are not resized and instead
// the overflow callbacks are called.
const now = Date.now();
const now = this.win.Date.now();
const viewportRect = this.viewport_.getRect();
const topOffset = viewportRect.height / 10;
const bottomOffset = viewportRect.height / 10;
Expand Down Expand Up @@ -1186,7 +1186,7 @@ export class ResourcesImpl {
discoverWork_() {
// TODO(dvoytenko): vsync separation may be needed for different phases

const now = Date.now();
const now = this.win.Date.now();

// Ensure all resources layout phase complete; when relayoutAll is requested
// force re-layout.
Expand Down Expand Up @@ -1460,7 +1460,7 @@ export class ResourcesImpl {
* @private
*/
work_() {
const now = Date.now();
const now = this.win.Date.now();

let timeout = -1;
let task = this.queue_.peek(this.boundTaskScorer_);
Expand Down Expand Up @@ -1613,7 +1613,7 @@ export class ResourcesImpl {
* @private
*/
calcTaskTimeout_(task) {
const now = Date.now();
const now = this.win.Date.now();

if (this.exec_.getSize() == 0) {
// If we've never been visible, return 0. This follows the previous
Expand Down Expand Up @@ -1787,7 +1787,7 @@ export class ResourcesImpl {
Math.max(resource.getLayoutPriority(), parentPriority) + priorityOffset,
forceOutsideViewport,
callback,
scheduleTime: Date.now(),
scheduleTime: this.win.Date.now(),
startTime: 0,
promise: null,
};
Expand Down
18 changes: 12 additions & 6 deletions test/unit/test-resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import * as lolex from 'lolex';
import {AmpDocSingle} from '../../src/service/ampdoc-impl';
import {LayoutPriority} from '../../src/layout';
import {Resource, ResourceState} from '../../src/service/resource';
Expand All @@ -25,12 +26,17 @@ import {layoutRectLtwh} from '../../src/layout-rect';
import {loadPromise} from '../../src/event-helper';

/*eslint "google-camelcase/google-camelcase": 0*/
describe('Resources', () => {
describes.realWin('Resources', {amp: true}, (env) => {
let window, document;
let clock;
let resources;

beforeEach(() => {
clock = window.sandbox.useFakeTimers();
window = env.win;
document = window.document;
delete window.requestIdleCallback;
delete window.cancelIdleCallback;
clock = lolex.install({target: window});
resources = new ResourcesImpl(new AmpDocSingle(window));
resources.isRuntimeOn_ = false;
});
Expand All @@ -41,7 +47,7 @@ describe('Resources', () => {

it('should calculate correct calcTaskScore', () => {
const viewportRect = layoutRectLtwh(0, 100, 300, 400);
window.sandbox.stub(resources.viewport_, 'getRect').returns(viewportRect);
env.sandbox.stub(resources.viewport_, 'getRect').returns(viewportRect);

// Task 1 is right in the middle of the viewport and priority 0
const task_in_viewport_p0 = {
Expand Down Expand Up @@ -272,7 +278,7 @@ describe('Resources', () => {
applySizesAndMediaQuery: () => {},
};
resources.visible_ = false;
window.sandbox
env.sandbox
.stub(resources.ampdoc, 'getVisibilityState')
.returns(VisibilityState.PRERENDER);
resources.scheduleLayoutOrPreload(resource, true);
Expand All @@ -295,7 +301,7 @@ describe('Resources', () => {
applySizesAndMediaQuery: () => {},
};
resources.visible_ = false;
window.sandbox
env.sandbox
.stub(resources.ampdoc, 'getVisibilityState')
.returns(VisibilityState.PRERENDER);
resources.scheduleLayoutOrPreload(resource, true);
Expand All @@ -318,7 +324,7 @@ describe('Resources', () => {
applySizesAndMediaQuery: () => {},
};
resources.visible_ = false;
window.sandbox
env.sandbox
.stub(resources.ampdoc, 'getVisibilityState')
.returns(VisibilityState.HIDDEN);
resources.scheduleLayoutOrPreload(resource, true);
Expand Down