|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 http://creativecommons.org/publicdomain/zero/1.0/ |
|
3 */ |
|
4 /* A testcase to make sure reading the failed profile lock count works. */ |
|
5 |
|
6 const Cc = Components.classes; |
|
7 const Ci = Components.interfaces; |
|
8 const Cu = Components.utils; |
|
9 const Cr = Components.results; |
|
10 |
|
11 Cu.import("resource://gre/modules/Services.jsm", this); |
|
12 |
|
13 const Telemetry = Cc["@mozilla.org/base/telemetry;1"].getService(Ci.nsITelemetry); |
|
14 |
|
15 const LOCK_FILE_NAME = "Telemetry.FailedProfileLocks.txt"; |
|
16 const N_FAILED_LOCKS = 10; |
|
17 |
|
18 // Constants from prio.h for nsIFileOutputStream.init |
|
19 const PR_WRONLY = 0x2; |
|
20 const PR_CREATE_FILE = 0x8; |
|
21 const PR_TRUNCATE = 0x20; |
|
22 const RW_OWNER = 0600; |
|
23 |
|
24 function write_string_to_file(file, contents) { |
|
25 let ostream = Cc["@mozilla.org/network/safe-file-output-stream;1"] |
|
26 .createInstance(Ci.nsIFileOutputStream); |
|
27 ostream.init(file, PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE, |
|
28 RW_OWNER, ostream.DEFER_OPEN); |
|
29 ostream.write(contents, contents.length); |
|
30 ostream.QueryInterface(Ci.nsISafeOutputStream).finish(); |
|
31 ostream.close(); |
|
32 } |
|
33 |
|
34 function construct_file() { |
|
35 let profileDirectory = Services.dirsvc.get("ProfD", Ci.nsIFile); |
|
36 let file = profileDirectory.clone(); |
|
37 file.append(LOCK_FILE_NAME); |
|
38 return file; |
|
39 } |
|
40 |
|
41 function run_test() { |
|
42 do_get_profile(); |
|
43 |
|
44 do_check_eq(Telemetry.failedProfileLockCount, 0); |
|
45 |
|
46 write_string_to_file(construct_file(), N_FAILED_LOCKS.toString()); |
|
47 |
|
48 // Make sure that we're not eagerly reading the count now that the |
|
49 // file exists. |
|
50 do_check_eq(Telemetry.failedProfileLockCount, 0); |
|
51 |
|
52 do_test_pending(); |
|
53 Telemetry.asyncFetchTelemetryData(actual_test); |
|
54 } |
|
55 |
|
56 function actual_test() { |
|
57 do_check_eq(Telemetry.failedProfileLockCount, N_FAILED_LOCKS); |
|
58 do_check_false(construct_file().exists()); |
|
59 do_test_finished(); |
|
60 } |