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.
29 #include "FrameTestHelpers.h"
30 #include "IDBBindingUtilities.h"
32 #include "IDBKeyPath.h"
33 #include "V8Binding.h"
34 #include "V8PerIsolateData.h"
35 #include "V8Utilities.h"
37 #include "WebFrameImpl.h"
39 #include "WorldContextHandle.h"
41 #include <gtest/gtest.h>
42 #include <wtf/Vector.h>
44 #if ENABLE(INDEXED_DATABASE)
46 using namespace WebCore;
47 using namespace WebKit;
51 PassRefPtr<IDBKey> checkKeyFromValueAndKeyPathInternal(const ScriptValue& value, const String& keyPath)
53 IDBKeyPath idbKeyPath(keyPath);
54 EXPECT_TRUE(idbKeyPath.isValid());
56 return createIDBKeyFromScriptValueAndKeyPath(0, value, idbKeyPath);
59 void checkKeyPathNullValue(const ScriptValue& value, const String& keyPath)
61 RefPtr<IDBKey> idbKey = checkKeyFromValueAndKeyPathInternal(value, keyPath);
62 ASSERT_FALSE(idbKey.get());
65 bool injectKey(PassRefPtr<IDBKey> key, ScriptValue& value, const String& keyPath)
67 IDBKeyPath idbKeyPath(keyPath);
68 EXPECT_TRUE(idbKeyPath.isValid());
69 return injectIDBKeyIntoScriptValue(0, key, value, idbKeyPath);
72 void checkInjection(PassRefPtr<IDBKey> prpKey, ScriptValue& value, const String& keyPath)
74 RefPtr<IDBKey> key = prpKey;
75 bool result = injectKey(key, value, keyPath);
77 RefPtr<IDBKey> extractedKey = checkKeyFromValueAndKeyPathInternal(value, keyPath);
78 EXPECT_TRUE(key->isEqual(extractedKey.get()));
81 void checkInjectionFails(PassRefPtr<IDBKey> key, ScriptValue& value, const String& keyPath)
83 EXPECT_FALSE(injectKey(key, value, keyPath));
86 void checkKeyPathStringValue(const ScriptValue& value, const String& keyPath, const String& expected)
88 RefPtr<IDBKey> idbKey = checkKeyFromValueAndKeyPathInternal(value, keyPath);
89 ASSERT_TRUE(idbKey.get());
90 ASSERT_EQ(IDBKey::StringType, idbKey->type());
91 ASSERT_TRUE(expected == idbKey->string());
94 void checkKeyPathNumberValue(const ScriptValue& value, const String& keyPath, int expected)
96 RefPtr<IDBKey> idbKey = checkKeyFromValueAndKeyPathInternal(value, keyPath);
97 ASSERT_TRUE(idbKey.get());
98 ASSERT_EQ(IDBKey::NumberType, idbKey->type());
99 ASSERT_TRUE(expected == idbKey->number());
102 class IDBKeyFromValueAndKeyPathTest : public testing::Test {
104 IDBKeyFromValueAndKeyPathTest()
109 void SetUp() OVERRIDE
111 m_webView = FrameTestHelpers::createWebViewAndLoad("about:blank");
112 m_webView->setFocus(true);
115 void TearDown() OVERRIDE
120 v8::Handle<v8::Context> context()
122 return static_cast<WebFrameImpl*>(m_webView->mainFrame())->frame()->script()->mainWorldContext();
129 TEST_F(IDBKeyFromValueAndKeyPathTest, TopLevelPropertyStringValue)
131 v8::HandleScope handleScope;
132 v8::Context::Scope scope(context());
134 v8::Local<v8::Object> object = v8::Object::New();
135 object->Set(v8::String::New("foo"), v8::String::New("zoo"));
137 ScriptValue scriptValue(object);
139 checkKeyPathStringValue(scriptValue, "foo", "zoo");
140 checkKeyPathNullValue(scriptValue, "bar");
143 TEST_F(IDBKeyFromValueAndKeyPathTest, TopLevelPropertyNumberValue)
145 v8::HandleScope handleScope;
146 v8::Context::Scope scope(context());
148 v8::Local<v8::Object> object = v8::Object::New();
149 object->Set(v8::String::New("foo"), v8::Number::New(456));
151 ScriptValue scriptValue(object);
153 checkKeyPathNumberValue(scriptValue, "foo", 456);
154 checkKeyPathNullValue(scriptValue, "bar");
157 TEST_F(IDBKeyFromValueAndKeyPathTest, SubProperty)
159 v8::HandleScope handleScope;
160 v8::Context::Scope scope(context());
162 v8::Local<v8::Object> object = v8::Object::New();
163 v8::Local<v8::Object> subProperty = v8::Object::New();
164 subProperty->Set(v8::String::New("bar"), v8::String::New("zee"));
165 object->Set(v8::String::New("foo"), subProperty);
167 ScriptValue scriptValue(object);
169 checkKeyPathStringValue(scriptValue, "foo.bar", "zee");
170 checkKeyPathNullValue(scriptValue, "bar");
173 class InjectIDBKeyTest : public IDBKeyFromValueAndKeyPathTest {
176 TEST_F(InjectIDBKeyTest, DISABLED_TopLevelPropertyStringValue)
178 v8::HandleScope handleScope;
179 v8::Context::Scope scope(context());
181 v8::Local<v8::Object> object = v8::Object::New();
182 object->Set(v8::String::New("foo"), v8::String::New("zoo"));
184 ScriptValue foozoo(object);
185 checkInjection(IDBKey::createString("myNewKey"), foozoo, "bar");
186 checkInjection(IDBKey::createNumber(1234), foozoo, "bar");
188 checkInjectionFails(IDBKey::createString("key"), foozoo, "foo.bar");
191 TEST_F(InjectIDBKeyTest, DISABLED_SubProperty)
193 v8::HandleScope handleScope;
194 v8::Context::Scope scope(context());
196 v8::Local<v8::Object> object = v8::Object::New();
197 v8::Local<v8::Object> subProperty = v8::Object::New();
198 subProperty->Set(v8::String::New("bar"), v8::String::New("zee"));
199 object->Set(v8::String::New("foo"), subProperty);
201 ScriptValue scriptObject(object);
202 checkInjection(IDBKey::createString("myNewKey"), scriptObject, "foo.baz");
203 checkInjection(IDBKey::createNumber(789), scriptObject, "foo.baz");
204 checkInjection(IDBKey::createDate(4567), scriptObject, "foo.baz");
205 checkInjection(IDBKey::createDate(4567), scriptObject, "bar");
206 checkInjection(IDBKey::createArray(IDBKey::KeyArray()), scriptObject, "foo.baz");
207 checkInjection(IDBKey::createArray(IDBKey::KeyArray()), scriptObject, "bar");
209 checkInjectionFails(IDBKey::createString("zoo"), scriptObject, "foo.bar.baz");
210 checkInjection(IDBKey::createString("zoo"), scriptObject, "foo.xyz.foo");
215 #endif // ENABLE(INDEXED_DATABASE)