1 // Copyright 2015 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.
5 // Utilities that are used in multiple tests.
6 function MockWindow(width
, height
, sizer
) {
7 this.innerWidth
= width
;
8 this.innerHeight
= height
;
9 this.addEventListener = function(e
, f
) {
11 this.scrollCallback
= f
;
13 this.resizeCallback
= f
;
15 this.setSize = function(width
, height
) {
16 this.innerWidth
= width
;
17 this.innerHeight
= height
;
18 this.resizeCallback();
20 this.scrollTo = function(x
, y
) {
22 x
= Math
.min(x
, parseInt(sizer
.style
.width
) - width
);
23 y
= Math
.min(y
, parseInt(sizer
.style
.height
) - height
);
25 this.pageXOffset
= Math
.max(0, x
);
26 this.pageYOffset
= Math
.max(0, y
);
27 this.scrollCallback();
29 this.setTimeout = function(callback
, time
) {
30 this.timerCallback
= callback
;
33 this.runTimeout = function() {
34 if (this.timerCallback
)
38 sizer
.resizeCallback_ = function() {
39 this.scrollTo(this.pageXOffset
, this.pageYOffset
);
44 this.scrollCallback
= null;
45 this.resizeCallback
= null;
46 this.timerCallback
= null;
49 function MockSizer() {
54 get height() { return this.height_
; },
56 this.height_
= height
;
57 if (sizer
.resizeCallback_
)
58 sizer
.resizeCallback_();
60 get width() { return this.width_
; },
63 if (sizer
.resizeCallback_
)
64 sizer
.resizeCallback_();
69 function MockViewportChangedCallback() {
70 this.wasCalled
= false;
71 this.callback = function() {
72 this.wasCalled
= true;
74 this.reset = function() {
75 this.wasCalled
= false;
79 function MockDocumentDimensions(width
, height
) {
80 this.width
= width
|| 0;
81 this.height
= height
? height
: 0;
82 this.pageDimensions
= [];
83 this.addPage = function(w
, h
) {
85 if (this.pageDimensions
.length
!= 0) {
86 y
= this.pageDimensions
[this.pageDimensions
.length
- 1].y
+
87 this.pageDimensions
[this.pageDimensions
.length
- 1].height
;
89 this.width
= Math
.max(this.width
, w
);
91 this.pageDimensions
.push({
98 this.reset = function() {
101 this.pageDimensions
= [];
105 function importHTML(src
) {
106 var link
= document
.createElement('link');
107 var promise
= new Promise(function(resolve
, reject
) {
108 link
.onload
= resolve
;
109 link
.onerror
= reject
;
113 document
.head
.appendChild(link
);
118 * Import iron-test-helpers into the test document.
120 * importTestHelpers().then(function() {
121 * chrome.test.runTests(...);
124 function importTestHelpers() {
125 return importHTML('chrome://resources/polymer/v1_0/iron-test-helpers/' +
126 'iron-test-helpers.html');