- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / spellchecker / spellcheck_action_unittest.cc
1 // Copyright (c) 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 <string>
6
7 #include "base/json/json_reader.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "base/values.h"
10 #include "chrome/browser/spellchecker/spellcheck_action.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12
13 TEST(SpellcheckActionTest, FinalActionsTest) {
14   static const SpellcheckAction::SpellcheckActionType kFinalActions[] = {
15     SpellcheckAction::TYPE_ADD_TO_DICT,
16     SpellcheckAction::TYPE_IGNORE,
17     SpellcheckAction::TYPE_IN_DICTIONARY,
18     SpellcheckAction::TYPE_MANUALLY_CORRECTED,
19     SpellcheckAction::TYPE_NO_ACTION,
20     SpellcheckAction::TYPE_SELECT,
21   };
22   SpellcheckAction action;
23   for (size_t i = 0; i < arraysize(kFinalActions); ++i) {
24     action.type = kFinalActions[i];
25     ASSERT_TRUE(action.IsFinal());
26   }
27 }
28
29 TEST(SpellcheckActionTest, PendingActionsTest) {
30   static const SpellcheckAction::SpellcheckActionType kPendingActions[] = {
31     SpellcheckAction::TYPE_PENDING,
32     SpellcheckAction::TYPE_PENDING_IGNORE,
33   };
34   SpellcheckAction action;
35   for (size_t i = 0; i < arraysize(kPendingActions); ++i) {
36     action.type = kPendingActions[i];
37     ASSERT_FALSE(action.IsFinal());
38   }
39 }
40
41 TEST(SpellcheckActionTest, FinalizeTest) {
42   SpellcheckAction action;
43   action.type = SpellcheckAction::TYPE_PENDING;
44   action.Finalize();
45   ASSERT_EQ(SpellcheckAction::TYPE_NO_ACTION, action.type);
46
47   action.type = SpellcheckAction::TYPE_PENDING_IGNORE;
48   action.Finalize();
49   ASSERT_EQ(SpellcheckAction::TYPE_IGNORE, action.type);
50 }
51
52 TEST(SpellcheckActionTest, SerializeTest) {
53   static const size_t kNumTestCases = 7;
54   static const struct {
55     SpellcheckAction action;
56     std::string expected;
57   } kTestCases[] = {
58     { SpellcheckAction(
59           SpellcheckAction::TYPE_ADD_TO_DICT, -1, ASCIIToUTF16("nothing")),
60       "{\"actionType\": \"ADD_TO_DICT\"}" },
61     { SpellcheckAction(
62           SpellcheckAction::TYPE_IGNORE, -1, ASCIIToUTF16("nothing")),
63       "{\"actionType\": \"IGNORE\"}" },
64     { SpellcheckAction(
65           SpellcheckAction::TYPE_IN_DICTIONARY, -1, ASCIIToUTF16("nothing")),
66       "{\"actionType\": \"IN_DICTIONARY\"}" },
67     { SpellcheckAction(
68           SpellcheckAction::TYPE_MANUALLY_CORRECTED, -1, ASCIIToUTF16("hello")),
69       "{\"actionTargetValue\": \"hello\","
70       "\"actionType\": \"MANUALLY_CORRECTED\"}" },
71     { SpellcheckAction(
72           SpellcheckAction::TYPE_NO_ACTION, -1, ASCIIToUTF16("nothing")),
73       "{\"actionType\": \"NO_ACTION\"}" },
74     { SpellcheckAction(
75           SpellcheckAction::TYPE_PENDING, -1, ASCIIToUTF16("nothing")),
76       "{\"actionType\": \"PENDING\"}" },
77     { SpellcheckAction(
78           SpellcheckAction::TYPE_PENDING_IGNORE, -1, ASCIIToUTF16("nothing")),
79       "{\"actionType\": \"PENDING\"}" },
80     { SpellcheckAction(
81           SpellcheckAction::TYPE_SELECT, 42, ASCIIToUTF16("nothing")),
82       "{\"actionTargetIndex\": 42, \"actionType\": \"SELECT\"}" },
83   };
84   for (size_t i = 0; i < kNumTestCases; ++i) {
85     scoped_ptr<base::DictionaryValue> serialized(
86         kTestCases[i].action.Serialize());
87     scoped_ptr<base::Value> expected(
88         base::JSONReader::Read(kTestCases[i].expected));
89     EXPECT_TRUE(serialized->Equals(expected.get()));
90   }
91 }