summaryrefslogtreecommitdiffstats
path: root/toolkit/mozapps/extensions/test/browser/browser_inlinesettings.js
blob: e2814ddf4cfb8785fa085ec04530b7adb063aec8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

// Tests various aspects of the details view
Components.utils.import("resource://gre/modules/Preferences.jsm");

var gManagerWindow;
var gCategoryUtilities;
var gProvider;

const SETTINGS_ROWS = 9;

var MockFilePicker = SpecialPowers.MockFilePicker;
MockFilePicker.init(window);

var observer = {
  lastDisplayed: null,
  callback: null,
  checkDisplayed: function(aExpected) {
    is(this.lastDisplayed, aExpected, "'addon-options-displayed' notification should have fired");
    this.lastDisplayed = null;
  },
  checkNotDisplayed: function() {
    is(this.lastDisplayed, null, "'addon-options-displayed' notification should not have fired");
  },
  lastHidden: null,
  checkHidden: function(aExpected) {
    is(this.lastHidden, aExpected, "'addon-options-hidden' notification should have fired");
    this.lastHidden = null;
  },
  checkNotHidden: function() {
    is(this.lastHidden, null, "'addon-options-hidden' notification should not have fired");
  },
  observe: function(aSubject, aTopic, aData) {
    if (aTopic == AddonManager.OPTIONS_NOTIFICATION_DISPLAYED) {
      this.lastDisplayed = aData;
      // Test if the binding has applied before the observers are notified. We test the second setting here,
      // because the code operates on the first setting and we want to check it applies to all.
      var setting = aSubject.querySelector("rows > setting[first-row] ~ setting");
      var input = gManagerWindow.document.getAnonymousElementByAttribute(setting, "class", "preferences-title");
      isnot(input, null, "XBL binding should be applied");

      // Add some extra height to the scrolling pane to ensure that it needs to scroll when appropriate.
      gManagerWindow.document.getElementById("detail-controls").style.marginBottom = "1000px";

      if (this.callback) {
        var tempCallback = this.callback;
        this.callback = null;
        tempCallback();
      }
    } else if (aTopic == AddonManager.OPTIONS_NOTIFICATION_HIDDEN) {
      this.lastHidden = aData;
    }
  }
};

function installAddon(aCallback) {
  AddonManager.getInstallForURL(TESTROOT + "addons/browser_inlinesettings1.xpi",
                                function(aInstall) {
    aInstall.addListener({
      onInstallEnded: function() {
        executeSoon(aCallback);
      }
    });
    aInstall.install();
  }, "application/x-xpinstall");
}

function checkScrolling(aShouldHaveScrolled) {
  var detailView = gManagerWindow.document.getElementById("detail-view");
  var boxObject = detailView.boxObject;
  ok(detailView.scrollHeight > boxObject.height, "Page should require scrolling");
  if (aShouldHaveScrolled)
    isnot(detailView.scrollTop, 0, "Page should have scrolled");
  else
    is(detailView.scrollTop, 0, "Page should not have scrolled");
}

function test() {
  waitForExplicitFinish();

  gProvider = new MockProvider();

  gProvider.createAddons([{
    id: "inlinesettings2@tests.mozilla.org",
    name: "Inline Settings (Regular)",
    version: "1",
    optionsURL: CHROMEROOT + "options.xul",
    optionsType: AddonManager.OPTIONS_TYPE_INLINE,
    operationsRequiringRestart: AddonManager.OP_NEEDS_RESTART_DISABLE,
  }, {
    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",
    version: "1",
    optionsURL: CHROMEROOT + "addon_prefs.xul"
  }]);

  installAddon(function () {
    open_manager("addons://list/extension", function(aWindow) {
      gManagerWindow = aWindow;
      gCategoryUtilities = new CategoryUtilities(gManagerWindow);

      Services.obs.addObserver(observer,
                               AddonManager.OPTIONS_NOTIFICATION_DISPLAYED,
                               false);
      Services.obs.addObserver(observer,
                               AddonManager.OPTIONS_NOTIFICATION_HIDDEN,
                               false);

      run_next_test();
    });
  });
}

function end_test() {
  Services.obs.removeObserver(observer,
                              AddonManager.OPTIONS_NOTIFICATION_DISPLAYED);

  Services.prefs.clearUserPref("extensions.inlinesettings1.bool");
  Services.prefs.clearUserPref("extensions.inlinesettings1.boolint");
  Services.prefs.clearUserPref("extensions.inlinesettings1.integer");
  Services.prefs.clearUserPref("extensions.inlinesettings1.string");
  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");
  Services.prefs.clearUserPref("extensions.inlinesettings3.menulist");

  MockFilePicker.cleanup();

  close_manager(gManagerWindow, function() {
    observer.checkHidden("inlinesettings3@tests.mozilla.org");
    Services.obs.removeObserver(observer,
                                AddonManager.OPTIONS_NOTIFICATION_HIDDEN);

    AddonManager.getAddonByID("inlinesettings1@tests.mozilla.org", function(aAddon) {
      aAddon.uninstall();
      finish();
    });
  });
}

// Addon with options.xul
add_test(function() {
  var addon = get_addon_element(gManagerWindow, "inlinesettings1@tests.mozilla.org");
  is(addon.mAddon.optionsType, AddonManager.OPTIONS_TYPE_INLINE, "Options should be inline type");
  addon.parentNode.ensureElementIsVisible(addon);

  var button = gManagerWindow.document.getAnonymousElementByAttribute(addon, "anonid", "preferences-btn");
  is_element_visible(button, "Preferences button should be visible");

  run_next_test();
});

// Addon with inline preferences as optionsURL
add_test(function() {
  var addon = get_addon_element(gManagerWindow, "inlinesettings2@tests.mozilla.org");
  is(addon.mAddon.optionsType, AddonManager.OPTIONS_TYPE_INLINE, "Options should be inline type");
  addon.parentNode.ensureElementIsVisible(addon);

  var button = gManagerWindow.document.getAnonymousElementByAttribute(addon, "anonid", "preferences-btn");
  is_element_visible(button, "Preferences button should be visible");

  run_next_test();
});

// Addon with non-inline preferences as optionsURL
add_test(function() {
  var addon = get_addon_element(gManagerWindow, "noninlinesettings@tests.mozilla.org");
  is(addon.mAddon.optionsType, AddonManager.OPTIONS_TYPE_DIALOG, "Options should be dialog type");
  addon.parentNode.ensureElementIsVisible(addon);

  var button = gManagerWindow.document.getAnonymousElementByAttribute(addon, "anonid", "preferences-btn");
  is_element_visible(button, "Preferences button should be visible");

  run_next_test();
});

// Addon with options.xul, also a test for the setting.xml bindings
add_test(function() {
  var addon = get_addon_element(gManagerWindow, "inlinesettings1@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() {
    observer.checkDisplayed("inlinesettings1@tests.mozilla.org");
    is(gManagerWindow.gViewController.currentViewId,
       "addons://detail/inlinesettings1%40tests.mozilla.org/preferences",
       "Current view should scroll to preferences");
    checkScrolling(true);

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

    ok(settings[0].hasAttribute("first-row"), "First visible row should have first-row attribute");
    Services.prefs.setBoolPref("extensions.inlinesettings1.bool", false);
    var input = gManagerWindow.document.getAnonymousElementByAttribute(settings[0], "anonid", "input");
    isnot(input.checked, true, "Checkbox should have initial value");
    is(input.label, "Check box label", "Checkbox should be labelled");
    EventUtils.synthesizeMouseAtCenter(input, { clickCount: 1 }, gManagerWindow);
    is(input.checked, true, "Checkbox should have updated value");
    is(Services.prefs.getBoolPref("extensions.inlinesettings1.bool"), true, "Bool pref should have been updated");
    EventUtils.synthesizeMouseAtCenter(input, { clickCount: 1 }, gManagerWindow);
    isnot(input.checked, true, "Checkbox should have updated value");
    is(Services.prefs.getBoolPref("extensions.inlinesettings1.bool"), false, "Bool pref should have been updated");

    ok(!settings[1].hasAttribute("first-row"), "Not the first row");
    Services.prefs.setIntPref("extensions.inlinesettings1.boolint", 0);
    input = gManagerWindow.document.getAnonymousElementByAttribute(settings[1], "anonid", "input");
    isnot(input.checked, true, "Checkbox should have initial value");
    EventUtils.synthesizeMouseAtCenter(input, { clickCount: 1 }, gManagerWindow);
    is(input.checked, true, "Checkbox should have updated value");
    is(Services.prefs.getIntPref("extensions.inlinesettings1.boolint"), 1, "BoolInt pref should have been updated");
    EventUtils.synthesizeMouseAtCenter(input, { clickCount: 1 }, gManagerWindow);
    isnot(input.checked, true, "Checkbox should have updated value");
    is(Services.prefs.getIntPref("extensions.inlinesettings1.boolint"), 2, "BoolInt pref should have been updated");

    ok(!settings[2].hasAttribute("first-row"), "Not the first row");
    Services.prefs.setIntPref("extensions.inlinesettings1.integer", 0);
    input = gManagerWindow.document.getAnonymousElementByAttribute(settings[2], "anonid", "input");
    is(input.value, "0", "Number box should have initial value");
    input.select();
    EventUtils.synthesizeKey("1", {}, gManagerWindow);
    EventUtils.synthesizeKey("3", {}, gManagerWindow);
    is(input.value, "13", "Number box should have updated value");
    is(Services.prefs.getIntPref("extensions.inlinesettings1.integer"), 13, "Integer pref should have been updated");
    EventUtils.synthesizeKey("VK_DOWN", {}, gManagerWindow);
    is(input.value, "12", "Number box should have updated value");
    is(Services.prefs.getIntPref("extensions.inlinesettings1.integer"), 12, "Integer pref should have been updated");

    ok(!settings[3].hasAttribute("first-row"), "Not the first row");
    Services.prefs.setCharPref("extensions.inlinesettings1.string", "foo");
    input = gManagerWindow.document.getAnonymousElementByAttribute(settings[3], "anonid", "input");
    is(input.value, "foo", "Text box should have initial value");
    input.select();
    EventUtils.synthesizeKey("b", {}, gManagerWindow);
    EventUtils.synthesizeKey("a", {}, gManagerWindow);
    EventUtils.synthesizeKey("r", {}, gManagerWindow);
    is(input.value, "bar", "Text box should have updated value");
    input.value += "\u03DE"; // Cheat to add this non-ASCII character without typing it.
    EventUtils.synthesizeKey("/", {}, gManagerWindow);
    is(input.value, "bar\u03DE/", "Text box should have updated value");
    is(gManagerWindow.document.getBindingParent(gManagerWindow.document.activeElement), input, "Search box should not have focus");
    is(Preferences.get("extensions.inlinesettings1.string", "wrong"), "bar\u03DE/", "String pref should have been updated");

    ok(!settings[4].hasAttribute("first-row"), "Not the first row");
    input = settings[4].firstElementChild;
    is(input.value, "1", "Menulist should have initial value");
    input.focus();
    EventUtils.synthesizeKey("b", {}, gManagerWindow);
    is(input.value, "2", "Menulist should have updated value");
    is(gManagerWindow._testValue, "2", "Menulist oncommand handler should've updated the test value");
    delete gManagerWindow._testValue;

    ok(!settings[5].hasAttribute("first-row"), "Not the first row");
    Services.prefs.setCharPref("extensions.inlinesettings1.color", "#FF0000");
    input = gManagerWindow.document.getAnonymousElementByAttribute(settings[5], "anonid", "input");
    is(input.color, "#FF0000", "Color picker should have initial value");
    input.focus();
    EventUtils.synthesizeKey("VK_RIGHT", {}, gManagerWindow);
    EventUtils.synthesizeKey("VK_RIGHT", {}, gManagerWindow);
    EventUtils.synthesizeKey("VK_RETURN", {}, gManagerWindow);
    input.hidePopup();
    is(input.color, "#FF9900", "Color picker should have updated value");
    is(Services.prefs.getCharPref("extensions.inlinesettings1.color"), "#FF9900", "Color pref should have been updated");

    try {
      ok(!settings[6].hasAttribute("first-row"), "Not the first row");
      var button = gManagerWindow.document.getAnonymousElementByAttribute(settings[6], "anonid", "button");
      input = gManagerWindow.document.getAnonymousElementByAttribute(settings[6], "anonid", "input");
      is(input.value, "", "Label value should be empty");
      is(input.tooltipText, "", "Label tooltip should be empty");

      var testFile = Services.dirsvc.get("ProfD", Ci.nsIFile);
      testFile.append("\u2622");
      var curProcD = Services.dirsvc.get("CurProcD", Ci.nsIFile);

      MockFilePicker.returnFiles = [testFile];
      MockFilePicker.returnValue = Ci.nsIFilePicker.returnOK;
      EventUtils.synthesizeMouseAtCenter(button, { clickCount: 1 }, gManagerWindow);
      is(MockFilePicker.mode, Ci.nsIFilePicker.modeOpen, "File picker mode should be open file");
      is(input.value, testFile.path, "Label value should match file chosen");
      is(input.tooltipText, testFile.path, "Label tooltip should match file chosen");
      is(Preferences.get("extensions.inlinesettings1.file", "wrong"), testFile.path, "File pref should match file chosen");

      MockFilePicker.returnFiles = [curProcD];
      MockFilePicker.returnValue = Ci.nsIFilePicker.returnCancel;
      EventUtils.synthesizeMouseAtCenter(button, { clickCount: 1 }, gManagerWindow);
      is(MockFilePicker.mode, Ci.nsIFilePicker.modeOpen, "File picker mode should be open file");
      is(input.value, testFile.path, "Label value should not have changed");
      is(input.tooltipText, testFile.path, "Label tooltip should not have changed");
      is(Preferences.get("extensions.inlinesettings1.file", "wrong"), testFile.path, "File pref should not have changed");

      ok(!settings[7].hasAttribute("first-row"), "Not the first row");
      button = gManagerWindow.document.getAnonymousElementByAttribute(settings[7], "anonid", "button");
      input = gManagerWindow.document.getAnonymousElementByAttribute(settings[7], "anonid", "input");
      is(input.value, "", "Label value should be empty");
      is(input.tooltipText, "", "Label tooltip should be empty");

      MockFilePicker.returnFiles = [testFile];
      MockFilePicker.returnValue = Ci.nsIFilePicker.returnOK;
      EventUtils.synthesizeMouseAtCenter(button, { clickCount: 1 }, gManagerWindow);
      is(MockFilePicker.mode, Ci.nsIFilePicker.modeGetFolder, "File picker mode should be directory");
      is(input.value, testFile.path, "Label value should match file chosen");
      is(input.tooltipText, testFile.path, "Label tooltip should match file chosen");
      is(Preferences.get("extensions.inlinesettings1.directory", "wrong"), testFile.path, "Directory pref should match file chosen");

      MockFilePicker.returnFiles = [curProcD];
      MockFilePicker.returnValue = Ci.nsIFilePicker.returnCancel;
      EventUtils.synthesizeMouseAtCenter(button, { clickCount: 1 }, gManagerWindow);
      is(MockFilePicker.mode, Ci.nsIFilePicker.modeGetFolder, "File picker mode should be directory");
      is(input.value, testFile.path, "Label value should not have changed");
      is(input.tooltipText, testFile.path, "Label tooltip should not have changed");
      is(Preferences.get("extensions.inlinesettings1.directory", "wrong"), testFile.path, "Directory pref should not have changed");

      var unsizedInput = gManagerWindow.document.getAnonymousElementByAttribute(settings[2], "anonid", "input");
      var sizedInput = gManagerWindow.document.getAnonymousElementByAttribute(settings[8], "anonid", "input");
      is(unsizedInput.clientWidth > sizedInput.clientWidth, true, "Input with size attribute should be smaller than input without");
    } finally {
      button = gManagerWindow.document.getElementById("detail-prefs-btn");
      is_element_hidden(button, "Preferences button should not be visible");

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

// Tests for the setting.xml bindings introduced after Mozilla 7
add_test(function() {
  observer.checkHidden("inlinesettings1@tests.mozilla.org");

  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() {
    observer.checkDisplayed("inlinesettings3@tests.mozilla.org");

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

    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);
    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");
    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(Preferences.get("extensions.inlinesettings3.radioString", "wrong"), "india", "Radio pref should have been updated");
    EventUtils.synthesizeMouseAtCenter(radios[2], { clickCount: 1 }, gManagerWindow);
    is(Preferences.get("extensions.inlinesettings3.radioString", "wrong"), "kilo \u338F", "Radio pref should have been updated");

    ok(!settings[3].hasAttribute("first-row"), "Not the first row");
    Services.prefs.setIntPref("extensions.inlinesettings3.menulist", 8);
    var input = settings[3].firstElementChild;
    is(input.value, "8", "Menulist should have initial value");
    input.focus();
    EventUtils.synthesizeKey("n", {}, gManagerWindow);
    is(input.value, "9", "Menulist should have updated value");
    is(Services.prefs.getIntPref("extensions.inlinesettings3.menulist"), 9, "Menulist 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() {
  observer.checkHidden("inlinesettings3@tests.mozilla.org");

  var addon = get_addon_element(gManagerWindow, "inlinesettings2@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() {
    observer.checkDisplayed("inlinesettings2@tests.mozilla.org");

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

    var node = settings[0];
    node = settings[0];
    is_element_hidden(node, "Unsupported settings should not be visible");
    ok(!node.hasAttribute("first-row"), "Hidden row is not the first row");

    node = settings[1];
    is(node.nodeName, "setting", "Should be a setting node");
    ok(node.hasAttribute("first-row"), "First visible row should have first-row attribute");
    var description = gManagerWindow.document.getAnonymousElementByAttribute(node, "class", "preferences-description");
    is(description.textContent, "Description Attribute", "Description node should contain description");

    node = settings[2];
    is(node.nodeName, "setting", "Should be a setting node");
    ok(!node.hasAttribute("first-row"), "Not the first row");
    description = gManagerWindow.document.getAnonymousElementByAttribute(node, "class", "preferences-description");
    is(description.textContent, "Description Text Node", "Description node should contain description");

    node = settings[3];
    is(node.nodeName, "setting", "Should be a setting node");
    ok(!node.hasAttribute("first-row"), "Not the first row");
    description = gManagerWindow.document.getAnonymousElementByAttribute(node, "class", "preferences-description");
    is(description.textContent, "This is a test, all this text should be visible", "Description node should contain description");
    var button = node.firstElementChild;
    isnot(button, null, "There should be a button");

    node = settings[4];
    is_element_hidden(node, "Unsupported settings should not be visible");
    ok(!node.hasAttribute("first-row"), "Hidden row is not the first row");

    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 non-inline preferences as optionsURL
add_test(function() {
  observer.checkHidden("inlinesettings2@tests.mozilla.org");

  var addon = get_addon_element(gManagerWindow, "noninlinesettings@tests.mozilla.org");
  addon.parentNode.ensureElementIsVisible(addon);

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

  wait_for_view_load(gManagerWindow, function() {
    observer.checkNotDisplayed();

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

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

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

// Addon with options.xul, disabling and enabling should hide and show settings UI
add_test(function() {
  observer.checkNotHidden();

  var addon = get_addon_element(gManagerWindow, "inlinesettings1@tests.mozilla.org");
  addon.parentNode.ensureElementIsVisible(addon);

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

  wait_for_view_load(gManagerWindow, function() {
    observer.checkDisplayed("inlinesettings1@tests.mozilla.org");
    is(gManagerWindow.gViewController.currentViewId,
       "addons://detail/inlinesettings1%40tests.mozilla.org",
       "Current view should not scroll to preferences");
    checkScrolling(false);

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

    // disable
    var button = gManagerWindow.document.getElementById("detail-disable-btn");
    button.scrollIntoView();
    EventUtils.synthesizeMouseAtCenter(button, { clickCount: 1 }, gManagerWindow);

    observer.checkHidden("inlinesettings1@tests.mozilla.org");

    settings = grid.querySelectorAll("rows > setting");
    is(settings.length, 0, "Grid should not have settings children");

    gCategoryUtilities.openType("extension", function() {
      var addon = get_addon_element(gManagerWindow, "inlinesettings1@tests.mozilla.org");
      addon.parentNode.ensureElementIsVisible(addon);

      var button = gManagerWindow.document.getAnonymousElementByAttribute(addon, "anonid", "preferences-btn");
      is_element_hidden(button, "Preferences button should not be visible");

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

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

        // enable
        var button = gManagerWindow.document.getElementById("detail-enable-btn");
        EventUtils.synthesizeMouseAtCenter(button, { clickCount: 1 }, gManagerWindow);

        observer.callback = function() {
          observer.checkDisplayed("inlinesettings1@tests.mozilla.org");

          settings = grid.querySelectorAll("rows > setting");
          is(settings.length, SETTINGS_ROWS, "Grid should have settings children");

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


// Addon with options.xul that requires a restart to disable,
// disabling and enabling should not hide and show settings UI.
add_test(function() {
  observer.checkHidden("inlinesettings1@tests.mozilla.org");

  var addon = get_addon_element(gManagerWindow, "inlinesettings2@tests.mozilla.org");
  addon.parentNode.ensureElementIsVisible(addon);

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

  wait_for_view_load(gManagerWindow, function() {
    observer.checkDisplayed("inlinesettings2@tests.mozilla.org");

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

    // disable
    var button = gManagerWindow.document.getElementById("detail-disable-btn");
    button.scrollIntoView();
    EventUtils.synthesizeMouseAtCenter(button, { clickCount: 1 }, gManagerWindow);
    observer.checkNotHidden();

    settings = grid.querySelectorAll("rows > setting");
    ok(settings.length > 0, "Grid should still have settings children");

    // cancel pending disable
    button = gManagerWindow.document.getElementById("detail-enable-btn");
    button.scrollIntoView();
    EventUtils.synthesizeMouseAtCenter(button, { clickCount: 1 }, gManagerWindow);
    observer.checkNotDisplayed();

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

// Tests bindings with existing prefs.
add_test(function() {
  observer.checkHidden("inlinesettings2@tests.mozilla.org");

  // Ensure these prefs are set. They should be set above, but somebody might
  // change the tests above.
  var profD = Services.dirsvc.get("ProfD", Ci.nsIFile);
  Services.prefs.setBoolPref("extensions.inlinesettings1.bool", false);
  Services.prefs.setIntPref("extensions.inlinesettings1.boolint", 1);
  Services.prefs.setIntPref("extensions.inlinesettings1.integer", 12);
  Preferences.set("extensions.inlinesettings1.string", "bar\u03DE/");
  Services.prefs.setCharPref("extensions.inlinesettings1.color", "#FF9900");
  Services.prefs.setCharPref("extensions.inlinesettings1.file", profD.path);
  Services.prefs.setCharPref("extensions.inlinesettings1.directory", profD.path);

  var addon = get_addon_element(gManagerWindow, "inlinesettings1@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() {
    observer.checkDisplayed("inlinesettings1@tests.mozilla.org");

    var grid = gManagerWindow.document.getElementById("detail-grid");
    var settings = grid.querySelectorAll("rows > setting");

    var input = gManagerWindow.document.getAnonymousElementByAttribute(settings[0], "anonid", "input");
    is(input.checked, false, "Checkbox should have initial value");

    input = gManagerWindow.document.getAnonymousElementByAttribute(settings[1], "anonid", "input");
    is(input.checked, true, "Checkbox should have initial value");

    input = gManagerWindow.document.getAnonymousElementByAttribute(settings[2], "anonid", "input");
    is(input.value, "12", "Number box should have initial value");

    input = gManagerWindow.document.getAnonymousElementByAttribute(settings[3], "anonid", "input");
    is(input.value, "bar\u03DE/", "Text box should have initial value");

    input = gManagerWindow.document.getAnonymousElementByAttribute(settings[5], "anonid", "input");
    is(input.color, "#FF9900", "Color picker should have initial value");

    input = gManagerWindow.document.getAnonymousElementByAttribute(settings[6], "anonid", "input");
    is(input.value, profD.path, "Label should have initial value");
    is(input.tooltipText, profD.path, "Label tooltip should have initial value");

    input = gManagerWindow.document.getAnonymousElementByAttribute(settings[7], "anonid", "input");
    is(input.value, profD.path, "Label value should have initial value");
    is(input.tooltipText, profD.path, "Label tooltip should have initial value");

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

// Tests bindings with existing prefs.
add_test(function() {
  observer.checkHidden("inlinesettings1@tests.mozilla.org");

  // Ensure these prefs are set. They should be set above, but somebody might
  // change the tests above.
  Services.prefs.setBoolPref("extensions.inlinesettings3.radioBool", false);
  Services.prefs.setIntPref("extensions.inlinesettings3.radioInt", 6);
  Preferences.set("extensions.inlinesettings3.radioString", "kilo \u338F");
  Services.prefs.setIntPref("extensions.inlinesettings3.menulist", 9);

  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() {
    observer.checkDisplayed("inlinesettings3@tests.mozilla.org");

    var grid = gManagerWindow.document.getElementById("detail-grid");
    var settings = grid.querySelectorAll("rows > setting");

    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");

    radios = settings[1].getElementsByTagName("radio");
    isnot(radios[0].selected, true, "Correct radio button should be selected");
    isnot(radios[1].selected, true, "Correct radio button should be selected");
    is(radios[2].selected, true, "Correct radio button should be selected");

    radios = settings[2].getElementsByTagName("radio");
    isnot(radios[0].selected, true, "Correct radio button should be selected");
    isnot(radios[1].selected, true, "Correct radio button should be selected");
    is(radios[2].selected, true, "Correct radio button should be selected");

    var input = settings[3].firstElementChild;
    is(input.value, "9", "Menulist should have initial value");

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