Bug 743336 - Settings API Service Tests. r=fabrice
authorGregor Wagner <anygregor@gmail.com>
Tue, 08 May 2012 11:28:59 -0700
changeset 95783 a0ec06f6c00db0d98992642688583383d756bd71
parent 95782 42939d5b6cc66beab46b6a718d904643451992db
child 95784 29d098de41ff65d7e89b859e2c7c49fd315f7f85
push id1439
push userlsblakk@mozilla.com
push dateMon, 04 Jun 2012 20:19:22 +0000
treeherdermozilla-aurora@ea74834dccd3 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfabrice
bugs743336
milestone15.0a1
Bug 743336 - Settings API Service Tests. r=fabrice
xpcom/tests/Makefile.in
xpcom/tests/TestSettingsAPI.cpp
--- a/xpcom/tests/Makefile.in
+++ b/xpcom/tests/Makefile.in
@@ -98,16 +98,17 @@ CPP_UNIT_TESTS = \
                  TestDeque.cpp \
                  TestFile.cpp \
                  TestHashtables.cpp \
                  TestID.cpp \
                  TestObserverArray.cpp \
                  TestObserverService.cpp \
                  TestPipe.cpp \
                  TestRefPtr.cpp \
+                 TestSettingsAPI.cpp \
                  TestTextFormatter.cpp \
                  TestCheckedInt.cpp \
                  TestTArray.cpp \
                  $(NULL)
 
 ifdef MOZ_MEMORY
 CPP_UNIT_TESTS += TestJemalloc.cpp
 endif
new file mode 100644
--- /dev/null
+++ b/xpcom/tests/TestSettingsAPI.cpp
@@ -0,0 +1,142 @@
+#include "TestHarness.h"
+
+#include "nsISettingsService.h"
+
+#include "nsCOMPtr.h"
+#include "nsIThread.h"
+#include "nsComponentManagerUtils.h"
+#include "nsDirectoryServiceDefs.h"
+#include "nsDirectoryServiceUtils.h"
+#include "nsThreadUtils.h"
+
+using namespace mozilla;
+
+static const char* gFunction = "main";
+
+static int callbackCount = 10;
+static int errors = 0;
+
+#define CHECK(x) \
+  _doCheck(x, #x, __LINE__)
+
+void _doCheck(bool cond, const char* msg, int line) {
+  if (cond) return;
+  fprintf(stderr, "FAIL: line %d: %s\n", line, msg);
+  errors++;
+}
+
+typedef nsresult(*TestFuncPtr)();
+
+class SettingsServiceCallback : public nsISettingsServiceCallback
+{
+public:
+  NS_DECL_ISUPPORTS
+
+  SettingsServiceCallback() { }
+
+  NS_IMETHOD Handle(const nsAString & name, const JS::Value & result, JSContext* cx) {
+    if (callbackCount == 9) {
+      CHECK(JSVAL_IS_BOOLEAN(result));
+      CHECK(JSVAL_TO_BOOLEAN(result) == true);
+      passed("boolean");
+    } else if (callbackCount == 7) {
+      CHECK(JSVAL_IS_BOOLEAN(result));
+      CHECK(JSVAL_TO_BOOLEAN(result) == false);
+      passed("Lock order");
+    } else if (callbackCount == 5) {
+      CHECK(JSVAL_IS_INT(result));
+      CHECK(JSVAL_TO_INT(result) == 9);
+      passed("integer");
+    }  else if (callbackCount == 3) {
+      CHECK(JSVAL_IS_DOUBLE(result));
+      CHECK(JSVAL_TO_DOUBLE(result) == 9.4);
+      passed("double");
+    } else if (callbackCount == 2) {
+      CHECK(JSVAL_IS_BOOLEAN(result));
+      CHECK(JSVAL_TO_BOOLEAN(result) == false);
+      passed("Lock order");
+    } else if (callbackCount == 1) {
+      passed("null");
+      CHECK(JSVAL_IS_NULL(result));
+    }
+    callbackCount--;
+    return NS_OK;
+  };
+
+  NS_IMETHOD HandleError(const nsAString & name, JSContext* cx) {
+    fprintf(stderr, "HANDLE Error! %s\n", NS_LossyConvertUTF16toASCII(name).get());
+    errors++;
+    return NS_OK;
+  };
+};
+
+NS_IMPL_THREADSAFE_ISUPPORTS1(SettingsServiceCallback, nsISettingsServiceCallback)
+
+nsresult
+TestSettingsAPI()
+{
+  nsresult rv;
+  nsCOMPtr<nsISettingsService> settingsService = do_CreateInstance("@mozilla.org/settingsService;1", &rv);
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  nsCOMPtr<nsISettingsServiceCallback> cb0 = new SettingsServiceCallback();
+  nsCOMPtr<nsISettingsServiceCallback> cb1 = new SettingsServiceCallback();
+  nsCOMPtr<nsISettingsServiceCallback> cb2 = new SettingsServiceCallback();
+  nsCOMPtr<nsISettingsServiceCallback> cb3 = new SettingsServiceCallback();
+  nsCOMPtr<nsISettingsServiceCallback> cb4 = new SettingsServiceCallback();
+  nsCOMPtr<nsISettingsServiceCallback> cb5 = new SettingsServiceCallback();
+  nsCOMPtr<nsISettingsServiceCallback> cb6 = new SettingsServiceCallback();
+  nsCOMPtr<nsISettingsServiceCallback> cb7 = new SettingsServiceCallback();
+  nsCOMPtr<nsISettingsServiceCallback> cb8 = new SettingsServiceCallback();
+  nsCOMPtr<nsISettingsServiceCallback> cb9 = new SettingsServiceCallback();
+
+  nsCOMPtr<nsISettingsService> iss = do_GetService("@mozilla.org/settingsService;1");
+  nsCOMPtr<nsISettingsServiceLock> lock;
+  iss->GetLock(getter_AddRefs(lock));
+
+  nsCOMPtr<nsISettingsServiceLock> lock1;
+  iss->GetLock(getter_AddRefs(lock1));
+
+  lock->Set("asdf", BOOLEAN_TO_JSVAL(true), cb0);
+  lock1->Get("asdf", cb1);
+  lock->Get("asdf", cb2);
+  lock->Set("asdf", BOOLEAN_TO_JSVAL(false), cb3);
+  lock->Get("asdf", cb4);
+  lock->Set("int", INT_TO_JSVAL(9), cb5);
+  lock->Get("int", cb6);
+  lock->Set("doub", DOUBLE_TO_JSVAL(9.4), cb7);
+  lock->Get("doub", cb8);
+  lock1->Get("asdfxxx", cb9);
+
+  return NS_OK;
+}
+
+int main(int argc, char** argv)
+{
+  passed("Start TestSettingsAPI");
+  ScopedXPCOM xpcom("TestSettingsAPI");
+  nsCOMPtr<nsIProperties> os =
+    do_GetService("@mozilla.org/file/directory_service;1");
+  if (!os) {
+    fprintf(stderr, "DIRSERVICE NULL!!!!!!\n");
+    return 1;
+  }
+
+  NS_ENSURE_FALSE(xpcom.failed(), 1);
+
+  static TestFuncPtr testsToRun[] = {
+    TestSettingsAPI
+  };
+  static PRUint32 testCount = sizeof(testsToRun) / sizeof(testsToRun[0]);
+
+  for (PRUint32 i = 0; i < testCount; i++) {
+    nsresult rv = testsToRun[i]();
+    NS_ENSURE_SUCCESS(rv, 1);
+  }
+  nsCOMPtr<nsIThread> current(do_GetCurrentThread());
+  while (callbackCount > 0)
+    NS_ProcessNextEvent(current);
+
+  passed("END!");
+  return errors;
+}