1 /*
2 * Copyright (C) 2013 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 are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "config.h"
32
33 #include "V8Event.h"
34 #include "bindings/v8/ExceptionStatePlaceholder.h"
35 #include "bindings/v8/ScriptController.h"
36 #include "bindings/v8/SerializedScriptValue.h"
37 #include "bindings/v8/V8AbstractEventListener.h"
38 #include "public/platform/Platform.h"
39 #include "public/platform/WebUnitTestSupport.h"
40 #include "public/web/WebDOMCustomEvent.h"
41 #include "public/web/WebFrame.h"
42 #include "public/web/WebView.h"
43 #include "v8.h"
44 #include "web/WebLocalFrameImpl.h"
45 #include "web/WebViewImpl.h"
46 #include "web/tests/FrameTestHelpers.h"
47 #include "web/tests/URLTestHelpers.h"
48
49 #include <gtest/gtest.h>
50
51 using namespace WebCore;
52 using namespace blink;
53
54 namespace {
55
56 class TestListener : public V8AbstractEventListener {
57 public:
operator ==(const EventListener &)58 virtual bool operator==(const EventListener&)
59 {
60 return true;
61 }
62
handleEvent(ExecutionContext * context,Event * event)63 virtual void handleEvent(ExecutionContext* context, Event* event)
64 {
65 EXPECT_EQ(event->type(), "blah");
66
67 ScriptState::Scope scope(scriptState());
68 v8::Handle<v8::Value> jsEvent = toV8(event, scriptState()->context()->Global(), isolate());
69
70 EXPECT_EQ(jsEvent->ToObject()->Get(v8::String::NewFromUtf8(scriptState()->isolate(), "detail")), v8::Boolean::New(scriptState()->isolate(), true));
71 }
72
create(ScriptState * scriptState)73 static PassRefPtr<TestListener> create(ScriptState* scriptState)
74 {
75 return adoptRef(new TestListener(scriptState));
76 }
77
78 private:
TestListener(ScriptState * scriptState)79 TestListener(ScriptState* scriptState)
80 : V8AbstractEventListener(false, scriptState)
81 {
82 }
83
callListenerFunction(v8::Handle<v8::Value> jsevent,Event *)84 virtual v8::Local<v8::Value> callListenerFunction(v8::Handle<v8::Value> jsevent, Event*)
85 {
86 ASSERT_NOT_REACHED();
87 return v8::Local<v8::Value>();
88 }
89 };
90
91 // Tests that a CustomEvent can be initialized with a 'detail' value that is a
92 // SerializedScriptValue, and that this value can be retrieved and read in an
93 // event listener. Initialization of a CustomEvent with a SerializedScriptValue
94 // is an internal API, so it cannot be tested with a LayoutTest.
95 //
96 // Triggers crash in GC. http://crbug.com/317669
TEST(CustomEventTest,InitWithSerializedScriptValue)97 TEST(CustomEventTest, InitWithSerializedScriptValue)
98 {
99 const std::string baseURL = "http://www.test.com";
100 const std::string path = "visible_iframe.html";
101
102 URLTestHelpers::registerMockedURLFromBaseURL(WebString::fromUTF8(baseURL.c_str()), WebString::fromUTF8(path.c_str()));
103 FrameTestHelpers::WebViewHelper webViewHelper;
104 WebLocalFrameImpl* frame = toWebLocalFrameImpl(webViewHelper.initializeAndLoad(baseURL + path)->mainFrame());
105 WebDOMEvent event = frame->frame()->document()->createEvent("CustomEvent", IGNORE_EXCEPTION);
106 WebDOMCustomEvent customEvent = event.to<WebDOMCustomEvent>();
107
108 v8::Isolate* isolate = toIsolate(frame->frame());
109 V8TestingScope scope(isolate);
110 customEvent.initCustomEvent("blah", false, false, WebSerializedScriptValue::serialize(v8::Boolean::New(isolate, true)));
111 RefPtr<EventListener> listener = TestListener::create(scope.scriptState());
112 frame->frame()->document()->addEventListener("blah", listener, false);
113 frame->frame()->document()->dispatchEvent(event);
114
115 Platform::current()->unitTestSupport()->unregisterAllMockedURLs();
116 }
117
118 }
119