list_selection_model_test.html revision 3345a6884c488ff3a535c2c9acdd33d74b37e311
1<!DOCTYPE html> 2<html> 3<head> 4<title></title> 5<style> 6 7</style> 8<script src="http://closure-library.googlecode.com/svn/trunk/closure/goog/base.js"></script> 9<script src="/cr.js"></script> 10<script src="/event_target.js"></script> 11<script src="list_selection_model.js"></script> 12<script> 13 14goog.require('goog.testing.jsunit'); 15 16</script> 17 18</head> 19<body> 20 21<script> 22 23function range(start, end) { 24 var a = []; 25 for (var i = start; i <= end; i++) { 26 a.push(i); 27 } 28 return a; 29} 30 31function createSelectionModel(len) { 32 return new cr.ui.ListSelectionModel(len); 33} 34 35function testAdjust1() { 36 var sm = createSelectionModel(200); 37 38 sm.leadIndex = sm.anchorIndex = sm.selectedIndex = 100; 39 sm.adjust(0, 10, 0); 40 41 assertEquals(90, sm.leadIndex); 42 assertEquals(90, sm.anchorIndex); 43 assertEquals(90, sm.selectedIndex); 44} 45 46function testAdjust2() { 47 var sm = createSelectionModel(200); 48 49 sm.leadIndex = sm.anchorIndex = sm.selectedIndex = 50; 50 sm.adjust(60, 10, 0); 51 52 assertEquals(50, sm.leadIndex); 53 assertEquals(50, sm.anchorIndex); 54 assertEquals(50, sm.selectedIndex); 55} 56 57function testAdjust3() { 58 var sm = createSelectionModel(200); 59 60 sm.leadIndex = sm.anchorIndex = sm.selectedIndex = 100; 61 sm.adjust(0, 0, 10); 62 63 assertEquals(110, sm.leadIndex); 64 assertEquals(110, sm.anchorIndex); 65 assertEquals(110, sm.selectedIndex); 66} 67 68function testAdjust4() { 69 var sm = createSelectionModel(200); 70 71 sm.leadIndex = sm.anchorIndex = 100; 72 sm.selectRange(100, 110); 73 74 sm.adjust(0, 10, 5); 75 76 assertEquals(95, sm.leadIndex); 77 assertEquals(95, sm.anchorIndex); 78 assertArrayEquals(range(95, 105), sm.selectedIndexes); 79} 80 81function testAdjust5() { 82 var sm = createSelectionModel(100); 83 84 sm.leadIndex = sm.anchorIndex = sm.selectedIndex = 99; 85 86 sm.adjust(99, 1, 0); 87 88 assertEquals('lead', 98, sm.leadIndex); 89 assertEquals('anchor', 98, sm.anchorIndex); 90 assertArrayEquals([], sm.selectedIndexes); 91} 92 93function testAdjust6() { 94 var sm = createSelectionModel(200); 95 96 sm.leadIndex = sm.anchorIndex = 105; 97 sm.selectRange(100, 110); 98 99 // Remove 100 - 105 100 sm.adjust(100, 5, 0); 101 102 assertEquals('lead', 100, sm.leadIndex); 103 assertEquals('anchor', 100, sm.anchorIndex); 104 assertArrayEquals(range(100, 105), sm.selectedIndexes); 105} 106 107function testAdjust7() { 108 var sm = createSelectionModel(1); 109 110 sm.leadIndex = sm.anchorIndex = sm.selectedIndex = 0; 111 112 sm.adjust(0, 0, 10); 113 114 assertEquals('lead', 10, sm.leadIndex); 115 assertEquals('anchor', 10, sm.anchorIndex); 116 assertArrayEquals([10], sm.selectedIndexes); 117} 118 119function testAdjust8() { 120 var sm = createSelectionModel(100); 121 122 sm.leadIndex = sm.anchorIndex = 50; 123 sm.selectAll(); 124 125 sm.adjust(10, 80, 0); 126 127 assertEquals('lead', 10, sm.leadIndex); 128 assertEquals('anchor', 10, sm.anchorIndex); 129 assertArrayEquals(range(0, 19), sm.selectedIndexes); 130} 131 132function testAdjust9() { 133 var sm = createSelectionModel(10); 134 135 sm.leadIndex = sm.anchorIndex = 5; 136 sm.selectAll(); 137 138 // Remove all 139 sm.adjust(0, 10, 0); 140 141 assertEquals('lead', -1, sm.leadIndex); 142 assertEquals('anchor', -1, sm.anchorIndex); 143 assertArrayEquals([], sm.selectedIndexes); 144} 145 146function testAdjust10() { 147 var sm = createSelectionModel(10); 148 149 sm.leadIndex = sm.anchorIndex = 5; 150 sm.selectAll(); 151 152 sm.adjust(0, 10, 20); 153 154 assertEquals('lead', 0, sm.leadIndex); 155 assertEquals('anchor', 0, sm.anchorIndex); 156 assertArrayEquals([], sm.selectedIndexes); 157} 158 159function testAdjust11() { 160 var sm = createSelectionModel(20); 161 162 sm.leadIndex = sm.anchorIndex = 10; 163 sm.selectAll(); 164 165 sm.adjust(5, 20, 10); 166 167 assertEquals('lead', 5, sm.leadIndex); 168 assertEquals('anchor', 5, sm.anchorIndex); 169 assertArrayEquals(range(0, 4), sm.selectedIndexes); 170} 171 172</script> 173 174</body> 175</html> 176