summaryrefslogtreecommitdiffstats
path: root/chrome/browser/spellchecker/spellcheck_action_unittest.cc
blob: ce3a86dce782f62c22ca9592180e25cf41cc97f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <string>

#include "base/json/json_reader.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "chrome/browser/spellchecker/spellcheck_action.h"
#include "testing/gtest/include/gtest/gtest.h"

TEST(SpellcheckActionTest, FinalActionsTest) {
  static const SpellcheckAction::SpellcheckActionType kFinalActions[] = {
    SpellcheckAction::TYPE_ADD_TO_DICT,
    SpellcheckAction::TYPE_IGNORE,
    SpellcheckAction::TYPE_IN_DICTIONARY,
    SpellcheckAction::TYPE_MANUALLY_CORRECTED,
    SpellcheckAction::TYPE_NO_ACTION,
    SpellcheckAction::TYPE_SELECT,
  };
  SpellcheckAction action;
  for (size_t i = 0; i < arraysize(kFinalActions); ++i) {
    action.type = kFinalActions[i];
    ASSERT_TRUE(action.IsFinal());
  }
}

TEST(SpellcheckActionTest, PendingActionsTest) {
  static const SpellcheckAction::SpellcheckActionType kPendingActions[] = {
    SpellcheckAction::TYPE_PENDING,
    SpellcheckAction::TYPE_PENDING_IGNORE,
  };
  SpellcheckAction action;
  for (size_t i = 0; i < arraysize(kPendingActions); ++i) {
    action.type = kPendingActions[i];
    ASSERT_FALSE(action.IsFinal());
  }
}

TEST(SpellcheckActionTest, FinalizeTest) {
  SpellcheckAction action;
  action.type = SpellcheckAction::TYPE_PENDING;
  action.Finalize();
  ASSERT_EQ(SpellcheckAction::TYPE_NO_ACTION, action.type);

  action.type = SpellcheckAction::TYPE_PENDING_IGNORE;
  action.Finalize();
  ASSERT_EQ(SpellcheckAction::TYPE_IGNORE, action.type);
}

TEST(SpellcheckActionTest, SerializeTest) {
  static const size_t kNumTestCases = 7;
  static const struct {
    SpellcheckAction action;
    std::string expected;
  } kTestCases[] = {
    { SpellcheckAction(
          SpellcheckAction::TYPE_ADD_TO_DICT, -1,
          base::ASCIIToUTF16("nothing")),
      "{\"actionType\": \"ADD_TO_DICT\"}" },
    { SpellcheckAction(
          SpellcheckAction::TYPE_IGNORE, -1, base::ASCIIToUTF16("nothing")),
      "{\"actionType\": \"IGNORE\"}" },
    { SpellcheckAction(
          SpellcheckAction::TYPE_IN_DICTIONARY, -1,
          base::ASCIIToUTF16("nothing")),
      "{\"actionType\": \"IN_DICTIONARY\"}" },
    { SpellcheckAction(
          SpellcheckAction::TYPE_MANUALLY_CORRECTED, -1,
          base::ASCIIToUTF16("hello")),
      "{\"actionTargetValue\": \"hello\","
      "\"actionType\": \"MANUALLY_CORRECTED\"}" },
    { SpellcheckAction(
          SpellcheckAction::TYPE_NO_ACTION, -1, base::ASCIIToUTF16("nothing")),
      "{\"actionType\": \"NO_ACTION\"}" },
    { SpellcheckAction(
          SpellcheckAction::TYPE_PENDING, -1, base::ASCIIToUTF16("nothing")),
      "{\"actionType\": \"PENDING\"}" },
    { SpellcheckAction(
          SpellcheckAction::TYPE_PENDING_IGNORE, -1,
          base::ASCIIToUTF16("nothing")),
      "{\"actionType\": \"PENDING\"}" },
    { SpellcheckAction(
          SpellcheckAction::TYPE_SELECT, 42, base::ASCIIToUTF16("nothing")),
      "{\"actionTargetIndex\": 42, \"actionType\": \"SELECT\"}" },
  };
  for (size_t i = 0; i < kNumTestCases; ++i) {
    scoped_ptr<base::DictionaryValue> serialized(
        kTestCases[i].action.Serialize());
    scoped_ptr<base::Value> expected(
        base::JSONReader::Read(kTestCases[i].expected));
    EXPECT_TRUE(serialized->Equals(expected.get()));
  }
}