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 "components/spellcheck/renderer/spellcheck_provider_test.h" | 5 #include "components/spellcheck/renderer/spellcheck_provider_test.h" |
6 | 6 |
7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
8 #include "base/stl_util.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "base/run_loop.h" |
| 10 #include "components/spellcheck/common/spellcheck.mojom.h" |
9 #include "components/spellcheck/common/spellcheck_messages.h" | 11 #include "components/spellcheck/common/spellcheck_messages.h" |
| 12 #include "components/spellcheck/common/spellcheck_result.h" |
10 #include "components/spellcheck/renderer/spellcheck.h" | 13 #include "components/spellcheck/renderer/spellcheck.h" |
11 #include "components/spellcheck/spellcheck_build_features.h" | 14 #include "components/spellcheck/spellcheck_build_features.h" |
12 #include "ipc/ipc_message_macros.h" | |
13 | |
14 class MockSpellcheck: public SpellCheck { | |
15 }; | |
16 | 15 |
17 FakeTextCheckingCompletion::FakeTextCheckingCompletion() | 16 FakeTextCheckingCompletion::FakeTextCheckingCompletion() |
18 : completion_count_(0), | 17 : completion_count_(0), cancellation_count_(0) {} |
19 cancellation_count_(0) { | |
20 } | |
21 | 18 |
22 FakeTextCheckingCompletion::~FakeTextCheckingCompletion() {} | 19 FakeTextCheckingCompletion::~FakeTextCheckingCompletion() {} |
23 | 20 |
24 void FakeTextCheckingCompletion::DidFinishCheckingText( | 21 void FakeTextCheckingCompletion::DidFinishCheckingText( |
25 const blink::WebVector<blink::WebTextCheckingResult>& results) { | 22 const blink::WebVector<blink::WebTextCheckingResult>& results) { |
26 ++completion_count_; | 23 ++completion_count_; |
27 } | 24 } |
28 | 25 |
29 void FakeTextCheckingCompletion::DidCancelCheckingText() { | 26 void FakeTextCheckingCompletion::DidCancelCheckingText() { |
30 ++completion_count_; | 27 ++completion_count_; |
31 ++cancellation_count_; | 28 ++cancellation_count_; |
32 } | 29 } |
33 | 30 |
34 TestingSpellCheckProvider::TestingSpellCheckProvider() | 31 TestingSpellCheckProvider::TestingSpellCheckProvider() |
35 : SpellCheckProvider(NULL, new MockSpellcheck), | 32 : SpellCheckProvider(nullptr, new SpellCheck), |
36 spelling_service_call_count_(0) { | 33 spelling_service_call_count_(0), |
37 } | 34 binding_(this) {} |
38 | 35 |
39 TestingSpellCheckProvider::TestingSpellCheckProvider( | 36 TestingSpellCheckProvider::TestingSpellCheckProvider(SpellCheck* spellcheck) |
40 SpellCheck* spellcheck) | |
41 : SpellCheckProvider(nullptr, spellcheck), | 37 : SpellCheckProvider(nullptr, spellcheck), |
42 spelling_service_call_count_(0) { | 38 spelling_service_call_count_(0), |
43 } | 39 binding_(this) {} |
44 | 40 |
45 TestingSpellCheckProvider::~TestingSpellCheckProvider() { | 41 TestingSpellCheckProvider::~TestingSpellCheckProvider() { |
| 42 binding_.Close(); |
46 delete spellcheck_; | 43 delete spellcheck_; |
47 } | 44 } |
48 | 45 |
49 bool TestingSpellCheckProvider::Send(IPC::Message* message) { | 46 void TestingSpellCheckProvider::RequestTextChecking( |
| 47 const base::string16& text, |
| 48 blink::WebTextCheckingCompletion* completion) { |
50 #if !BUILDFLAG(USE_BROWSER_SPELLCHECKER) | 49 #if !BUILDFLAG(USE_BROWSER_SPELLCHECKER) |
51 // Call our mock message handlers. | 50 if (!loop_ && !base::MessageLoop::current()) |
52 bool handled = true; | 51 loop_ = base::MakeUnique<base::MessageLoop>(); |
53 IPC_BEGIN_MESSAGE_MAP(TestingSpellCheckProvider, *message) | 52 if (!binding_.is_bound()) |
54 IPC_MESSAGE_HANDLER(SpellCheckHostMsg_CallSpellingService, | 53 SetSpellCheckHostForTesting(binding_.CreateInterfacePtrAndBind()); |
55 OnCallSpellingService) | 54 SpellCheckProvider::RequestTextChecking(text, completion); |
56 IPC_MESSAGE_UNHANDLED(handled = false) | 55 base::RunLoop().RunUntilIdle(); |
57 IPC_END_MESSAGE_MAP() | 56 #else |
| 57 SpellCheckProvider::RequestTextChecking(text, completion); |
| 58 #endif |
| 59 } |
58 | 60 |
59 if (handled) { | 61 bool TestingSpellCheckProvider::Send(IPC::Message* message) { |
60 delete message; | |
61 return true; | |
62 } | |
63 #endif | |
64 | |
65 messages_.push_back(base::WrapUnique<IPC::Message>(message)); | 62 messages_.push_back(base::WrapUnique<IPC::Message>(message)); |
66 return true; | 63 return true; |
67 } | 64 } |
68 | 65 |
| 66 void TestingSpellCheckProvider::RequestDictionary() {} |
| 67 |
| 68 void TestingSpellCheckProvider::NotifyChecked(const base::string16& word, |
| 69 bool misspelled) {} |
| 70 |
| 71 void TestingSpellCheckProvider::CallSpellingService( |
| 72 const base::string16& text, |
| 73 const CallSpellingServiceCallback& callback) { |
| 74 #if !BUILDFLAG(USE_BROWSER_SPELLCHECKER) |
| 75 OnCallSpellingService(text); |
| 76 callback.Run(true, std::vector<SpellCheckResult>()); |
| 77 #else |
| 78 NOTREACHED(); |
| 79 #endif |
| 80 } |
| 81 |
69 void TestingSpellCheckProvider::OnCallSpellingService( | 82 void TestingSpellCheckProvider::OnCallSpellingService( |
70 int route_id, | |
71 int identifier, | |
72 const base::string16& text) { | 83 const base::string16& text) { |
73 #if BUILDFLAG(USE_BROWSER_SPELLCHECKER) | 84 #if !BUILDFLAG(USE_BROWSER_SPELLCHECKER) |
74 NOTREACHED(); | |
75 #else | |
76 ++spelling_service_call_count_; | 85 ++spelling_service_call_count_; |
77 blink::WebTextCheckingCompletion* completion = | 86 blink::WebTextCheckingCompletion* completion = |
78 text_check_completions_.Lookup(identifier); | 87 text_check_completions_.Lookup(last_identifier_); |
79 if (!completion) { | 88 if (!completion) { |
80 ResetResult(); | 89 ResetResult(); |
81 return; | 90 return; |
82 } | 91 } |
83 text_.assign(text); | 92 text_.assign(text); |
84 text_check_completions_.Remove(identifier); | 93 text_check_completions_.Remove(last_identifier_); |
85 std::vector<blink::WebTextCheckingResult> results; | 94 std::vector<blink::WebTextCheckingResult> results; |
86 results.push_back(blink::WebTextCheckingResult( | 95 results.push_back(blink::WebTextCheckingResult( |
87 blink::kWebTextDecorationTypeSpelling, 0, 5, blink::WebString("hello"))); | 96 blink::kWebTextDecorationTypeSpelling, 0, 5, blink::WebString("hello"))); |
88 completion->DidFinishCheckingText(results); | 97 completion->DidFinishCheckingText(results); |
89 last_request_ = text; | 98 last_request_ = text; |
90 last_results_ = results; | 99 last_results_ = results; |
| 100 #else |
| 101 NOTREACHED(); |
91 #endif | 102 #endif |
92 } | 103 } |
93 | 104 |
94 void TestingSpellCheckProvider::ResetResult() { | 105 void TestingSpellCheckProvider::ResetResult() { |
95 text_.clear(); | 106 text_.clear(); |
96 } | 107 } |
97 | 108 |
98 void TestingSpellCheckProvider::SetLastResults( | 109 void TestingSpellCheckProvider::SetLastResults( |
99 const base::string16 last_request, | 110 const base::string16 last_request, |
100 blink::WebVector<blink::WebTextCheckingResult>& last_results) { | 111 blink::WebVector<blink::WebTextCheckingResult>& last_results) { |
101 last_request_ = last_request; | 112 last_request_ = last_request; |
102 last_results_ = last_results; | 113 last_results_ = last_results; |
103 } | 114 } |
104 | 115 |
105 bool TestingSpellCheckProvider::SatisfyRequestFromCache( | 116 bool TestingSpellCheckProvider::SatisfyRequestFromCache( |
106 const base::string16& text, | 117 const base::string16& text, |
107 blink::WebTextCheckingCompletion* completion) { | 118 blink::WebTextCheckingCompletion* completion) { |
108 return SpellCheckProvider::SatisfyRequestFromCache(text, completion); | 119 return SpellCheckProvider::SatisfyRequestFromCache(text, completion); |
109 } | 120 } |
110 | 121 |
111 SpellCheckProviderTest::SpellCheckProviderTest() {} | 122 SpellCheckProviderTest::SpellCheckProviderTest() {} |
112 SpellCheckProviderTest::~SpellCheckProviderTest() {} | 123 SpellCheckProviderTest::~SpellCheckProviderTest() {} |
OLD | NEW |