test_breakpoint-actor-map.js (6213B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 // Test the functionality of the BreakpointActorMap object. 7 8 const { 9 BreakpointActorMap, 10 } = require("resource://devtools/server/actors/utils/breakpoint-actor-map.js"); 11 12 function run_test() { 13 test_get_actor(); 14 test_set_actor(); 15 test_delete_actor(); 16 test_find_actors(); 17 test_duplicate_actors(); 18 } 19 20 function test_get_actor() { 21 const bpStore = new BreakpointActorMap(); 22 const location = { 23 generatedSourceActor: { actor: "actor1" }, 24 generatedLine: 3, 25 }; 26 const columnLocation = { 27 generatedSourceActor: { actor: "actor2" }, 28 generatedLine: 5, 29 generatedColumn: 15, 30 }; 31 32 // Shouldn't have breakpoint 33 Assert.equal( 34 null, 35 bpStore.getActor(location), 36 "Breakpoint not added and shouldn't exist." 37 ); 38 39 bpStore.setActor(location, {}); 40 Assert.ok( 41 !!bpStore.getActor(location), 42 "Breakpoint added but not found in Breakpoint Store." 43 ); 44 45 bpStore.deleteActor(location); 46 Assert.equal( 47 null, 48 bpStore.getActor(location), 49 "Breakpoint removed but still exists." 50 ); 51 52 // Same checks for breakpoint with a column 53 Assert.equal( 54 null, 55 bpStore.getActor(columnLocation), 56 "Breakpoint with column not added and shouldn't exist." 57 ); 58 59 bpStore.setActor(columnLocation, {}); 60 Assert.ok( 61 !!bpStore.getActor(columnLocation), 62 "Breakpoint with column added but not found in Breakpoint Store." 63 ); 64 65 bpStore.deleteActor(columnLocation); 66 Assert.equal( 67 null, 68 bpStore.getActor(columnLocation), 69 "Breakpoint with column removed but still exists in Breakpoint Store." 70 ); 71 } 72 73 function test_set_actor() { 74 // Breakpoint with column 75 const bpStore = new BreakpointActorMap(); 76 let location = { 77 generatedSourceActor: { actor: "actor1" }, 78 generatedLine: 10, 79 generatedColumn: 9, 80 }; 81 bpStore.setActor(location, {}); 82 Assert.ok( 83 !!bpStore.getActor(location), 84 "We should have the column breakpoint we just added" 85 ); 86 87 // Breakpoint without column (whole line breakpoint) 88 location = { 89 generatedSourceActor: { actor: "actor2" }, 90 generatedLine: 103, 91 }; 92 bpStore.setActor(location, {}); 93 Assert.ok( 94 !!bpStore.getActor(location), 95 "We should have the whole line breakpoint we just added" 96 ); 97 } 98 99 function test_delete_actor() { 100 // Breakpoint with column 101 const bpStore = new BreakpointActorMap(); 102 let location = { 103 generatedSourceActor: { actor: "actor1" }, 104 generatedLine: 10, 105 generatedColumn: 9, 106 }; 107 bpStore.setActor(location, {}); 108 bpStore.deleteActor(location); 109 Assert.equal( 110 bpStore.getActor(location), 111 null, 112 "We should not have the column breakpoint anymore" 113 ); 114 115 // Breakpoint without column (whole line breakpoint) 116 location = { 117 generatedSourceActor: { actor: "actor2" }, 118 generatedLine: 103, 119 }; 120 bpStore.setActor(location, {}); 121 bpStore.deleteActor(location); 122 Assert.equal( 123 bpStore.getActor(location), 124 null, 125 "We should not have the whole line breakpoint anymore" 126 ); 127 } 128 129 function test_find_actors() { 130 const bps = [ 131 { generatedSourceActor: { actor: "actor1" }, generatedLine: 10 }, 132 { 133 generatedSourceActor: { actor: "actor1" }, 134 generatedLine: 10, 135 generatedColumn: 3, 136 }, 137 { 138 generatedSourceActor: { actor: "actor1" }, 139 generatedLine: 10, 140 generatedColumn: 10, 141 }, 142 { 143 generatedSourceActor: { actor: "actor1" }, 144 generatedLine: 23, 145 generatedColumn: 89, 146 }, 147 { 148 generatedSourceActor: { actor: "actor2" }, 149 generatedLine: 10, 150 generatedColumn: 1, 151 }, 152 { 153 generatedSourceActor: { actor: "actor2" }, 154 generatedLine: 20, 155 generatedColumn: 5, 156 }, 157 { 158 generatedSourceActor: { actor: "actor2" }, 159 generatedLine: 30, 160 generatedColumn: 34, 161 }, 162 { 163 generatedSourceActor: { actor: "actor2" }, 164 generatedLine: 40, 165 generatedColumn: 56, 166 }, 167 ]; 168 169 const bpStore = new BreakpointActorMap(); 170 171 for (const bp of bps) { 172 bpStore.setActor(bp, bp); 173 } 174 175 // All breakpoints 176 177 let bpSet = new Set(bps); 178 for (const bp of bpStore.findActors()) { 179 bpSet.delete(bp); 180 } 181 Assert.equal(bpSet.size, 0, "Should be able to iterate over all breakpoints"); 182 183 // Breakpoints by URL 184 185 bpSet = new Set( 186 bps.filter(bp => { 187 return bp.generatedSourceActor.actorID === "actor1"; 188 }) 189 ); 190 for (const bp of bpStore.findActors({ 191 generatedSourceActor: { actorID: "actor1" }, 192 })) { 193 bpSet.delete(bp); 194 } 195 Assert.equal(bpSet.size, 0, "Should be able to filter the iteration by url"); 196 197 // Breakpoints by URL and line 198 199 bpSet = new Set( 200 bps.filter(bp => { 201 return ( 202 bp.generatedSourceActor.actorID === "actor1" && bp.generatedLine === 10 203 ); 204 }) 205 ); 206 let first = true; 207 for (const bp of bpStore.findActors({ 208 generatedSourceActor: { actorID: "actor1" }, 209 generatedLine: 10, 210 })) { 211 if (first) { 212 Assert.equal( 213 bp.generatedColumn, 214 undefined, 215 "Should always get the whole line breakpoint first" 216 ); 217 first = false; 218 } else { 219 Assert.notEqual( 220 bp.generatedColumn, 221 undefined, 222 "Should not get the whole line breakpoint any time other than first." 223 ); 224 } 225 bpSet.delete(bp); 226 } 227 Assert.equal( 228 bpSet.size, 229 0, 230 "Should be able to filter the iteration by url and line" 231 ); 232 } 233 234 function test_duplicate_actors() { 235 const bpStore = new BreakpointActorMap(); 236 237 // Breakpoint with column 238 let location = { 239 generatedSourceActor: { actorID: "foo-actor" }, 240 generatedLine: 10, 241 generatedColumn: 9, 242 }; 243 bpStore.setActor(location, {}); 244 bpStore.setActor(location, {}); 245 Assert.equal(bpStore.size, 1, "We should have only 1 column breakpoint"); 246 bpStore.deleteActor(location); 247 248 // Breakpoint without column (whole line breakpoint) 249 location = { 250 generatedSourceActor: { actorID: "foo-actor" }, 251 generatedLine: 15, 252 }; 253 bpStore.setActor(location, {}); 254 bpStore.setActor(location, {}); 255 Assert.equal(bpStore.size, 1, "We should have only 1 whole line breakpoint"); 256 bpStore.deleteActor(location); 257 }