Commit 51833c20 authored by Geoff Lankow's avatar Geoff Lankow
Browse files

Bug 669345 - Add support for radio button elements for inline preferences; r=Unfocused

parent d68cbdf1
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -133,6 +133,11 @@ setting[type="directory"] {
  -moz-binding: url("chrome://mozapps/content/extensions/setting.xml#setting-path");
}

setting[type="radio"] {
  display: -moz-grid-line;
  -moz-binding: url("chrome://mozapps/content/extensions/setting.xml#setting-radio");
}

#addonitem-popup > menuitem[disabled="true"] {
  display: none;
}
+68 −0
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@
   - Contributor(s):
   -   Daniel Brooks <db48x@yahoo.com>
   -   Mark Finkle <mfinkle@mozilla.com>
   -   Geoff Lankow <geoff@darktrojan.net>
   -
   - Alternatively, the contents of this file may be used under the terms of
   - either the GNU General Public License Version 2 or later (the "GPL"), or
@@ -471,4 +472,71 @@
      </property>
    </implementation>
  </binding>

  <binding id="setting-radio" extends="chrome://mozapps/content/extensions/setting.xml#setting-base">
    <content>
      <xul:vbox class="setting-label">
        <xul:label class="preferences-title" xbl:inherits="value=title" crop="end" flex="1"/>
        <xul:label class="preferences-description" xbl:inherits="value=desc" crop="end" flex="1">
          <children/>
        </xul:label>
      </xul:vbox>
      <xul:hbox class="setting-input">
        <children includes="radiogroup" />
      </xul:hbox>
    </content>

    <implementation>
      <constructor>
      <![CDATA[
        this.radioGroup.addEventListener("command", this.valueToPreference.bind(this), false);
      ]]>
      </constructor>

      <method name="valueFromPreference">
        <body>
        <![CDATA[
          let val;
          switch (Services.prefs.getPrefType(this.pref)) {
            case Ci.nsIPrefBranch.PREF_STRING:
              val = Services.prefs.getCharPref(this.pref);
              break;
            case Ci.nsIPrefBranch.PREF_INT:
              val = Services.prefs.getIntPref(this.pref);
              break;
            case Ci.nsIPrefBranch.PREF_BOOL:
              val = Services.prefs.getBoolPref(this.pref).toString();
              break;
            default:
              return;
          }

          for (let i = 0; i < this.radioGroup.itemCount; i++) {
            if (this.radioGroup.getItemAtIndex(i).value == val) {
              this.radioGroup.selectedIndex = i;
              break;
            }
          }
        ]]>
        </body>
      </method>

      <method name="valueToPreference">
        <body>
        <![CDATA[
          // We might not have a pref already set, so we guess the type from the value attribute
          let val = this.radioGroup.selectedItem.value;
          if (val == "true" || val == "false")
            Services.prefs.setBoolPref(this.pref, val == "true");
          else if (/^-?\d+$/.test(val))
            Services.prefs.setIntPref(this.pref, val);
          else
            Services.prefs.setCharPref(this.pref, val);
        ]]>
        </body>
      </method>

      <field name="radioGroup">this.getElementsByTagName("radiogroup")[0];</field>
    </implementation>
  </binding>
</bindings>
+1 −0
Original line number Diff line number Diff line
@@ -123,6 +123,7 @@ _TEST_RESOURCES = \
  browser_eula.xml \
  browser_purchase.xml \
  discovery.html \
  more_options.xul \
  options.xul \
  redirect.sjs \
  releaseNotes.xhtml \
+67 −0
Original line number Diff line number Diff line
@@ -41,6 +41,13 @@ function test() {
    version: "1",
    optionsURL: CHROMEROOT + "options.xul",
    optionsType: AddonManager.OPTIONS_TYPE_INLINE
  },{
    id: "inlinesettings3@tests.mozilla.org",
    name: "Inline Settings (More Options)",
    description: "Tests for option types introduced after Mozilla 7.0",
    version: "1",
    optionsURL: CHROMEROOT + "more_options.xul",
    optionsType: AddonManager.OPTIONS_TYPE_INLINE
  },{
    id: "noninlinesettings@tests.mozilla.org",
    name: "Non-Inline Settings",
@@ -70,6 +77,9 @@ function end_test() {
  Services.prefs.clearUserPref("extensions.inlinesettings1.color");
  Services.prefs.clearUserPref("extensions.inlinesettings1.file");
  Services.prefs.clearUserPref("extensions.inlinesettings1.directory");
  Services.prefs.clearUserPref("extensions.inlinesettings3.radioBool");
  Services.prefs.clearUserPref("extensions.inlinesettings3.radioInt");
  Services.prefs.clearUserPref("extensions.inlinesettings3.radioString");

  close_manager(gManagerWindow, function() {
    AddonManager.getAddonByID("inlinesettings1@tests.mozilla.org", function(aAddon) {
@@ -255,6 +265,63 @@ add_test(function() {
  });
});

// Tests for the setting.xml bindings introduced after Mozilla 7
add_test(function() {
  var addon = get_addon_element(gManagerWindow, "inlinesettings3@tests.mozilla.org");
  addon.parentNode.ensureElementIsVisible(addon);

  var button = gManagerWindow.document.getAnonymousElementByAttribute(addon, "anonid", "preferences-btn");
  EventUtils.synthesizeMouseAtCenter(button, { clickCount: 1 }, gManagerWindow);

  wait_for_view_load(gManagerWindow, function() {
    is(observer.lastData, "inlinesettings3@tests.mozilla.org", "Observer notification should have fired");

    var grid = gManagerWindow.document.getElementById("detail-grid");
    var settings = grid.querySelectorAll("rows > setting");
    is(settings.length, 3, "Grid should have settings children");

    // Force bindings to apply
    settings[0].clientTop;

    ok(settings[0].hasAttribute("first-row"), "First visible row should have first-row attribute");
    Services.prefs.setBoolPref("extensions.inlinesettings3.radioBool", false);
    var radios = settings[0].getElementsByTagName("radio");
    isnot(radios[0].selected, true, "Correct radio button should be selected");
    is(radios[1].selected, true, "Correct radio button should be selected");
    EventUtils.synthesizeMouseAtCenter(radios[0], { clickCount: 1 }, gManagerWindow);
    is(Services.prefs.getBoolPref("extensions.inlinesettings3.radioBool"), true, "Radio pref should have been updated");
    EventUtils.synthesizeMouseAtCenter(radios[1], { clickCount: 1 }, gManagerWindow);
    is(Services.prefs.getBoolPref("extensions.inlinesettings3.radioBool"), false, "Radio pref should have been updated");

    ok(!settings[1].hasAttribute("first-row"), "Not the first row");
    Services.prefs.setIntPref("extensions.inlinesettings3.radioInt", 5);
    var radios = settings[1].getElementsByTagName("radio");
    isnot(radios[0].selected, true, "Correct radio button should be selected");
    is(radios[1].selected, true, "Correct radio button should be selected");
    isnot(radios[2].selected, true, "Correct radio button should be selected");
    EventUtils.synthesizeMouseAtCenter(radios[0], { clickCount: 1 }, gManagerWindow);
    is(Services.prefs.getIntPref("extensions.inlinesettings3.radioInt"), 4, "Radio pref should have been updated");
    EventUtils.synthesizeMouseAtCenter(radios[2], { clickCount: 1 }, gManagerWindow);
    is(Services.prefs.getIntPref("extensions.inlinesettings3.radioInt"), 6, "Radio pref should have been updated");

    ok(!settings[2].hasAttribute("first-row"), "Not the first row");
    Services.prefs.setCharPref("extensions.inlinesettings3.radioString", "juliet");
    var radios = settings[2].getElementsByTagName("radio");
    isnot(radios[0].selected, true, "Correct radio button should be selected");
    is(radios[1].selected, true, "Correct radio button should be selected");
    isnot(radios[2].selected, true, "Correct radio button should be selected");
    EventUtils.synthesizeMouseAtCenter(radios[0], { clickCount: 1 }, gManagerWindow);
    is(Services.prefs.getCharPref("extensions.inlinesettings3.radioString"), "india", "Radio pref should have been updated");
    EventUtils.synthesizeMouseAtCenter(radios[2], { clickCount: 1 }, gManagerWindow);
    is(Services.prefs.getCharPref("extensions.inlinesettings3.radioString"), "kilo", "Radio pref should have been updated");

    button = gManagerWindow.document.getElementById("detail-prefs-btn");
    is_element_hidden(button, "Preferences button should not be visible");

    gCategoryUtilities.openType("extension", run_next_test);
  });
});

// Addon with inline preferences as optionsURL
add_test(function() {
  var addon = get_addon_element(gManagerWindow, "inlinesettings2@tests.mozilla.org");
+23 −0
Original line number Diff line number Diff line
<?xml version="1.0" ?>
<vbox xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
  <setting pref="extensions.inlinesettings3.radioBool" type="radio" title="Radio">
    <radiogroup>
      <radio label="Delta" value="true" />
      <radio label="Echo" value="false" />
    </radiogroup>
  </setting>
  <setting pref="extensions.inlinesettings3.radioInt" type="radio" title="Radio">
    <radiogroup>
      <radio label="Foxtrot" value="4" />
      <radio label="Golf" value="5" />
      <radio label="Hotel" value="6" />
    </radiogroup>
  </setting>
  <setting pref="extensions.inlinesettings3.radioString" type="radio" title="Radio">
    <radiogroup>
      <radio label="India" value="india" />
      <radio label="Juliet" value="juliet" />
      <radio label="Kilo" value="kilo" />
    </radiogroup>
  </setting>
</vbox>
Loading