toolkit/profile/test/test_create_profile.xul

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/profile/test/test_create_profile.xul	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,134 @@
     1.4 +<?xml version="1.0"?>
     1.5 +<?xml-stylesheet type="text/css" href="chrome://global/skin"?>
     1.6 +<?xml-stylesheet type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?>
     1.7 +<!--
     1.8 +https://bugzilla.mozilla.org/show_bug.cgi?id=543854
     1.9 +-->
    1.10 +<window title="Mozilla Bug 543854"
    1.11 +        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
    1.12 +  <script type="application/javascript"
    1.13 +   src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
    1.14 +   
    1.15 +    <!-- test results are displayed in the html:body -->
    1.16 +  <body xmlns="http://www.w3.org/1999/xhtml">
    1.17 +  <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=543854"
    1.18 +     target="_blank">Mozilla Bug 543854</a>
    1.19 +  </body>
    1.20 +
    1.21 +  <!-- test code goes here -->
    1.22 +  <script type="application/javascript">
    1.23 +  <![CDATA[
    1.24 +
    1.25 +  /** Test for Bug 543854 **/
    1.26 +
    1.27 +  SimpleTest.waitForExplicitFinish();
    1.28 +
    1.29 +  const Cc = Components.classes;
    1.30 +  const Ci = Components.interfaces;
    1.31 +
    1.32 +  const ASCIIName = "myprofile";
    1.33 +  const UnicodeName = "\u09A0\u09BE\u0995\u09C1\u09B0"; // A Bengali name
    1.34 +
    1.35 +  var gDirService;
    1.36 +  var gIOService;
    1.37 +  var gProfileService;
    1.38 +
    1.39 +  var gDefaultLocalProfileParent;
    1.40 +
    1.41 +  gDirService = Cc["@mozilla.org/file/directory_service;1"].
    1.42 +    getService(Ci.nsIProperties);
    1.43 +
    1.44 +  gIOService = Cc["@mozilla.org/network/io-service;1"].
    1.45 +    getService(Ci.nsIIOService);
    1.46 +
    1.47 +  gProfileService = Cc["@mozilla.org/toolkit/profile-service;1"].
    1.48 +    getService(Ci.nsIToolkitProfileService);
    1.49 +
    1.50 +  gDefaultLocalProfileParent = gDirService.get("DefProfLRt", Ci.nsIFile);
    1.51 +
    1.52 +  createProfile(ASCIIName);
    1.53 +  createProfile(UnicodeName);
    1.54 +  SimpleTest.finish();
    1.55 +
    1.56 +/**
    1.57 + * Read the contents of an nsIFile. Throws on error.
    1.58 +
    1.59 + * @param file an nsIFile instance.
    1.60 + * @return string contents.
    1.61 + */
    1.62 +function readFile(file) {
    1.63 +  let fstream = Cc["@mozilla.org/network/file-input-stream;1"].
    1.64 +    createInstance(Ci.nsIFileInputStream);
    1.65 +  let sstream = Cc["@mozilla.org/scriptableinputstream;1"].
    1.66 +    createInstance(Components.interfaces.nsIScriptableInputStream);
    1.67 +
    1.68 +  const RO = 0x01;
    1.69 +  const READ_OTHERS = 4;
    1.70 +
    1.71 +  fstream.init(file, RO, READ_OTHERS, 0);
    1.72 +  sstream.init(fstream);
    1.73 +  let out = sstream.read(sstream.available());
    1.74 +  sstream.close();
    1.75 +  fstream.close();
    1.76 +  return out;
    1.77 +}
    1.78 +
    1.79 +function checkBounds(lowerBound, value, upperBound) {
    1.80 +  ok(lowerBound <= value, "value " + value +
    1.81 +                          " is above lower bound " + lowerBound);
    1.82 +  ok(upperBound >= value, "value " + value +
    1.83 +                          " is within upper bound " + upperBound);
    1.84 +}
    1.85 +
    1.86 +function createProfile(profileName) {
    1.87 +  // Filesystem precision is lower than Date precision.
    1.88 +  let lowerBound = Date.now() - 1000;
    1.89 +
    1.90 +  let profile = gProfileService.createProfile(null, profileName);
    1.91 +
    1.92 +  // check that the directory was created
    1.93 +  isnot(profile, null, "Profile " + profileName + " created");
    1.94 +
    1.95 +  let profileDir = profile.rootDir;
    1.96 +
    1.97 +  ok(profileDir.exists(), "Profile dir created");
    1.98 +  ok(profileDir.isDirectory(), "Profile dir is a directory");
    1.99 +
   1.100 +  let profileDirPath = profileDir.path;
   1.101 +
   1.102 +  is(profileDirPath.substr(profileDirPath.length - profileName.length),
   1.103 +     profileName, "Profile dir has expected name");
   1.104 +
   1.105 +  // Ensure that our timestamp file was created.
   1.106 +  let jsonFile = profileDir.clone();
   1.107 +  jsonFile.append("times.json");
   1.108 +  ok(jsonFile.path, "Path is " + jsonFile.path);
   1.109 +  ok(jsonFile.exists(), "Times file was created");
   1.110 +  ok(jsonFile.isFile(), "Times file is a file");
   1.111 +  let json = JSON.parse(readFile(jsonFile));
   1.112 +
   1.113 +  let upperBound = Date.now() + 1000;
   1.114 +
   1.115 +  let created = json.created;
   1.116 +  ok(created, "created is set");
   1.117 +
   1.118 +  // Check against real clock time.
   1.119 +  checkBounds(lowerBound, created, upperBound);
   1.120 +
   1.121 +  // Clean up the profile before local profile test.
   1.122 +  profile.remove(true);
   1.123 +
   1.124 +  // Create with non-null aRootDir
   1.125 +  let profile = gProfileService.createProfile(profileDir, profileName);
   1.126 +
   1.127 +  let localProfileDir = profile.localDir;
   1.128 +  ok(gDefaultLocalProfileParent.contains(localProfileDir, false),
   1.129 +    "Local profile dir created in DefProfLRt");
   1.130 +
   1.131 +  // Clean up the profile.
   1.132 +  profile.remove(true);
   1.133 +}
   1.134 +
   1.135 +  ]]>
   1.136 +  </script>
   1.137 +</window>

mercurial