1 // Copyright (c) 2013 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. 4 5 'use strict'; 6 7 base.require('base.bbox2'); 8 base.require('base.quad'); 9 base.require('ui.quad_view'); 10 base.require('ui.quad_view_viewport'); 11 12 base.unittest.testSuite('ui.quad_view', function() { 13 var Quad = base.Quad; 14 var QuadView = ui.QuadView; 15 var QuadViewViewport = ui.QuadViewViewport; 16 17 test('instantiate', function() { 18 var quadView = new QuadView(); 19 var quads = [ 20 Quad.FromXYWH(0, 0, 10, 10), 21 Quad.FromXYWH(10, 10, 10, 10), 22 Quad.FromXYWH(20, 4, 10, 10), 23 Quad.FromXYWH(30, 10, 20, 20), 24 Quad.FromXYWH(20, 20, 10, 10), 25 Quad.FromXYWH(15, 15, 10, 10) 26 ]; 27 quads[2].upperBorderColor = 'rgba(255,255,0,1)'; 28 quads[3].backgroundColor = 'rgba(255,0,255,0.15)'; 29 quads[3].borderColor = 'rgba(0,255,255,1)'; 30 var quadsBBox = new base.BBox2(); 31 for (var i = 0; i < quads.length; i++) 32 quadsBBox.addQuad(quads[i]); 33 34 quadView.title = 'Test Tree'; 35 quadView.quads = quads; 36 quadView.viewport = new QuadViewViewport(quadsBBox.asRect(), 10.0); 37 quadView.deviceViewportSizeForFrame = {width: 50, height: 30}; 38 39 quadView.addEventListener( 40 'selectionChanged', 41 function(e) { 42 quads.forEach(function(q) { 43 q.upperBorderColor = undefined; 44 }); 45 46 e.selectedQuadIndices.forEach(function(i) { 47 quads[i].upperBorderColor = 'rgba(255,255,0,1)'; 48 }); 49 quadView.quads = quads; 50 }.bind(this)); 51 52 this.addHTMLOutput(quadView); 53 }); 54 55 test('instantiate_backgroundTexture', function() { 56 var quadView = new QuadView(); 57 var quads = [Quad.FromXYWH(0, 0, 10, 10)]; 58 var data = new Uint8Array(2 * 2 * 4); 59 data[0] = 0; 60 data[1] = 0; 61 data[2] = 0; 62 data[3] = 255; 63 64 data[4] = 255; 65 data[5] = 0; 66 data[6] = 0; 67 data[7] = 255; 68 69 data[8] = 0; 70 data[9] = 255; 71 data[10] = 0; 72 data[11] = 255; 73 74 data[12] = 0; 75 data[13] = 0; 76 data[14] = 255; 77 data[15] = 255; 78 79 var quadsBBox = new base.BBox2(); 80 for (var i = 0; i < quads.length; i++) 81 quadsBBox.addQuad(quads[i]); 82 83 quadView.title = 'Test Tree'; 84 quadView.quads = quads; 85 quadView.viewport = new QuadViewViewport(quadsBBox.asRect(), 50.0); 86 87 this.addHTMLOutput(quadView); 88 }); 89 90 test('instantiate_warpedTexturedQuad', function() { 91 var quadView = new QuadView(); 92 var quads = [base.Quad.From8Array([0, 0, 93 10, 0, 94 10, 5, 95 0, 10])]; 96 var data = new Uint8Array(2 * 2 * 4); 97 data[0] = 0; 98 data[1] = 0; 99 data[2] = 0; 100 data[3] = 255; 101 102 data[4] = 255; 103 data[5] = 0; 104 data[6] = 0; 105 data[7] = 255; 106 107 data[8] = 0; 108 data[9] = 255; 109 data[10] = 0; 110 data[11] = 255; 111 112 data[12] = 0; 113 data[13] = 0; 114 data[14] = 255; 115 data[15] = 255; 116 var quadsBBox = new base.BBox2(); 117 for (var i = 0; i < quads.length; i++) 118 quadsBBox.addQuad(quads[i]); 119 120 quadView.title = 'Test Tree'; 121 quadView.quads = quads; 122 quadView.viewport = new QuadViewViewport(quadsBBox.asRect(), 50.0); 123 124 this.addHTMLOutput(quadView); 125 }); 126 127 test('findTiles', function() { 128 var quadView = new QuadView(); 129 var quads = [ 130 Quad.FromXYWH(0, 0, 10, 10), 131 Quad.FromXYWH(10, 10, 10, 10), 132 Quad.FromXYWH(20, 4, 10, 10), 133 Quad.FromXYWH(30, 10, 20, 20), 134 Quad.FromXYWH(20, 20, 10, 10), 135 Quad.FromXYWH(15, 15, 10, 10) 136 ]; 137 138 var quadsBBox = new base.BBox2(); 139 for (var i = 0; i < quads.length; i++) 140 quadsBBox.addQuad(quads[i]); 141 142 quadView.title = 'Test Tree'; 143 quadView.quads = quads; 144 145 var deviceViewportSizeForFrame = {width: 50, height: 30}; 146 quadView.viewport = new QuadViewViewport( 147 quadsBBox.asRect(), 10.0, 0, 2); 148 149 var rect = quadView.canvas_.getBoundingClientRect(); 150 var hitIndices = quadView.findQuadsAtCanvasClientPoint( 151 rect.left + 75, rect.top + 75); 152 153 assertEquals(2, hitIndices.length); 154 assertArrayEquals(hitIndices, [1, 5]); 155 }); 156 }); 157