Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / web / tests / CustomEventTest.cpp
blob38809ea1c66450baaec92ce6515f6dc93b10437b
1 /*
2 * Copyright (C) 2013 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 are
6 * met:
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.
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.
31 #include "config.h"
33 #include "bindings/core/v8/ExceptionStatePlaceholder.h"
34 #include "bindings/core/v8/ScriptController.h"
35 #include "bindings/core/v8/SerializedScriptValue.h"
36 #include "bindings/core/v8/V8AbstractEventListener.h"
37 #include "bindings/core/v8/V8BindingForTesting.h"
38 #include "bindings/core/v8/V8Event.h"
39 #include "platform/testing/URLTestHelpers.h"
40 #include "public/platform/Platform.h"
41 #include "public/platform/WebUnitTestSupport.h"
42 #include "public/web/WebDOMCustomEvent.h"
43 #include "public/web/WebFrame.h"
44 #include "public/web/WebView.h"
45 #include "v8.h"
46 #include "web/WebLocalFrameImpl.h"
47 #include "web/WebViewImpl.h"
48 #include "web/tests/FrameTestHelpers.h"
49 #include <gtest/gtest.h>
51 namespace blink {
53 class TestListener final : public V8AbstractEventListener {
54 public:
55 bool operator==(const EventListener&) override
57 return true;
60 void handleEvent(ScriptState* scriptState, Event* event) override
62 EXPECT_EQ(event->type(), "blah");
64 ScriptState::Scope scope(scriptState);
65 v8::Local<v8::Context> context = scriptState->context();
66 v8::Local<v8::Value> jsEvent = toV8(event, context->Global(), isolate());
68 EXPECT_EQ(jsEvent->ToObject(context).ToLocalChecked()->Get(context, v8AtomicString(isolate(), "detail")).ToLocalChecked(), v8::Boolean::New(isolate(), true));
71 static PassRefPtrWillBeRawPtr<TestListener> create(ScriptState* scriptState)
73 return adoptRefWillBeNoop(new TestListener(scriptState));
76 private:
77 explicit TestListener(ScriptState* scriptState)
78 : V8AbstractEventListener(false, scriptState->world(), scriptState->isolate())
82 v8::Local<v8::Value> callListenerFunction(ScriptState*, v8::Local<v8::Value>, Event*) override
84 ASSERT_NOT_REACHED();
85 return v8::Local<v8::Value>();
89 // Tests that a CustomEvent can be initialized with a 'detail' value that is a
90 // SerializedScriptValue, and that this value can be retrieved and read in an
91 // event listener. Initialization of a CustomEvent with a SerializedScriptValue
92 // is an internal API, so it cannot be tested with a LayoutTest.
94 // Triggers crash in GC. http://crbug.com/317669
95 TEST(CustomEventTest, InitWithSerializedScriptValue)
97 const std::string baseURL = "http://www.test.com";
98 const std::string path = "visible_iframe.html";
100 URLTestHelpers::registerMockedURLFromBaseURL(WebString::fromUTF8(baseURL.c_str()), WebString::fromUTF8(path.c_str()));
101 FrameTestHelpers::WebViewHelper webViewHelper;
102 WebLocalFrameImpl* frame = toWebLocalFrameImpl(webViewHelper.initializeAndLoad(baseURL + path)->mainFrame());
103 WebDOMEvent event = frame->frame()->document()->createEvent("CustomEvent", IGNORE_EXCEPTION);
104 WebDOMCustomEvent customEvent = event.to<WebDOMCustomEvent>();
106 v8::Isolate* isolate = toIsolate(frame->frame());
107 V8TestingScope scope(isolate);
108 customEvent.initCustomEvent("blah", false, false, WebSerializedScriptValue::serialize(v8::Boolean::New(isolate, true)));
109 RefPtrWillBeRawPtr<EventListener> listener = TestListener::create(scope.scriptState());
110 frame->frame()->document()->addEventListener("blah", listener, false);
111 frame->frame()->document()->dispatchEvent(event);
113 Platform::current()->unitTestSupport()->unregisterAllMockedURLs();
116 } // namespace blink