| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/renderer/spellchecker/spellcheck.h" | 5 #include "chrome/renderer/spellchecker/spellcheck.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <utility> | 10 #include <utility> |
| 11 | 11 |
| 12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 13 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
| 14 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
| 16 #include "base/path_service.h" | 16 #include "base/path_service.h" |
| 17 #include "base/run_loop.h" | 17 #include "base/run_loop.h" |
| 18 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
| 19 #include "base/strings/utf_string_conversions.h" | 19 #include "base/strings/utf_string_conversions.h" |
| 20 #include "build/build_config.h" | 20 #include "build/build_config.h" |
| 21 #include "chrome/common/spellcheck_common.h" | 21 #include "chrome/common/spellcheck_common.h" |
| 22 #include "chrome/common/spellcheck_result.h" | 22 #include "chrome/common/spellcheck_result.h" |
| 23 #include "chrome/renderer/spellchecker/hunspell_engine.h" | 23 #include "chrome/renderer/spellchecker/hunspell_engine.h" |
| 24 #include "chrome/renderer/spellchecker/spellcheck_language.h" | 24 #include "chrome/renderer/spellchecker/spellcheck_language.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
| 26 #include "third_party/WebKit/public/platform/WebVector.h" | 26 #include "third_party/WebKit/public/platform/WebVector.h" |
| 27 #include "third_party/WebKit/public/web/WebTextCheckingCompletion.h" | 27 #include "third_party/WebKit/public/web/WebTextCheckingCompletion.h" |
| 28 #include "third_party/WebKit/public/web/WebTextCheckingResult.h" | 28 #include "third_party/WebKit/public/web/WebTextCheckingResult.h" |
| 29 #include "third_party/hunspell/src/hunspell/hunspell.hxx" |
| 29 | 30 |
| 30 #define TYPOGRAPHICAL_APOSTROPHE L"\x2019" | 31 #define TYPOGRAPHICAL_APOSTROPHE L"\x2019" |
| 31 | 32 |
| 32 namespace { | 33 namespace { |
| 33 const int kNoOffset = 0; | 34 const int kNoOffset = 0; |
| 34 const int kNoTag = 0; | 35 const int kNoTag = 0; |
| 35 | 36 |
| 36 base::FilePath GetHunspellDirectory() { | 37 base::FilePath GetHunspellDirectory() { |
| 37 base::FilePath hunspell_directory; | 38 base::FilePath hunspell_directory; |
| 38 if (!PathService::Get(base::DIR_SOURCE_ROOT, &hunspell_directory)) | 39 if (!PathService::Get(base::DIR_SOURCE_ROOT, &hunspell_directory)) |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 // Instead, tests should exercise individual spelling engines. | 78 // Instead, tests should exercise individual spelling engines. |
| 78 spell_check_->languages_.push_back(new SpellcheckLanguage()); | 79 spell_check_->languages_.push_back(new SpellcheckLanguage()); |
| 79 spell_check_->languages_.front()->platform_spelling_engine_.reset( | 80 spell_check_->languages_.front()->platform_spelling_engine_.reset( |
| 80 new HunspellEngine); | 81 new HunspellEngine); |
| 81 spell_check_->languages_.front()->Init(std::move(file), language); | 82 spell_check_->languages_.front()->Init(std::move(file), language); |
| 82 #else | 83 #else |
| 83 spell_check_->AddSpellcheckLanguage(std::move(file), language); | 84 spell_check_->AddSpellcheckLanguage(std::move(file), language); |
| 84 #endif | 85 #endif |
| 85 } | 86 } |
| 86 | 87 |
| 88 HunspellEngine* GetHunspellEngine() { |
| 89 // We're guaranteed this is a HunspellEngine. FOR REALZ. |
| 90 return static_cast<HunspellEngine*>( |
| 91 spell_check_->languages_.front()->platform_spelling_engine_.get()); |
| 92 } |
| 93 |
| 87 ~SpellCheckTest() override {} | 94 ~SpellCheckTest() override {} |
| 88 | 95 |
| 89 SpellCheck* spell_check() { return spell_check_.get(); } | 96 SpellCheck* spell_check() { return spell_check_.get(); } |
| 90 | 97 |
| 91 bool CheckSpelling(const std::string& word, int tag) { | 98 bool CheckSpelling(const std::string& word, int tag) { |
| 92 return spell_check_->languages_.front() | 99 return spell_check_->languages_.front() |
| 93 ->platform_spelling_engine_->CheckSpelling(base::ASCIIToUTF16(word), | 100 ->platform_spelling_engine_->CheckSpelling(base::ASCIIToUTF16(word), |
| 94 tag); | 101 tag); |
| 95 } | 102 } |
| 96 | 103 |
| (...skipping 1465 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1562 // to be updated accordingly. | 1569 // to be updated accordingly. |
| 1563 ASSERT_EQ(5, chrome::spellcheck_common::kMaxSuggestions); | 1570 ASSERT_EQ(5, chrome::spellcheck_common::kMaxSuggestions); |
| 1564 FillSuggestions(suggestions_list, &suggestion_results); | 1571 FillSuggestions(suggestions_list, &suggestion_results); |
| 1565 ASSERT_EQ(5U, suggestion_results.size()); | 1572 ASSERT_EQ(5U, suggestion_results.size()); |
| 1566 EXPECT_EQ(base::ASCIIToUTF16("0foo"), suggestion_results[0]); | 1573 EXPECT_EQ(base::ASCIIToUTF16("0foo"), suggestion_results[0]); |
| 1567 EXPECT_EQ(base::ASCIIToUTF16("1foo"), suggestion_results[1]); | 1574 EXPECT_EQ(base::ASCIIToUTF16("1foo"), suggestion_results[1]); |
| 1568 EXPECT_EQ(base::ASCIIToUTF16("2foo"), suggestion_results[2]); | 1575 EXPECT_EQ(base::ASCIIToUTF16("2foo"), suggestion_results[2]); |
| 1569 EXPECT_EQ(base::ASCIIToUTF16("0bar"), suggestion_results[3]); | 1576 EXPECT_EQ(base::ASCIIToUTF16("0bar"), suggestion_results[3]); |
| 1570 EXPECT_EQ(base::ASCIIToUTF16("1bar"), suggestion_results[4]); | 1577 EXPECT_EQ(base::ASCIIToUTF16("1bar"), suggestion_results[4]); |
| 1571 } | 1578 } |
| 1579 |
| 1580 TEST_F(SpellCheckTest, Suggestions_Crasher) { |
| 1581 const char kTrigger[] = |
| 1582 "R:<?xl<<?x(ml?>Ω<abuts?xºml?>b<?xe?<}\r" |
| 1583 ":'€<abuts?xml?>b<?x-?->paralleldi!pe.Tselctionp"; |
| 1584 |
| 1585 int misspelling_start = 0; |
| 1586 int misspelling_length = 0; |
| 1587 std::vector<base::string16> suggestions; |
| 1588 |
| 1589 bool result = spell_check()->SpellCheckWord( |
| 1590 base::UTF8ToUTF16(kTrigger).c_str(), kNoOffset, |
| 1591 static_cast<int>(strlen(kTrigger)), kNoTag, &misspelling_start, |
| 1592 &misspelling_length, &suggestions); |
| 1593 |
| 1594 EXPECT_NE(0, misspelling_length); |
| 1595 EXPECT_FALSE(result); |
| 1596 |
| 1597 // We're guaranteed this is a HunspellEngine. FOR REALZ. |
| 1598 HunspellEngine* engine = GetHunspellEngine(); |
| 1599 ASSERT_NE(nullptr, engine); |
| 1600 |
| 1601 Hunspell* hunspell = engine->get_hunspell(); |
| 1602 ASSERT_NE(nullptr, hunspell); |
| 1603 |
| 1604 char** suggest = nullptr; |
| 1605 int suggestion_length = hunspell->suggest(&suggest, kTrigger); |
| 1606 hunspell->free_list(&suggest, suggestion_length); |
| 1607 |
| 1608 LOG(ERROR) << "Length:" << suggestion_length; |
| 1609 } |
| OLD | NEW |