Bug 1498059 - Tests for CEnums in XPIDL; r=nika,froydnj
authorKyle Machulis <kyle@nonpolynomial.com>
Tue, 06 Nov 2018 00:05:44 +0000
changeset 444502 58b76f270c221b2433e1b6e543822c9f2640a76e
parent 444501 40c1a7f2500861af2acbb24707e3838a0b779dc7
child 444503 0cb0757649fa52ece6e3fdb94c8a5018f3f6ea3a
push id34996
push userrgurzau@mozilla.com
push dateTue, 06 Nov 2018 09:53:23 +0000
treeherdermozilla-central@e160f0a60e4f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersnika, froydnj
bugs1498059
milestone65.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1498059 - Tests for CEnums in XPIDL; r=nika,froydnj Depends on D8594 Differential Revision: https://phabricator.services.mozilla.com/D8595
js/xpconnect/tests/components/js/xpctest.manifest
js/xpconnect/tests/components/js/xpctest_cenums.js
js/xpconnect/tests/components/native/moz.build
js/xpconnect/tests/components/native/xpctest_cenums.cpp
js/xpconnect/tests/components/native/xpctest_module.cpp
js/xpconnect/tests/components/native/xpctest_private.h
js/xpconnect/tests/idl/moz.build
js/xpconnect/tests/idl/xpctest_cenums.idl
js/xpconnect/tests/moz.build
js/xpconnect/tests/unit/test_cenums.js
js/xpconnect/tests/unit/xpcshell.ini
--- a/js/xpconnect/tests/components/js/xpctest.manifest
+++ b/js/xpconnect/tests/components/js/xpctest.manifest
@@ -19,8 +19,11 @@ contract @mozilla.org/js/xpc/test/js/Int
 component {90ec5c9e-f6da-406b-9a38-14d00f59db76} xpctest_interfaces.js
 contract @mozilla.org/js/xpc/test/js/TestInterfaceAll;1 {90ec5c9e-f6da-406b-9a38-14d00f59db76}
 
 component {38dd78aa-467f-4fad-8dcf-4383a743e235} xpctest_returncode_child.js
 contract @mozilla.org/js/xpc/test/js/ReturnCodeChild;1 {38dd78aa-467f-4fad-8dcf-4383a743e235}
 
 component {e86573c4-a384-441a-8c92-7b99e8575b28} xpctest_utils.js
 contract @mozilla.org/js/xpc/test/js/TestUtils;1 {e86573c4-a384-441a-8c92-7b99e8575b28}
+
+component {43929c74-dc70-11e8-b6f9-8fce71a2796a} xpctest_cenums.js
+contract @mozilla.org/js/xpc/test/js/CEnums;1 {43929c74-dc70-11e8-b6f9-8fce71a2796a}
new file mode 100644
--- /dev/null
+++ b/js/xpconnect/tests/components/js/xpctest_cenums.js
@@ -0,0 +1,27 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
+
+function TestCEnums() {
+}
+
+TestCEnums.prototype = {
+  /* Boilerplate */
+  QueryInterface: ChromeUtils.generateQI([Ci["nsIXPCTestCEnums"]]),
+  contractID: "@mozilla.org/js/xpc/test/js/CEnums;1",
+  classID: Components.ID("{43929c74-dc70-11e8-b6f9-8fce71a2796a}"),
+
+  testCEnumInput: function(input) {
+    if (input != Ci.nsIXPCTestCEnums.shouldBe12Explicit)
+    {
+      throw new Error("Enum values do not match expected value");
+    }
+  },
+
+  testCEnumOutput: function() {
+    return Ci.nsIXPCTestCEnums.shouldBe8Explicit;
+  },
+};
+
+this.NSGetFactory = XPCOMUtils.generateNSGetFactory([TestCEnums]);
--- a/js/xpconnect/tests/components/native/moz.build
+++ b/js/xpconnect/tests/components/native/moz.build
@@ -5,14 +5,15 @@
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 EXPORTS += [
     'xpctest_private.h',
 ]
 
 UNIFIED_SOURCES += [
     'xpctest_attributes.cpp',
+    'xpctest_cenums.cpp',
     'xpctest_module.cpp',
     'xpctest_params.cpp',
     'xpctest_returncode.cpp',
 ]
 
 FINAL_LIBRARY = 'xul'
new file mode 100644
--- /dev/null
+++ b/js/xpconnect/tests/components/native/xpctest_cenums.cpp
@@ -0,0 +1,58 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+/* local header for xpconnect tests components */
+
+#include "xpctest_private.h"
+
+NS_IMPL_ISUPPORTS(xpcTestCEnums, nsIXPCTestCEnums)
+
+// If this compiles, we pass. Otherwise, this means that XPIDL bitflag
+// generation is broken.
+xpcTestCEnums::xpcTestCEnums() {
+    static_assert(0 == static_cast<uint32_t>(shouldBe0Implicit),
+                  "XPIDL bitflag generation did not create correct shouldBe0Implicit flag");
+    static_assert(1 == static_cast<uint32_t>(shouldBe1Implicit),
+                  "XPIDL bitflag generation did not create correct shouldBe1Implicit flag");
+    static_assert(2 == static_cast<uint32_t>(shouldBe2Implicit),
+                  "XPIDL bitflag generation did not create correct shouldBe2Implicit flag");
+    static_assert(3 == static_cast<uint32_t>(shouldBe3Implicit),
+                  "XPIDL bitflag generation did not create correct shouldBe3Implicit flag");
+    static_assert(5 == static_cast<uint32_t>(shouldBe5Implicit),
+                  "XPIDL bitflag generation did not create correct shouldBe5Implicit flag");
+    static_assert(6 == static_cast<uint32_t>(shouldBe6Implicit),
+                  "XPIDL bitflag generation did not create correct shouldBe6Implicit flag");
+    static_assert(2 == static_cast<uint32_t>(shouldBe2AgainImplicit),
+                  "XPIDL bitflag generation did not create correct shouldBe2AgainImplicit flag");
+    static_assert(3 == static_cast<uint32_t>(shouldBe3AgainImplicit),
+                  "XPIDL bitflag generation did not create correct shouldBe3AgainImplicit flag");
+    static_assert(1 == static_cast<uint32_t>(shouldBe1Explicit),
+                  "XPIDL bitflag generation did not create correct shouldBe1Explicit flag");
+    static_assert(2 == static_cast<uint32_t>(shouldBe2Explicit),
+                  "XPIDL bitflag generation did not create correct shouldBe2Explicit flag");
+    static_assert(4 == static_cast<uint32_t>(shouldBe4Explicit),
+                  "XPIDL bitflag generation did not create correct shouldBe4Explicit flag");
+    static_assert(8 == static_cast<uint32_t>(shouldBe8Explicit),
+                  "XPIDL bitflag generation did not create correct shouldBe8Explicit flag");
+    static_assert(12 == static_cast<uint32_t>(shouldBe12Explicit),
+                  "XPIDL bitflag generation did not create correct shouldBe12Explicit flag");
+}
+
+nsresult
+xpcTestCEnums::TestCEnumInput(testFlagsExplicit a)
+{
+    if (a != shouldBe12Explicit) {
+        return NS_ERROR_FAILURE;
+    }
+    return NS_OK;
+}
+
+nsresult
+xpcTestCEnums::TestCEnumOutput(testFlagsExplicit* a)
+{
+    *a = shouldBe8Explicit;
+    return NS_OK;
+}
--- a/js/xpconnect/tests/components/native/xpctest_module.cpp
+++ b/js/xpconnect/tests/components/native/xpctest_module.cpp
@@ -19,34 +19,42 @@
 #define NS_XPCTESTPARAMS_CID                                                  \
 { 0x1f11076a, 0x0fa2, 0x4f07,                                                 \
     { 0xb4, 0x7a, 0xa1, 0x54, 0x31, 0xf2, 0xce, 0xf7 } }
 
 #define NS_XPCTESTRETURNCODEPARENT_CID                                        \
 { 0x3818f744, 0x5445, 0x4e9c,                                                 \
     { 0x9b, 0xb8, 0x64, 0x62, 0xfe, 0x81, 0xb6, 0x19 } }
 
+#define NS_XPCTESTCENUMS_CID                                                  \
+{ 0x89ba673a, 0xa987, 0xb89c,                                                 \
+    { 0x92, 0x02, 0xb9, 0xc6, 0x23, 0x38, 0x64, 0x55 } }
+
+NS_GENERIC_FACTORY_CONSTRUCTOR(xpcTestCEnums)
 NS_GENERIC_FACTORY_CONSTRUCTOR(xpcTestObjectReadOnly)
 NS_GENERIC_FACTORY_CONSTRUCTOR(xpcTestObjectReadWrite)
 NS_GENERIC_FACTORY_CONSTRUCTOR(nsXPCTestParams)
 NS_GENERIC_FACTORY_CONSTRUCTOR(nsXPCTestReturnCodeParent)
 NS_DEFINE_NAMED_CID(NS_XPCTESTOBJECTREADONLY_CID);
 NS_DEFINE_NAMED_CID(NS_XPCTESTOBJECTREADWRITE_CID);
 NS_DEFINE_NAMED_CID(NS_XPCTESTPARAMS_CID);
 NS_DEFINE_NAMED_CID(NS_XPCTESTRETURNCODEPARENT_CID);
+NS_DEFINE_NAMED_CID(NS_XPCTESTCENUMS_CID);
 
 static const mozilla::Module::CIDEntry kXPCTestCIDs[] = {
     { &kNS_XPCTESTOBJECTREADONLY_CID, false, nullptr, xpcTestObjectReadOnlyConstructor },
     { &kNS_XPCTESTOBJECTREADWRITE_CID, false, nullptr, xpcTestObjectReadWriteConstructor },
     { &kNS_XPCTESTPARAMS_CID, false, nullptr, nsXPCTestParamsConstructor },
     { &kNS_XPCTESTRETURNCODEPARENT_CID, false, nullptr, nsXPCTestReturnCodeParentConstructor },
+    { &kNS_XPCTESTCENUMS_CID, false, nullptr, xpcTestCEnumsConstructor },
     { nullptr }
 };
 
 static const mozilla::Module::ContractIDEntry kXPCTestContracts[] = {
+    { "@mozilla.org/js/xpc/test/native/CEnums;1", &kNS_XPCTESTCENUMS_CID },
     { "@mozilla.org/js/xpc/test/native/ObjectReadOnly;1", &kNS_XPCTESTOBJECTREADONLY_CID },
     { "@mozilla.org/js/xpc/test/native/ObjectReadWrite;1", &kNS_XPCTESTOBJECTREADWRITE_CID },
     { "@mozilla.org/js/xpc/test/native/Params;1", &kNS_XPCTESTPARAMS_CID },
     { "@mozilla.org/js/xpc/test/native/ReturnCodeParent;1", &kNS_XPCTESTRETURNCODEPARENT_CID },
     { nullptr }
 };
 
 const mozilla::Module kXPCTestModule = {
--- a/js/xpconnect/tests/components/native/xpctest_private.h
+++ b/js/xpconnect/tests/components/native/xpctest_private.h
@@ -10,16 +10,17 @@
 #define xpctest_private_h___
 
 #include "nsISupports.h"
 #include "nsMemory.h"
 #include "nsString.h"
 #include "xpctest_attributes.h"
 #include "xpctest_params.h"
 #include "xpctest_returncode.h"
+#include "xpctest_cenums.h"
 #include "mozilla/Attributes.h"
 #include "mozilla/ModuleUtils.h"
 
 extern const mozilla::Module kXPCTestModule;
 
 class xpcTestObjectReadOnly final : public nsIXPCTestObjectReadOnly {
  public:
   NS_DECL_ISUPPORTS
@@ -75,9 +76,18 @@ public:
     NS_DECL_NSIXPCTESTRETURNCODEPARENT
 
     nsXPCTestReturnCodeParent();
 
 private:
     ~nsXPCTestReturnCodeParent();
 };
 
+class xpcTestCEnums final : public nsIXPCTestCEnums {
+public:
+  NS_DECL_ISUPPORTS
+  NS_DECL_NSIXPCTESTCENUMS
+
+  xpcTestCEnums();
+private:
+  ~xpcTestCEnums() = default;
+};
 #endif /* xpctest_private_h___ */
--- a/js/xpconnect/tests/idl/moz.build
+++ b/js/xpconnect/tests/idl/moz.build
@@ -2,15 +2,16 @@
 # vim: set filetype=python:
 # This Source Code Form is subject to the terms of the Mozilla Public
 # License, v. 2.0. If a copy of the MPL was not distributed with this
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 XPIDL_SOURCES += [
     'xpctest_attributes.idl',
     'xpctest_bug809674.idl',
+    'xpctest_cenums.idl',
     'xpctest_interfaces.idl',
     'xpctest_params.idl',
     'xpctest_returncode.idl',
     'xpctest_utils.idl',
 ]
 
 XPIDL_MODULE = 'xpctest'
new file mode 100644
--- /dev/null
+++ b/js/xpconnect/tests/idl/xpctest_cenums.idl
@@ -0,0 +1,39 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#include "nsISupports.idl"
+/*
+ *	This defines the interface for a test object.
+ *
+ */
+
+[scriptable, uuid(6a2f918e-cda2-11e8-bc9a-a34c716d1f2a)]
+interface nsIXPCTestCEnums : nsISupports {
+  const long testConst = 1;
+
+  cenum testFlagsExplicit: 8 {
+    shouldBe1Explicit = 1,
+    shouldBe2Explicit = 2,
+    shouldBe4Explicit = 4,
+    shouldBe8Explicit = 8,
+    shouldBe12Explicit = shouldBe4Explicit | shouldBe8Explicit,
+  };
+
+  cenum testFlagsImplicit: 8 {
+    shouldBe0Implicit,
+    shouldBe1Implicit,
+    shouldBe2Implicit,
+    shouldBe3Implicit,
+    shouldBe5Implicit = 5,
+    shouldBe6Implicit,
+    shouldBe2AgainImplicit = 2,
+    shouldBe3AgainImplicit,
+  };
+
+  void testCEnumInput(in nsIXPCTestCEnums_testFlagsExplicit abc);
+
+  nsIXPCTestCEnums_testFlagsExplicit testCEnumOutput();
+};
--- a/js/xpconnect/tests/moz.build
+++ b/js/xpconnect/tests/moz.build
@@ -20,13 +20,14 @@ if CONFIG['COMPILE_ENVIRONMENT']:
 XPCSHELL_TESTS_MANIFESTS += [
     'unit/xpcshell.ini',
 ]
 
 TEST_HARNESS_FILES.xpcshell.js.xpconnect.tests.components.js += [
     'components/js/xpctest.manifest',
     'components/js/xpctest_attributes.js',
     'components/js/xpctest_bug809674.js',
+    'components/js/xpctest_cenums.js',
     'components/js/xpctest_interfaces.js',
     'components/js/xpctest_params.js',
     'components/js/xpctest_returncode_child.js',
     'components/js/xpctest_utils.js',
 ]
new file mode 100644
--- /dev/null
+++ b/js/xpconnect/tests/unit/test_cenums.js
@@ -0,0 +1,41 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+function run_test() {
+
+  // Load the component manifests.
+  registerXPCTestComponents();
+  registerAppManifest(do_get_file('../components/js/xpctest.manifest'));
+
+  // Test for each component.
+  test_interface_consts();
+  test_component("@mozilla.org/js/xpc/test/native/CEnums;1");
+  test_component("@mozilla.org/js/xpc/test/js/CEnums;1");
+}
+
+function test_interface_consts() {
+  Assert.equal(Ci.nsIXPCTestCEnums.testConst, 1);
+  Assert.equal(Ci.nsIXPCTestCEnums.shouldBe1Explicit, 1);
+  Assert.equal(Ci.nsIXPCTestCEnums.shouldBe2Explicit, 2);
+  Assert.equal(Ci.nsIXPCTestCEnums.shouldBe4Explicit, 4);
+  Assert.equal(Ci.nsIXPCTestCEnums.shouldBe8Explicit, 8);
+  Assert.equal(Ci.nsIXPCTestCEnums.shouldBe12Explicit, 12);
+  Assert.equal(Ci.nsIXPCTestCEnums.shouldBe1Implicit, 1);
+  Assert.equal(Ci.nsIXPCTestCEnums.shouldBe2Implicit, 2);
+  Assert.equal(Ci.nsIXPCTestCEnums.shouldBe3Implicit, 3);
+  Assert.equal(Ci.nsIXPCTestCEnums.shouldBe5Implicit, 5);
+  Assert.equal(Ci.nsIXPCTestCEnums.shouldBe6Implicit, 6);
+  Assert.equal(Ci.nsIXPCTestCEnums.shouldBe2AgainImplicit, 2);
+  Assert.equal(Ci.nsIXPCTestCEnums.shouldBe3AgainImplicit, 3);
+}
+
+function test_component(contractid) {
+
+  // Instantiate the object.
+  var o = Cc[contractid].createInstance(Ci["nsIXPCTestCEnums"]);
+  o.testCEnumInput(Ci.nsIXPCTestCEnums.shouldBe12Explicit);
+  o.testCEnumInput(Ci.nsIXPCTestCEnums.shouldBe8Explicit | Ci.nsIXPCTestCEnums.shouldBe4Explicit);
+  var a = o.testCEnumOutput();
+  Assert.equal(a, Ci.nsIXPCTestCEnums.shouldBe8Explicit);
+}
+
--- a/js/xpconnect/tests/unit/xpcshell.ini
+++ b/js/xpconnect/tests/unit/xpcshell.ini
@@ -59,16 +59,17 @@ support-files =
 [test_bug1131707.js]
 [test_bug1150106.js]
 [test_bug1150771.js]
 [test_bug1151385.js]
 [test_bug1170311.js]
 [test_bug1244222.js]
 [test_bug_442086.js]
 [test_callFunctionWithAsyncStack.js]
+[test_cenums.js]
 [test_classesByID_instanceof.js]
 [test_compileScript.js]
 [test_deepFreezeClone.js]
 [test_defineModuleGetter.js]
 [test_file.js]
 [test_blob.js]
 [test_blob2.js]
 [test_file2.js]