test-self.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. "use strict";
  5. const { Cc, Ci, Cu, Cm, components } = require("chrome");
  6. const xulApp = require("sdk/system/xul-app");
  7. const self = require("sdk/self");
  8. const { Loader, main, unload } = require("toolkit/loader");
  9. const loaderOptions = require("@loader/options");
  10. const { AddonManager } = Cu.import("resource://gre/modules/AddonManager.jsm", {});
  11. exports.testSelf = function(assert) {
  12. // Likewise, we can't assert anything about the full URL, because that
  13. // depends on self.id . We can only assert that it ends in the right
  14. // thing.
  15. var url = self.data.url("test-content-symbiont.js");
  16. assert.equal(typeof(url), "string", "self.data.url('x') returns string");
  17. assert.equal(/\/test-content-symbiont\.js$/.test(url), true);
  18. // Make sure 'undefined' is not in url when no string is provided.
  19. url = self.data.url();
  20. assert.equal(typeof(url), "string", "self.data.url() returns string");
  21. assert.equal(/\/undefined$/.test(url), false);
  22. // When tests are run on just the api-utils package, self.name is
  23. // api-utils. When they're run as 'cfx testall', self.name is testpkgs.
  24. assert.equal(self.name, "addon-sdk", "self.name is addon-sdk");
  25. // loadReason may change here, as we change the way tests addons are installed
  26. // Bug 854937 fixed loadReason and is now install
  27. let testLoadReason = xulApp.versionInRange(xulApp.platformVersion,
  28. "23.0a1", "*") ? "install"
  29. : "startup";
  30. assert.equal(self.loadReason, testLoadReason,
  31. "self.loadReason is either startup or install on test runs");
  32. assert.equal(self.isPrivateBrowsingSupported, false,
  33. 'usePrivateBrowsing property is false by default');
  34. };
  35. exports.testSelfID = function(assert, done) {
  36. var self = require("sdk/self");
  37. // We can't assert anything about the ID inside the unit test right now,
  38. // because the ID we get depends upon how the test was invoked. The idea
  39. // is that it is supposed to come from the main top-level package's
  40. // package.json file, from the "id" key.
  41. assert.equal(typeof(self.id), "string", "self.id is a string");
  42. assert.ok(self.id.length > 0);
  43. AddonManager.getAddonByID(self.id, function(addon) {
  44. if (!addon) {
  45. assert.fail("did not find addon with self.id");
  46. }
  47. else {
  48. assert.pass("found addon with self.id");
  49. }
  50. done();
  51. });
  52. }
  53. exports.testSelfHandlesLackingLoaderOptions = function (assert) {
  54. let root = module.uri.substr(0, module.uri.lastIndexOf('/'));
  55. let uri = root + '/fixtures/loader/self/';
  56. let sdkPath = loaderOptions.paths[''] + 'sdk';
  57. let loader = Loader({ paths: { '': uri, 'sdk': sdkPath }});
  58. let program = main(loader, 'main');
  59. let self = program.self;
  60. assert.pass("No errors thrown when including sdk/self without loader options");
  61. assert.equal(self.isPrivateBrowsingSupported, false,
  62. "safely checks sdk/self.isPrivateBrowsingSupported");
  63. assert.equal(self.packed, false,
  64. "safely checks sdk/self.packed");
  65. unload(loader);
  66. };
  67. require("sdk/test").run(exports);