Bug 1167478 - Menu items "Larger Font" and "Smaller Font" should be disabled if the current font size is equals to MAXIMUM_FONT_SIZE or MINIMUM_FONT_SIZE constants. r=past
authorWillian Gustavo Veiga <contact@willianveiga.com>
Tue, 16 Jun 2015 14:12:27 +0300
changeset 279726 ca6bc23357aad14b4c4cd9e57435d2dfcc9a7b89
parent 279725 82cab91c01cd74819145ca9b23f687378f5f0d5c
child 279727 87c9b69a0ec085e4bb78560c80c195112a803aa7
push id4932
push userjlund@mozilla.com
push dateMon, 10 Aug 2015 18:23:06 +0000
treeherdermozilla-beta@6dd5a4f5f745 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerspast
bugs1167478
milestone41.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1167478 - Menu items "Larger Font" and "Smaller Font" should be disabled if the current font size is equals to MAXIMUM_FONT_SIZE or MINIMUM_FONT_SIZE constants. r=past
browser/devtools/scratchpad/scratchpad.js
browser/devtools/scratchpad/test/browser.ini
browser/devtools/scratchpad/test/browser_scratchpad_disable_view_menu_items.js
--- a/browser/devtools/scratchpad/scratchpad.js
+++ b/browser/devtools/scratchpad/scratchpad.js
@@ -259,28 +259,43 @@ var Scratchpad = {
 
   /**
    * Check or uncheck view menu items according to stored preferences.
    */
   _updateViewMenuItems: function SP_updateViewMenuItems() {
     this._updateViewMenuItem(SHOW_LINE_NUMBERS, "sp-menu-line-numbers");
     this._updateViewMenuItem(WRAP_TEXT, "sp-menu-word-wrap");
     this._updateViewMenuItem(SHOW_TRAILING_SPACE, "sp-menu-highlight-trailing-space");
+    this._updateViewFontMenuItem(MINIMUM_FONT_SIZE, "sp-cmd-smaller-font");
+    this._updateViewFontMenuItem(MAXIMUM_FONT_SIZE, "sp-cmd-larger-font");
   },
 
+  /**
+   * Check or uncheck view menu item according to stored preferences.
+   */
   _updateViewMenuItem: function SP_updateViewMenuItem(preferenceName, menuId) {
     let checked = Services.prefs.getBoolPref(preferenceName);
     if (checked) {
         document.getElementById(menuId).setAttribute('checked', true);
     } else {
         document.getElementById(menuId).removeAttribute('checked');
     }
   },
 
   /**
+   * Disable view menu item if the stored font size is equals to the given one.
+   */
+  _updateViewFontMenuItem: function SP_updateViewFontMenuItem(fontSize, commandId) {
+    let prefFontSize = Services.prefs.getIntPref(EDITOR_FONT_SIZE);
+    if (prefFontSize === fontSize) {
+      document.getElementById(commandId).setAttribute('disabled', true);
+    }
+  },
+
+  /**
    * The script execution context. This tells Scratchpad in which context the
    * script shall execute.
    *
    * Possible values:
    *   - SCRATCHPAD_CONTEXT_CONTENT to execute code in the context of the current
    *   tab content window object.
    *   - SCRATCHPAD_CONTEXT_BROWSER to execute code in the context of the
    *   currently active chrome window object.
@@ -1934,40 +1949,55 @@ var Scratchpad = {
   increaseFontSize: function SP_increaseFontSize()
   {
     let size = this.editor.getFontSize();
 
     if (size < MAXIMUM_FONT_SIZE) {
       let newFontSize = size + 1;
       this.editor.setFontSize(newFontSize);
       Services.prefs.setIntPref(EDITOR_FONT_SIZE, newFontSize);
+
+      if (newFontSize === MAXIMUM_FONT_SIZE) {
+        document.getElementById("sp-cmd-larger-font").setAttribute('disabled', true);
+      }
+
+      document.getElementById("sp-cmd-smaller-font").removeAttribute('disabled');
     }
   },
 
   /**
    * Decrease the editor's font size by 1 px.
    */
   decreaseFontSize: function SP_decreaseFontSize()
   {
     let size = this.editor.getFontSize();
 
     if (size > MINIMUM_FONT_SIZE) {
       let newFontSize = size - 1;
       this.editor.setFontSize(newFontSize);
       Services.prefs.setIntPref(EDITOR_FONT_SIZE, newFontSize);
+
+      if (newFontSize === MINIMUM_FONT_SIZE) {
+        document.getElementById("sp-cmd-smaller-font").setAttribute('disabled', true);
+      }
     }
+
+    document.getElementById("sp-cmd-larger-font").removeAttribute('disabled');
   },
 
   /**
    * Restore the editor's original font size.
    */
   normalFontSize: function SP_normalFontSize()
   {
     this.editor.setFontSize(NORMAL_FONT_SIZE);
     Services.prefs.setIntPref(EDITOR_FONT_SIZE, NORMAL_FONT_SIZE);
+
+    document.getElementById("sp-cmd-larger-font").removeAttribute('disabled');
+    document.getElementById("sp-cmd-smaller-font").removeAttribute('disabled');
   },
 
   _observers: [],
 
   /**
    * Add an observer for Scratchpad events.
    *
    * The observer implements IScratchpadObserver := {
--- a/browser/devtools/scratchpad/test/browser.ini
+++ b/browser/devtools/scratchpad/test/browser.ini
@@ -39,8 +39,9 @@ support-files = NS_ERROR_ILLEGAL_INPUT.t
 [browser_scratchpad_pprint-02.js]
 [browser_scratchpad_pprint.js]
 [browser_scratchpad_pprint_error_goto_line.js]
 [browser_scratchpad_restore.js]
 [browser_scratchpad_tab_switch.js]
 [browser_scratchpad_ui.js]
 [browser_scratchpad_close_toolbox.js]
 [browser_scratchpad_remember_view_options.js]
+[browser_scratchpad_disable_view_menu_items.js]
new file mode 100644
--- /dev/null
+++ b/browser/devtools/scratchpad/test/browser_scratchpad_disable_view_menu_items.js
@@ -0,0 +1,66 @@
+/* Any copyright is dedicated to the Public Domain.
+   http://creativecommons.org/publicdomain/zero/1.0/ */
+
+// Test if the view menu items "Larger Font" and "Smaller Font" are disabled
+// when the font size reaches the maximum/minimum values.
+
+let {Task} = Cu.import("resource://gre/modules/Task.jsm", {});
+
+function test() {
+  const options = {
+    tabContent: 'test if view menu items "Larger Font" and "Smaller Font" are enabled/disabled.'
+  };
+  openTabAndScratchpad(options)
+    .then(Task.async(runTests))
+    .then(finish, console.error);
+}
+
+function* runTests([win, sp]) {
+  yield testMaximumFontSize(win, sp);
+
+  yield testMinimumFontSize(win, sp);
+}
+
+const MAXIMUM_FONT_SIZE = 96;
+const MINIMUM_FONT_SIZE = 6;
+const NORMAL_FONT_SIZE = 12;
+
+let testMaximumFontSize = Task.async(function* (win, sp) {
+  let doc = win.document;
+
+  Services.prefs.clearUserPref('devtools.scratchpad.editorFontSize');
+
+  let menu = doc.getElementById('sp-menu-larger-font');
+
+  for (let i = NORMAL_FONT_SIZE; i <= MAXIMUM_FONT_SIZE; i++) {
+    menu.doCommand();
+  }
+
+  let cmd = doc.getElementById('sp-cmd-larger-font');
+  ok(cmd.getAttribute('disabled') === 'true', 'Command "sp-cmd-larger-font" is disabled.');
+
+  menu = doc.getElementById('sp-menu-smaller-font');
+  menu.doCommand();
+
+  ok(cmd.hasAttribute('disabled') === false, 'Command "sp-cmd-larger-font" is enabled.');
+});
+
+let testMinimumFontSize = Task.async(function* (win, sp) {
+  let doc = win.document;
+
+  let menu = doc.getElementById('sp-menu-smaller-font');
+
+  for (let i = MAXIMUM_FONT_SIZE; i >= MINIMUM_FONT_SIZE; i--) {
+    menu.doCommand();
+  }
+
+  let cmd = doc.getElementById('sp-cmd-smaller-font');
+  ok(cmd.getAttribute('disabled') === 'true', 'Command "sp-cmd-smaller-font" is disabled.');
+
+  menu = doc.getElementById('sp-menu-larger-font');
+  menu.doCommand();
+
+  ok(cmd.hasAttribute('disabled') === false, 'Command "sp-cmd-smaller-font" is enabled.');
+
+  Services.prefs.clearUserPref('devtools.scratchpad.editorFontSize');
+});