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 "MockSpellCheck.h"
     32 
     33 #include "TestCommon.h"
     34 #include "public/platform/WebCString.h"
     35 
     36 using namespace WebKit;
     37 using namespace std;
     38 
     39 namespace WebTestRunner {
     40 
     41 namespace {
     42 
     43 void append(WebVector<WebString>* data, const WebString& item)
     44 {
     45     WebVector<WebString> result(data->size() + 1);
     46     for (size_t i = 0; i < data->size(); ++i)
     47         result[i] = (*data)[i];
     48     result[data->size()] = item;
     49     data->swap(result);
     50 }
     51 
     52 }
     53 
     54 MockSpellCheck::MockSpellCheck()
     55     : m_initialized(false) { }
     56 
     57 MockSpellCheck::~MockSpellCheck() { }
     58 
     59 bool MockSpellCheck::spellCheckWord(const WebString& text, int* misspelledOffset, int* misspelledLength)
     60 {
     61     WEBKIT_ASSERT(misspelledOffset);
     62     WEBKIT_ASSERT(misspelledLength);
     63 
     64     // Initialize this spellchecker.
     65     initializeIfNeeded();
     66 
     67     // Reset the result values as our spellchecker does.
     68     *misspelledOffset = 0;
     69     *misspelledLength = 0;
     70 
     71     // Convert to a string16 because we store string16 instances in
     72     // m_misspelledWords and WebString has no find().
     73     string16 stringText = text;
     74     int skippedLength = 0;
     75 
     76     while (!stringText.empty()) {
     77         // Extract the first possible English word from the given string.
     78         // The given string may include non-ASCII characters or numbers. So, we
     79         // should filter out such characters before start looking up our
     80         // misspelled-word table.
     81         // (This is a simple version of our SpellCheckWordIterator class.)
     82         // If the given string doesn't include any ASCII characters, we can treat the
     83         // string as valid one.
     84         string16::iterator firstChar = find_if(stringText.begin(), stringText.end(), isASCIIAlpha);
     85         if (firstChar == stringText.end())
     86             return true;
     87         int wordOffset = distance(stringText.begin(), firstChar);
     88         int maxWordLength = static_cast<int>(stringText.length()) - wordOffset;
     89         int wordLength;
     90         string16 word;
     91 
     92         // Look up our misspelled-word table to check if the extracted word is a
     93         // known misspelled word, and return the offset and the length of the
     94         // extracted word if this word is a known misspelled word.
     95         // (See the comment in MockSpellCheck::initializeIfNeeded() why we use a
     96         // misspelled-word table.)
     97         for (size_t i = 0; i < m_misspelledWords.size(); ++i) {
     98             wordLength = static_cast<int>(m_misspelledWords.at(i).length()) > maxWordLength ? maxWordLength : static_cast<int>(m_misspelledWords.at(i).length());
     99             word = stringText.substr(wordOffset, wordLength);
    100             if (word == m_misspelledWords.at(i) && (static_cast<int>(stringText.length()) == wordOffset + wordLength || isNotASCIIAlpha(stringText[wordOffset + wordLength]))) {
    101                 *misspelledOffset = wordOffset + skippedLength;
    102                 *misspelledLength = wordLength;
    103                 break;
    104             }
    105         }
    106 
    107         if (*misspelledLength > 0)
    108             break;
    109 
    110         string16::iterator lastChar = find_if(stringText.begin() + wordOffset, stringText.end(), isNotASCIIAlpha);
    111         if (lastChar == stringText.end())
    112             wordLength = static_cast<int>(stringText.length()) - wordOffset;
    113         else
    114             wordLength = distance(firstChar, lastChar);
    115 
    116         WEBKIT_ASSERT(0 < wordOffset + wordLength);
    117         stringText = stringText.substr(wordOffset + wordLength);
    118         skippedLength += wordOffset + wordLength;
    119     }
    120 
    121     return false;
    122 }
    123 
    124 bool MockSpellCheck::hasInCache(const WebString& word)
    125 {
    126     return word == WebString::fromUTF8("Spell wellcome. Is it broken?") || word == WebString::fromUTF8("Spell wellcome.\x007F");
    127 }
    128 
    129 void MockSpellCheck::fillSuggestionList(const WebString& word, WebVector<WebString>* suggestions)
    130 {
    131     if (word == WebString::fromUTF8("wellcome"))
    132         append(suggestions, WebString::fromUTF8("welcome"));
    133     else if (word == WebString::fromUTF8("upper case"))
    134         append(suggestions, WebString::fromUTF8("uppercase"));
    135 }
    136 
    137 bool MockSpellCheck::initializeIfNeeded()
    138 {
    139     // Exit if we have already initialized this object.
    140     if (m_initialized)
    141         return false;
    142 
    143     // Create a table that consists of misspelled words used in WebKit layout
    144     // tests.
    145     // Since WebKit layout tests don't have so many misspelled words as
    146     // well-spelled words, it is easier to compare the given word with misspelled
    147     // ones than to compare with well-spelled ones.
    148     static const char* misspelledWords[] = {
    149         // These words are known misspelled words in webkit tests.
    150         // If there are other misspelled words in webkit tests, please add them in
    151         // this array.
    152         "foo",
    153         "Foo",
    154         "baz",
    155         "fo",
    156         "LibertyF",
    157         "chello",
    158         "xxxtestxxx",
    159         "XXxxx",
    160         "Textx",
    161         "blockquoted",
    162         "asd",
    163         "Lorem",
    164         "Nunc",
    165         "Curabitur",
    166         "eu",
    167         "adlj",
    168         "adaasj",
    169         "sdklj",
    170         "jlkds",
    171         "jsaada",
    172         "jlda",
    173         "zz",
    174         "contentEditable",
    175         // The following words are used by unit tests.
    176         "ifmmp",
    177         "qwertyuiopasd",
    178         "qwertyuiopasdf",
    179         "upper case",
    180         "wellcome"
    181     };
    182 
    183     m_misspelledWords.clear();
    184     for (size_t i = 0; i < arraysize(misspelledWords); ++i)
    185         m_misspelledWords.push_back(string16(misspelledWords[i], misspelledWords[i] + strlen(misspelledWords[i])));
    186 
    187     // Mark as initialized to prevent this object from being initialized twice
    188     // or more.
    189     m_initialized = true;
    190 
    191     // Since this MockSpellCheck class doesn't download dictionaries, this
    192     // function always returns false.
    193     return false;
    194 }
    195 
    196 }
    197