1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/xpconnect/tests/unit/test_file2.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,69 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +const Ci = Components.interfaces; 1.9 + 1.10 +function run_test() { 1.11 + // throw if anything goes wrong 1.12 + 1.13 + // find the current directory path 1.14 + var file = Components.classes["@mozilla.org/file/directory_service;1"] 1.15 + .getService(Ci.nsIProperties) 1.16 + .get("CurWorkD", Ci.nsIFile); 1.17 + file.append("xpcshell.ini"); 1.18 + 1.19 + // should be able to construct a file 1.20 + var f1 = File(file.path); 1.21 + // with either constructor syntax 1.22 + var f2 = new File(file.path); 1.23 + // and with nsIFiles 1.24 + var f3 = File(file); 1.25 + var f4 = new File(file); 1.26 + 1.27 + // do some tests 1.28 + do_check_true(f1 instanceof Ci.nsIDOMFile, "Should be a DOM File"); 1.29 + do_check_true(f2 instanceof Ci.nsIDOMFile, "Should be a DOM File"); 1.30 + do_check_true(f3 instanceof Ci.nsIDOMFile, "Should be a DOM File"); 1.31 + do_check_true(f4 instanceof Ci.nsIDOMFile, "Should be a DOM File"); 1.32 + 1.33 + do_check_true(f1.name == "xpcshell.ini", "Should be the right file"); 1.34 + do_check_true(f2.name == "xpcshell.ini", "Should be the right file"); 1.35 + do_check_true(f3.name == "xpcshell.ini", "Should be the right file"); 1.36 + do_check_true(f4.name == "xpcshell.ini", "Should be the right file"); 1.37 + 1.38 + do_check_true(f1.type == "", "Should be the right type"); 1.39 + do_check_true(f2.type == "", "Should be the right type"); 1.40 + do_check_true(f3.type == "", "Should be the right type"); 1.41 + do_check_true(f4.type == "", "Should be the right type"); 1.42 + 1.43 + var threw = false; 1.44 + try { 1.45 + // Needs a ctor argument 1.46 + var f7 = File(); 1.47 + } catch (e) { 1.48 + threw = true; 1.49 + } 1.50 + do_check_true(threw, "No ctor arguments should throw"); 1.51 + 1.52 + var threw = false; 1.53 + try { 1.54 + // Needs a valid ctor argument 1.55 + var f7 = File(Date(132131532)); 1.56 + } catch (e) { 1.57 + threw = true; 1.58 + } 1.59 + do_check_true(threw, "Passing a random object should fail"); 1.60 + 1.61 + var threw = false 1.62 + try { 1.63 + // Directories fail 1.64 + var dir = Components.classes["@mozilla.org/file/directory_service;1"] 1.65 + .getService(Ci.nsIProperties) 1.66 + .get("CurWorkD", Ci.nsIFile); 1.67 + var f7 = File(dir) 1.68 + } catch (e) { 1.69 + threw = true; 1.70 + } 1.71 + do_check_true(threw, "Can't create a File object for a directory"); 1.72 +}