1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
14 * The Original Code is MozMill Test code.
16 * The Initial Developer of the Original Code is the Mozilla Foundation.
17 * Portions created by the Initial Developer are Copyright (C) 2010
18 * the Initial Developer. All Rights Reserved.
21 * Adrian Kalla <akalla@aviary.pl>
22 * Axel Hecht <axel@pike.org>
23 * Henrik Skupin <hskupin@mozilla.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 // Include the required modules
40 var utils = require("utils");
43 * This function creates a screenshot of the window provided in the given
44 * controller and highlights elements from the coordinates provided in the
47 * @param {array of array of int} boxes
48 * @param {MozmillController} controller
50 function create(controller, boxes) {
51 var doc = controller.window.document;
52 var maxWidth = doc.documentElement.boxObject.width;
53 var maxHeight = doc.documentElement.boxObject.height;
55 for (var i = 0, j = boxes.length; i < j; ++i) {
57 if (rect[0] + rect[2] > maxWidth) maxWidth = rect[0] + rect[2];
58 if (rect[1] + rect[3] > maxHeight) maxHeight = rect[1] + rect[3];
60 var canvas = doc.createElementNS("http://www.w3.org/1999/xhtml", "canvas");
61 var width = doc.documentElement.boxObject.width;
62 var height = doc.documentElement.boxObject.height;
63 canvas.width = maxWidth;
64 canvas.height = maxHeight;
65 var ctx = canvas.getContext("2d");
66 ctx.clearRect(0,0, canvas.width, canvas.height);
68 ctx.drawWindow(controller.window, 0, 0, width, height, "rgb(0,0,0)");
71 ctx.fillStyle = "rgba(255,0,0,0.4)";
72 for (var i = 0, j = boxes.length; i < j; ++i) {
74 ctx.fillRect(rect[0], rect[1], rect[2], rect[3]);
82 * Saves a given Canvas object to a file.
83 * The path to save the file under should be given on the command line. If not,
84 * it will be saved in the temporary folder of the system.
86 * @param {canvas} canvas
88 function _saveCanvas(canvas) {
89 // Use the path given on the command line and saved under
90 // persisted.screenshotPath, if available. If not, use the path to the
91 // temporary folder as a fallback.
93 if ("screenshotPath" in persisted) {
94 file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
95 file.initWithPath(persisted.screenshotPath);
98 file = Cc["@mozilla.org/file/directory_service;1"].
99 getService(Ci.nsIProperties).
100 get("TmpD", Ci.nsIFile);
103 var fileName = utils.appInfo.name + "-" +
104 utils.appInfo.locale + "." +
105 utils.appInfo.version + "." +
106 utils.appInfo.buildID + "." +
107 utils.appInfo.os + ".png";
108 file.append(fileName);
110 // if a file already exists, don't overwrite it and create a new name
111 file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, parseInt("0666", 8));
113 // create a data url from the canvas and then create URIs of the source
115 var io = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
116 var source = io.newURI(canvas.toDataURL("image/png", ""), "UTF8", null);
117 var target = io.newFileURI(file)
119 // prepare to save the canvas data
120 var wbPersist = Cc["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"].
121 createInstance(Ci.nsIWebBrowserPersist);
123 wbPersist.persistFlags = Ci.nsIWebBrowserPersist.PERSIST_FLAGS_REPLACE_EXISTING_FILES;
124 wbPersist.persistFlags |= Ci.nsIWebBrowserPersist.PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION;
126 // save the canvas data to the file
127 wbPersist.saveURI(source, null, null, null, null, file);
130 // Export of functions
131 exports.create = create;