dom/html/test/forms/test_input_file_picker.html
author Julian Descottes <jdescottes@mozilla.com>
Tue, 23 Oct 2018 07:13:35 +0000
changeset 490876 dfaaf3978e3c7f1c98fd20270a0e50e4a6ddd733
parent 448919 50d6fe2d0bca50837dd34eb4a79957885c55bffc
permissions -rw-r--r--
Bug 1499096 - Use ok() with 2 arguments instead of 3 when possible;r=Standard8 Depends on D8740. This changeset replaces calls to ok with 3 arguments to calls with 2 arguments in situations where the switch does not have a significant impact on the assert. Differential Revision: https://phabricator.services.mozilla.com/D8741

<!DOCTYPE HTML>
<html>
<head>
  <title>Test for &lt;input type='file'&gt; file picker</title>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="application/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=377624">Mozilla Bug 36619</a>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=377624">Mozilla Bug 377624</a>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=565274">Mozilla Bug 565274</a>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=701353">Mozilla Bug 701353</a>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=826176">Mozilla Bug 826176</a>
<p id="display"></p>
<div id="content">
  <input id='a' type='file' accept="image/*">
  <input id='b' type='file' accept="audio/*">
  <input id='c' type='file' accept="video/*">
  <input id='d' type='file' accept="image/*, audio/* ">
  <input id='e' type='file' accept=" image/*,video/*">
  <input id='f' type='file' accept="audio/*,video/*">
  <input id='g' type='file' accept="image/*, audio/* ,video/*">
  <input id='h' type='file' accept="foo/baz,image/*,bogus/duh">
  <input id='i' type='file' accept="mime/type;parameter,video/*">
  <input id='j' type='file' accept="audio/*, audio/*, audio/*">
  <input id='k' type="file" accept="image/gif,image/png">
  <input id='l' type="file" accept="image/*,image/gif,image/png">
  <input id='m' type="file" accept="image/gif,image/gif">
  <input id='n' type="file" accept="">
  <input id='o' type="file" accept=".test">
  <input id='p' type="file" accept="image/gif,.csv">
  <input id='q' type="file" accept="image/gif,.gif">
  <input id='r' type="file" accept=".prefix,.prefixPlusSomething">
  <input id='s' type="file" accept=".xls,.xlsx">
  <input id='t' type="file" accept=".mp3,.wav,.flac">
  <input id='u' type="file" accept=".xls, .xlsx">
  <input id='v' type="file" accept=".xlsx,  .xls">
  <input id='w' type="file" accept=".xlsx;  .xls">
  <input id='x' type="file" accept=".xls,  .xlsx">
  <input id='y' type="file" accept=".xlsx, .xls">
  <input id='z' type='file' accept="i/am,a,pathological,;,,,,test/case">
  <input id='A' type="file" accept=".xlsx, .xls*">
  <input id='mix-ref' type="file" accept="image/jpeg">
  <input id='mix' type="file" accept="image/jpeg,.jpg">
  <input id='hidden' hidden type='file'>
  <input id='untrusted-click' type='file'>
  <input id='prevent-default' type='file'>
  <input id='prevent-default-false' type='file'>
  <input id='right-click' type='file'>
  <input id='middle-click' type='file'>
  <input id='left-click' type='file'>
  <label id='label-1'>foo<input type='file'></label>
  <label id='label-2' for='labeled-2'>foo</label><input id='labeled-2' type='file'></label>
  <label id='label-3'>foo<input type='file'></label>
  <label id='label-4' for='labeled-4'>foo</label><input id='labeled-4' type='file'></label>
  <input id='by-button' type='file'>
  <button id='button-click' onclick="document.getElementById('by-button').click();">foo</button>
  <button id='button-down' onclick="document.getElementById('by-button').click();">foo</button>
  <button id='button-up' onclick="document.getElementById('by-button').click();">foo</button>
  <div id='div-click' onclick="document.getElementById('by-button').click();" tabindex='1'>foo</div>
  <div id='div-click-on-demand' onclick="var i=document.createElement('input'); i.type='file'; i.click();" tabindex='1'>foo</div>
  <div id='div-keydown' onkeydown="document.getElementById('by-button').click();" tabindex='1'>foo</div>
  <a id='link-click' href="javascript:document.getElementById('by-button').click();" tabindex='1'>foo</a>
</div>
<pre id="test">
<script type="application/javascript">

/**
 * This test checks various scenarios and make sure that a file picker is being
 * shown in all of them (minus a few exceptions).
 * |testData| defines the tests to do and |launchNextTest| can be used to have
 * specific behaviour for some tests. Everything else should just work.
 */

SimpleTest.waitForExplicitFinish();
SimpleTest.requestFlakyTimeout("untriaged");

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

// The following lists are from toolkit/content/filepicker.properties which is used by filePicker
var imageExtensionList = "*.jpe; *.jpg; *.jpeg; *.gif; *.png; *.bmp; *.ico; *.svg; *.svgz; *.tif; *.tiff; *.ai; *.drw; *.pct; *.psp; *.xcf; *.psd; *.raw"
var audioExtensionList = "*.aac; *.aif; *.flac; *.iff; *.m4a; *.m4b; *.mid; *.midi; *.mp3; *.mpa; *.mpc; *.oga; *.ogg; *.ra; *.ram; *.snd; *.wav; *.wma"
var videoExtensionList = "*.avi; *.divx; *.flv; *.m4v; *.mkv; *.mov; *.mp4; *.mpeg; *.mpg; *.ogm; *.ogv; *.ogx; *.rm; *.rmvb; *.smil; *.webm; *.wmv; *.xvid"

// [ element name | number of filters | extension list or filter mask | filter index ]
var testData = [["a", 1, MockFilePicker.filterImages, 1],
                ["b", 1, MockFilePicker.filterAudio, 1],
                ["c", 1, MockFilePicker.filterVideo, 1],
                ["d", 3, imageExtensionList + "; " + audioExtensionList, 1],
                ["e", 3, imageExtensionList + "; " + videoExtensionList, 1],
                ["f", 3, audioExtensionList + "; " + videoExtensionList, 1],
                ["g", 4, imageExtensionList + "; " + audioExtensionList + "; " + videoExtensionList, 1],
                ["h", 1, MockFilePicker.filterImages, 0],
                ["i", 1, MockFilePicker.filterVideo, 0],
                ["j", 1, MockFilePicker.filterAudio, 1],
                ["k", 3, "*.gif; *.png", 1],
                ["l", 4, imageExtensionList + "; " + "*.gif; *.png", 1],
                ["m", 1, "*.gif", 1],
                ["n", 0, undefined, 0],
                ["o", 1, "*.test", 1],
                ["p", 3, "*.gif; *.csv", 1],
                ["q", 1, "*.gif", 1],
                ["r", 3, "*.prefix; *.prefixPlusSomething", 1],
                ["s", 3, "*.xls; *.xlsx", 1],
                ["t", 4, "*.mp3; *.wav; *.flac", 1],
                ["u", 3, "*.xls; *.xlsx", 1],
                ["v", 3, "*.xlsx; *.xls", 1],
                ["w", 0, undefined, 0],
                ["x", 3, "*.xls; *.xlsx", 1],
                ["y", 3, "*.xlsx; *.xls", 1],
                ["z", 0, undefined, 0],
                ["A", 1, "*.xlsx", 1],
                // Note: mix and mix-ref tests extension lists are checked differently: see SimpleTest.executeSoon below
                ["mix-ref", undefined, undefined, undefined],
                ["mix", 1, undefined, 1],
                ["hidden", 0, undefined, 0],
                ["untrusted-click", 0, undefined, 0],
                ["prevent-default", 0, undefined, 0, true],
                ["prevent-default-false", 0, undefined, 0, true],
                ["right-click", 0, undefined, 0, true],
                ["middle-click", 0, undefined, 0, true],
                ["left-click", 0, undefined, 0],
                ["label-1", 0, undefined, 0],
                ["label-2", 0, undefined, 0],
                ["label-3", 0, undefined, 0],
                ["label-4", 0, undefined, 0],
                ["button-click", 0, undefined, 0],
                ["button-down", 0, undefined, 0],
                ["button-up", 0, undefined, 0],
                ["div-click", 0, undefined, 0],
                ["div-click-on-demand", 0, undefined, 0],
                ["div-keydown", 0, undefined, 0],
                ["link-click", 0, undefined, 0],
               ];

var currentTest = 0;
var filterAllAdded;
var filters;
var filterIndex;
var mixRefExtensionList;

// Make sure picker works with popup blocker enabled and no allowed events
SpecialPowers.pushPrefEnv({'set': [["dom.popup_allowed_events", ""]]}, runTests);

function launchNextTest() {
  MockFilePicker.shown = false;
  filterAllAdded = false;
  filters = [];
  filterIndex = 0;

  // Focusing the element will scroll them into view so making sure the clicks
  // will work.
  document.getElementById(testData[currentTest][0]).focus();

  if (testData[currentTest][0] == "untrusted-click") {
    var e = document.createEvent('MouseEvents');
    e.initEvent('click', true, false);
    document.getElementById(testData[currentTest][0]).dispatchEvent(e);
  // All tests that should *NOT* show a file picker.
  } else if (testData[currentTest][0] == "prevent-default" ||
             testData[currentTest][0] == "prevent-default-false" ||
             testData[currentTest][0] == "right-click" ||
             testData[currentTest][0] == "middle-click") {
    if (testData[currentTest][0] == "right-click" ||
        testData[currentTest][0] == "middle-click") {
      var b = testData[currentTest][0] == "middle-click" ? 1 : 2;
      synthesizeMouseAtCenter(document.getElementById(testData[currentTest][0]),
                              { button: b });
    } else {
      if (testData[currentTest][0] == "prevent-default-false") {
        document.getElementById(testData[currentTest][0]).onclick = function() {
          return false;
        };
      } else {
        document.getElementById(testData[currentTest][0]).onclick = function(e) {
          e.preventDefault();
        };
      }
      document.getElementById(testData[currentTest][0]).click();
    }

    // Wait a bit and assume we can continue. If the file picker shows later,
    // behaviour is uncertain but that would be a random green, no big deal...
    setTimeout(function() {
      ok(true, "we should be there without a file picker being opened");
      ++currentTest;
      launchNextTest();
    }, 500);
  } else if (testData[currentTest][0] == 'label-3' ||
             testData[currentTest][0] == 'label-4') {
    synthesizeMouse(document.getElementById(testData[currentTest][0]), 5, 5, {});
  } else if (testData[currentTest][0] == 'button-click' ||
             testData[currentTest][0] == 'button-down' ||
             testData[currentTest][0] == 'button-up' ||
             testData[currentTest][0] == 'div-click' ||
             testData[currentTest][0] == 'div-click-on-demand' ||
             testData[currentTest][0] == 'link-click') {
    synthesizeMouseAtCenter(document.getElementById(testData[currentTest][0]), {});
  } else if (testData[currentTest][0] == 'div-keydown') {
    sendString("a");
  } else {
    document.getElementById(testData[currentTest][0]).click();
  }
}

function runTests() {
  MockFilePicker.appendFilterCallback = function(filepicker, title, val) {
    filters.push(val);
  };
  MockFilePicker.appendFiltersCallback = function(filepicker, val) {
    if (val === MockFilePicker.filterAll) {
      filterAllAdded = true;
    } else {
      filters.push(val);
    }
  };
  MockFilePicker.showCallback = function(filepicker) {
    if (testData[currentTest][4]) {
      ok(false, "we shouldn't have a file picker showing!");
      return;
    }

    filterIndex = filepicker.filterIndex;
    testName = testData[currentTest][0];
    SimpleTest.executeSoon(function () {
      ok(MockFilePicker.shown,
         "File picker show method should have been called (" + testName + ")");
      ok(filterAllAdded,
         "filterAll is missing (" + testName + ")");
      if (testName == "mix-ref") {
        // Used only for reference for next test: nothing to be tested here
        mixRefExtensionList = filters[0];
        if (mixRefExtensionList == undefined) {
          mixRefExtensionList = "";
        }
      } else {
        if (testName == "mix") {
          // Mixing mime type and file extension filters ("image/jpeg" and
          // ".jpg" here) shouldn't restrict the list but only extend it, if file
          // extension filter isn't a duplicate
          ok(filters[0].includes(mixRefExtensionList),
             "Mixing mime types and file extension filters shouldn't restrict extension list: " +
             mixRefExtensionList + " | " + filters[0]);
          ok(filters[0].includes("*.jpg"),
             "Filter should contain '.jpg' extension. Filter was:" + filters[0]);
        } else {
          is(filters[0], testData[currentTest][2],
             "Correct filters should have been added (" + testName + ")");
          is(filters.length, testData[currentTest][1],
             "appendFilters not called as often as expected (" + testName + ")");
        }
        is(filterIndex, testData[currentTest][3],
           "File picker should show the correct filter index (" + testName + ")");
      }

      if (++currentTest == testData.length) {
        MockFilePicker.cleanup();
        SimpleTest.finish();
      } else {
        launchNextTest();
      }
    });
  };

  launchNextTest();
}

</script>
</pre>
</body>
</html>