Bug 679467 - scratchpad doesn't display the value false; r=rcampbell
authorRob Campbell <rcampbell@mozilla.com>
Thu, 03 Nov 2011 12:39:32 -0300
changeset 81047 2970de97ef2ef52401396e8bb8952907dae77576
parent 81046 5f76a64650406657644d86540b4560aafa2f5b9d
child 81049 f603632321e4e876a48ce0232d2869ae9ff85fce
push idunknown
push userunknown
push dateunknown
reviewersrcampbell
bugs679467
milestone10.0a1
Bug 679467 - scratchpad doesn't display the value false; r=rcampbell
browser/devtools/scratchpad/scratchpad.js
browser/devtools/scratchpad/test/Makefile.in
browser/devtools/scratchpad/test/browser_scratchpad_bug_679467_falsy.js
browser/devtools/scratchpad/test/browser_scratchpad_contexts.js
browser/devtools/scratchpad/test/browser_scratchpad_execute_print.js
browser/devtools/scratchpad/test/browser_scratchpad_tab_switch.js
--- a/browser/devtools/scratchpad/scratchpad.js
+++ b/browser/devtools/scratchpad/scratchpad.js
@@ -308,17 +308,17 @@ var Scratchpad = {
    *
    * @param string aString
    *        The script you want evaluated.
    * @return mixed
    *         The script evaluation result.
    */
   evalInContentSandbox: function SP_evalInContentSandbox(aString)
   {
-    let result;
+    let error, result;
     try {
       result = Cu.evalInSandbox(aString, this.contentSandbox, "1.8",
                                 "Scratchpad", 1);
     }
     catch (ex) {
       this.openWebConsole();
 
       let contentWindow = this.gBrowser.selectedBrowser.contentWindow;
@@ -327,43 +327,47 @@ var Scratchpad = {
                         createInstance(Ci.nsIScriptError2);
 
       scriptError.initWithWindowID(ex.message + "\n" + ex.stack, ex.fileName,
                                    "", ex.lineNumber, 0, scriptError.errorFlag,
                                    "content javascript",
                                    this.getInnerWindowId(contentWindow));
 
       Services.console.logMessage(scriptError);
+
+      error = true;
     }
 
-    return result;
+    return [error, result];
   },
 
   /**
    * Evaluate a string in the most recent navigator:browser chrome window.
    *
    * @param string aString
    *        The script you want evaluated.
    * @return mixed
    *         The script evaluation result.
    */
   evalInChromeSandbox: function SP_evalInChromeSandbox(aString)
   {
-    let result;
+    let error, result;
     try {
       result = Cu.evalInSandbox(aString, this.chromeSandbox, "1.8",
                                 "Scratchpad", 1);
     }
     catch (ex) {
       Cu.reportError(ex);
       Cu.reportError(ex.stack);
       this.openErrorConsole();
+
+      error = true;
     }
 
-    return result;
+    return [error, result];
   },
 
   /**
    * Evaluate a string in the currently desired context, that is either the
    * chrome window or the tab content window object.
    *
    * @param string aString
    *        The script you want to evaluate.
@@ -379,31 +383,31 @@ var Scratchpad = {
 
   /**
    * Execute the selected text (if any) or the entire editor content in the
    * current context.
    */
   run: function SP_run()
   {
     let selection = this.selectedText || this.getText();
-    let result = this.evalForContext(selection);
+    let [error, result] = this.evalForContext(selection);
     this.deselect();
-    return [selection, result];
+    return [selection, error, result];
   },
 
   /**
    * Execute the selected text (if any) or the entire editor content in the
    * current context. The resulting object is opened up in the Property Panel
    * for inspection.
    */
   inspect: function SP_inspect()
   {
-    let [selection, result] = this.run();
+    let [selection, error, result] = this.run();
 
-    if (result) {
+    if (!error) {
       this.openPropertyPanel(selection, result);
     }
   },
 
   /**
    * Execute the selected text (if any) or the entire editor content in the
    * current context. The evaluation result is inserted into the editor after
    * the selected text, or at the end of the editor content if there is no
@@ -411,22 +415,22 @@ var Scratchpad = {
    */
   display: function SP_display()
   {
     let selection = this.getSelectionRange();
     let insertionPoint = selection.start != selection.end ?
                          selection.end : // after selected text
                          this.editor.getCharCount(); // after text end
 
-    let [selectedText, result] = this.run();
-    if (!result) {
+    let [selectedText, error, result] = this.run();
+    if (error) {
       return;
     }
 
-    let newComment = "/*\n" + result.toString() + "\n*/";
+    let newComment = "/*\n" + result + "\n*/";
 
     this.setText(newComment, insertionPoint, insertionPoint);
 
     // Select the new comment.
     this.selectRange(insertionPoint, insertionPoint + newComment.length);
   },
 
   /**
@@ -454,24 +458,21 @@ var Scratchpad = {
     // the content of the panel.
     if (aEvalString !== null) {
       buttons.push({
         label: this.strings.
                GetStringFromName("propertyPanel.updateButton.label"),
         accesskey: this.strings.
                    GetStringFromName("propertyPanel.updateButton.accesskey"),
         oncommand: function () {
-          try {
-            let result = self.evalForContext(aEvalString);
+          let [error, result] = self.evalForContext(aEvalString);
 
-            if (result !== undefined) {
-              propPanel.treeView.data = result;
-            }
+          if (!error) {
+            propPanel.treeView.data = result;
           }
-          catch (ex) { }
         }
       });
     }
 
     let doc = this.browserWindow.document;
     let parent = doc.getElementById("mainPopupSet");
     let title = aOutputObject.toString();
     propPanel = new PropertyPanel(parent, doc, title, aOutputObject, buttons);
--- a/browser/devtools/scratchpad/test/Makefile.in
+++ b/browser/devtools/scratchpad/test/Makefile.in
@@ -50,11 +50,12 @@ include $(topsrcdir)/config/rules.mk
 		browser_scratchpad_execute_print.js \
 		browser_scratchpad_inspect.js \
 		browser_scratchpad_files.js \
 		browser_scratchpad_ui.js \
 		browser_scratchpad_bug_646070_chrome_context_pref.js \
 		browser_scratchpad_bug_660560_tab.js \
 		browser_scratchpad_open.js \
 		browser_scratchpad_restore.js \
+		browser_scratchpad_bug_679467_falsy.js \
 
 libs:: $(_BROWSER_TEST_FILES)
 	$(INSTALL) $(foreach f,$^,"$f") $(DEPTH)/_tests/testing/mochitest/browser/$(relativesrcdir)
new file mode 100644
--- /dev/null
+++ b/browser/devtools/scratchpad/test/browser_scratchpad_bug_679467_falsy.js
@@ -0,0 +1,64 @@
+/* vim: set ts=2 et sw=2 tw=80: */
+/* Any copyright is dedicated to the Public Domain.
+   http://creativecommons.org/publicdomain/zero/1.0/ */
+
+// Reference to the Scratchpad chrome window object.
+let gScratchpadWindow;
+
+function test()
+{
+  waitForExplicitFinish();
+
+  gBrowser.selectedTab = gBrowser.addTab();
+  gBrowser.selectedBrowser.addEventListener("load", function() {
+    gBrowser.selectedBrowser.removeEventListener("load", arguments.callee, true);
+
+    gScratchpadWindow = Scratchpad.openScratchpad();
+    gScratchpadWindow.addEventListener("load", testFalsy, false);
+  }, true);
+
+  content.location = "data:text/html,<p>test falsy display() values in Scratchpad";
+}
+
+function testFalsy(sp)
+{
+  gScratchpadWindow.removeEventListener("load", testFalsy, false);
+
+  let sp = gScratchpadWindow.Scratchpad;
+  verifyFalsies(sp);
+  
+  sp.setBrowserContext();
+  verifyFalsies(sp);
+
+  gScratchpadWindow.close();
+  gScratchpadWindow = null;
+  gBrowser.removeCurrentTab();
+  finish();
+}
+
+function verifyFalsies(sp)
+{
+  sp.setText("undefined");
+  sp.display();
+  is(sp.selectedText, "/*\nundefined\n*/", "'undefined' is displayed");
+
+  sp.setText("false");
+  sp.display();
+  is(sp.selectedText, "/*\nfalse\n*/", "'false' is displayed");
+
+  sp.setText("0");
+  sp.display();
+  is(sp.selectedText, "/*\n0\n*/", "'0' is displayed");
+
+  sp.setText("null");
+  sp.display();
+  is(sp.selectedText, "/*\nnull\n*/", "'null' is displayed");
+
+  sp.setText("NaN");
+  sp.display();
+  is(sp.selectedText, "/*\nNaN\n*/", "'NaN' is displayed");
+
+  sp.setText("''");
+  sp.display();
+  is(sp.selectedText, "/*\n\n*/", "empty string is displayed");
+}
--- a/browser/devtools/scratchpad/test/browser_scratchpad_contexts.js
+++ b/browser/devtools/scratchpad/test/browser_scratchpad_contexts.js
@@ -83,48 +83,48 @@ function runTests()
 
   is(window.foobarBug636725, "aloha2", "window.foobarBug636725 has been set");
 
   sp.setText("gBrowser", 7);
 
   ok(sp.getText(), "window.gBrowser",
      "setText() worked with no end for the replace range");
 
-  is(typeof sp.run()[1].addTab, "function",
+  is(typeof sp.run()[2].addTab, "function",
      "chrome context has access to chrome objects");
 
   // Check that the sandbox is cached.
 
   sp.setText("typeof foobarBug636725cache;");
-  is(sp.run()[1], "undefined", "global variable does not exist");
+  is(sp.run()[2], "undefined", "global variable does not exist");
 
   sp.setText("var foobarBug636725cache = 'foo';");
   sp.run();
 
   sp.setText("typeof foobarBug636725cache;");
-  is(sp.run()[1], "string",
+  is(sp.run()[2], "string",
      "global variable exists across two different executions");
 
   sp.resetContext();
 
-  is(sp.run()[1], "undefined",
+  is(sp.run()[2], "undefined",
      "global variable no longer exists after calling resetContext()");
 
   sp.setText("var foobarBug636725cache2 = 'foo';");
   sp.run();
 
   sp.setText("typeof foobarBug636725cache2;");
-  is(sp.run()[1], "string",
+  is(sp.run()[2], "string",
      "global variable exists across two different executions");
 
   sp.setContentContext();
 
   is(sp.executionContext, gScratchpadWindow.SCRATCHPAD_CONTEXT_CONTENT,
      "executionContext is content");
 
-  is(sp.run()[1], "undefined",
+  is(sp.run()[2], "undefined",
      "global variable no longer exists after changing the context");
 
   gScratchpadWindow.close();
   gScratchpadWindow = null;
   gBrowser.removeCurrentTab();
   finish();
 }
--- a/browser/devtools/scratchpad/test/browser_scratchpad_execute_print.js
+++ b/browser/devtools/scratchpad/test/browser_scratchpad_execute_print.js
@@ -27,18 +27,19 @@ function runTests()
   let sp = gScratchpadWindow.Scratchpad;
 
   content.wrappedJSObject.foobarBug636725 = 1;
 
   sp.setText("++window.foobarBug636725");
 
   let exec = sp.run();
   is(exec[0], sp.getText(), "run()[0] is correct");
-  is(exec[1], content.wrappedJSObject.foobarBug636725,
-     "run()[1] is correct");
+  ok(!exec[1], "run()[1] is correct");
+  is(exec[2], content.wrappedJSObject.foobarBug636725,
+     "run()[2] is correct");
 
   is(sp.getText(), "++window.foobarBug636725",
      "run() does not change the editor content");
 
   is(content.wrappedJSObject.foobarBug636725, 2,
      "run() updated window.foobarBug636725");
 
   sp.display();
@@ -72,18 +73,20 @@ function runTests()
 
   is(selection.start, 0, "selection.start is 0");
   is(selection.end, 29, "selection.end is 29");
 
   exec = sp.run();
 
   is(exec[0], "window.foobarBug636725 = 'a';",
      "run()[0] is correct");
-  is(exec[1], "a",
+  ok(!exec[1], 
      "run()[1] is correct");
+  is(exec[2], "a",
+     "run()[2] is correct");
 
   is(sp.getText(), "window.foobarBug636725 = 'a';\n" +
                    "window.foobarBug636725 = 'b';",
      "run() does not change the textbox value");
 
   is(content.wrappedJSObject.foobarBug636725, "a",
      "run() worked for the selected range");
 
--- a/browser/devtools/scratchpad/test/browser_scratchpad_tab_switch.js
+++ b/browser/devtools/scratchpad/test/browser_scratchpad_tab_switch.js
@@ -92,17 +92,17 @@ function runTests2() {
   content.location = "data:text/html,test context switch in Scratchpad location 2";
 }
 
 function runTests3() {
   gBrowser.selectedBrowser.removeEventListener("load", runTests3, true);
   // Check that the sandbox is not cached.
 
   sp.setText("typeof foosbug653108;");
-  is(sp.run()[1], "undefined", "global variable does not exist");
+  is(sp.run()[2], "undefined", "global variable does not exist");
 
   gScratchpadWindow.close();
   gScratchpadWindow = null;
   tab1 = null;
   tab2 = null;
   sp = null;
   gBrowser.removeCurrentTab();
   gBrowser.removeCurrentTab();