summaryrefslogtreecommitdiffstats
path: root/mobile/android/tests/browser/robocop/testFilePicker.js
blob: 69be415a531bf0f7fdd0ecc41504f9d196961296 (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
// -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

const { classes: Cc, interfaces: Ci, utils: Cu } = Components;

Cu.import("resource://gre/modules/Services.jsm");

add_test(function filepicker_open() {
  let chromeWin = Services.wm.getMostRecentWindow("navigator:browser");

  do_test_pending();

  let fp = Cc["@mozilla.org/filepicker;1"].createInstance(Ci.nsIFilePicker);
  fp.appendFilter("Martian files", "*.martian");
  fp.appendFilters(Ci.nsIFilePicker.filterAll);
  fp.filterIndex = 0;

  let fpCallback = function(result) {
    if (result == Ci.nsIFilePicker.returnOK || result == Ci.nsIFilePicker.returnReplace) {
      do_print("File: " + fp.file.path);
      is(fp.file.path, "/mnt/sdcard/my-favorite-martian.png", "Retrieve the right martian file!");

      let files = fp.files;
      while (files.hasMoreElements()) {
        let file = files.getNext().QueryInterface(Ci.nsIFile);
        do_print("File: " + file.path);
        is(file.path, "/mnt/sdcard/my-favorite-martian.png", "Retrieve the right martian file from array!");
      }

      let file = fp.domFileOrDirectory;
      do_print("DOMFile: " + file.mozFullPath);
      is(file.mozFullPath, "/mnt/sdcard/my-favorite-martian.png", "Retrieve the right martian DOM File!");

      let e = fp.domFileOrDirectoryEnumerator;
      while (e.hasMoreElements()) {
        let file = e.getNext();
        do_print("DOMFile: " + file.mozFullPath);
        is(file.mozFullPath, "/mnt/sdcard/my-favorite-martian.png", "Retrieve the right martian file from domFileOrDirectoryEnumerator array!");
      }

      do_test_finished();

      run_next_test();
    }
  };

  try {
    fp.init(chromeWin, "Open", Ci.nsIFilePicker.modeOpen);
  } catch(ex) {
    ok(false, "Android should support FilePicker.modeOpen: " + ex);
  }
  fp.open(fpCallback);
});

add_test(function filepicker_save() {
  let failed = false;
  let fp = Cc["@mozilla.org/filepicker;1"].createInstance(Ci.nsIFilePicker);
  try {
    fp.init(null, "Save", Ci.nsIFilePicker.modeSave);
  } catch(ex) {
    failed = true;
  }
  ok(failed, "Android does not support FilePicker.modeSave");

  run_next_test();
});

run_next_test();