IndexedDB: tests for injection/extraction of idb keys
authorcommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 23 Aug 2012 01:49:56 +0000 (01:49 +0000)
committercommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 23 Aug 2012 01:49:56 +0000 (01:49 +0000)
https://bugs.webkit.org/show_bug.cgi?id=94653

Patch by Alec Flett <alecflett@chromium.org> on 2012-08-22
Reviewed by Tony Chang.

Added unit tests for key injection/extraction using
SerializedScriptValue. These were moved from chromium, now
that all uses of the API are through WebKit.

* tests/IDBKeyPathTest.cpp:
(WebCore):
(WebCore::TEST):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@126381 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebKit/chromium/ChangeLog
Source/WebKit/chromium/tests/IDBKeyPathTest.cpp

index 253cce6..41b2bb9 100644 (file)
@@ -1,3 +1,18 @@
+2012-08-22  Alec Flett  <alecflett@chromium.org>
+
+        IndexedDB: tests for injection/extraction of idb keys
+        https://bugs.webkit.org/show_bug.cgi?id=94653
+
+        Reviewed by Tony Chang.
+
+        Added unit tests for key injection/extraction using
+        SerializedScriptValue. These were moved from chromium, now
+        that all uses of the API are through WebKit.
+
+        * tests/IDBKeyPathTest.cpp:
+        (WebCore):
+        (WebCore::TEST):
+
 2012-08-22  James Robinson  <jamesr@chromium.org>
 
         [chromium] Change WebLayer from a concrete type to a pure virtual interface
index 7d662c0..c3d47d7 100644 (file)
 #include "config.h"
 #include "IDBKeyPath.h"
 
+#include "IDBBindingUtilities.h"
+#include "IDBKey.h"
+#include "SerializedScriptValue.h"
+
 #include <gtest/gtest.h>
 #include <wtf/Vector.h>
 
@@ -123,6 +127,92 @@ TEST(IDBKeyPathTest, InvalidKeyPath5)
     checkKeyPath(keyPath, expected, 3);
 }
 
+TEST(IDBKeyPathTest, Extract)
+{
+    IDBKeyPath keyPath("foo");
+    RefPtr<IDBKey> stringZooKey(IDBKey::createString("zoo"));
+    RefPtr<IDBKey> invalidKey(IDBKey::createInvalid());
+    RefPtr<SerializedScriptValue> ssv;
+    RefPtr<IDBKey> result;
+
+    // keypath: "foo", value: {foo: "zoo"}, expected: "zoo"
+    UChar dataFooZoo[] = {0x0353, 0x6f66, 0x536f, 0x7a03, 0x6f6f, 0x017b};
+    ssv = SerializedScriptValue::createFromWire(String(dataFooZoo, WTF_ARRAY_LENGTH(dataFooZoo)));
+    result = createIDBKeyFromSerializedValueAndKeyPath(ssv, keyPath);
+    EXPECT_TRUE(stringZooKey->isEqual(result.get()));
+
+    // keypath: "foo", value: {foo: null}, expected: invalid
+    UChar dataFooNull[] = {0x0353, 0x6f66, 0x306f, 0x017b};
+    ssv = SerializedScriptValue::createFromWire(String(dataFooNull, WTF_ARRAY_LENGTH(dataFooNull)));
+    result = createIDBKeyFromSerializedValueAndKeyPath(ssv, keyPath);
+    EXPECT_FALSE(result->isValid());
+
+    // keypath: "foo", value: {}, expected: null
+    UChar dataObject[] = {0x017b};
+    ssv = SerializedScriptValue::createFromWire(String(dataObject, WTF_ARRAY_LENGTH(dataObject)));
+    result = createIDBKeyFromSerializedValueAndKeyPath(ssv, keyPath);
+    EXPECT_EQ(0, result.get());
+
+    // keypath: "foo", value: null, expected: null
+    ssv = SerializedScriptValue::nullValue();
+    result = createIDBKeyFromSerializedValueAndKeyPath(ssv, keyPath);
+    EXPECT_EQ(0, result.get());
+}
+
+TEST(IDBKeyPathTest, IDBKeyPathPropertyNotAvailable)
+{
+    IDBKeyPath keyPath("PropertyNotAvailable");
+    RefPtr<SerializedScriptValue> ssv;
+    // {foo: "zoo", bar: null}
+    UChar data[] = {0x0353, 0x6f66, 0x536f, 0x7a03, 0x6f6f, 0x0353, 0x6162,
+                    0x3072, 0x027b};
+    ssv = SerializedScriptValue::createFromWire(String(data, WTF_ARRAY_LENGTH(data)));
+    RefPtr<IDBKey> result = createIDBKeyFromSerializedValueAndKeyPath(ssv, keyPath);
+    EXPECT_EQ(0, result.get());
+
+    // null
+    ssv = SerializedScriptValue::nullValue();
+    result = createIDBKeyFromSerializedValueAndKeyPath(ssv, keyPath);
+    EXPECT_EQ(0, result.get());
+}
+
+TEST(IDBKeyPathTest, InjectIDBKey)
+{
+    // {foo: 'zoo'}
+    const UChar initialData[] = {0x0353, 0x6f66, 0x536f, 0x7a03, 0x6f6f, 0x017b};
+    RefPtr<SerializedScriptValue> value = SerializedScriptValue::createFromWire(String(initialData, WTF_ARRAY_LENGTH(initialData)));
+
+    RefPtr<IDBKey> key = IDBKey::createString("myNewKey");
+    IDBKeyPath keyPath("bar");
+
+    // {foo: 'zoo', bar: 'myNewKey'}
+    const UChar expectedData[] = {0x01ff, 0x003f, 0x3f6f, 0x5301, 0x6603,
+                                  0x6f6f, 0x013f, 0x0353, 0x6f7a, 0x3f6f,
+                                  0x5301, 0x6203, 0x7261, 0x013f, 0x0853,
+                                  0x796d, 0x654e, 0x4b77, 0x7965, 0x027b};
+    RefPtr<SerializedScriptValue> expectedValue = 
+            SerializedScriptValue::createFromWire(String(expectedData, WTF_ARRAY_LENGTH(expectedData)));
+    RefPtr<SerializedScriptValue> result = injectIDBKeyIntoSerializedValue(key, value, keyPath);
+    EXPECT_EQ(expectedValue->toWireString(), result->toWireString());
+
+    // Should fail - can't apply properties to string value of key foo
+    keyPath = IDBKeyPath("foo.bad.path");
+    result = injectIDBKeyIntoSerializedValue(key, value, keyPath);
+    EXPECT_EQ(0, result.get());
+
+    // {foo: 'zoo', bar: {baz: 'myNewKey'}}
+    const UChar expectedData2[] = {0x01ff, 0x003f, 0x3f6f, 0x5301, 0x6603,
+                                   0x6f6f, 0x013f, 0x0353, 0x6f7a, 0x3f6f,
+                                   0x5301, 0x6203, 0x7261, 0x013f, 0x3f6f,
+                                   0x5302, 0x6203, 0x7a61, 0x023f, 0x0853,
+                                   0x796d, 0x654e, 0x4b77, 0x7965, 0x017b,
+                                   0x027b};
+    RefPtr<SerializedScriptValue> expectedValue2 = SerializedScriptValue::createFromWire(String(expectedData2, WTF_ARRAY_LENGTH(expectedData2)));
+    keyPath = IDBKeyPath("bar.baz");
+    result = injectIDBKeyIntoSerializedValue(key, value, keyPath);
+    EXPECT_EQ(expectedValue2->toWireString(), result->toWireString());
+}
+
 } // namespace
 
 #endif // ENABLE(INDEXED_DATABASE)