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