Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(50)

Side by Side Diff: chrome/renderer/spellchecker/spellcheck_unittest.cc

Issue 9169082: Asynchronous spellchecking on Win and Linux (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Created 8 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 "webkit/glue/webkit_glue.h" 5 #include "webkit/glue/webkit_glue.h"
6 6
7 #include "base/file_util.h" 7 #include "base/file_util.h"
8 #include "base/message_loop.h" 8 #include "base/message_loop.h"
9 #include "base/path_service.h" 9 #include "base/path_service.h"
10 #include "base/platform_file.h" 10 #include "base/platform_file.h"
11 #include "base/sys_string_conversions.h" 11 #include "base/sys_string_conversions.h"
12 #include "base/utf_string_conversions.h" 12 #include "base/utf_string_conversions.h"
13 #include "chrome/renderer/spellchecker/spellcheck.h" 13 #include "chrome/renderer/spellchecker/spellcheck.h"
14 #include "chrome/common/chrome_paths.h" 14 #include "chrome/common/chrome_paths.h"
15 #include "chrome/common/spellcheck_common.h" 15 #include "chrome/common/spellcheck_common.h"
16 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebTextCheckingComple tion.h"
17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebTextCheckingResult .h" 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebTextCheckingResult .h"
18 19
19 namespace { 20 namespace {
20 21
21 FilePath GetHunspellDirectory() { 22 FilePath GetHunspellDirectory() {
22 FilePath hunspell_directory; 23 FilePath hunspell_directory;
23 if (!PathService::Get(base::DIR_SOURCE_ROOT, &hunspell_directory)) 24 if (!PathService::Get(base::DIR_SOURCE_ROOT, &hunspell_directory))
24 return FilePath(); 25 return FilePath();
25 26
26 hunspell_directory = hunspell_directory.AppendASCII("third_party"); 27 hunspell_directory = hunspell_directory.AppendASCII("third_party");
27 hunspell_directory = hunspell_directory.AppendASCII("hunspell_dictionaries"); 28 hunspell_directory = hunspell_directory.AppendASCII("hunspell_dictionaries");
28 return hunspell_directory; 29 return hunspell_directory;
29 } 30 }
30 31
31 } // namespace 32 } // namespace
32 33
33 class SpellCheckTest : public testing::Test { 34 class SpellCheckTest : public testing::Test {
34 public: 35 public:
35 SpellCheckTest() { 36 SpellCheckTest() {
36 ReinitializeSpellCheck("en-US"); 37 ReinitializeSpellCheck("en-US");
37 } 38 }
38 39
39 void ReinitializeSpellCheck(const std::string& language) { 40 void ReinitializeSpellCheck(const std::string& language) {
40 spell_check_.reset(new SpellCheck()); 41 spell_check_.reset(new SpellCheck());
42 InitializeSpellCheck(language);
43 }
41 44
45 void UninitializeSpellCheck() {
46 spell_check_.reset(new SpellCheck());
47 }
48
49 void InitializeSpellCheck(const std::string& language) {
42 FilePath hunspell_directory = GetHunspellDirectory(); 50 FilePath hunspell_directory = GetHunspellDirectory();
43 EXPECT_FALSE(hunspell_directory.empty()); 51 EXPECT_FALSE(hunspell_directory.empty());
44 base::PlatformFile file = base::CreatePlatformFile( 52 base::PlatformFile file = base::CreatePlatformFile(
45 SpellCheckCommon::GetVersionedFileName(language, hunspell_directory), 53 SpellCheckCommon::GetVersionedFileName(language, hunspell_directory),
46 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ, NULL, NULL); 54 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ, NULL, NULL);
47 spell_check_->Init( 55 spell_check_->Init(
48 file, std::vector<std::string>(), language); 56 file, std::vector<std::string>(), language);
49 } 57 }
50 58
51 virtual ~SpellCheckTest() { 59 virtual ~SpellCheckTest() {
(...skipping 16 matching lines...) Expand all
68 for (size_t j = 0; j < size; ++j) { 76 for (size_t j = 0; j < size; ++j) {
69 EXPECT_EQ(results[j].type, WebKit::WebTextCheckingTypeSpelling); 77 EXPECT_EQ(results[j].type, WebKit::WebTextCheckingTypeSpelling);
70 EXPECT_EQ(results[j].location, expected[j].location); 78 EXPECT_EQ(results[j].location, expected[j].location);
71 EXPECT_EQ(results[j].length, expected[j].length); 79 EXPECT_EQ(results[j].length, expected[j].length);
72 } 80 }
73 } 81 }
74 #endif 82 #endif
75 83
76 private: 84 private:
77 scoped_ptr<SpellCheck> spell_check_; 85 scoped_ptr<SpellCheck> spell_check_;
86 MessageLoop loop;
78 }; 87 };
79 88
89 // A fake completion object for verification.
90 class FakeTextCheckingCompletion : public WebKit::WebTextCheckingCompletion {
Hironori Bono 2012/01/27 06:17:41 nit: we use "Mock..." instead of "Fake...".
shinyak 2012/01/30 06:53:05 Done.
91 public:
92 FakeTextCheckingCompletion()
93 : completion_count_(0) {
94 }
95
96 virtual void didFinishCheckingText(
97 const WebKit::WebVector<WebKit::WebTextCheckingResult>& results) {
98 completion_count_++;
99 last_results_ = results;
100 }
101
102 size_t completion_count_;
103 WebKit::WebVector<WebKit::WebTextCheckingResult> last_results_;
104 };
105
106
80 // Operates unit tests for the webkit_glue::SpellCheckWord() function 107 // Operates unit tests for the webkit_glue::SpellCheckWord() function
81 // with the US English dictionary. 108 // with the US English dictionary.
82 // The unit tests in this function consist of: 109 // The unit tests in this function consist of:
83 // * Tests for the function with empty strings; 110 // * Tests for the function with empty strings;
84 // * Tests for the function with a valid English word; 111 // * Tests for the function with a valid English word;
85 // * Tests for the function with a valid non-English word; 112 // * Tests for the function with a valid non-English word;
86 // * Tests for the function with a valid English word with a preceding 113 // * Tests for the function with a valid English word with a preceding
87 // space character; 114 // space character;
88 // * Tests for the function with a valid English word with a preceding 115 // * Tests for the function with a valid English word with a preceding
89 // non-English word; 116 // non-English word;
(...skipping 710 matching lines...) Expand 10 before | Expand all | Expand 10 after
800 WebKit::WebTextCheckingTypeSpelling, 135, 3)); 827 WebKit::WebTextCheckingTypeSpelling, 135, 3));
801 expected.push_back(WebKit::WebTextCheckingResult( 828 expected.push_back(WebKit::WebTextCheckingResult(
802 WebKit::WebTextCheckingTypeSpelling, 163, 3)); 829 WebKit::WebTextCheckingTypeSpelling, 163, 3));
803 expected.push_back(WebKit::WebTextCheckingResult( 830 expected.push_back(WebKit::WebTextCheckingResult(
804 WebKit::WebTextCheckingTypeSpelling, 195, 3)); 831 WebKit::WebTextCheckingTypeSpelling, 195, 3));
805 expected.push_back(WebKit::WebTextCheckingResult( 832 expected.push_back(WebKit::WebTextCheckingResult(
806 WebKit::WebTextCheckingTypeSpelling, 298, 3)); 833 WebKit::WebTextCheckingTypeSpelling, 298, 3));
807 834
808 TestSpellCheckParagraph(text, expected); 835 TestSpellCheckParagraph(text, expected);
809 } 836 }
837
838 // We also skip RequestSpellCheck tests on Mac, because a system spellchecker
839 // is used on Mac instead of SpellCheck::RequestTextChecking.
840
841 // Make sure RequestTextChecking does not crash if input is empty.
842 TEST_F(SpellCheckTest, RequestSpellCheckWithEmptyString) {
843 FakeTextCheckingCompletion completion;
844
845 const string16 text = ASCIIToUTF16("");
846 spell_check()->RequestTextChecking(text, 0, &completion);
847
848 MessageLoop::current()->RunAllPending();
849
850 EXPECT_EQ(completion.completion_count_, 1U);
851 }
852
853 // A simple test case having no misspellings.
854 TEST_F(SpellCheckTest, RequestSpellCheckWithoutMisspelling) {
855 FakeTextCheckingCompletion completion;
856
857 const string16 text = ASCIIToUTF16("hello");
858 spell_check()->RequestTextChecking(text, 0, &completion);
859
860 MessageLoop::current()->RunAllPending();
861
862 EXPECT_EQ(completion.completion_count_, 1U);
863 }
864
865 // A simple test case having one misspelling.
866 TEST_F(SpellCheckTest, RequestSpellCheckWithSingleMisspelling) {
867 FakeTextCheckingCompletion completion;
868
869 const string16 text = ASCIIToUTF16("apple, zz");
870 spell_check()->RequestTextChecking(text, 0, &completion);
871
872 MessageLoop::current()->RunAllPending();
873
874 EXPECT_EQ(completion.completion_count_, 1U);
875 EXPECT_EQ(completion.last_results_.size(), 1U);
876 EXPECT_EQ(completion.last_results_[0].location, 7);
877 EXPECT_EQ(completion.last_results_[0].length, 2);
878 }
879
880 // A simple test case having a few misspellings.
881 TEST_F(SpellCheckTest, RequestSpellCheckWithMisspellings) {
882 FakeTextCheckingCompletion completion;
883
884 const string16 text = ASCIIToUTF16("apple, zz, orange, zz");
885 spell_check()->RequestTextChecking(text, 0, &completion);
886
887 MessageLoop::current()->RunAllPending();
888
889 EXPECT_EQ(completion.completion_count_, 1U);
890 EXPECT_EQ(completion.last_results_.size(), 2U);
891 EXPECT_EQ(completion.last_results_[0].location, 7);
892 EXPECT_EQ(completion.last_results_[0].length, 2);
893 EXPECT_EQ(completion.last_results_[1].location, 19);
894 EXPECT_EQ(completion.last_results_[1].length, 2);
895 }
896
897 // A test case that multiple requests comes at once. Make sure all
898 // requests are processed.
899 TEST_F(SpellCheckTest, RequestSpellCheckWithMultipleRequests) {
900 FakeTextCheckingCompletion completion[3];
901
902 const string16 text[3] = {
903 ASCIIToUTF16("what, zz"),
904 ASCIIToUTF16("apple, zz"),
905 ASCIIToUTF16("orange, zz")
906 };
907
908 for (int i = 0; i < 3; ++i)
909 spell_check()->RequestTextChecking(text[i], 0, &completion[i]);
910
911 MessageLoop::current()->RunAllPending();
912
913 for (int i = 0; i < 3; ++i) {
914 EXPECT_EQ(completion[i].completion_count_, 1U);
915 EXPECT_EQ(completion[i].last_results_.size(), 1U);
916 EXPECT_EQ(completion[i].last_results_[0].location, 6 + i);
917 EXPECT_EQ(completion[i].last_results_[0].length, 2);
918 }
919 }
920
921 // A test case that spellchecking is requested before initializing.
922 // In this case, we postpone to post a request.
923 TEST_F(SpellCheckTest, RequestSpellCheckWithoutInitialization) {
924 UninitializeSpellCheck();
925
926 FakeTextCheckingCompletion completion;
927 const string16 text = ASCIIToUTF16("zz");
928
929 spell_check()->RequestTextChecking(text, 0, &completion);
930
931 // The task will not be posted yet.
932 MessageLoop::current()->RunAllPending();
933 EXPECT_EQ(completion.completion_count_, 0U);
934 }
935
936 // Requests several spellchecking before initializing. Except the last one,
937 // posting requests is cancelled and text is rendered as correct one.
938 TEST_F(SpellCheckTest, RequestSpellCheckMultipleTimesWithoutInitialization) {
939 UninitializeSpellCheck();
940
941 FakeTextCheckingCompletion completion[3];
942 const string16 text[3] = {
943 ASCIIToUTF16("what, zz"),
944 ASCIIToUTF16("apple, zz"),
945 ASCIIToUTF16("orange, zz")
946 };
947
948 // Calls RequestTextchecking a few times.
949 for (int i = 0; i < 3; ++i)
950 spell_check()->RequestTextChecking(text[i], 0, &completion[i]);
951
952 // The last task will be posted after initialization, however the other
953 // requests should be pressed without spellchecking.
954 MessageLoop::current()->RunAllPending();
955 for (int i = 0; i < 2; ++i)
956 EXPECT_EQ(completion[i].completion_count_, 1U);
957 EXPECT_EQ(completion[2].completion_count_, 0U);
958
959 // Checks the last request is processed after initialization.
960 InitializeSpellCheck("en-US");
961
962 // Calls PostDelayedSpellCheckTask instead of OnInit here for simplicity.
963 spell_check()->PostDelayedSpellCheckTask();
964 MessageLoop::current()->RunAllPending();
965 for (int i = 0; i < 3; ++i)
966 EXPECT_EQ(completion[i].completion_count_, 1U);
967 }
968
810 #endif 969 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698