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 "FrameTestHelpers.h" 34 #include "URLTestHelpers.h" 35 #include "V8Event.h" 36 #include "WebFrame.h" 37 #include "WebFrameImpl.h" 38 #include "WebView.h" 39 #include "WebViewImpl.h" 40 #include "bindings/v8/ExceptionStatePlaceholder.h" 41 #include "bindings/v8/ScriptController.h" 42 #include "bindings/v8/SerializedScriptValue.h" 43 #include "bindings/v8/V8AbstractEventListener.h" 44 #include "public/platform/Platform.h" 45 #include "public/platform/WebUnitTestSupport.h" 46 #include "public/web/WebDOMCustomEvent.h" 47 #include "v8.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: 58 virtual bool operator==(const EventListener& listener) 59 { 60 return true; 61 } 62 63 virtual void handleEvent(ExecutionContext* context, Event* event) 64 { 65 EXPECT_EQ(event->type(), "blah"); 66 67 v8::Local<v8::Context> v8Context = WebCore::toV8Context(context, mainThreadNormalWorld()); 68 v8::Isolate* isolate = v8Context->GetIsolate(); 69 v8::Context::Scope scope(v8Context); 70 v8::Handle<v8::Value> jsEvent = toV8(event, v8::Handle<v8::Object>(), isolate); 71 72 EXPECT_EQ(jsEvent->ToObject()->Get(v8::String::NewFromUtf8(isolate, "detail")), v8::Boolean::New(isolate, true)); 73 } 74 75 static PassRefPtr<TestListener> create(v8::Isolate* isolate) 76 { 77 return adoptRef(new TestListener(isolate)); 78 } 79 80 private: 81 TestListener(v8::Isolate* isolate) 82 : V8AbstractEventListener(false, 0, isolate) 83 { 84 } 85 86 virtual v8::Local<v8::Value> callListenerFunction(ExecutionContext*, v8::Handle<v8::Value> jsevent, Event*) 87 { 88 ASSERT_NOT_REACHED(); 89 return v8::Local<v8::Value>(); 90 } 91 }; 92 93 // Tests that a CustomEvent can be initialized with a 'detail' value that is a 94 // SerializedScriptValue, and that this value can be retrieved and read in an 95 // event listener. Initialization of a CustomEvent with a SerializedScriptValue 96 // is an internal API, so it cannot be tested with a LayoutTest. 97 // 98 // Triggers crash in GC. http://crbug.com/317669 99 TEST(CustomEventTest, InitWithSerializedScriptValue) 100 { 101 const std::string baseURL = "http://www.test.com"; 102 const std::string path = "visible_iframe.html"; 103 104 URLTestHelpers::registerMockedURLFromBaseURL(WebString::fromUTF8(baseURL.c_str()), WebString::fromUTF8(path.c_str())); 105 FrameTestHelpers::WebViewHelper webViewHelper; 106 WebFrameImpl* frame = toWebFrameImpl(webViewHelper.initializeAndLoad(baseURL + path)->mainFrame()); 107 108 WebDOMEvent event = frame->frame()->document()->createEvent("CustomEvent", IGNORE_EXCEPTION); 109 WebDOMCustomEvent customEvent = event.to<WebDOMCustomEvent>(); 110 111 v8::Isolate* isolate = toIsolate(frame->frame()); 112 v8::HandleScope handleScope(isolate); 113 customEvent.initCustomEvent("blah", false, false, WebSerializedScriptValue::serialize(v8::Boolean::New(isolate, true))); 114 RefPtr<EventListener> listener = TestListener::create(isolate); 115 frame->frame()->document()->addEventListener("blah", listener, false); 116 frame->frame()->document()->dispatchEvent(event); 117 118 Platform::current()->unitTestSupport()->unregisterAllMockedURLs(); 119 } 120 121 } 122