Home | History | Annotate | Download | only in runner
      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 "SpellCheckClient.h"
     32 
     33 #include "MockGrammarCheck.h"
     34 #include "public/testing/WebTestDelegate.h"
     35 #include "public/testing/WebTestProxy.h"
     36 #include "public/web/WebTextCheckingCompletion.h"
     37 #include "public/web/WebTextCheckingResult.h"
     38 
     39 using namespace blink;
     40 using namespace std;
     41 
     42 namespace WebTestRunner {
     43 
     44 namespace {
     45 
     46 class HostMethodTask : public WebMethodTask<SpellCheckClient> {
     47 public:
     48     typedef void (SpellCheckClient::*CallbackMethodType)();
     49     HostMethodTask(SpellCheckClient* object, CallbackMethodType callback)
     50         : WebMethodTask<SpellCheckClient>(object)
     51         , m_callback(callback)
     52     { }
     53 
     54     virtual void runIfValid() { (m_object->*m_callback)(); }
     55 
     56 private:
     57     CallbackMethodType m_callback;
     58 };
     59 
     60 }
     61 
     62 SpellCheckClient::SpellCheckClient(WebTestProxyBase* webTestProxy)
     63     : m_lastRequestedTextCheckingCompletion(0)
     64     , m_webTestProxy(webTestProxy)
     65 {
     66 }
     67 
     68 SpellCheckClient::~SpellCheckClient()
     69 {
     70 }
     71 
     72 void SpellCheckClient::setDelegate(WebTestDelegate* delegate)
     73 {
     74     m_delegate = delegate;
     75 }
     76 
     77 // blink::WebSpellCheckClient
     78 void SpellCheckClient::spellCheck(const WebString& text, int& misspelledOffset, int& misspelledLength, WebVector<WebString>* optionalSuggestions)
     79 {
     80     // Check the spelling of the given text.
     81     m_spellcheck.spellCheckWord(text, &misspelledOffset, &misspelledLength);
     82 }
     83 
     84 void SpellCheckClient::checkTextOfParagraph(const WebString& text, WebTextCheckingTypeMask mask, WebVector<WebTextCheckingResult>* webResults)
     85 {
     86     vector<WebTextCheckingResult> results;
     87     if (mask & WebTextCheckingTypeSpelling) {
     88         size_t offset = 0;
     89         string16 data = text;
     90         while (offset < data.length()) {
     91             int misspelledPosition = 0;
     92             int misspelledLength = 0;
     93             m_spellcheck.spellCheckWord(data.substr(offset), &misspelledPosition, &misspelledLength);
     94             if (!misspelledLength)
     95                 break;
     96             WebTextCheckingResult result;
     97             result.decoration = WebTextDecorationTypeSpelling;
     98             result.location = offset + misspelledPosition;
     99             result.length = misspelledLength;
    100             results.push_back(result);
    101             offset += misspelledPosition + misspelledLength;
    102         }
    103     }
    104     if (mask & WebTextCheckingTypeGrammar)
    105         MockGrammarCheck::checkGrammarOfString(text, &results);
    106     webResults->assign(results);
    107 }
    108 
    109 void SpellCheckClient::requestCheckingOfText(
    110         const WebString& text,
    111         const WebVector<uint32_t>& markers,
    112         const WebVector<unsigned>& markerOffsets,
    113         WebTextCheckingCompletion* completion)
    114 {
    115     if (text.isEmpty()) {
    116         if (completion)
    117             completion->didCancelCheckingText();
    118         return;
    119     }
    120 
    121     if (m_lastRequestedTextCheckingCompletion)
    122         m_lastRequestedTextCheckingCompletion->didCancelCheckingText();
    123 
    124     m_lastRequestedTextCheckingCompletion = completion;
    125     m_lastRequestedTextCheckString = text;
    126     if (m_spellcheck.hasInCache(text))
    127         finishLastTextCheck();
    128     else
    129         m_delegate->postDelayedTask(new HostMethodTask(this, &SpellCheckClient::finishLastTextCheck), 0);
    130 }
    131 
    132 void SpellCheckClient::finishLastTextCheck()
    133 {
    134     if (!m_lastRequestedTextCheckingCompletion)
    135         return;
    136     vector<WebTextCheckingResult> results;
    137     int offset = 0;
    138     string16 text = m_lastRequestedTextCheckString;
    139     if (!m_spellcheck.isMultiWordMisspelling(WebString(text), &results)) {
    140         while (text.length()) {
    141             int misspelledPosition = 0;
    142             int misspelledLength = 0;
    143             m_spellcheck.spellCheckWord(WebString(text), &misspelledPosition, &misspelledLength);
    144             if (!misspelledLength)
    145                 break;
    146             WebVector<WebString> suggestions;
    147             m_spellcheck.fillSuggestionList(WebString(text.substr(misspelledPosition, misspelledLength)), &suggestions);
    148             results.push_back(WebTextCheckingResult(WebTextDecorationTypeSpelling, offset + misspelledPosition, misspelledLength, suggestions.isEmpty() ? WebString() : suggestions[0]));
    149             text = text.substr(misspelledPosition + misspelledLength);
    150             offset += misspelledPosition + misspelledLength;
    151         }
    152         MockGrammarCheck::checkGrammarOfString(m_lastRequestedTextCheckString, &results);
    153     }
    154     m_lastRequestedTextCheckingCompletion->didFinishCheckingText(results);
    155     m_lastRequestedTextCheckingCompletion = 0;
    156 
    157     m_webTestProxy->postSpellCheckEvent(WebString("finishLastTextCheck"));
    158 }
    159 
    160 WebString SpellCheckClient::autoCorrectWord(const WebString&)
    161 {
    162     // Returns an empty string as Mac WebKit ('WebKitSupport/WebEditorClient.mm')
    163     // does. (If this function returns a non-empty string, WebKit replaces the
    164     // given misspelled string with the result one. This process executes some
    165     // editor commands and causes layout-test failures.)
    166     return WebString();
    167 }
    168 
    169 }
    170