michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ michael@0: */ michael@0: /* A testcase to make sure reading the failed profile lock count works. */ michael@0: michael@0: const Cc = Components.classes; michael@0: const Ci = Components.interfaces; michael@0: const Cu = Components.utils; michael@0: const Cr = Components.results; michael@0: michael@0: Cu.import("resource://gre/modules/Services.jsm", this); michael@0: michael@0: const Telemetry = Cc["@mozilla.org/base/telemetry;1"].getService(Ci.nsITelemetry); michael@0: michael@0: const LOCK_FILE_NAME = "Telemetry.FailedProfileLocks.txt"; michael@0: const N_FAILED_LOCKS = 10; michael@0: michael@0: // Constants from prio.h for nsIFileOutputStream.init michael@0: const PR_WRONLY = 0x2; michael@0: const PR_CREATE_FILE = 0x8; michael@0: const PR_TRUNCATE = 0x20; michael@0: const RW_OWNER = 0600; michael@0: michael@0: function write_string_to_file(file, contents) { michael@0: let ostream = Cc["@mozilla.org/network/safe-file-output-stream;1"] michael@0: .createInstance(Ci.nsIFileOutputStream); michael@0: ostream.init(file, PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE, michael@0: RW_OWNER, ostream.DEFER_OPEN); michael@0: ostream.write(contents, contents.length); michael@0: ostream.QueryInterface(Ci.nsISafeOutputStream).finish(); michael@0: ostream.close(); michael@0: } michael@0: michael@0: function construct_file() { michael@0: let profileDirectory = Services.dirsvc.get("ProfD", Ci.nsIFile); michael@0: let file = profileDirectory.clone(); michael@0: file.append(LOCK_FILE_NAME); michael@0: return file; michael@0: } michael@0: michael@0: function run_test() { michael@0: do_get_profile(); michael@0: michael@0: do_check_eq(Telemetry.failedProfileLockCount, 0); michael@0: michael@0: write_string_to_file(construct_file(), N_FAILED_LOCKS.toString()); michael@0: michael@0: // Make sure that we're not eagerly reading the count now that the michael@0: // file exists. michael@0: do_check_eq(Telemetry.failedProfileLockCount, 0); michael@0: michael@0: do_test_pending(); michael@0: Telemetry.asyncFetchTelemetryData(actual_test); michael@0: } michael@0: michael@0: function actual_test() { michael@0: do_check_eq(Telemetry.failedProfileLockCount, N_FAILED_LOCKS); michael@0: do_check_false(construct_file().exists()); michael@0: do_test_finished(); michael@0: }