2 * Copyright (C) 2011 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "bindings/v8/IDBBindingUtilities.h"
29 #include "bindings/v8/V8Binding.h"
30 #include "bindings/v8/V8PerIsolateData.h"
31 #include "modules/indexeddb/IDBKey.h"
32 #include "modules/indexeddb/IDBKeyPath.h"
34 #include <gtest/gtest.h>
36 using namespace WebCore;
40 PassRefPtrWillBeRawPtr<IDBKey> checkKeyFromValueAndKeyPathInternal(v8::Isolate* isolate, const ScriptValue& value, const String& keyPath)
42 IDBKeyPath idbKeyPath(keyPath);
43 EXPECT_TRUE(idbKeyPath.isValid());
45 return createIDBKeyFromScriptValueAndKeyPath(isolate, value, idbKeyPath);
48 void checkKeyPathNullValue(v8::Isolate* isolate, const ScriptValue& value, const String& keyPath)
50 RefPtrWillBeRawPtr<IDBKey> idbKey = checkKeyFromValueAndKeyPathInternal(isolate, value, keyPath);
51 ASSERT_FALSE(idbKey.get());
54 bool injectKey(ScriptState* scriptState, PassRefPtrWillBeRawPtr<IDBKey> key, ScriptValue& value, const String& keyPath)
56 IDBKeyPath idbKeyPath(keyPath);
57 EXPECT_TRUE(idbKeyPath.isValid());
58 ScriptValue keyValue = idbKeyToScriptValue(scriptState, key);
59 return injectV8KeyIntoV8Value(scriptState->isolate(), keyValue.v8Value(), value.v8Value(), idbKeyPath);
62 void checkInjection(ScriptState* scriptState, PassRefPtrWillBeRawPtr<IDBKey> prpKey, ScriptValue& value, const String& keyPath)
64 RefPtrWillBeRawPtr<IDBKey> key = prpKey;
65 bool result = injectKey(scriptState, key, value, keyPath);
67 RefPtrWillBeRawPtr<IDBKey> extractedKey = checkKeyFromValueAndKeyPathInternal(scriptState->isolate(), value, keyPath);
68 EXPECT_TRUE(key->isEqual(extractedKey.get()));
71 void checkInjectionFails(ScriptState* scriptState, PassRefPtrWillBeRawPtr<IDBKey> key, ScriptValue& value, const String& keyPath)
73 EXPECT_FALSE(injectKey(scriptState, key, value, keyPath));
76 void checkKeyPathStringValue(v8::Isolate* isolate, const ScriptValue& value, const String& keyPath, const String& expected)
78 RefPtrWillBeRawPtr<IDBKey> idbKey = checkKeyFromValueAndKeyPathInternal(isolate, value, keyPath);
79 ASSERT_TRUE(idbKey.get());
80 ASSERT_EQ(IDBKey::StringType, idbKey->type());
81 ASSERT_TRUE(expected == idbKey->string());
84 void checkKeyPathNumberValue(v8::Isolate* isolate, const ScriptValue& value, const String& keyPath, int expected)
86 RefPtrWillBeRawPtr<IDBKey> idbKey = checkKeyFromValueAndKeyPathInternal(isolate, value, keyPath);
87 ASSERT_TRUE(idbKey.get());
88 ASSERT_EQ(IDBKey::NumberType, idbKey->type());
89 ASSERT_TRUE(expected == idbKey->number());
92 class IDBKeyFromValueAndKeyPathTest : public testing::Test {
94 IDBKeyFromValueAndKeyPathTest()
95 : m_scope(V8ExecutionScope::create(v8::Isolate::GetCurrent()))
99 OwnPtr<V8ExecutionScope> m_scope;
102 TEST_F(IDBKeyFromValueAndKeyPathTest, TopLevelPropertyStringValue)
104 v8::Isolate* isolate = v8::Isolate::GetCurrent();
105 v8::Local<v8::Object> object = v8::Object::New(isolate);
106 object->Set(v8AtomicString(isolate, "foo"), v8AtomicString(isolate, "zoo"));
108 ScriptValue scriptValue(ScriptState::current(isolate), object);
110 checkKeyPathStringValue(isolate, scriptValue, "foo", "zoo");
111 checkKeyPathNullValue(isolate, scriptValue, "bar");
114 TEST_F(IDBKeyFromValueAndKeyPathTest, TopLevelPropertyNumberValue)
116 v8::Isolate* isolate = v8::Isolate::GetCurrent();
117 v8::Local<v8::Object> object = v8::Object::New(isolate);
118 object->Set(v8AtomicString(isolate, "foo"), v8::Number::New(isolate, 456));
120 ScriptValue scriptValue(ScriptState::current(isolate), object);
122 checkKeyPathNumberValue(isolate, scriptValue, "foo", 456);
123 checkKeyPathNullValue(isolate, scriptValue, "bar");
126 TEST_F(IDBKeyFromValueAndKeyPathTest, SubProperty)
128 v8::Isolate* isolate = v8::Isolate::GetCurrent();
129 v8::Local<v8::Object> object = v8::Object::New(isolate);
130 v8::Local<v8::Object> subProperty = v8::Object::New(isolate);
131 subProperty->Set(v8AtomicString(isolate, "bar"), v8AtomicString(isolate, "zee"));
132 object->Set(v8AtomicString(isolate, "foo"), subProperty);
134 ScriptValue scriptValue(ScriptState::current(isolate), object);
136 checkKeyPathStringValue(isolate, scriptValue, "foo.bar", "zee");
137 checkKeyPathNullValue(isolate, scriptValue, "bar");
140 class InjectIDBKeyTest : public IDBKeyFromValueAndKeyPathTest {
143 TEST_F(InjectIDBKeyTest, TopLevelPropertyStringValue)
145 v8::Isolate* isolate = v8::Isolate::GetCurrent();
146 ScriptState* scriptState = ScriptState::current(isolate);
147 v8::Local<v8::Object> object = v8::Object::New(isolate);
148 object->Set(v8AtomicString(isolate, "foo"), v8AtomicString(isolate, "zoo"));
150 ScriptValue foozoo(object, isolate);
151 checkInjection(scriptState, IDBKey::createString("myNewKey"), foozoo, "bar");
152 checkInjection(scriptState, IDBKey::createNumber(1234), foozoo, "bar");
154 checkInjectionFails(scriptState, IDBKey::createString("key"), foozoo, "foo.bar");
157 TEST_F(InjectIDBKeyTest, SubProperty)
159 v8::Isolate* isolate = v8::Isolate::GetCurrent();
160 ScriptState* scriptState = ScriptState::current(isolate);
161 v8::Local<v8::Object> object = v8::Object::New(isolate);
162 v8::Local<v8::Object> subProperty = v8::Object::New(isolate);
163 subProperty->Set(v8AtomicString(isolate, "bar"), v8AtomicString(isolate, "zee"));
164 object->Set(v8AtomicString(isolate, "foo"), subProperty);
166 ScriptValue scriptObject(object, isolate);
167 checkInjection(scriptState, IDBKey::createString("myNewKey"), scriptObject, "foo.baz");
168 checkInjection(scriptState, IDBKey::createNumber(789), scriptObject, "foo.baz");
169 checkInjection(scriptState, IDBKey::createDate(4567), scriptObject, "foo.baz");
170 checkInjection(scriptState, IDBKey::createDate(4567), scriptObject, "bar");
171 checkInjection(scriptState, IDBKey::createArray(IDBKey::KeyArray()), scriptObject, "foo.baz");
172 checkInjection(scriptState, IDBKey::createArray(IDBKey::KeyArray()), scriptObject, "bar");
174 checkInjectionFails(scriptState, IDBKey::createString("zoo"), scriptObject, "foo.bar.baz");
175 checkInjection(scriptState, IDBKey::createString("zoo"), scriptObject, "foo.xyz.foo");