1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/devtools/webconsole/test/unit/test_js_property_provider.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,71 @@ 1.4 +/* -*- js2-basic-offset: 2; indent-tabs-mode: nil; -*- */ 1.5 +// Any copyright is dedicated to the Public Domain. 1.6 +// http://creativecommons.org/publicdomain/zero/1.0/ 1.7 + 1.8 +"use strict"; 1.9 +const { devtools } = Components.utils.import("resource://gre/modules/devtools/Loader.jsm", {}); 1.10 +let JSPropertyProvider = devtools.require("devtools/toolkit/webconsole/utils").JSPropertyProvider; 1.11 + 1.12 +Components.utils.import("resource://gre/modules/jsdebugger.jsm"); 1.13 +addDebuggerToGlobal(this); 1.14 + 1.15 +function run_test() { 1.16 + const testArray = 'var testArray = [\ 1.17 + {propA: "A"},\ 1.18 + {\ 1.19 + propB: "B", \ 1.20 + propC: [\ 1.21 + {propD: "D"}\ 1.22 + ]\ 1.23 + },\ 1.24 + [\ 1.25 + {propE: "E"}\ 1.26 + ]\ 1.27 + ];' 1.28 + 1.29 + const testObject = 'var testObject = {"propA": [{"propB": "B"}]}'; 1.30 + 1.31 + let sandbox = Components.utils.Sandbox("http://example.com"); 1.32 + let dbg = new Debugger; 1.33 + let dbgObject = dbg.addDebuggee(sandbox); 1.34 + Components.utils.evalInSandbox(testArray, sandbox); 1.35 + Components.utils.evalInSandbox(testObject, sandbox); 1.36 + 1.37 + let results = JSPropertyProvider(dbgObject, null, "testArray[0]."); 1.38 + do_print("Test that suggestions are given for 'foo[n]' where n is an integer."); 1.39 + test_has_result(results, "propA"); 1.40 + 1.41 + do_print("Test that suggestions are given for multidimensional arrays."); 1.42 + results = JSPropertyProvider(dbgObject, null, "testArray[2][0]."); 1.43 + test_has_result(results, "propE"); 1.44 + 1.45 + do_print("Test that suggestions are not given for index that's out of bounds."); 1.46 + results = JSPropertyProvider(dbgObject, null, "testArray[10]."); 1.47 + do_check_null(results); 1.48 + 1.49 + do_print("Test that no suggestions are given if an index is not numerical somewhere in the chain."); 1.50 + results = JSPropertyProvider(dbgObject, null, "testArray[0]['propC'][0]."); 1.51 + do_check_null(results); 1.52 + 1.53 + results = JSPropertyProvider(dbgObject, null, "testObject['propA'][0]."); 1.54 + do_check_null(results); 1.55 + 1.56 + results = JSPropertyProvider(dbgObject, null, "testArray[0]['propC']."); 1.57 + do_check_null(results); 1.58 + 1.59 + results = JSPropertyProvider(dbgObject, null, "testArray[][1]."); 1.60 + do_check_null(results); 1.61 +} 1.62 + 1.63 +/** 1.64 + * A helper that ensures (required) results were found. 1.65 + * @param Object aResults 1.66 + * The results returned by JSPropertyProvider. 1.67 + * @param String aRequiredSuggestion 1.68 + * A suggestion that must be found from the results. 1.69 + */ 1.70 +function test_has_result(aResults, aRequiredSuggestion) { 1.71 + do_check_neq(aResults, null); 1.72 + do_check_true(aResults.matches.length > 0); 1.73 + do_check_true(aResults.matches.indexOf(aRequiredSuggestion) !== -1); 1.74 +}