Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
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. */
6 const Cc = Components.classes;
7 const Ci = Components.interfaces;
8 const Cu = Components.utils;
9 const Cr = Components.results;
11 Cu.import("resource://gre/modules/Services.jsm", this);
13 const Telemetry = Cc["@mozilla.org/base/telemetry;1"].getService(Ci.nsITelemetry);
15 const LOCK_FILE_NAME = "Telemetry.FailedProfileLocks.txt";
16 const N_FAILED_LOCKS = 10;
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;
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 }
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 }
41 function run_test() {
42 do_get_profile();
44 do_check_eq(Telemetry.failedProfileLockCount, 0);
46 write_string_to_file(construct_file(), N_FAILED_LOCKS.toString());
48 // Make sure that we're not eagerly reading the count now that the
49 // file exists.
50 do_check_eq(Telemetry.failedProfileLockCount, 0);
52 do_test_pending();
53 Telemetry.asyncFetchTelemetryData(actual_test);
54 }
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 }