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 "bindings/v8/V8Utilities.h"
32 #include "modules/indexeddb/IDBKey.h"
33 #include "modules/indexeddb/IDBKeyPath.h"
35 #include <gtest/gtest.h>
37 using namespace WebCore;
41 PassRefPtr<IDBKey> checkKeyFromValueAndKeyPathInternal(const ScriptValue& value, const String& keyPath)
43 IDBKeyPath idbKeyPath(keyPath);
44 EXPECT_TRUE(idbKeyPath.isValid());
46 return createIDBKeyFromScriptValueAndKeyPath(0, value, idbKeyPath);
49 void checkKeyPathNullValue(const ScriptValue& value, const String& keyPath)
51 RefPtr<IDBKey> idbKey = checkKeyFromValueAndKeyPathInternal(value, keyPath);
52 ASSERT_FALSE(idbKey.get());
55 bool injectKey(PassRefPtr<IDBKey> key, ScriptValue& value, const String& keyPath)
57 IDBKeyPath idbKeyPath(keyPath);
58 EXPECT_TRUE(idbKeyPath.isValid());
59 ScriptValue keyValue = idbKeyToScriptValue(0, key);
60 return injectV8KeyIntoV8Value(keyValue.v8Value(), value.v8Value(), idbKeyPath, v8::Isolate::GetCurrent());
63 void checkInjection(PassRefPtr<IDBKey> prpKey, ScriptValue& value, const String& keyPath)
65 RefPtr<IDBKey> key = prpKey;
66 bool result = injectKey(key, value, keyPath);
68 RefPtr<IDBKey> extractedKey = checkKeyFromValueAndKeyPathInternal(value, keyPath);
69 EXPECT_TRUE(key->isEqual(extractedKey.get()));
72 void checkInjectionFails(PassRefPtr<IDBKey> key, ScriptValue& value, const String& keyPath)
74 EXPECT_FALSE(injectKey(key, value, keyPath));
77 void checkKeyPathStringValue(const ScriptValue& value, const String& keyPath, const String& expected)
79 RefPtr<IDBKey> idbKey = checkKeyFromValueAndKeyPathInternal(value, keyPath);
80 ASSERT_TRUE(idbKey.get());
81 ASSERT_EQ(IDBKey::StringType, idbKey->type());
82 ASSERT_TRUE(expected == idbKey->string());
85 void checkKeyPathNumberValue(const ScriptValue& value, const String& keyPath, int expected)
87 RefPtr<IDBKey> idbKey = checkKeyFromValueAndKeyPathInternal(value, keyPath);
88 ASSERT_TRUE(idbKey.get());
89 ASSERT_EQ(IDBKey::NumberType, idbKey->type());
90 ASSERT_TRUE(expected == idbKey->number());
93 class IDBKeyFromValueAndKeyPathTest : public testing::Test {
95 IDBKeyFromValueAndKeyPathTest()
96 : m_handleScope(v8::Isolate::GetCurrent())
97 , m_scope(v8::Context::New(v8::Isolate::GetCurrent()))
102 v8::HandleScope m_handleScope;
103 v8::Context::Scope m_scope;
106 TEST_F(IDBKeyFromValueAndKeyPathTest, TopLevelPropertyStringValue)
108 v8::Isolate* isolate = v8::Isolate::GetCurrent();
109 v8::Local<v8::Object> object = v8::Object::New(isolate);
110 object->Set(v8AtomicString(isolate, "foo"), v8AtomicString(isolate, "zoo"));
112 ScriptValue scriptValue(object, isolate);
114 checkKeyPathStringValue(scriptValue, "foo", "zoo");
115 checkKeyPathNullValue(scriptValue, "bar");
118 TEST_F(IDBKeyFromValueAndKeyPathTest, TopLevelPropertyNumberValue)
120 v8::Isolate* isolate = v8::Isolate::GetCurrent();
121 v8::Local<v8::Object> object = v8::Object::New(isolate);
122 object->Set(v8AtomicString(isolate, "foo"), v8::Number::New(isolate, 456));
124 ScriptValue scriptValue(object, isolate);
126 checkKeyPathNumberValue(scriptValue, "foo", 456);
127 checkKeyPathNullValue(scriptValue, "bar");
130 TEST_F(IDBKeyFromValueAndKeyPathTest, SubProperty)
132 v8::Isolate* isolate = v8::Isolate::GetCurrent();
133 v8::Local<v8::Object> object = v8::Object::New(isolate);
134 v8::Local<v8::Object> subProperty = v8::Object::New(isolate);
135 subProperty->Set(v8AtomicString(isolate, "bar"), v8AtomicString(isolate, "zee"));
136 object->Set(v8AtomicString(isolate, "foo"), subProperty);
138 ScriptValue scriptValue(object, isolate);
140 checkKeyPathStringValue(scriptValue, "foo.bar", "zee");
141 checkKeyPathNullValue(scriptValue, "bar");
144 class InjectIDBKeyTest : public IDBKeyFromValueAndKeyPathTest {
147 TEST_F(InjectIDBKeyTest, TopLevelPropertyStringValue)
149 v8::Isolate* isolate = v8::Isolate::GetCurrent();
150 v8::Local<v8::Object> object = v8::Object::New(isolate);
151 object->Set(v8AtomicString(isolate, "foo"), v8AtomicString(isolate, "zoo"));
153 ScriptValue foozoo(object, isolate);
154 checkInjection(IDBKey::createString("myNewKey"), foozoo, "bar");
155 checkInjection(IDBKey::createNumber(1234), foozoo, "bar");
157 checkInjectionFails(IDBKey::createString("key"), foozoo, "foo.bar");
160 TEST_F(InjectIDBKeyTest, SubProperty)
162 v8::Isolate* isolate = v8::Isolate::GetCurrent();
163 v8::Local<v8::Object> object = v8::Object::New(isolate);
164 v8::Local<v8::Object> subProperty = v8::Object::New(isolate);
165 subProperty->Set(v8AtomicString(isolate, "bar"), v8AtomicString(isolate, "zee"));
166 object->Set(v8AtomicString(isolate, "foo"), subProperty);
168 ScriptValue scriptObject(object, isolate);
169 checkInjection(IDBKey::createString("myNewKey"), scriptObject, "foo.baz");
170 checkInjection(IDBKey::createNumber(789), scriptObject, "foo.baz");
171 checkInjection(IDBKey::createDate(4567), scriptObject, "foo.baz");
172 checkInjection(IDBKey::createDate(4567), scriptObject, "bar");
173 checkInjection(IDBKey::createArray(IDBKey::KeyArray()), scriptObject, "foo.baz");
174 checkInjection(IDBKey::createArray(IDBKey::KeyArray()), scriptObject, "bar");
176 checkInjectionFails(IDBKey::createString("zoo"), scriptObject, "foo.bar.baz");
177 checkInjection(IDBKey::createString("zoo"), scriptObject, "foo.xyz.foo");