blob: b172dfa9ece0f81035ec72d4877b196961dcef6b [file] [log] [blame]
[email protected]f82478792014-05-14 17:50:571// 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
jochen73e711c2015-06-03 10:01:465#include "components/test_runner/mock_grammar_check.h"
[email protected]f82478792014-05-14 17:50:576
avi5dd91f82015-12-25 22:30:467#include <stddef.h>
8
[email protected]f82478792014-05-14 17:50:579#include <algorithm>
10
11#include "base/logging.h"
avi5dd91f82015-12-25 22:30:4612#include "base/macros.h"
jochen73e711c2015-06-03 10:01:4613#include "components/test_runner/test_common.h"
[email protected]f82478792014-05-14 17:50:5714#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
jochenf5f31752015-06-03 12:06:3418namespace test_runner {
[email protected]f82478792014-05-14 17:50:5719
20bool MockGrammarCheck::CheckGrammarOfString(
21 const blink::WebString& text,
22 std::vector<blink::WebTextCheckingResult>* results) {
23 DCHECK(results);
24 base::string16 string_text = text;
[email protected]f5b1ad882014-08-05 16:30:4125 if (std::find_if(string_text.begin(), string_text.end(), IsASCIIAlpha) ==
[email protected]f82478792014-05-14 17:50:5726 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 };
viettrungluu2dfaba72014-10-16 05:30:2550 for (size_t i = 0; i < arraysize(kGrammarErrors); ++i) {
[email protected]f82478792014-05-14 17:50:5751 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
jochenf5f31752015-06-03 12:06:3466} // namespace test_runner