toolkit/profile/test/test_create_profile.xul

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 <?xml version="1.0"?>
michael@0 2 <?xml-stylesheet type="text/css" href="chrome://global/skin"?>
michael@0 3 <?xml-stylesheet type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?>
michael@0 4 <!--
michael@0 5 https://bugzilla.mozilla.org/show_bug.cgi?id=543854
michael@0 6 -->
michael@0 7 <window title="Mozilla Bug 543854"
michael@0 8 xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
michael@0 9 <script type="application/javascript"
michael@0 10 src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
michael@0 11
michael@0 12 <!-- test results are displayed in the html:body -->
michael@0 13 <body xmlns="http://www.w3.org/1999/xhtml">
michael@0 14 <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=543854"
michael@0 15 target="_blank">Mozilla Bug 543854</a>
michael@0 16 </body>
michael@0 17
michael@0 18 <!-- test code goes here -->
michael@0 19 <script type="application/javascript">
michael@0 20 <![CDATA[
michael@0 21
michael@0 22 /** Test for Bug 543854 **/
michael@0 23
michael@0 24 SimpleTest.waitForExplicitFinish();
michael@0 25
michael@0 26 const Cc = Components.classes;
michael@0 27 const Ci = Components.interfaces;
michael@0 28
michael@0 29 const ASCIIName = "myprofile";
michael@0 30 const UnicodeName = "\u09A0\u09BE\u0995\u09C1\u09B0"; // A Bengali name
michael@0 31
michael@0 32 var gDirService;
michael@0 33 var gIOService;
michael@0 34 var gProfileService;
michael@0 35
michael@0 36 var gDefaultLocalProfileParent;
michael@0 37
michael@0 38 gDirService = Cc["@mozilla.org/file/directory_service;1"].
michael@0 39 getService(Ci.nsIProperties);
michael@0 40
michael@0 41 gIOService = Cc["@mozilla.org/network/io-service;1"].
michael@0 42 getService(Ci.nsIIOService);
michael@0 43
michael@0 44 gProfileService = Cc["@mozilla.org/toolkit/profile-service;1"].
michael@0 45 getService(Ci.nsIToolkitProfileService);
michael@0 46
michael@0 47 gDefaultLocalProfileParent = gDirService.get("DefProfLRt", Ci.nsIFile);
michael@0 48
michael@0 49 createProfile(ASCIIName);
michael@0 50 createProfile(UnicodeName);
michael@0 51 SimpleTest.finish();
michael@0 52
michael@0 53 /**
michael@0 54 * Read the contents of an nsIFile. Throws on error.
michael@0 55
michael@0 56 * @param file an nsIFile instance.
michael@0 57 * @return string contents.
michael@0 58 */
michael@0 59 function readFile(file) {
michael@0 60 let fstream = Cc["@mozilla.org/network/file-input-stream;1"].
michael@0 61 createInstance(Ci.nsIFileInputStream);
michael@0 62 let sstream = Cc["@mozilla.org/scriptableinputstream;1"].
michael@0 63 createInstance(Components.interfaces.nsIScriptableInputStream);
michael@0 64
michael@0 65 const RO = 0x01;
michael@0 66 const READ_OTHERS = 4;
michael@0 67
michael@0 68 fstream.init(file, RO, READ_OTHERS, 0);
michael@0 69 sstream.init(fstream);
michael@0 70 let out = sstream.read(sstream.available());
michael@0 71 sstream.close();
michael@0 72 fstream.close();
michael@0 73 return out;
michael@0 74 }
michael@0 75
michael@0 76 function checkBounds(lowerBound, value, upperBound) {
michael@0 77 ok(lowerBound <= value, "value " + value +
michael@0 78 " is above lower bound " + lowerBound);
michael@0 79 ok(upperBound >= value, "value " + value +
michael@0 80 " is within upper bound " + upperBound);
michael@0 81 }
michael@0 82
michael@0 83 function createProfile(profileName) {
michael@0 84 // Filesystem precision is lower than Date precision.
michael@0 85 let lowerBound = Date.now() - 1000;
michael@0 86
michael@0 87 let profile = gProfileService.createProfile(null, profileName);
michael@0 88
michael@0 89 // check that the directory was created
michael@0 90 isnot(profile, null, "Profile " + profileName + " created");
michael@0 91
michael@0 92 let profileDir = profile.rootDir;
michael@0 93
michael@0 94 ok(profileDir.exists(), "Profile dir created");
michael@0 95 ok(profileDir.isDirectory(), "Profile dir is a directory");
michael@0 96
michael@0 97 let profileDirPath = profileDir.path;
michael@0 98
michael@0 99 is(profileDirPath.substr(profileDirPath.length - profileName.length),
michael@0 100 profileName, "Profile dir has expected name");
michael@0 101
michael@0 102 // Ensure that our timestamp file was created.
michael@0 103 let jsonFile = profileDir.clone();
michael@0 104 jsonFile.append("times.json");
michael@0 105 ok(jsonFile.path, "Path is " + jsonFile.path);
michael@0 106 ok(jsonFile.exists(), "Times file was created");
michael@0 107 ok(jsonFile.isFile(), "Times file is a file");
michael@0 108 let json = JSON.parse(readFile(jsonFile));
michael@0 109
michael@0 110 let upperBound = Date.now() + 1000;
michael@0 111
michael@0 112 let created = json.created;
michael@0 113 ok(created, "created is set");
michael@0 114
michael@0 115 // Check against real clock time.
michael@0 116 checkBounds(lowerBound, created, upperBound);
michael@0 117
michael@0 118 // Clean up the profile before local profile test.
michael@0 119 profile.remove(true);
michael@0 120
michael@0 121 // Create with non-null aRootDir
michael@0 122 let profile = gProfileService.createProfile(profileDir, profileName);
michael@0 123
michael@0 124 let localProfileDir = profile.localDir;
michael@0 125 ok(gDefaultLocalProfileParent.contains(localProfileDir, false),
michael@0 126 "Local profile dir created in DefProfLRt");
michael@0 127
michael@0 128 // Clean up the profile.
michael@0 129 profile.remove(true);
michael@0 130 }
michael@0 131
michael@0 132 ]]>
michael@0 133 </script>
michael@0 134 </window>

mercurial