[email protected] | f8247879 | 2014-05-14 17:50:57 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
jochen | 73e711c | 2015-06-03 10:01:46 | [diff] [blame] | 5 | #include "components/test_runner/mock_grammar_check.h" |
[email protected] | f8247879 | 2014-05-14 17:50:57 | [diff] [blame] | 6 | |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
[email protected] | f8247879 | 2014-05-14 17:50:57 | [diff] [blame] | 9 | #include <algorithm> |
| 10 | |
| 11 | #include "base/logging.h" |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame] | 12 | #include "base/macros.h" |
jochen | 73e711c | 2015-06-03 10:01:46 | [diff] [blame] | 13 | #include "components/test_runner/test_common.h" |
[email protected] | f8247879 | 2014-05-14 17:50:57 | [diff] [blame] | 14 | #include "third_party/WebKit/public/platform/WebCString.h" |
| 15 | #include "third_party/WebKit/public/platform/WebString.h" |
| 16 | #include "third_party/WebKit/public/web/WebTextCheckingResult.h" |
| 17 | |
jochen | f5f3175 | 2015-06-03 12:06:34 | [diff] [blame] | 18 | namespace test_runner { |
[email protected] | f8247879 | 2014-05-14 17:50:57 | [diff] [blame] | 19 | |
| 20 | bool MockGrammarCheck::CheckGrammarOfString( |
| 21 | const blink::WebString& text, |
| 22 | std::vector<blink::WebTextCheckingResult>* results) { |
| 23 | DCHECK(results); |
| 24 | base::string16 string_text = text; |
[email protected] | f5b1ad88 | 2014-08-05 16:30:41 | [diff] [blame] | 25 | if (std::find_if(string_text.begin(), string_text.end(), IsASCIIAlpha) == |
[email protected] | f8247879 | 2014-05-14 17:50:57 | [diff] [blame] | 26 | string_text.end()) |
| 27 | return true; |
| 28 | |
| 29 | // Find matching grammatical errors from known ones. This function has to |
| 30 | // check all errors because the given text may consist of two or more |
| 31 | // sentences that have grammatical errors. |
| 32 | static const struct { |
| 33 | const char* text; |
| 34 | int location; |
| 35 | int length; |
| 36 | } kGrammarErrors[] = { |
| 37 | {"I have a issue.", 7, 1}, |
| 38 | {"I have an grape.", 7, 2}, |
| 39 | {"I have an kiwi.", 7, 2}, |
| 40 | {"I have an muscat.", 7, 2}, |
| 41 | {"You has the right.", 4, 3}, |
| 42 | {"apple orange zz.", 0, 16}, |
| 43 | {"apple zz orange.", 0, 16}, |
| 44 | {"apple,zz,orange.", 0, 16}, |
| 45 | {"orange,zz,apple.", 0, 16}, |
| 46 | {"the the adlj adaasj sdklj. there there", 4, 3}, |
| 47 | {"the the adlj adaasj sdklj. there there", 33, 5}, |
| 48 | {"zz apple orange.", 0, 16}, |
| 49 | }; |
viettrungluu | 2dfaba7 | 2014-10-16 05:30:25 | [diff] [blame] | 50 | for (size_t i = 0; i < arraysize(kGrammarErrors); ++i) { |
[email protected] | f8247879 | 2014-05-14 17:50:57 | [diff] [blame] | 51 | size_t offset = 0; |
| 52 | base::string16 error( |
| 53 | kGrammarErrors[i].text, |
| 54 | kGrammarErrors[i].text + strlen(kGrammarErrors[i].text)); |
| 55 | while ((offset = string_text.find(error, offset)) != base::string16::npos) { |
| 56 | results->push_back( |
| 57 | blink::WebTextCheckingResult(blink::WebTextDecorationTypeGrammar, |
| 58 | offset + kGrammarErrors[i].location, |
| 59 | kGrammarErrors[i].length)); |
| 60 | offset += kGrammarErrors[i].length; |
| 61 | } |
| 62 | } |
| 63 | return false; |
| 64 | } |
| 65 | |
jochen | f5f3175 | 2015-06-03 12:06:34 | [diff] [blame] | 66 | } // namespace test_runner |