Home | History | Annotate | Download | only in tests
      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 "FrameTestHelpers.h"
     32 #include "URLTestHelpers.h"
     33 #include "V8Event.h"
     34 #include "WebFrame.h"
     35 #include "WebFrameImpl.h"
     36 #include "WebView.h"
     37 #include "WebViewImpl.h"
     38 #include "bindings/v8/ExceptionStatePlaceholder.h"
     39 #include "bindings/v8/SerializedScriptValue.h"
     40 #include "public/platform/Platform.h"
     41 #include "public/platform/WebUnitTestSupport.h"
     42 #include "public/web/WebDOMCustomEvent.h"
     43 #include "v8.h"
     44 
     45 #include <gtest/gtest.h>
     46 
     47 using namespace WebCore;
     48 using namespace WebKit;
     49 
     50 namespace {
     51 
     52 class TestListener : public EventListener {
     53 public:
     54     virtual bool operator==(const EventListener& listener)
     55     {
     56         return true;
     57     }
     58 
     59     virtual void handleEvent(ScriptExecutionContext* context, Event* event)
     60     {
     61         EXPECT_EQ(event->type(), "blah");
     62 
     63         v8::Context::Scope scope(m_v8Context);
     64         v8::Isolate* isolate = m_v8Context->GetIsolate();
     65         v8::Handle<v8::Value> jsEvent = toV8(event, v8::Handle<v8::Object>(), isolate);
     66 
     67         EXPECT_EQ(jsEvent->ToObject()->Get(v8::String::New("detail")), v8::Boolean::New(true));
     68     }
     69 
     70     static PassRefPtr<TestListener> create(v8::Local<v8::Context> v8Context)
     71     {
     72         return adoptRef(new TestListener(v8Context));
     73     }
     74 
     75 private:
     76     TestListener()
     77         : EventListener(JSEventListenerType)
     78     {
     79     }
     80 
     81     TestListener(v8::Local<v8::Context> v8Context)
     82         : EventListener(JSEventListenerType)
     83         , m_v8Context(v8Context)
     84     {
     85     }
     86 
     87     v8::Local<v8::Context> m_v8Context;
     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 TEST(CustomEventTest, InitWithSerializedScriptValue)
     95 {
     96     const std::string baseURL = "http://www.test.com";
     97     const std::string path = "visible_iframe.html";
     98 
     99     URLTestHelpers::registerMockedURLFromBaseURL(WebString::fromUTF8(baseURL.c_str()), WebString::fromUTF8(path.c_str()));
    100     WebView* webView = FrameTestHelpers::createWebViewAndLoad(baseURL + path);
    101     WebFrameImpl* frame = static_cast<WebFrameImpl*>(webView->mainFrame());
    102 
    103     WebDOMEvent event = frame->frame()->document()->createEvent("CustomEvent", IGNORE_EXCEPTION);
    104     WebDOMCustomEvent customEvent = event.to<WebDOMCustomEvent>();
    105 
    106     v8::HandleScope handleScope;
    107     customEvent.initCustomEvent("blah", false, false, WebSerializedScriptValue::serialize(v8::Boolean::New(true)));
    108     RefPtr<EventListener> listener = TestListener::create(frame->mainWorldScriptContext());
    109     frame->frame()->document()->addEventListener("blah", listener, false);
    110     frame->frame()->document()->dispatchEvent(event);
    111 
    112     Platform::current()->unitTestSupport()->unregisterAllMockedURLs();
    113     webView->close();
    114 }
    115 
    116 }
    117