Bug 692642: IndexedDB: support indexedDB.cmp. r=sicking.
authorBen Turner <bent.mozilla@gmail.com>
Tue, 08 Nov 2011 11:03:18 -0800
changeset 80057 938630505470610b53542221129668df77255f52
parent 80056 e0add41f9a8f140fa77b5d530fe3c7b7dd6745f9
child 80058 b259af61fe3f2777f13565b97d4377851f1979ae
push id323
push userrcampbell@mozilla.com
push dateTue, 15 Nov 2011 21:58:36 +0000
treeherderfx-team@3ea216303184 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssicking
bugs692642
milestone10.0a1
Bug 692642: IndexedDB: support indexedDB.cmp. r=sicking.
dom/indexedDB/IDBFactory.cpp
dom/indexedDB/nsIIDBFactory.idl
dom/indexedDB/test/Makefile.in
dom/indexedDB/test/test_cmp.html
--- a/dom/indexedDB/IDBFactory.cpp
+++ b/dom/indexedDB/IDBFactory.cpp
@@ -62,16 +62,17 @@
 
 #include "AsyncConnectionHelper.h"
 #include "CheckPermissionsHelper.h"
 #include "DatabaseInfo.h"
 #include "IDBDatabase.h"
 #include "IDBEvents.h"
 #include "IDBKeyRange.h"
 #include "IndexedDatabaseManager.h"
+#include "Key.h"
 #include "LazyIdleThread.h"
 #include "nsIScriptSecurityManager.h"
 
 using namespace mozilla;
 
 USING_INDEXEDDB_NAMESPACE
 
 namespace {
@@ -458,8 +459,29 @@ IDBFactory::Open(const nsAString& aName,
 }
 
 NS_IMETHODIMP
 IDBFactory::DeleteDatabase(const nsAString& aName,
                            nsIIDBOpenDBRequest** _retval)
 {
   return OpenCommon(aName, 0, true, _retval);
 }
+
+NS_IMETHODIMP
+IDBFactory::Cmp(const jsval& aFirst,
+                const jsval& aSecond,
+                JSContext* aCx,
+                PRInt16* _retval)
+{
+  Key first, second;
+  nsresult rv = first.SetFromJSVal(aCx, aFirst);
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  rv = second.SetFromJSVal(aCx, aSecond);
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  if (first.IsUnset() || second.IsUnset()) {
+    return NS_ERROR_DOM_INDEXEDDB_DATA_ERR;
+  }
+
+  *_retval = first == second ? 0 : first < second ? -1 : 1;
+  return NS_OK;
+}
--- a/dom/indexedDB/nsIIDBFactory.idl
+++ b/dom/indexedDB/nsIIDBFactory.idl
@@ -53,9 +53,14 @@ interface nsIIDBFactory : nsISupports
 {
   [optional_argc]
   nsIIDBOpenDBRequest
   open([Null(Stringify)] in DOMString name,
        [optional] in long long version);
 
   nsIIDBOpenDBRequest
   deleteDatabase(in AString name);
+
+  [implicit_jscontext]
+  short
+  cmp(in jsval first,
+      in jsval second);
 };
--- a/dom/indexedDB/test/Makefile.in
+++ b/dom/indexedDB/test/Makefile.in
@@ -53,16 +53,17 @@ TEST_FILES = \
   helpers.js \
   leaving_page_iframe.html \
   test_add_twice_failure.html \
   test_advance.html \
   test_autoIncrement_indexes.html \
   test_bad_keypath.html \
   test_bfcache.html \
   test_clear.html \
+  test_cmp.html \
   test_count.html \
   test_create_index.html \
   test_create_index_with_integer_keys.html \
   test_create_objectStore.html \
   test_cursors.html \
   test_cursor_mutation.html \
   test_cursor_update_updates_indexes.html \
   test_deleteDatabase.html \
new file mode 100644
--- /dev/null
+++ b/dom/indexedDB/test/test_cmp.html
@@ -0,0 +1,85 @@
+<!--
+  Any copyright is dedicated to the Public Domain.
+  http://creativecommons.org/publicdomain/zero/1.0/
+-->
+<html>
+<head>
+  <title>Indexed Database Property Test</title>
+
+  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
+
+  <script type="text/javascript;version=1.7">
+    function testSteps()
+    {
+      function compare(key1, key2, expected, exception) {
+        function maybeQuote(key) {
+          return typeof(key) == "string" ? "\"" + key + "\"" : key;
+        }
+
+        var cmp = "cmp(" + maybeQuote(key1) + ", " + maybeQuote(key2) + ")";
+
+        if (exception) {
+          var caught;
+          try {
+            var result = mozIndexedDB.cmp(key1, key2);
+          }
+          catch(e) {
+            caught = e;
+          }
+          ok(caught, "Got an exception for " + cmp);
+          is(caught instanceof IDBDatabaseException, true,
+             "Got IDBDatabaseException for " + cmp);
+          is(caught.code, IDBDatabaseException.DATA_ERR,
+             "Got correct exception code for " + cmp);
+        }
+        else {
+          is(mozIndexedDB.cmp(key1, key2), expected,
+             "Correct result for " + cmp);
+        }
+      }
+
+      compare(NaN, 0, 0, true);
+      compare(0, NaN, 0, true);
+      compare(undefined, 0, 0, true);
+      compare(0, undefined, 0, true);
+      compare(null, 0, 0, true);
+      compare(0, null, 0, true);
+
+      compare(0, 0, 0);
+      compare(1, 0, 1);
+      compare(0, 1, -1);
+      compare(1, 1, 0);
+      compare(2, 1, 1);
+      compare(1, 2, -1);
+      compare(-1, -1, 0);
+      compare(0, -1, 1);
+      compare(-1, 0, -1);
+
+      compare("", "", 0);
+      compare("a", "", 1);
+      compare("", "a", -1);
+      compare("a", "a", 0);
+      compare("a", "b", -1);
+      compare("b", "a", 1);
+      compare("a", "aa", -1);
+      compare("aa", "a", 1);
+
+      compare(0, "", -1);
+      compare("", 0, 1);
+      compare(0, "a", -1);
+      compare("a", 0, 1);
+      compare(99999, "", -1);
+      compare("", 99999, 1);
+
+      finishTest();
+      yield;
+    }
+  </script>
+  <script type="text/javascript;version=1.7" src="helpers.js"></script>
+
+</head>
+
+<body onload="runTest();"></body>
+
+</html>