Bug 1299846 - Complete the implementation of chrome.idle.queryState, r?kmag draft
authorBob Silverberg <bsilverberg@mozilla.com>
Thu, 01 Sep 2016 09:28:56 -0400
changeset 408847 0385721050905a21d83ba0ad0afdcbbbe1085e5b
parent 408409 b7f7ae14590aced450bb0b0469dfb38edd2c0ace
child 530182 a6ac2587db680fd43f26b513481c42d2a063a4e0
push id28290
push userbmo:bob.silverberg@gmail.com
push dateThu, 01 Sep 2016 17:50:17 +0000
reviewerskmag
bugs1299846
milestone51.0a1
Bug 1299846 - Complete the implementation of chrome.idle.queryState, r?kmag MozReview-Commit-ID: jYsePeTluZ
toolkit/components/extensions/ext-idle.js
toolkit/components/extensions/test/xpcshell/test_ext_idle.js
--- a/toolkit/components/extensions/ext-idle.js
+++ b/toolkit/components/extensions/ext-idle.js
@@ -1,11 +1,18 @@
 "use strict";
 
+XPCOMUtils.defineLazyServiceGetter(this, "idleService",
+                                   "@mozilla.org/widget/idleservice;1",
+                                   "nsIIdleService");
+
 extensions.registerSchemaAPI("idle", "addon_parent", context => {
   return {
     idle: {
       queryState: function(detectionIntervalInSeconds) {
-        return Promise.resolve("active");
+        if (idleService.idleTime / 1000 < detectionIntervalInSeconds) {
+          return Promise.resolve("active");
+        }
+        return Promise.resolve("idle");
       },
     },
   };
 });
--- a/toolkit/components/extensions/test/xpcshell/test_ext_idle.js
+++ b/toolkit/components/extensions/test/xpcshell/test_ext_idle.js
@@ -1,30 +1,59 @@
 /* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
 /* vim: set sts=2 sw=2 et tw=80: */
 "use strict";
 
-add_task(function* testIdle() {
+Cu.import("resource://testing-common/MockRegistrar.jsm");
+
+let idleService = {
+  QueryInterface: XPCOMUtils.generateQI([Ci.nsIIdleService]),
+  idleTime: 19999,
+};
+MockRegistrar.register("@mozilla.org/widget/idleservice;1", idleService);
+
+add_task(function* testIdleActive() {
   function background() {
-    browser.idle.queryState(15).then(status => {
-      browser.test.assertEq("active", status, "Expected status");
+    browser.idle.queryState(20).then(status => {
+      browser.test.assertEq("active", status, "Idle status is active");
       browser.test.notifyPass("idle");
     },
-    e => {
-      browser.test.fail(`Error: ${e} :: ${e.stack}`);
+    err => {
+      browser.test.fail(`Error: ${err} :: ${err.stack}`);
       browser.test.notifyFail("idle");
     });
   }
 
   let extension = ExtensionTestUtils.loadExtension({
     background,
-
     manifest: {
       permissions: ["idle"],
     },
   });
 
   yield extension.startup();
-
   yield extension.awaitFinish("idle");
-
   yield extension.unload();
 });
+
+add_task(function* testIdleIdle() {
+  function background() {
+    browser.idle.queryState(15).then(status => {
+      browser.test.assertEq("idle", status, "Idle status is idle");
+      browser.test.notifyPass("idle");
+    },
+    err => {
+      browser.test.fail(`Error: ${err} :: ${err.stack}`);
+      browser.test.notifyFail("idle");
+    });
+  }
+
+  let extension = ExtensionTestUtils.loadExtension({
+    background,
+    manifest: {
+      permissions: ["idle"],
+    },
+  });
+
+  yield extension.startup();
+  yield extension.awaitFinish("idle");
+  yield extension.unload();
+});