WinCairo build fails to link.
[WebKit-https.git] / Source / WebKit / chromium / tests / IDBBindingUtilitiesTest.cpp
1 /*
2  * Copyright (C) 2011 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
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.
13  *
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.
24  */
25
26 #include "config.h"
27 #include "Document.h"
28 #include "Frame.h"
29 #include "FrameTestHelpers.h"
30 #include "IDBBindingUtilities.h"
31 #include "IDBKey.h"
32 #include "IDBKeyPath.h"
33 #include "V8Binding.h"
34 #include "V8PerIsolateData.h"
35 #include "V8Utilities.h"
36 #include "WebFrame.h"
37 #include "WebFrameImpl.h"
38 #include "WebView.h"
39 #include "WorldContextHandle.h"
40
41 #include <gtest/gtest.h>
42 #include <wtf/Vector.h>
43
44 #if ENABLE(INDEXED_DATABASE)
45
46 using namespace WebCore;
47 using namespace WebKit;
48
49 namespace {
50
51 PassRefPtr<IDBKey> checkKeyFromValueAndKeyPathInternal(const ScriptValue& value, const String& keyPath)
52 {
53     IDBKeyPath idbKeyPath(keyPath);
54     EXPECT_TRUE(idbKeyPath.isValid());
55     
56     return createIDBKeyFromScriptValueAndKeyPath(0, value, idbKeyPath);
57 }
58
59 void checkKeyPathNullValue(const ScriptValue& value, const String& keyPath)
60 {
61     RefPtr<IDBKey> idbKey = checkKeyFromValueAndKeyPathInternal(value, keyPath);
62     ASSERT_FALSE(idbKey.get());
63 }
64
65 bool injectKey(PassRefPtr<IDBKey> key, ScriptValue& value, const String& keyPath)
66 {
67     IDBKeyPath idbKeyPath(keyPath);
68     EXPECT_TRUE(idbKeyPath.isValid());
69     return injectIDBKeyIntoScriptValue(0, key, value, idbKeyPath);
70 }
71
72 void checkInjection(PassRefPtr<IDBKey> prpKey, ScriptValue& value, const String& keyPath)
73 {
74     RefPtr<IDBKey> key = prpKey;
75     bool result = injectKey(key, value, keyPath);
76     ASSERT_TRUE(result);
77     RefPtr<IDBKey> extractedKey = checkKeyFromValueAndKeyPathInternal(value, keyPath);
78     EXPECT_TRUE(key->isEqual(extractedKey.get()));
79 }
80
81 void checkInjectionFails(PassRefPtr<IDBKey> key, ScriptValue& value, const String& keyPath)
82 {
83     EXPECT_FALSE(injectKey(key, value, keyPath));
84 }
85
86 void checkKeyPathStringValue(const ScriptValue& value, const String& keyPath, const String& expected)
87 {
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());
92 }
93
94 void checkKeyPathNumberValue(const ScriptValue& value, const String& keyPath, int expected)
95 {
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());
100 }
101
102 class IDBKeyFromValueAndKeyPathTest : public testing::Test {
103 public:
104     IDBKeyFromValueAndKeyPathTest()
105         : m_webView(0)
106     {
107     }
108
109     void SetUp() OVERRIDE
110     {
111         m_webView = FrameTestHelpers::createWebViewAndLoad("about:blank");
112         m_webView->setFocus(true);
113     }
114
115     void TearDown() OVERRIDE
116     {
117         m_webView->close();
118     }
119
120     v8::Handle<v8::Context> context()
121     {
122         return static_cast<WebFrameImpl*>(m_webView->mainFrame())->frame()->script()->mainWorldContext();
123     }
124
125 private:
126     WebView* m_webView;
127 };
128
129 TEST_F(IDBKeyFromValueAndKeyPathTest, TopLevelPropertyStringValue)
130 {
131     v8::HandleScope handleScope;
132     v8::Context::Scope scope(context());
133
134     v8::Local<v8::Object> object = v8::Object::New();
135     object->Set(v8::String::New("foo"), v8::String::New("zoo"));
136
137     ScriptValue scriptValue(object);
138
139     checkKeyPathStringValue(scriptValue, "foo", "zoo");
140     checkKeyPathNullValue(scriptValue, "bar");
141 }
142
143 TEST_F(IDBKeyFromValueAndKeyPathTest, TopLevelPropertyNumberValue)
144 {
145     v8::HandleScope handleScope;
146     v8::Context::Scope scope(context());
147
148     v8::Local<v8::Object> object = v8::Object::New();
149     object->Set(v8::String::New("foo"), v8::Number::New(456));
150
151     ScriptValue scriptValue(object);
152
153     checkKeyPathNumberValue(scriptValue, "foo", 456);
154     checkKeyPathNullValue(scriptValue, "bar");
155 }
156
157 TEST_F(IDBKeyFromValueAndKeyPathTest, SubProperty)
158 {
159     v8::HandleScope handleScope;
160     v8::Context::Scope scope(context());
161
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);
166
167     ScriptValue scriptValue(object);
168
169     checkKeyPathStringValue(scriptValue, "foo.bar", "zee");
170     checkKeyPathNullValue(scriptValue, "bar");
171 }
172
173 class InjectIDBKeyTest : public IDBKeyFromValueAndKeyPathTest {
174 };
175
176 TEST_F(InjectIDBKeyTest, DISABLED_TopLevelPropertyStringValue)
177 {
178     v8::HandleScope handleScope;
179     v8::Context::Scope scope(context());
180
181     v8::Local<v8::Object> object = v8::Object::New();
182     object->Set(v8::String::New("foo"), v8::String::New("zoo"));
183
184     ScriptValue foozoo(object);
185     checkInjection(IDBKey::createString("myNewKey"), foozoo, "bar");
186     checkInjection(IDBKey::createNumber(1234), foozoo, "bar");
187
188     checkInjectionFails(IDBKey::createString("key"), foozoo, "foo.bar");
189 }
190
191 TEST_F(InjectIDBKeyTest, DISABLED_SubProperty)
192 {
193     v8::HandleScope handleScope;
194     v8::Context::Scope scope(context());
195
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);
200
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");
208
209     checkInjectionFails(IDBKey::createString("zoo"), scriptObject, "foo.bar.baz");
210     checkInjection(IDBKey::createString("zoo"), scriptObject, "foo.xyz.foo");
211 }
212
213 } // namespace
214
215 #endif // ENABLE(INDEXED_DATABASE)