summaryrefslogtreecommitdiffstats
path: root/toolkit/profile/test/test_create_profile.xul
blob: 040b1256b2cf9c2bc72dbfcae4511be47baab728 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="chrome://global/skin"?>
<?xml-stylesheet type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=543854
-->
<window title="Mozilla Bug 543854"
        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
  <script type="application/javascript"
   src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
   
    <!-- test results are displayed in the html:body -->
  <body xmlns="http://www.w3.org/1999/xhtml">
  <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=543854"
     target="_blank">Mozilla Bug 543854</a>
  </body>

  <!-- test code goes here -->
  <script type="application/javascript">
  <![CDATA[

  /** Test for Bug 543854 **/

  SimpleTest.waitForExplicitFinish();

  const Cc = Components.classes;
  const Ci = Components.interfaces;

  const ASCIIName = "myprofile";
  const UnicodeName = "\u09A0\u09BE\u0995\u09C1\u09B0"; // A Bengali name

  var gDirService;
  var gIOService;
  var gProfileService;

  var gDefaultLocalProfileParent;

  gDirService = Cc["@mozilla.org/file/directory_service;1"].
    getService(Ci.nsIProperties);

  gIOService = Cc["@mozilla.org/network/io-service;1"].
    getService(Ci.nsIIOService);

  gProfileService = Cc["@mozilla.org/toolkit/profile-service;1"].
    getService(Ci.nsIToolkitProfileService);

  gDefaultLocalProfileParent = gDirService.get("DefProfLRt", Ci.nsIFile);

  createProfile(ASCIIName);
  createProfile(UnicodeName);
  SimpleTest.finish();

/**
 * Read the contents of an nsIFile. Throws on error.

 * @param file an nsIFile instance.
 * @return string contents.
 */
function readFile(file) {
  let fstream = Cc["@mozilla.org/network/file-input-stream;1"].
    createInstance(Ci.nsIFileInputStream);
  let sstream = Cc["@mozilla.org/scriptableinputstream;1"].
    createInstance(Components.interfaces.nsIScriptableInputStream);

  const RO = 0x01;
  const READ_OTHERS = 4;

  fstream.init(file, RO, READ_OTHERS, 0);
  sstream.init(fstream);
  let out = sstream.read(sstream.available());
  sstream.close();
  fstream.close();
  return out;
}

function checkBounds(lowerBound, value, upperBound) {
  ok(lowerBound <= value, "value " + value +
                          " is above lower bound " + lowerBound);
  ok(upperBound >= value, "value " + value +
                          " is within upper bound " + upperBound);
}

function createProfile(profileName) {
  // Filesystem precision is lower than Date precision.
  let lowerBound = Date.now() - 1000;

  let profile = gProfileService.createProfile(null, profileName);

  // check that the directory was created
  isnot(profile, null, "Profile " + profileName + " created");

  let profileDir = profile.rootDir;

  ok(profileDir.exists(), "Profile dir created");
  ok(profileDir.isDirectory(), "Profile dir is a directory");

  let profileDirPath = profileDir.path;

  is(profileDirPath.substr(profileDirPath.length - profileName.length),
     profileName, "Profile dir has expected name");

  // Ensure that our timestamp file was created.
  let jsonFile = profileDir.clone();
  jsonFile.append("times.json");
  ok(jsonFile.path, "Path is " + jsonFile.path);
  ok(jsonFile.exists(), "Times file was created");
  ok(jsonFile.isFile(), "Times file is a file");
  let json = JSON.parse(readFile(jsonFile));

  let upperBound = Date.now() + 1000;

  let created = json.created;
  ok(created, "created is set");

  // Check against real clock time.
  checkBounds(lowerBound, created, upperBound);

  // Clean up the profile before local profile test.
  profile.remove(true);

  // Create with non-null aRootDir
  profile = gProfileService.createProfile(profileDir, profileName);

  let localProfileDir = profile.localDir;
  ok(gDefaultLocalProfileParent.contains(localProfileDir, false),
    "Local profile dir created in DefProfLRt");

  // Clean up the profile.
  profile.remove(true);
}

  ]]>
  </script>
</window>