Bug 1287007 - Refactor test_ext_schemas_api_injection.js draft
authorRob Wu <rob@robwu.nl>
Fri, 02 Sep 2016 05:36:33 -0700
changeset 428417 9cc4f92f5de4676ae0db3623a080d9c4cdcc8f6a
parent 428416 b545457a9fb030e66b3d2510cc7beae2b60882f3
child 428418 0dc7324462f87f001e99dba0ad1d869ee3692e89
push id33305
push userbmo:rob@robwu.nl
push dateSun, 23 Oct 2016 20:56:25 +0000
bugs1287007
milestone52.0a1
Bug 1287007 - Refactor test_ext_schemas_api_injection.js When the background API move to a separate implementation, then the schema APIs will be generated (and ChildAPIManager will just delegate the implementation to the parent process). The purpose of the test is to verify that nested namespaces and null-feturn values work, so just use the base classes instead of a concrete implementation for BaseContext / SchemaAPIManager instead of setting up a full extension. MozReview-Commit-ID: CB5s7Ae24zS
toolkit/components/extensions/test/xpcshell/test_ext_schemas_api_injection.js
--- a/toolkit/components/extensions/test/xpcshell/test_ext_schemas_api_injection.js
+++ b/toolkit/components/extensions/test/xpcshell/test_ext_schemas_api_injection.js
@@ -1,15 +1,17 @@
 "use strict";
 
+Components.utils.import("resource://gre/modules/ExtensionUtils.jsm");
 Components.utils.import("resource://gre/modules/Schemas.jsm");
 
 let {
-  Management,
-} = Components.utils.import("resource://gre/modules/Extension.jsm", {});
+  BaseContext,
+  SchemaAPIManager,
+} = ExtensionUtils;
 
 let nestedNamespaceJson = [
   {
     "namespace": "backgroundAPI.testnamespace",
     "functions": [
       {
         "name": "create",
         "type": "function",
@@ -37,67 +39,64 @@ let nestedNamespaceJson = [
             "type": "string",
           },
         ],
       },
     ],
   },
 ];
 
+let global = this;
+class StubContext extends BaseContext {
+  constructor() {
+    let fakeExtension = {id: "test@web.extension"};
+    super("addon_child", fakeExtension);
+    this.sandbox = Cu.Sandbox(global);
+    this.viewType = "background";
+  }
+
+  get cloneScope() {
+    return this.sandbox;
+  }
+}
+
 add_task(function* testSchemaAPIInjection() {
   let url = "data:," + JSON.stringify(nestedNamespaceJson);
 
   // Load the schema of the fake APIs.
   yield Schemas.load(url);
 
+  let apiManager = new SchemaAPIManager("addon");
+
   // Register an API that will skip the background page.
-  Management.registerSchemaAPI("noBackgroundAPI.testnamespace", "addon_child", context => {
-    if (context.type !== "background") {
-      return {
-        noBackgroundAPI: {
-          testnamespace: {
-            create(title) {},
-          },
-        },
-      };
-    }
-
+  apiManager.registerSchemaAPI("noBackgroundAPI.testnamespace", "addon_child", context => {
     // This API should not be available in this context, return null so that
     // the schema wrapper is removed as well.
     return null;
   });
 
   // Register an API that will skip any but the background page.
-  Management.registerSchemaAPI("backgroundAPI.testnamespace", "addon_child", context => {
-    if (context.type === "background") {
+  apiManager.registerSchemaAPI("backgroundAPI.testnamespace", "addon_child", context => {
+    if (context.viewType === "background") {
       return {
         backgroundAPI: {
           testnamespace: {
             create(title) {
               return title;
             },
           },
         },
       };
     }
 
     // This API should not be available in this context, return null so that
     // the schema wrapper is removed as well.
     return null;
   });
 
-  let extension = ExtensionTestUtils.loadExtension({
-    background() {
-      if (browser.noBackgroundAPI) {
-        browser.test.notifyFail("skipAPIonNull.done");
-      } else {
-        const res = browser.backgroundAPI.testnamespace.create("param-value");
-        browser.test.assertEq("param-value", res,
-                              "Got the expected result from the fake API method");
-        browser.test.notifyPass("skipAPIonNull.done");
-      }
-    },
-  });
+  let context = new StubContext();
+  let browserObj = {};
+  apiManager.generateAPIs(context, browserObj);
 
-  yield extension.startup();
-  yield extension.awaitFinish("skipAPIonNull.done");
-  yield extension.unload();
+  do_check_eq(browserObj.noBackgroundAPI, undefined);
+  const res = browserObj.backgroundAPI.testnamespace.create("param-value");
+  do_check_eq(res, "param-value");
 });