1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/devtools/server/tests/unit/test_breakpointstore.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,168 @@ 1.4 +/* -*- Mode: js; js-indent-level: 2; -*- */ 1.5 +/* Any copyright is dedicated to the Public Domain. 1.6 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.7 + 1.8 +// Test the functionality of the BreakpointStore object. 1.9 + 1.10 +function run_test() 1.11 +{ 1.12 + Cu.import("resource://gre/modules/jsdebugger.jsm"); 1.13 + addDebuggerToGlobal(this); 1.14 + let loader = Cc["@mozilla.org/moz/jssubscript-loader;1"] 1.15 + .getService(Components.interfaces.mozIJSSubScriptLoader); 1.16 + loader.loadSubScript("resource://gre/modules/devtools/server/actors/script.js"); 1.17 + 1.18 + test_has_breakpoint(); 1.19 + test_bug_754251(); 1.20 + test_add_breakpoint(); 1.21 + test_remove_breakpoint(); 1.22 + test_find_breakpoints(); 1.23 +} 1.24 + 1.25 +function test_has_breakpoint() { 1.26 + let bpStore = new BreakpointStore(); 1.27 + let location = { 1.28 + url: "http://example.com/foo.js", 1.29 + line: 3 1.30 + }; 1.31 + let columnLocation = { 1.32 + url: "http://example.com/bar.js", 1.33 + line: 5, 1.34 + column: 15 1.35 + }; 1.36 + 1.37 + // Shouldn't have breakpoint 1.38 + do_check_eq(null, bpStore.hasBreakpoint(location), 1.39 + "Breakpoint not added and shouldn't exist."); 1.40 + 1.41 + bpStore.addBreakpoint(location); 1.42 + do_check_true(!!bpStore.hasBreakpoint(location), 1.43 + "Breakpoint added but not found in Breakpoint Store."); 1.44 + 1.45 + bpStore.removeBreakpoint(location); 1.46 + do_check_eq(null, bpStore.hasBreakpoint(location), 1.47 + "Breakpoint removed but still exists."); 1.48 + 1.49 + // Same checks for breakpoint with a column 1.50 + do_check_eq(null, bpStore.hasBreakpoint(columnLocation), 1.51 + "Breakpoint with column not added and shouldn't exist."); 1.52 + 1.53 + bpStore.addBreakpoint(columnLocation); 1.54 + do_check_true(!!bpStore.hasBreakpoint(columnLocation), 1.55 + "Breakpoint with column added but not found in Breakpoint Store."); 1.56 + 1.57 + bpStore.removeBreakpoint(columnLocation); 1.58 + do_check_eq(null, bpStore.hasBreakpoint(columnLocation), 1.59 + "Breakpoint with column removed but still exists in Breakpoint Store."); 1.60 +} 1.61 + 1.62 +// Note: Removing this test will regress bug 754251. See comment above 1.63 +// ThreadActor.breakpointStore. 1.64 +function test_bug_754251() { 1.65 + let instance1 = new ThreadActor(); 1.66 + let instance2 = new ThreadActor(); 1.67 + do_check_true(instance1.breakpointStore instanceof BreakpointStore); 1.68 + do_check_eq(instance1.breakpointStore, ThreadActor.breakpointStore); 1.69 + do_check_eq(instance2.breakpointStore, ThreadActor.breakpointStore); 1.70 +} 1.71 + 1.72 +function test_add_breakpoint() { 1.73 + // Breakpoint with column 1.74 + let bpStore = new BreakpointStore(); 1.75 + let location = { 1.76 + url: "http://example.com/foo.js", 1.77 + line: 10, 1.78 + column: 9 1.79 + }; 1.80 + bpStore.addBreakpoint(location); 1.81 + do_check_true(!!bpStore.hasBreakpoint(location), 1.82 + "We should have the column breakpoint we just added"); 1.83 + 1.84 + // Breakpoint without column (whole line breakpoint) 1.85 + location = { 1.86 + url: "http://example.com/bar.js", 1.87 + line: 103 1.88 + }; 1.89 + bpStore.addBreakpoint(location); 1.90 + do_check_true(!!bpStore.hasBreakpoint(location), 1.91 + "We should have the whole line breakpoint we just added"); 1.92 +} 1.93 + 1.94 +function test_remove_breakpoint() { 1.95 + // Breakpoint with column 1.96 + let bpStore = new BreakpointStore(); 1.97 + let location = { 1.98 + url: "http://example.com/foo.js", 1.99 + line: 10, 1.100 + column: 9 1.101 + }; 1.102 + bpStore.addBreakpoint(location); 1.103 + bpStore.removeBreakpoint(location); 1.104 + do_check_eq(bpStore.hasBreakpoint(location), null, 1.105 + "We should not have the column breakpoint anymore"); 1.106 + 1.107 + // Breakpoint without column (whole line breakpoint) 1.108 + location = { 1.109 + url: "http://example.com/bar.js", 1.110 + line: 103 1.111 + }; 1.112 + bpStore.addBreakpoint(location); 1.113 + bpStore.removeBreakpoint(location); 1.114 + do_check_eq(bpStore.hasBreakpoint(location), null, 1.115 + "We should not have the whole line breakpoint anymore"); 1.116 +} 1.117 + 1.118 +function test_find_breakpoints() { 1.119 + let bps = [ 1.120 + { url: "foo.js", line: 10 }, 1.121 + { url: "foo.js", line: 10, column: 3 }, 1.122 + { url: "foo.js", line: 10, column: 10 }, 1.123 + { url: "foo.js", line: 23, column: 89 }, 1.124 + { url: "bar.js", line: 10, column: 1 }, 1.125 + { url: "bar.js", line: 20, column: 5 }, 1.126 + { url: "bar.js", line: 30, column: 34 }, 1.127 + { url: "bar.js", line: 40, column: 56 } 1.128 + ]; 1.129 + 1.130 + let bpStore = new BreakpointStore(); 1.131 + 1.132 + for (let bp of bps) { 1.133 + bpStore.addBreakpoint(bp); 1.134 + } 1.135 + 1.136 + // All breakpoints 1.137 + 1.138 + let bpSet = Set(bps); 1.139 + for (let bp of bpStore.findBreakpoints()) { 1.140 + bpSet.delete(bp); 1.141 + } 1.142 + do_check_eq(bpSet.size, 0, 1.143 + "Should be able to iterate over all breakpoints"); 1.144 + 1.145 + // Breakpoints by URL 1.146 + 1.147 + bpSet = Set(bps.filter(bp => { return bp.url === "foo.js" })); 1.148 + for (let bp of bpStore.findBreakpoints({ url: "foo.js" })) { 1.149 + bpSet.delete(bp); 1.150 + } 1.151 + do_check_eq(bpSet.size, 0, 1.152 + "Should be able to filter the iteration by url"); 1.153 + 1.154 + // Breakpoints by URL and line 1.155 + 1.156 + bpSet = Set(bps.filter(bp => { return bp.url === "foo.js" && bp.line === 10; })); 1.157 + let first = true; 1.158 + for (let bp of bpStore.findBreakpoints({ url: "foo.js", line: 10 })) { 1.159 + if (first) { 1.160 + do_check_eq(bp.column, undefined, 1.161 + "Should always get the whole line breakpoint first"); 1.162 + first = false; 1.163 + } else { 1.164 + do_check_neq(bp.column, undefined, 1.165 + "Should not get the whole line breakpoint any time other than first."); 1.166 + } 1.167 + bpSet.delete(bp); 1.168 + } 1.169 + do_check_eq(bpSet.size, 0, 1.170 + "Should be able to filter the iteration by url and line"); 1.171 +}