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
5#include "content/shell/renderer/test_runner/mock_grammar_check.h"
6
7#include <algorithm>
8
9#include "base/logging.h"
10#include "content/shell/renderer/test_runner/test_common.h"
11#include "third_party/WebKit/public/platform/WebCString.h"
12#include "third_party/WebKit/public/platform/WebString.h"
13#include "third_party/WebKit/public/web/WebTextCheckingResult.h"
14
15namespace content {
16
17bool MockGrammarCheck::CheckGrammarOfString(
18    const blink::WebString& text,
19    std::vector<blink::WebTextCheckingResult>* results) {
20  DCHECK(results);
21  base::string16 string_text = text;
22  if (std::find_if(string_text.begin(), string_text.end(), IsASCIIAlpha) ==
23      string_text.end())
24    return true;
25
26  // Find matching grammatical errors from known ones. This function has to
27  // check all errors because the given text may consist of two or more
28  // sentences that have grammatical errors.
29  static const struct {
30    const char* text;
31    int location;
32    int length;
33  } kGrammarErrors[] = {
34        {"I have a issue.", 7, 1},
35        {"I have an grape.", 7, 2},
36        {"I have an kiwi.", 7, 2},
37        {"I have an muscat.", 7, 2},
38        {"You has the right.", 4, 3},
39        {"apple orange zz.", 0, 16},
40        {"apple zz orange.", 0, 16},
41        {"apple,zz,orange.", 0, 16},
42        {"orange,zz,apple.", 0, 16},
43        {"the the adlj adaasj sdklj. there there", 4, 3},
44        {"the the adlj adaasj sdklj. there there", 33, 5},
45        {"zz apple orange.", 0, 16},
46    };
47  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kGrammarErrors); ++i) {
48    size_t offset = 0;
49    base::string16 error(
50        kGrammarErrors[i].text,
51        kGrammarErrors[i].text + strlen(kGrammarErrors[i].text));
52    while ((offset = string_text.find(error, offset)) != base::string16::npos) {
53      results->push_back(
54          blink::WebTextCheckingResult(blink::WebTextDecorationTypeGrammar,
55                                       offset + kGrammarErrors[i].location,
56                                       kGrammarErrors[i].length));
57      offset += kGrammarErrors[i].length;
58    }
59  }
60  return false;
61}
62
63}  // namespace content
64