Home | History | Annotate | Download | only in runner
      1 /*
      2  * Copyright (C) 2010 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 "TextInputController.h"
     32 
     33 #include "TestCommon.h"
     34 #include "public/platform/WebString.h"
     35 #include "public/platform/WebVector.h"
     36 #include "public/web/WebBindings.h"
     37 #include "public/web/WebCompositionUnderline.h"
     38 #include "public/web/WebFrame.h"
     39 #include "public/web/WebInputEvent.h"
     40 #include "public/web/WebRange.h"
     41 #include "public/web/WebView.h"
     42 #include <string>
     43 
     44 using namespace WebKit;
     45 using namespace std;
     46 
     47 namespace WebTestRunner {
     48 
     49 TextInputController::TextInputController()
     50 {
     51     bindMethod("doCommand", &TextInputController::doCommand);
     52     bindMethod("firstRectForCharacterRange", &TextInputController::firstRectForCharacterRange);
     53     bindMethod("hasMarkedText", &TextInputController::hasMarkedText);
     54     bindMethod("insertText", &TextInputController::insertText);
     55     bindMethod("markedRange", &TextInputController::markedRange);
     56     bindMethod("selectedRange", &TextInputController::selectedRange);
     57     bindMethod("setMarkedText", &TextInputController::setMarkedText);
     58     bindMethod("unmarkText", &TextInputController::unmarkText);
     59     bindMethod("setComposition", &TextInputController::setComposition);
     60 }
     61 
     62 void TextInputController::insertText(const CppArgumentList& arguments, CppVariant* result)
     63 {
     64     result->setNull();
     65 
     66     if (arguments.size() < 1 || !arguments[0].isString())
     67         return;
     68 
     69     m_webView->confirmComposition(WebString::fromUTF8(arguments[0].toString()));
     70 }
     71 
     72 void TextInputController::doCommand(const CppArgumentList& arguments, CppVariant* result)
     73 {
     74     result->setNull();
     75 
     76     WebFrame* mainFrame = m_webView->mainFrame();
     77     if (!mainFrame)
     78         return;
     79 
     80     if (arguments.size() >= 1 && arguments[0].isString())
     81         mainFrame->executeCommand(WebString::fromUTF8(arguments[0].toString()));
     82 }
     83 
     84 void TextInputController::setMarkedText(const CppArgumentList& arguments, CppVariant* result)
     85 {
     86     result->setNull();
     87 
     88     if (arguments.size() >= 3 && arguments[0].isString()
     89         && arguments[1].isNumber() && arguments[2].isNumber()) {
     90         WebVector<WebCompositionUnderline> underlines;
     91         m_webView->setComposition(WebString::fromUTF8(arguments[0].toString()),
     92                                   underlines,
     93                                   arguments[1].toInt32(),
     94                                   arguments[1].toInt32() + arguments[2].toInt32());
     95     }
     96 }
     97 
     98 void TextInputController::unmarkText(const CppArgumentList&, CppVariant* result)
     99 {
    100     result->setNull();
    101 
    102     m_webView->confirmComposition();
    103 }
    104 
    105 void TextInputController::hasMarkedText(const CppArgumentList&, CppVariant* result)
    106 {
    107     result->setNull();
    108 
    109     WebFrame* mainFrame = m_webView->mainFrame();
    110     if (!mainFrame)
    111         return;
    112 
    113     result->set(mainFrame->hasMarkedText());
    114 }
    115 
    116 void TextInputController::markedRange(const CppArgumentList&, CppVariant* result)
    117 {
    118     result->setNull();
    119 
    120     WebFrame* mainFrame = m_webView->mainFrame();
    121     if (!mainFrame)
    122         return;
    123 
    124     WebRange range = mainFrame->markedRange();
    125     vector<int> intArray(2);
    126     intArray[0] = range.startOffset();
    127     intArray[1] = range.endOffset();
    128 
    129     NPObject* resultArray = WebBindings::makeIntArray(intArray);
    130     result->set(resultArray);
    131     WebBindings::releaseObject(resultArray);
    132 }
    133 
    134 void TextInputController::selectedRange(const CppArgumentList&, CppVariant* result)
    135 {
    136     result->setNull();
    137 
    138     WebFrame* mainFrame = m_webView->mainFrame();
    139     if (!mainFrame)
    140         return;
    141 
    142     WebRange range = mainFrame->selectionRange();
    143     vector<int> intArray(2);
    144     intArray[0] = range.startOffset();
    145     intArray[1] = range.endOffset();
    146 
    147     NPObject* resultArray = WebBindings::makeIntArray(intArray);
    148     result->set(resultArray);
    149     WebBindings::releaseObject(resultArray);
    150 }
    151 
    152 void TextInputController::firstRectForCharacterRange(const CppArgumentList& arguments, CppVariant* result)
    153 {
    154     result->setNull();
    155 
    156     WebFrame* frame = m_webView->focusedFrame();
    157     if (!frame)
    158         return;
    159 
    160     if (arguments.size() < 2 || !arguments[0].isNumber() || !arguments[1].isNumber())
    161         return;
    162 
    163     WebRect rect;
    164     if (!frame->firstRectForCharacterRange(arguments[0].toInt32(), arguments[1].toInt32(), rect))
    165         return;
    166 
    167     vector<int> intArray(4);
    168     intArray[0] = rect.x;
    169     intArray[1] = rect.y;
    170     intArray[2] = rect.width;
    171     intArray[3] = rect.height;
    172 
    173     NPObject* resultArray = WebBindings::makeIntArray(intArray);
    174     result->set(resultArray);
    175     WebBindings::releaseObject(resultArray);
    176 }
    177 
    178 void TextInputController::setComposition(const CppArgumentList& arguments, CppVariant* result)
    179 {
    180     result->setNull();
    181 
    182     if (arguments.size() < 1)
    183         return;
    184 
    185     // Sends a keydown event with key code = 0xE5 to emulate input method behavior.
    186     WebKeyboardEvent keyDown;
    187     keyDown.type = WebInputEvent::RawKeyDown;
    188     keyDown.modifiers = 0;
    189     keyDown.windowsKeyCode = 0xE5; // VKEY_PROCESSKEY
    190     keyDown.setKeyIdentifierFromWindowsKeyCode();
    191     m_webView->handleInputEvent(keyDown);
    192 
    193     WebVector<WebCompositionUnderline> underlines;
    194     WebString text(WebString::fromUTF8(arguments[0].toString()));
    195     m_webView->setComposition(text, underlines, 0, text.length());
    196 }
    197 
    198 }
    199