toolkit/components/telemetry/tests/unit/test_TelemetryLockCount.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/telemetry/tests/unit/test_TelemetryLockCount.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,60 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 +   http://creativecommons.org/publicdomain/zero/1.0/ 
     1.6 +*/
     1.7 +/* A testcase to make sure reading the failed profile lock count works.  */
     1.8 +
     1.9 +const Cc = Components.classes;
    1.10 +const Ci = Components.interfaces;
    1.11 +const Cu = Components.utils;
    1.12 +const Cr = Components.results;
    1.13 +
    1.14 +Cu.import("resource://gre/modules/Services.jsm", this);
    1.15 +
    1.16 +const Telemetry = Cc["@mozilla.org/base/telemetry;1"].getService(Ci.nsITelemetry);
    1.17 +
    1.18 +const LOCK_FILE_NAME = "Telemetry.FailedProfileLocks.txt";
    1.19 +const N_FAILED_LOCKS = 10;
    1.20 +
    1.21 +// Constants from prio.h for nsIFileOutputStream.init
    1.22 +const PR_WRONLY = 0x2;
    1.23 +const PR_CREATE_FILE = 0x8;
    1.24 +const PR_TRUNCATE = 0x20;
    1.25 +const RW_OWNER = 0600;
    1.26 +
    1.27 +function write_string_to_file(file, contents) {
    1.28 +  let ostream = Cc["@mozilla.org/network/safe-file-output-stream;1"]
    1.29 +                .createInstance(Ci.nsIFileOutputStream);
    1.30 +  ostream.init(file, PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE,
    1.31 +	       RW_OWNER, ostream.DEFER_OPEN);
    1.32 +  ostream.write(contents, contents.length);
    1.33 +  ostream.QueryInterface(Ci.nsISafeOutputStream).finish();
    1.34 +  ostream.close();
    1.35 +}
    1.36 +
    1.37 +function construct_file() {
    1.38 +  let profileDirectory = Services.dirsvc.get("ProfD", Ci.nsIFile);
    1.39 +  let file = profileDirectory.clone();
    1.40 +  file.append(LOCK_FILE_NAME);
    1.41 +  return file;
    1.42 +}
    1.43 +
    1.44 +function run_test() {
    1.45 +  do_get_profile();
    1.46 +
    1.47 +  do_check_eq(Telemetry.failedProfileLockCount, 0);
    1.48 +
    1.49 +  write_string_to_file(construct_file(), N_FAILED_LOCKS.toString());
    1.50 +
    1.51 +  // Make sure that we're not eagerly reading the count now that the
    1.52 +  // file exists.
    1.53 +  do_check_eq(Telemetry.failedProfileLockCount, 0);
    1.54 +
    1.55 +  do_test_pending();
    1.56 +  Telemetry.asyncFetchTelemetryData(actual_test);
    1.57 +}
    1.58 +
    1.59 +function actual_test() {
    1.60 +  do_check_eq(Telemetry.failedProfileLockCount, N_FAILED_LOCKS);
    1.61 +  do_check_false(construct_file().exists());
    1.62 +  do_test_finished();
    1.63 +}

mercurial