Bug 684327 - Misc fixes to xpctest_attributes. r=khuey
authorBobby Holley <bobbyholley@gmail.com>
Sun, 25 Sep 2011 15:37:59 +0100
changeset 77519 96405a264d9df003ed88748a8752b6586e5c296a
parent 77518 aef192bef23532e47391b9d070aadad7f0765810
child 77520 de1c69fb961e0ad49bf3447b553c28bca660f694
push id3
push userfelipc@gmail.com
push dateFri, 30 Sep 2011 20:09:13 +0000
reviewerskhuey
bugs684327
milestone9.0a1
Bug 684327 - Misc fixes to xpctest_attributes. r=khuey This patch is best reviewed while listening to http://www.youtube.com/watch?v=MK6TXMsvgQg NB - The code here would make a great "find the bugs in this code" interview screen question.
js/src/xpconnect/tests/components/xpctest_attributes.cpp
js/src/xpconnect/tests/components/xpctest_module.cpp
js/src/xpconnect/tests/components/xpctest_private.h
js/src/xpconnect/tests/idl/xpctest_attributes.idl
--- a/js/src/xpconnect/tests/components/xpctest_attributes.cpp
+++ b/js/src/xpconnect/tests/components/xpctest_attributes.cpp
@@ -33,66 +33,35 @@
  * use your version of this file under the terms of the MPL, indicate your
  * decision by deleting the provisions above and replace them with the notice
  * and other provisions required by the GPL or the LGPL. If you do not delete
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
-#include "xpctest_attributes.h"
-#include "nsISupports.h"
 #include "xpctest_private.h"
 
-#define NS_IXPCTESTOBJECTREADONLY_IID \
-  {0x1364941e, 0x4462, 0x11d3, \
-    { 0x82, 0xee, 0x00, 0x60, 0xb0, 0xeb, 0x59, 0x6f }}
-
-class xpcTestObjectReadOnly : public nsIXPCTestObjectReadOnly {
- public: 
-  NS_DECL_ISUPPORTS
-  NS_DECL_NSIXPCTESTOBJECTREADONLY
-  xpcTestObjectReadOnly();
-   
- private:
-    PRBool  boolProperty;
-    PRInt16 shortProperty;
-    PRInt32 longProperty;
-    float   floatProperty;
-    char    charProperty;
-    char    *stringID;
-};
-
 NS_IMPL_ISUPPORTS1(xpcTestObjectReadOnly, nsIXPCTestObjectReadOnly)
 
 xpcTestObjectReadOnly :: xpcTestObjectReadOnly() {
-    NS_ADDREF_THIS();
-
     boolProperty = PR_TRUE;
     shortProperty = 32767;
     longProperty =  2147483647;
+    floatProperty = 5.5f;
     charProperty = 'X';
-
-    const char _id[] = "a68cc6a6-6552-11d3-82ef-0060b0eb596f";
-    stringID = (char*) nsMemory::Clone(_id, sizeof(char)*(strlen(_id)+1));
-}
-
-NS_IMETHODIMP xpcTestObjectReadOnly :: GetID(char **_retval) {
-    *_retval= (char*) nsMemory::Clone(stringID, 
-                                         sizeof(char)*(strlen(stringID)+1));
-    return *_retval? NS_OK : NS_ERROR_OUT_OF_MEMORY;
 }
 
 NS_IMETHODIMP xpcTestObjectReadOnly :: GetStrReadOnly(char * *aStrReadOnly){
     char aString[] = "XPConnect Read-Only String";
 
     if(!aStrReadOnly)
         return NS_ERROR_NULL_POINTER;
-    *aStrReadOnly = (char*) nsMemory::Clone(aStrReadOnly, 
-                                               sizeof(char)*(strlen(aString)+1));
+    *aStrReadOnly = (char*) nsMemory::Clone(aString,
+                                            sizeof(char)*(strlen(aString)+1));
     return *aStrReadOnly ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
 }
 
 NS_IMETHODIMP xpcTestObjectReadOnly :: GetBoolReadOnly(PRBool *aBoolReadOnly) {
     *aBoolReadOnly = boolProperty;
     return NS_OK;
 }
 NS_IMETHODIMP xpcTestObjectReadOnly :: GetShortReadOnly(PRInt16 *aShortReadOnly){
@@ -106,98 +75,56 @@ NS_IMETHODIMP xpcTestObjectReadOnly :: G
 NS_IMETHODIMP xpcTestObjectReadOnly :: GetFloatReadOnly(float *aFloatReadOnly){
     *aFloatReadOnly = floatProperty;
     return NS_OK;
 }
 NS_IMETHODIMP xpcTestObjectReadOnly :: GetCharReadOnly(char *aCharReadOnly){
     *aCharReadOnly = charProperty;
     return NS_OK;
 }
-nsresult
-xpctest::ConstructXPCTestObjectReadOnly(nsISupports *aOuter, REFNSIID aIID, void **aResult)
-{
-    nsresult rv;
-    NS_ASSERTION(aOuter == nsnull, "no aggregation");
-    xpcTestObjectReadOnly *obj = new xpcTestObjectReadOnly();
-
-    if(obj)
-    {
-        rv = obj->QueryInterface(aIID, aResult);
-        NS_ASSERTION(NS_SUCCEEDED(rv), "unable to find correct interface");
-        NS_RELEASE(obj);
-    }
-    else
-    {
-        *aResult = nsnull;
-        rv = NS_ERROR_OUT_OF_MEMORY;
-    }
-
-    return rv;
-}
-/****************************************************************************/
-/* starting interface:    nsIXPCTestObjectReadWrite */
-/* {3b9b1d38-491a-11d3-82ef-0060b0eb596f} */
-/*
-
-#define NS_IXPCTESTOBJECTREADWRITE_IID_STR "3b9b1d38-491a-11d3-82ef-0060b0eb596f"
-*/
-#define NS_IXPCTESTOBJECTREADWRITE_IID \
-  {0x3b9b1d38, 0x491a, 0x11d3, \
-    { 0x82, 0xef, 0x00, 0x60, 0xb0, 0xeb, 0x59, 0x6f }}
-
-class xpcTestObjectReadWrite : public nsIXPCTestObjectReadWrite {
-  public: 
-  NS_DECL_ISUPPORTS
-  NS_DECL_NSIXPCTESTOBJECTREADWRITE
-  
-  xpcTestObjectReadWrite();
-
- private:
-     PRBool boolProperty;
-     PRInt16 shortProperty;
-     PRInt32 longProperty;
-     float floatProperty;
-     char charProperty;
-     const char *stringProperty;
-};
 
 NS_IMPL_ISUPPORTS1(xpcTestObjectReadWrite, nsIXPCTestObjectReadWrite)
 
 xpcTestObjectReadWrite :: xpcTestObjectReadWrite() {
-    NS_ADDREF_THIS();
-
-
+    const char s[] = "XPConnect Read-Writable String";
+    stringProperty = (char*) nsMemory::Clone(s, sizeof(char)*(strlen(s)+1));
     boolProperty = PR_TRUE;
     shortProperty = 32767;
     longProperty =  2147483647;
+    floatProperty = 5.5f;
     charProperty = 'X';
+}
 
-    const char s[] = "XPConnect Read-Writable String";
-    stringProperty = (char*) nsMemory::Clone(s, 
-                                                sizeof(char)*(strlen(s)+1));
+xpcTestObjectReadWrite :: ~xpcTestObjectReadWrite()
+{
+    nsMemory::Free(stringProperty);
 }
 
 NS_IMETHODIMP xpcTestObjectReadWrite :: GetStringProperty(char * *aStringProperty) {
     if(!aStringProperty)
         return NS_ERROR_NULL_POINTER;
     *aStringProperty = (char*) nsMemory::Clone(stringProperty, 
                                                sizeof(char)*(strlen(stringProperty)+1));
     return *aStringProperty ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
 
 }
 NS_IMETHODIMP xpcTestObjectReadWrite :: SetStringProperty(const char * aStringProperty) {
-    stringProperty = aStringProperty;
+    nsMemory::Free(stringProperty);
+    stringProperty = (char*) nsMemory::Clone(aStringProperty,
+                                             sizeof(char)*(strlen(aStringProperty)+1));
     return NS_OK;
 }
 
 NS_IMETHODIMP xpcTestObjectReadWrite :: GetBooleanProperty(PRBool *aBooleanProperty) {
     *aBooleanProperty = boolProperty;
     return NS_OK;
 }
 NS_IMETHODIMP xpcTestObjectReadWrite :: SetBooleanProperty(PRBool aBooleanProperty) {
+    NS_ENSURE_TRUE(aBooleanProperty == PR_TRUE || aBooleanProperty == PR_FALSE,
+                   NS_ERROR_INVALID_ARG);
     boolProperty = aBooleanProperty;
     return NS_OK;
 }
 NS_IMETHODIMP xpcTestObjectReadWrite :: GetShortProperty(PRInt16 *aShortProperty) {
     *aShortProperty = shortProperty;
     return NS_OK;
 }
 NS_IMETHODIMP xpcTestObjectReadWrite :: SetShortProperty(PRInt16 aShortProperty) {
@@ -223,83 +150,8 @@ NS_IMETHODIMP xpcTestObjectReadWrite :: 
 NS_IMETHODIMP xpcTestObjectReadWrite :: GetCharProperty(char *aCharProperty) {
     *aCharProperty = charProperty;
     return NS_OK;
 }
 NS_IMETHODIMP xpcTestObjectReadWrite :: SetCharProperty(char aCharProperty) {
     charProperty = aCharProperty;
     return NS_OK;
 }
-nsresult
-xpctest::ConstructXPCTestObjectReadWrite(nsISupports *aOuter, REFNSIID aIID, void **aResult)
-{
-    nsresult rv;
-    NS_ASSERTION(aOuter == nsnull, "no aggregation");
-    xpcTestObjectReadWrite *obj = new xpcTestObjectReadWrite();
-
-    if(obj)
-    {
-        rv = obj->QueryInterface(aIID, aResult);
-        NS_ASSERTION(NS_SUCCEEDED(rv), "unable to find correct interface");
-        NS_RELEASE(obj);
-    }
-    else
-    {
-        *aResult = nsnull;
-        rv = NS_ERROR_OUT_OF_MEMORY;
-    }
-    return rv;
-}
-
-
-/****************************************************************************/
-/*
-class xpcTestAttributes : public nsIXPCTestObjectReadWrite, 
-    public nsIXPCTestObjectReadOnly
-{
-public: 
-  NS_DECL_ISUPPORTS
-  NS_DECL_NSIXPCTESTOBJECTREADONLY
-  NS_DECL_NSIXPCTESTOBJECTREADWRITE
-
-  NS_IMETHOD GetName(char * *aString);
-  NS_IMETHOD SetName(char * aString );
-
-private:
-    char *name;
-};
-  
-NS_IMPL_ISUPPORTS2(xpcTestAttributes, nsIXPCTestObjectReadWrite, nsIXPCTestObjectReadOnly)
-
-NS_IMETHODIMP xpcTestAttributes ::GetName(char * *aString) {
-    if(!aString)
-        return NS_ERROR_NULL_POINTER;
-    *aString = (char*) nsMemory::Clone(name, 
-                sizeof(char)*(strlen(name)+1));
-    return *aString ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
-
-}
-NS_IMETHODIMP xpcTestAttributes ::SetName(char * aString) {
-    name = aString;
-    return NS_OK;
-}
-
-nsresult
-xpctest::ConstructXPCTestAttributes(nsISupports *aOuter, REFNSIID aIID, void **aResult)
-{
-    nsresult rv;
-    NS_ASSERTION(aOuter == nsnull, "no aggregation");
-    xpcTestAttributes *obj = new xpcTestAttributes();
-
-    if(obj)
-    {
-        rv = obj->QueryInterface(aIID, aResult);
-        NS_ASSERTION(NS_SUCCEEDED(rv), "unable to find correct interface");
-        NS_RELEASE(obj);
-    }
-    else
-    {
-        *aResult = nsnull;
-        rv = NS_ERROR_OUT_OF_MEMORY;
-    }
-    return rv;
-}
-*/
--- a/js/src/xpconnect/tests/components/xpctest_module.cpp
+++ b/js/src/xpconnect/tests/components/xpctest_module.cpp
@@ -35,29 +35,39 @@
  * and other provisions required by the GPL or the LGPL. If you do not delete
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
 /* module registration and factory code. */
 
-#include "xpctest_private.h"
 #include "nsCOMPtr.h"
 #include "nsIModule.h"
 #include "mozilla/ModuleUtils.h"
 #include "nsCRT.h"
 #include "nsIClassInfoImpl.h"
+#include "xpctest_private.h"
 
+#define NS_XPCTESTOBJECTREADONLY_CID \
+{ 0x492609a7, 0x2582, 0x436b, \
+   { 0xb0, 0xef, 0x92, 0xe2, 0x9b, 0xb9, 0xe1, 0x43 } }
+
+#define NS_XPCTESTOBJECTREADWRITE_CID \
+{ 0x8f37f760, 0x3686, 0x4dbb, \
+   { 0xb1, 0x21, 0x96, 0x93, 0xba, 0x81, 0x3f, 0x8f } }
+
+NS_GENERIC_FACTORY_CONSTRUCTOR(xpcTestObjectReadOnly)
+NS_GENERIC_FACTORY_CONSTRUCTOR(xpcTestObjectReadWrite)
 NS_DEFINE_NAMED_CID(NS_XPCTESTOBJECTREADONLY_CID);
 NS_DEFINE_NAMED_CID(NS_XPCTESTOBJECTREADWRITE_CID);
 
 static const mozilla::Module::CIDEntry kXPCTestCIDs[] = {
-    { &kNS_XPCTESTOBJECTREADONLY_CID, false, NULL, xpctest::ConstructXPCTestObjectReadOnly },
-    { &kNS_XPCTESTOBJECTREADWRITE_CID, false, NULL, xpctest::ConstructXPCTestObjectReadWrite },
+    { &kNS_XPCTESTOBJECTREADONLY_CID, false, NULL, xpcTestObjectReadOnlyConstructor },
+    { &kNS_XPCTESTOBJECTREADWRITE_CID, false, NULL, xpcTestObjectReadWriteConstructor },
     { NULL }
 };
 
 static const mozilla::Module::ContractIDEntry kXPCTestContracts[] = {
     { "@mozilla.org/js/xpc/test/ObjectReadOnly;1", &kNS_XPCTESTOBJECTREADONLY_CID },
     { "@mozilla.org/js/xpc/test/ObjectReadWrite;1", &kNS_XPCTESTOBJECTREADWRITE_CID },
     { NULL }
 };
--- a/js/src/xpconnect/tests/components/xpctest_private.h
+++ b/js/src/xpconnect/tests/components/xpctest_private.h
@@ -39,41 +39,44 @@
  * ***** END LICENSE BLOCK ***** */
 
 /* local header for xpconnect tests components */
 
 #ifndef xpctest_private_h___
 #define xpctest_private_h___
 
 #include "nsISupports.h"
-#include "nsIFactory.h"
 #include "nsMemory.h"
-#include "nsIXPConnect.h"
-#include "nsIServiceManager.h"
-#include "nsIComponentManager.h"
-#include "mozilla/ModuleUtils.h"
-#include "nscore.h"
-#include "nsCOMPtr.h"
-#include "nsStringGlue.h"
-#include <stdio.h>
-
 #include "jsapi.h"
+#include "xpctest_attributes.h"
 
-#define NS_XPCTESTOBJECTREADONLY_CID \
-  {0x1364941e, 0x4462, 0x11d3, \
-    { 0x82, 0xee, 0x00, 0x60, 0xb0, 0xeb, 0x59, 0x6f }}
-
-#define NS_XPCTESTOBJECTREADWRITE_CID \
-  {0x3b9b1d38, 0x491a, 0x11d3, \
-    { 0x82, 0xef, 0x00, 0x60, 0xb0, 0xeb, 0x59, 0x6f }}
+class xpcTestObjectReadOnly : public nsIXPCTestObjectReadOnly {
+ public: 
+  NS_DECL_ISUPPORTS
+  NS_DECL_NSIXPCTESTOBJECTREADONLY
+  xpcTestObjectReadOnly();
+   
+ private:
+    PRBool  boolProperty;
+    PRInt16 shortProperty;
+    PRInt32 longProperty;
+    float   floatProperty;
+    char    charProperty;
+};
 
-// 'namespace' class
-class xpctest
-{
-public:
-  static nsresult ConstructXPCTestObjectReadOnly(nsISupports *aOuter, REFNSIID aIID, void **aResult);
-  static nsresult ConstructXPCTestObjectReadWrite(nsISupports *aOuter, REFNSIID aIID, void **aResult);
+class xpcTestObjectReadWrite : public nsIXPCTestObjectReadWrite {
+  public: 
+  NS_DECL_ISUPPORTS
+  NS_DECL_NSIXPCTESTOBJECTREADWRITE
+
+  xpcTestObjectReadWrite();
+  ~xpcTestObjectReadWrite();
 
-private:
-    xpctest();  // not implemented
+ private:
+     PRBool boolProperty;
+     PRInt16 shortProperty;
+     PRInt32 longProperty;
+     float floatProperty;
+     char charProperty;
+     char *stringProperty;
 };
 
 #endif /* xpctest_private_h___ */
--- a/js/src/xpconnect/tests/idl/xpctest_attributes.idl
+++ b/js/src/xpconnect/tests/idl/xpctest_attributes.idl
@@ -39,29 +39,27 @@
  * ***** END LICENSE BLOCK ***** */
 
 #include "nsISupports.idl"
 /*
  *	This defines the interface for a test object.
  *
  */
 
-[scriptable, uuid(1364941e-4462-11d3-82ee-0060b0eb596f)]
+[scriptable, uuid(29e950a0-0134-44bc-b947-5e0ee95c8f7e)]
 interface nsIXPCTestObjectReadOnly : nsISupports {
 	readonly attribute string  strReadOnly;
 	readonly attribute boolean boolReadOnly;
 	readonly attribute short   shortReadOnly;
 	readonly attribute long    longReadOnly;
 	readonly attribute float   floatReadOnly;
 	readonly attribute char    charReadOnly;
-
-	string getID();
 };
 
-[scriptable, uuid(3b9b1d38-491a-11d3-82ef-0060b0eb596f)]
+[scriptable, uuid(492609a7-2582-436b-b0ef-92e29bb9e143)]
 interface nsIXPCTestObjectReadWrite : nsISupports {
 	attribute string  stringProperty;
 	attribute boolean booleanProperty;
 	attribute short   shortProperty;
 	attribute long    longProperty;
 	attribute float   floatProperty;
 	attribute char    charProperty;
 };