toolkit/modules/tests/browser/browser_Geometry.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 let tempScope = {};
michael@0 6 Components.utils.import("resource://gre/modules/Geometry.jsm", tempScope);
michael@0 7 let Point = tempScope.Point;
michael@0 8 let Rect = tempScope.Rect;
michael@0 9
michael@0 10 function test() {
michael@0 11 ok(Rect, "Rect class exists");
michael@0 12 for (var fname in tests) {
michael@0 13 tests[fname]();
michael@0 14 }
michael@0 15 }
michael@0 16
michael@0 17 let tests = {
michael@0 18 testGetDimensions: function() {
michael@0 19 let r = new Rect(5, 10, 100, 50);
michael@0 20 ok(r.left == 5, "rect has correct left value");
michael@0 21 ok(r.top == 10, "rect has correct top value");
michael@0 22 ok(r.right == 105, "rect has correct right value");
michael@0 23 ok(r.bottom == 60, "rect has correct bottom value");
michael@0 24 ok(r.width == 100, "rect has correct width value");
michael@0 25 ok(r.height == 50, "rect has correct height value");
michael@0 26 ok(r.x == 5, "rect has correct x value");
michael@0 27 ok(r.y == 10, "rect has correct y value");
michael@0 28 },
michael@0 29
michael@0 30 testIsEmpty: function() {
michael@0 31 let r = new Rect(0, 0, 0, 10);
michael@0 32 ok(r.isEmpty(), "rect with nonpositive width is empty");
michael@0 33 let r = new Rect(0, 0, 10, 0);
michael@0 34 ok(r.isEmpty(), "rect with nonpositive height is empty");
michael@0 35 let r = new Rect(0, 0, 10, 10);
michael@0 36 ok(!r.isEmpty(), "rect with positive dimensions is not empty");
michael@0 37 },
michael@0 38
michael@0 39 testRestrictTo: function() {
michael@0 40 let r1 = new Rect(10, 10, 100, 100);
michael@0 41 let r2 = new Rect(50, 50, 100, 100);
michael@0 42 r1.restrictTo(r2);
michael@0 43 ok(r1.equals(new Rect(50, 50, 60, 60)), "intersection is non-empty");
michael@0 44
michael@0 45 let r1 = new Rect(10, 10, 100, 100);
michael@0 46 let r2 = new Rect(120, 120, 100, 100);
michael@0 47 r1.restrictTo(r2);
michael@0 48 ok(r1.isEmpty(), "intersection is empty");
michael@0 49
michael@0 50 let r1 = new Rect(10, 10, 100, 100);
michael@0 51 let r2 = new Rect(0, 0, 0, 0);
michael@0 52 r1.restrictTo(r2);
michael@0 53 ok(r1.isEmpty(), "intersection of rect and empty is empty");
michael@0 54
michael@0 55 let r1 = new Rect(0, 0, 0, 0);
michael@0 56 let r2 = new Rect(0, 0, 0, 0);
michael@0 57 r1.restrictTo(r2);
michael@0 58 ok(r1.isEmpty(), "intersection of empty and empty is empty");
michael@0 59 },
michael@0 60
michael@0 61 testExpandToContain: function() {
michael@0 62 let r1 = new Rect(10, 10, 100, 100);
michael@0 63 let r2 = new Rect(50, 50, 100, 100);
michael@0 64 r1.expandToContain(r2);
michael@0 65 ok(r1.equals(new Rect(10, 10, 140, 140)), "correct expandToContain on intersecting rectangles");
michael@0 66
michael@0 67 let r1 = new Rect(10, 10, 100, 100);
michael@0 68 let r2 = new Rect(120, 120, 100, 100);
michael@0 69 r1.expandToContain(r2);
michael@0 70 ok(r1.equals(new Rect(10, 10, 210, 210)), "correct expandToContain on non-intersecting rectangles");
michael@0 71
michael@0 72 let r1 = new Rect(10, 10, 100, 100);
michael@0 73 let r2 = new Rect(0, 0, 0, 0);
michael@0 74 r1.expandToContain(r2);
michael@0 75 ok(r1.equals(new Rect(10, 10, 100, 100)), "expandToContain of rect and empty is rect");
michael@0 76
michael@0 77 let r1 = new Rect(10, 10, 0, 0);
michael@0 78 let r2 = new Rect(0, 0, 0, 0);
michael@0 79 r1.expandToContain(r2);
michael@0 80 ok(r1.isEmpty(), "expandToContain of empty and empty is empty");
michael@0 81 },
michael@0 82
michael@0 83 testSubtract: function testSubtract() {
michael@0 84 function equals(rects1, rects2) {
michael@0 85 return rects1.length == rects2.length && rects1.every(function(r, i) {
michael@0 86 return r.equals(rects2[i]);
michael@0 87 });
michael@0 88 }
michael@0 89
michael@0 90 let r1 = new Rect(0, 0, 100, 100);
michael@0 91 let r2 = new Rect(500, 500, 100, 100);
michael@0 92 ok(equals(r1.subtract(r2), [r1]), "subtract area outside of region yields same region");
michael@0 93
michael@0 94 let r1 = new Rect(0, 0, 100, 100);
michael@0 95 let r2 = new Rect(-10, -10, 50, 120);
michael@0 96 ok(equals(r1.subtract(r2), [new Rect(40, 0, 60, 100)]), "subtracting vertical bar from edge leaves one rect");
michael@0 97
michael@0 98 let r1 = new Rect(0, 0, 100, 100);
michael@0 99 let r2 = new Rect(-10, -10, 120, 50);
michael@0 100 ok(equals(r1.subtract(r2), [new Rect(0, 40, 100, 60)]), "subtracting horizontal bar from edge leaves one rect");
michael@0 101
michael@0 102 let r1 = new Rect(0, 0, 100, 100);
michael@0 103 let r2 = new Rect(40, 40, 20, 20);
michael@0 104 ok(equals(r1.subtract(r2), [
michael@0 105 new Rect(0, 0, 40, 100),
michael@0 106 new Rect(40, 0, 20, 40),
michael@0 107 new Rect(40, 60, 20, 40),
michael@0 108 new Rect(60, 0, 40, 100)]),
michael@0 109 "subtracting rect in middle leaves union of rects");
michael@0 110 },
michael@0 111 };

mercurial