michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: let tempScope = {}; michael@0: Components.utils.import("resource://gre/modules/Geometry.jsm", tempScope); michael@0: let Point = tempScope.Point; michael@0: let Rect = tempScope.Rect; michael@0: michael@0: function test() { michael@0: ok(Rect, "Rect class exists"); michael@0: for (var fname in tests) { michael@0: tests[fname](); michael@0: } michael@0: } michael@0: michael@0: let tests = { michael@0: testGetDimensions: function() { michael@0: let r = new Rect(5, 10, 100, 50); michael@0: ok(r.left == 5, "rect has correct left value"); michael@0: ok(r.top == 10, "rect has correct top value"); michael@0: ok(r.right == 105, "rect has correct right value"); michael@0: ok(r.bottom == 60, "rect has correct bottom value"); michael@0: ok(r.width == 100, "rect has correct width value"); michael@0: ok(r.height == 50, "rect has correct height value"); michael@0: ok(r.x == 5, "rect has correct x value"); michael@0: ok(r.y == 10, "rect has correct y value"); michael@0: }, michael@0: michael@0: testIsEmpty: function() { michael@0: let r = new Rect(0, 0, 0, 10); michael@0: ok(r.isEmpty(), "rect with nonpositive width is empty"); michael@0: let r = new Rect(0, 0, 10, 0); michael@0: ok(r.isEmpty(), "rect with nonpositive height is empty"); michael@0: let r = new Rect(0, 0, 10, 10); michael@0: ok(!r.isEmpty(), "rect with positive dimensions is not empty"); michael@0: }, michael@0: michael@0: testRestrictTo: function() { michael@0: let r1 = new Rect(10, 10, 100, 100); michael@0: let r2 = new Rect(50, 50, 100, 100); michael@0: r1.restrictTo(r2); michael@0: ok(r1.equals(new Rect(50, 50, 60, 60)), "intersection is non-empty"); michael@0: michael@0: let r1 = new Rect(10, 10, 100, 100); michael@0: let r2 = new Rect(120, 120, 100, 100); michael@0: r1.restrictTo(r2); michael@0: ok(r1.isEmpty(), "intersection is empty"); michael@0: michael@0: let r1 = new Rect(10, 10, 100, 100); michael@0: let r2 = new Rect(0, 0, 0, 0); michael@0: r1.restrictTo(r2); michael@0: ok(r1.isEmpty(), "intersection of rect and empty is empty"); michael@0: michael@0: let r1 = new Rect(0, 0, 0, 0); michael@0: let r2 = new Rect(0, 0, 0, 0); michael@0: r1.restrictTo(r2); michael@0: ok(r1.isEmpty(), "intersection of empty and empty is empty"); michael@0: }, michael@0: michael@0: testExpandToContain: function() { michael@0: let r1 = new Rect(10, 10, 100, 100); michael@0: let r2 = new Rect(50, 50, 100, 100); michael@0: r1.expandToContain(r2); michael@0: ok(r1.equals(new Rect(10, 10, 140, 140)), "correct expandToContain on intersecting rectangles"); michael@0: michael@0: let r1 = new Rect(10, 10, 100, 100); michael@0: let r2 = new Rect(120, 120, 100, 100); michael@0: r1.expandToContain(r2); michael@0: ok(r1.equals(new Rect(10, 10, 210, 210)), "correct expandToContain on non-intersecting rectangles"); michael@0: michael@0: let r1 = new Rect(10, 10, 100, 100); michael@0: let r2 = new Rect(0, 0, 0, 0); michael@0: r1.expandToContain(r2); michael@0: ok(r1.equals(new Rect(10, 10, 100, 100)), "expandToContain of rect and empty is rect"); michael@0: michael@0: let r1 = new Rect(10, 10, 0, 0); michael@0: let r2 = new Rect(0, 0, 0, 0); michael@0: r1.expandToContain(r2); michael@0: ok(r1.isEmpty(), "expandToContain of empty and empty is empty"); michael@0: }, michael@0: michael@0: testSubtract: function testSubtract() { michael@0: function equals(rects1, rects2) { michael@0: return rects1.length == rects2.length && rects1.every(function(r, i) { michael@0: return r.equals(rects2[i]); michael@0: }); michael@0: } michael@0: michael@0: let r1 = new Rect(0, 0, 100, 100); michael@0: let r2 = new Rect(500, 500, 100, 100); michael@0: ok(equals(r1.subtract(r2), [r1]), "subtract area outside of region yields same region"); michael@0: michael@0: let r1 = new Rect(0, 0, 100, 100); michael@0: let r2 = new Rect(-10, -10, 50, 120); michael@0: ok(equals(r1.subtract(r2), [new Rect(40, 0, 60, 100)]), "subtracting vertical bar from edge leaves one rect"); michael@0: michael@0: let r1 = new Rect(0, 0, 100, 100); michael@0: let r2 = new Rect(-10, -10, 120, 50); michael@0: ok(equals(r1.subtract(r2), [new Rect(0, 40, 100, 60)]), "subtracting horizontal bar from edge leaves one rect"); michael@0: michael@0: let r1 = new Rect(0, 0, 100, 100); michael@0: let r2 = new Rect(40, 40, 20, 20); michael@0: ok(equals(r1.subtract(r2), [ michael@0: new Rect(0, 0, 40, 100), michael@0: new Rect(40, 0, 20, 40), michael@0: new Rect(40, 60, 20, 40), michael@0: new Rect(60, 0, 40, 100)]), michael@0: "subtracting rect in middle leaves union of rects"); michael@0: }, michael@0: };