2010-04-27 11:22:38 -07:00
|
|
|
// -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*-
|
|
|
|
/*
|
|
|
|
* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla Mobile Browser.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Mozilla Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2008
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Mark Finkle <mfinkle@mozilla.com>
|
|
|
|
* Matt Brubeck <mbrubeck@mozilla.com>
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
2010-10-04 17:30:43 -07:00
|
|
|
let testURL_blank = baseURI + "browser_blank_01.html";
|
|
|
|
|
2011-01-06 13:17:47 -08:00
|
|
|
const DEFAULT_WIDTH = 800;
|
|
|
|
|
2010-10-04 17:30:43 -07:00
|
|
|
function testURL(n) {
|
2011-03-15 01:40:17 -07:00
|
|
|
return baseURI + "browser_viewport.sjs" +
|
|
|
|
"?metadata=" + encodeURIComponent(gTestData[n].metadata || "") +
|
|
|
|
"&style=" + encodeURIComponent(gTestData[n].style || "") +
|
|
|
|
"&xhtml=" + encodeURIComponent(!!gTestData[n].xhtml);
|
2010-04-27 11:22:38 -07:00
|
|
|
}
|
2010-12-28 11:10:01 -08:00
|
|
|
|
2010-11-16 14:50:34 -08:00
|
|
|
function scaleRatio(n) {
|
|
|
|
if ("scaleRatio" in gTestData[n])
|
|
|
|
return gTestData[n].scaleRatio;
|
|
|
|
return 150; // Default value matches our main target hardware (N900, Nexus One, etc.)
|
|
|
|
}
|
2010-03-04 12:05:17 -08:00
|
|
|
|
2010-12-28 11:10:01 -08:00
|
|
|
let currentTab;
|
|
|
|
|
|
|
|
let loadURL = function loadURL(aPageURL, aCallback, aScale) {
|
2011-03-30 10:19:00 -07:00
|
|
|
messageManager.addMessageListener("pageshow", function(aMessage) {
|
2010-12-28 11:10:01 -08:00
|
|
|
if (aMessage.target.currentURI.spec == aPageURL) {
|
|
|
|
messageManager.removeMessageListener(aMessage.name, arguments.callee);
|
2010-04-27 11:22:38 -07:00
|
|
|
|
2010-12-28 11:10:01 -08:00
|
|
|
waitFor(aCallback, function() {
|
2011-03-15 01:40:17 -07:00
|
|
|
return !aScale || aScale == aMessage.target.scale;
|
2010-12-28 11:10:01 -08:00
|
|
|
});
|
2010-10-04 17:30:43 -07:00
|
|
|
}
|
|
|
|
});
|
2010-12-28 11:10:01 -08:00
|
|
|
|
|
|
|
BrowserUI.goToURI(aPageURL);
|
2010-10-04 17:30:43 -07:00
|
|
|
};
|
|
|
|
|
2011-03-15 01:40:17 -07:00
|
|
|
// XXX Tests do not yet run correctly in portrait.
|
|
|
|
window.resizeTo(800, 480);
|
|
|
|
|
2010-10-04 17:30:43 -07:00
|
|
|
let gTestData = [
|
2011-01-06 13:17:47 -08:00
|
|
|
{ metadata: "", width: DEFAULT_WIDTH, scale: 1 },
|
2010-10-04 17:30:43 -07:00
|
|
|
{ metadata: "width=device-width, initial-scale=1", width: 533.33, scale: 1.5 },
|
2010-11-16 14:50:34 -08:00
|
|
|
{ metadata: "width=device-width", width: 533.33, scale: 1.5 },
|
|
|
|
{ metadata: "width=device-width, initial-scale=1", scaleRatio: 100, width: 800, scale: 1 },
|
2010-10-04 17:30:43 -07:00
|
|
|
{ metadata: "width=320, initial-scale=1", width: 533.33, scale: 1.5 },
|
|
|
|
{ metadata: "initial-scale=1.0, user-scalable=no", width: 533.33, scale: 1.5, disableZoom: true },
|
2010-11-16 09:22:31 -08:00
|
|
|
{ metadata: "initial-scale=1.0, user-scalable=0", width: 533.33, scale: 1.5, disableZoom: true },
|
|
|
|
{ metadata: "initial-scale=1.0, user-scalable=false", width: 533.33, scale: 1.5, disableZoom: true },
|
|
|
|
{ metadata: "initial-scale=1.0, user-scalable=NO", width: 533.33, scale: 1.5, disableZoom: false }, // values are case-sensitive
|
2010-10-04 17:30:43 -07:00
|
|
|
{ metadata: "width=200,height=500", width: 200, scale: 4 },
|
|
|
|
{ metadata: "width=2000, minimum-scale=0.75", width: 2000, scale: 1.125, minScale: 1.125 },
|
|
|
|
{ metadata: "width=100, maximum-scale=2.0", width: 266.67, scale: 3, maxScale: 3 },
|
|
|
|
{ metadata: "width=2000, initial-scale=0.75", width: 2000, scale: 1.125 },
|
|
|
|
{ metadata: "width=20000, initial-scale=100", width: 10000, scale: 4 },
|
2011-03-15 01:40:17 -07:00
|
|
|
{ xhtml: true, width: 533.33, scale: 1.5, disableZoom: false },
|
2010-11-05 11:10:55 -07:00
|
|
|
/* testing spaces between arguments (bug 572696) */
|
|
|
|
{ metadata: "width= 2000, minimum-scale=0.75", width: 2000, scale: 1.125 },
|
|
|
|
{ metadata: "width = 2000, minimum-scale=0.75", width: 2000, scale: 1.125 },
|
|
|
|
{ metadata: "width = 2000 , minimum-scale=0.75", width: 2000, scale: 1.125 },
|
|
|
|
{ metadata: "width = 2000 , minimum-scale =0.75", width: 2000, scale: 1.125 },
|
|
|
|
{ metadata: "width = 2000 , minimum-scale = 0.75", width: 2000, scale: 1.125 },
|
2011-01-06 13:17:47 -08:00
|
|
|
{ metadata: "width = 2000 , minimum-scale = 0.75", width: 2000, scale: 1.125 },
|
|
|
|
/* testing opening and switching between pages without a viewport */
|
2011-03-15 01:40:17 -07:00
|
|
|
{ style: "width:400px;margin:0px;", width: DEFAULT_WIDTH, scale: 1 },
|
|
|
|
{ style: "width:2000px;margin:0px;", width: 980, scale: window.innerWidth/2000 },
|
|
|
|
{ style: "width:800px;margin:0px;", width: DEFAULT_WIDTH, scale: 1 },
|
2010-07-22 07:29:59 -07:00
|
|
|
];
|
2010-03-04 12:05:17 -08:00
|
|
|
|
2010-10-04 17:30:43 -07:00
|
|
|
|
2010-03-04 12:05:17 -08:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Entry point (must be named "test")
|
|
|
|
function test() {
|
|
|
|
// This test is async
|
|
|
|
waitForExplicitFinish();
|
2010-11-16 09:22:31 -08:00
|
|
|
requestLongerTimeout(2);
|
2010-03-04 12:05:17 -08:00
|
|
|
|
2010-12-28 11:10:01 -08:00
|
|
|
currentTab = Browser.addTab("about:blank", true);
|
|
|
|
ok(currentTab, "Tab Opened");
|
2010-07-22 07:29:59 -07:00
|
|
|
|
2010-10-04 17:30:43 -07:00
|
|
|
startTest(0);
|
2010-03-04 12:05:17 -08:00
|
|
|
}
|
|
|
|
|
2010-04-27 11:22:38 -07:00
|
|
|
function startTest(n) {
|
2011-03-15 01:40:17 -07:00
|
|
|
info(JSON.stringify(gTestData[n]));
|
2010-03-04 12:05:17 -08:00
|
|
|
BrowserUI.goToURI(testURL_blank);
|
2011-03-15 01:40:17 -07:00
|
|
|
loadURL(testURL_blank, verifyBlank(n));
|
2010-11-16 14:50:34 -08:00
|
|
|
Services.prefs.setIntPref("browser.viewport.scaleRatio", scaleRatio(n));
|
2010-03-04 12:05:17 -08:00
|
|
|
}
|
|
|
|
|
2010-04-27 11:22:38 -07:00
|
|
|
function verifyBlank(n) {
|
|
|
|
return function() {
|
|
|
|
// Do sanity tests
|
2010-12-28 11:10:01 -08:00
|
|
|
let uri = currentTab.browser.currentURI.spec;
|
|
|
|
is(uri, testURL_blank, "URL Matches blank page " + n);
|
2010-03-04 12:05:17 -08:00
|
|
|
|
2011-01-06 13:17:47 -08:00
|
|
|
waitFor(function() {
|
|
|
|
loadURL(testURL(n), verifyTest(n), gTestData[n].scale);
|
|
|
|
}, function() {
|
2011-03-15 01:40:17 -07:00
|
|
|
return currentTab.browser.contentWindowWidth == DEFAULT_WIDTH;
|
|
|
|
});
|
2010-04-27 11:22:38 -07:00
|
|
|
}
|
|
|
|
}
|
2010-03-04 12:05:17 -08:00
|
|
|
|
2010-05-05 12:51:55 -07:00
|
|
|
function is_approx(actual, expected, fuzz, description) {
|
2010-10-04 17:30:43 -07:00
|
|
|
ok(Math.abs(actual - expected) <= fuzz,
|
2010-05-05 12:51:55 -07:00
|
|
|
description + " [got " + actual + ", expected " + expected + "]");
|
|
|
|
}
|
|
|
|
|
2010-04-27 11:22:38 -07:00
|
|
|
function verifyTest(n) {
|
2011-03-15 07:30:56 -07:00
|
|
|
let assumedWidth = 480;
|
|
|
|
if (!Util.isPortrait())
|
|
|
|
assumedWidth = 800;
|
|
|
|
|
2010-04-27 11:22:38 -07:00
|
|
|
return function() {
|
2011-03-15 07:30:56 -07:00
|
|
|
is(window.innerWidth, assumedWidth, "Test assumes window width is " + assumedWidth + "px");
|
2010-03-04 12:05:17 -08:00
|
|
|
|
2010-04-27 11:22:38 -07:00
|
|
|
// Do sanity tests
|
2010-12-28 11:10:01 -08:00
|
|
|
let uri = currentTab.browser.currentURI.spec;
|
|
|
|
is(uri, testURL(n), "URL is " + testURL(n));
|
2010-03-04 12:05:17 -08:00
|
|
|
|
2010-10-04 17:30:43 -07:00
|
|
|
let data = gTestData[n];
|
2010-12-28 11:10:01 -08:00
|
|
|
let actualWidth = currentTab.browser.contentWindowWidth;
|
2010-07-22 07:29:59 -07:00
|
|
|
is_approx(actualWidth, parseFloat(data.width), .01, "Viewport width=" + data.width);
|
2010-03-04 12:05:17 -08:00
|
|
|
|
2010-09-16 12:29:03 -07:00
|
|
|
let zoomLevel = getBrowser().scale;
|
|
|
|
is_approx(zoomLevel, parseFloat(data.scale), .01, "Viewport scale=" + data.scale);
|
2010-04-29 12:29:01 -07:00
|
|
|
|
|
|
|
// Test zooming
|
|
|
|
if (data.disableZoom) {
|
2010-12-28 11:10:01 -08:00
|
|
|
ok(!currentTab.allowZoom, "Zoom disabled");
|
2010-04-29 12:29:01 -07:00
|
|
|
|
|
|
|
Browser.zoom(-1);
|
2010-09-16 12:29:03 -07:00
|
|
|
is(getBrowser().scale, zoomLevel, "Zoom in does nothing");
|
2010-04-29 12:29:01 -07:00
|
|
|
|
|
|
|
Browser.zoom(1);
|
2010-09-16 12:29:03 -07:00
|
|
|
is(getBrowser().scale, zoomLevel, "Zoom out does nothing");
|
2010-04-29 12:29:01 -07:00
|
|
|
}
|
|
|
|
else {
|
2010-09-16 12:29:03 -07:00
|
|
|
ok(Browser.selectedTab.allowZoom, "Zoom enabled");
|
2010-04-29 12:29:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (data.minScale) {
|
|
|
|
do { // Zoom out until we can't go any farther.
|
2010-09-16 12:29:03 -07:00
|
|
|
zoomLevel = getBrowser().scale;
|
2010-04-29 12:29:01 -07:00
|
|
|
Browser.zoom(1);
|
2010-09-16 12:29:03 -07:00
|
|
|
} while (getBrowser().scale != zoomLevel);
|
|
|
|
ok(getBrowser().scale >= data.minScale, "Zoom out limited");
|
2010-04-29 12:29:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (data.maxScale) {
|
|
|
|
do { // Zoom in until we can't go any farther.
|
2010-09-16 12:29:03 -07:00
|
|
|
zoomLevel = getBrowser().scale;
|
2010-04-29 12:29:01 -07:00
|
|
|
Browser.zoom(-1);
|
2010-09-16 12:29:03 -07:00
|
|
|
} while (getBrowser().scale != zoomLevel);
|
|
|
|
ok(getBrowser().scale <= data.maxScale, "Zoom in limited");
|
2010-04-29 12:29:01 -07:00
|
|
|
}
|
2010-03-04 12:05:17 -08:00
|
|
|
|
2010-04-27 11:22:38 -07:00
|
|
|
finishTest(n);
|
|
|
|
}
|
|
|
|
}
|
2010-03-04 12:05:17 -08:00
|
|
|
|
2010-04-27 11:22:38 -07:00
|
|
|
function finishTest(n) {
|
2010-11-16 14:50:34 -08:00
|
|
|
Services.prefs.clearUserPref("browser.viewport.scaleRatio");
|
2010-12-28 11:10:01 -08:00
|
|
|
if (n + 1 < gTestData.length) {
|
|
|
|
startTest(n + 1);
|
2010-04-27 11:22:38 -07:00
|
|
|
} else {
|
2011-03-15 01:40:17 -07:00
|
|
|
window.resizeTo(480, 800);
|
2010-12-28 11:10:01 -08:00
|
|
|
Browser.closeTab(currentTab);
|
2010-04-27 11:22:38 -07:00
|
|
|
finish();
|
|
|
|
}
|
2010-03-04 12:05:17 -08:00
|
|
|
}
|