Home | History | Annotate | Download | only in spellchecker
      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,
     60           base::ASCIIToUTF16("nothing")),
     61       "{\"actionType\": \"ADD_TO_DICT\"}" },
     62     { SpellcheckAction(
     63           SpellcheckAction::TYPE_IGNORE, -1, base::ASCIIToUTF16("nothing")),
     64       "{\"actionType\": \"IGNORE\"}" },
     65     { SpellcheckAction(
     66           SpellcheckAction::TYPE_IN_DICTIONARY, -1,
     67           base::ASCIIToUTF16("nothing")),
     68       "{\"actionType\": \"IN_DICTIONARY\"}" },
     69     { SpellcheckAction(
     70           SpellcheckAction::TYPE_MANUALLY_CORRECTED, -1,
     71           base::ASCIIToUTF16("hello")),
     72       "{\"actionTargetValue\": \"hello\","
     73       "\"actionType\": \"MANUALLY_CORRECTED\"}" },
     74     { SpellcheckAction(
     75           SpellcheckAction::TYPE_NO_ACTION, -1, base::ASCIIToUTF16("nothing")),
     76       "{\"actionType\": \"NO_ACTION\"}" },
     77     { SpellcheckAction(
     78           SpellcheckAction::TYPE_PENDING, -1, base::ASCIIToUTF16("nothing")),
     79       "{\"actionType\": \"PENDING\"}" },
     80     { SpellcheckAction(
     81           SpellcheckAction::TYPE_PENDING_IGNORE, -1,
     82           base::ASCIIToUTF16("nothing")),
     83       "{\"actionType\": \"PENDING\"}" },
     84     { SpellcheckAction(
     85           SpellcheckAction::TYPE_SELECT, 42, base::ASCIIToUTF16("nothing")),
     86       "{\"actionTargetIndex\": 42, \"actionType\": \"SELECT\"}" },
     87   };
     88   for (size_t i = 0; i < kNumTestCases; ++i) {
     89     scoped_ptr<base::DictionaryValue> serialized(
     90         kTestCases[i].action.Serialize());
     91     scoped_ptr<base::Value> expected(
     92         base::JSONReader::Read(kTestCases[i].expected));
     93     EXPECT_TRUE(serialized->Equals(expected.get()));
     94   }
     95 }
     96