Commit 5fe2c6a2 authored by xiaochengh's avatar xiaochengh Committed by Commit bot

Allow test runner to run a callback when finishing a spellcheck request

This patch adds a new method setSpellCheckResolvedCallback to test
runner, so that layout tests can run script when a spell check request
is resolved, which can be used to inspect spelling markers in a
better manner. It also adds removeSpellCheckResolvedCallback for clearing
a previously set callback.

Currently layout tests can only set a timeout to wait for markers to appear.
Follow-up patches will utilize this new method in layout tests.

BUG=674819

Review-Url: https://codereview.chromium.org/2587823003
Cr-Commit-Position: refs/heads/master@{#439725}
parent a5ade1a6
...@@ -13,6 +13,8 @@ ...@@ -13,6 +13,8 @@
#include "components/test_runner/mock_grammar_check.h" #include "components/test_runner/mock_grammar_check.h"
#include "components/test_runner/test_runner.h" #include "components/test_runner/test_runner.h"
#include "components/test_runner/web_test_delegate.h" #include "components/test_runner/web_test_delegate.h"
#include "third_party/WebKit/public/web/WebFrame.h"
#include "third_party/WebKit/public/web/WebKit.h"
#include "third_party/WebKit/public/web/WebTextCheckingCompletion.h" #include "third_party/WebKit/public/web/WebTextCheckingCompletion.h"
#include "third_party/WebKit/public/web/WebTextCheckingResult.h" #include "third_party/WebKit/public/web/WebTextCheckingResult.h"
...@@ -36,6 +38,11 @@ void SpellCheckClient::SetEnabled(bool enabled) { ...@@ -36,6 +38,11 @@ void SpellCheckClient::SetEnabled(bool enabled) {
enabled_ = enabled; enabled_ = enabled;
} }
void SpellCheckClient::Reset() {
enabled_ = false;
resolved_callback_.Reset();
}
// blink::WebSpellCheckClient // blink::WebSpellCheckClient
void SpellCheckClient::spellCheck( void SpellCheckClient::spellCheck(
const blink::WebString& text, const blink::WebString& text,
...@@ -58,13 +65,17 @@ void SpellCheckClient::requestCheckingOfText( ...@@ -58,13 +65,17 @@ void SpellCheckClient::requestCheckingOfText(
const blink::WebVector<unsigned>& marker_offsets, const blink::WebVector<unsigned>& marker_offsets,
blink::WebTextCheckingCompletion* completion) { blink::WebTextCheckingCompletion* completion) {
if (!enabled_ || text.isEmpty()) { if (!enabled_ || text.isEmpty()) {
if (completion) if (completion) {
completion->didCancelCheckingText(); completion->didCancelCheckingText();
RequestResolved();
}
return; return;
} }
if (last_requested_text_checking_completion_) if (last_requested_text_checking_completion_) {
last_requested_text_checking_completion_->didCancelCheckingText(); last_requested_text_checking_completion_->didCancelCheckingText();
RequestResolved();
}
last_requested_text_checking_completion_ = completion; last_requested_text_checking_completion_ = completion;
last_requested_text_check_string_ = text; last_requested_text_check_string_ = text;
...@@ -115,9 +126,41 @@ void SpellCheckClient::FinishLastTextCheck() { ...@@ -115,9 +126,41 @@ void SpellCheckClient::FinishLastTextCheck() {
} }
last_requested_text_checking_completion_->didFinishCheckingText(results); last_requested_text_checking_completion_->didFinishCheckingText(results);
last_requested_text_checking_completion_ = 0; last_requested_text_checking_completion_ = 0;
RequestResolved();
if (test_runner_->shouldDumpSpellCheckCallbacks()) if (test_runner_->shouldDumpSpellCheckCallbacks())
delegate_->PrintMessage("SpellCheckEvent: FinishLastTextCheck\n"); delegate_->PrintMessage("SpellCheckEvent: FinishLastTextCheck\n");
} }
void SpellCheckClient::SetSpellCheckResolvedCallback(
v8::Local<v8::Function> callback) {
resolved_callback_.Reset(blink::mainThreadIsolate(), callback);
}
void SpellCheckClient::RemoveSpellCheckResolvedCallback() {
resolved_callback_.Reset();
}
void SpellCheckClient::RequestResolved() {
if (resolved_callback_.IsEmpty())
return;
v8::Isolate* isolate = blink::mainThreadIsolate();
v8::HandleScope handle_scope(isolate);
blink::WebFrame* frame = test_runner_->mainFrame();
if (!frame || frame->isWebRemoteFrame())
return;
v8::Local<v8::Context> context = frame->mainWorldScriptContext();
if (context.IsEmpty())
return;
v8::Context::Scope context_scope(context);
frame->callFunctionEvenIfScriptDisabled(
v8::Local<v8::Function>::New(isolate, resolved_callback_),
context->Global(), 0, nullptr);
}
} // namespace test_runner } // namespace test_runner
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#include "third_party/WebKit/public/platform/WebString.h" #include "third_party/WebKit/public/platform/WebString.h"
#include "third_party/WebKit/public/platform/WebVector.h" #include "third_party/WebKit/public/platform/WebVector.h"
#include "third_party/WebKit/public/web/WebSpellCheckClient.h" #include "third_party/WebKit/public/web/WebSpellCheckClient.h"
#include "v8/include/v8.h"
namespace blink { namespace blink {
class WebTextCheckingCompletion; class WebTextCheckingCompletion;
...@@ -31,6 +32,14 @@ class SpellCheckClient : public blink::WebSpellCheckClient { ...@@ -31,6 +32,14 @@ class SpellCheckClient : public blink::WebSpellCheckClient {
void SetDelegate(WebTestDelegate* delegate); void SetDelegate(WebTestDelegate* delegate);
void SetEnabled(bool enabled); void SetEnabled(bool enabled);
// Sets a callback that will be invoked after each request is revoled.
void SetSpellCheckResolvedCallback(v8::Local<v8::Function> callback);
// Remove the above callback. Beware: don't call it inside the callback.
void RemoveSpellCheckResolvedCallback();
void Reset();
// blink::WebSpellCheckClient implementation. // blink::WebSpellCheckClient implementation.
void spellCheck( void spellCheck(
const blink::WebString& text, const blink::WebString& text,
...@@ -47,6 +56,8 @@ class SpellCheckClient : public blink::WebSpellCheckClient { ...@@ -47,6 +56,8 @@ class SpellCheckClient : public blink::WebSpellCheckClient {
private: private:
void FinishLastTextCheck(); void FinishLastTextCheck();
void RequestResolved();
// Do not perform any checking when |enabled_ == false|. // Do not perform any checking when |enabled_ == false|.
// Tests related to spell checking should enable it manually. // Tests related to spell checking should enable it manually.
bool enabled_ = false; bool enabled_ = false;
...@@ -57,6 +68,8 @@ class SpellCheckClient : public blink::WebSpellCheckClient { ...@@ -57,6 +68,8 @@ class SpellCheckClient : public blink::WebSpellCheckClient {
blink::WebString last_requested_text_check_string_; blink::WebString last_requested_text_check_string_;
blink::WebTextCheckingCompletion* last_requested_text_checking_completion_; blink::WebTextCheckingCompletion* last_requested_text_checking_completion_;
v8::Persistent<v8::Function> resolved_callback_;
TestRunner* test_runner_; TestRunner* test_runner_;
WebTestDelegate* delegate_; WebTestDelegate* delegate_;
......
...@@ -181,6 +181,7 @@ class TestRunnerBindings : public gin::Wrappable<TestRunnerBindings> { ...@@ -181,6 +181,7 @@ class TestRunnerBindings : public gin::Wrappable<TestRunnerBindings> {
const std::string& destination_protocol, const std::string& destination_protocol,
const std::string& destination_host, const std::string& destination_host,
bool allow_destination_subdomains); bool allow_destination_subdomains);
void RemoveSpellCheckResolvedCallback();
void RemoveWebPageOverlay(); void RemoveWebPageOverlay();
void ResetDeviceLight(); void ResetDeviceLight();
void ResetTestHelperControllers(); void ResetTestHelperControllers();
...@@ -241,6 +242,7 @@ class TestRunnerBindings : public gin::Wrappable<TestRunnerBindings> { ...@@ -241,6 +242,7 @@ class TestRunnerBindings : public gin::Wrappable<TestRunnerBindings> {
void SetPrinting(); void SetPrinting();
void SetScriptsAllowed(bool allowed); void SetScriptsAllowed(bool allowed);
void SetShouldStayOnPageAfterHandlingBeforeUnload(bool value); void SetShouldStayOnPageAfterHandlingBeforeUnload(bool value);
void SetSpellCheckResolvedCallback(v8::Local<v8::Function> callback);
void SetStorageAllowed(bool allowed); void SetStorageAllowed(bool allowed);
void SetTabKeyCyclesThroughElements(bool tab_key_cycles_through_elements); void SetTabKeyCyclesThroughElements(bool tab_key_cycles_through_elements);
void SetTextDirection(const std::string& direction_name); void SetTextDirection(const std::string& direction_name);
...@@ -469,6 +471,8 @@ gin::ObjectTemplateBuilder TestRunnerBindings::GetObjectTemplateBuilder( ...@@ -469,6 +471,8 @@ gin::ObjectTemplateBuilder TestRunnerBindings::GetObjectTemplateBuilder(
.SetMethod("queueReload", &TestRunnerBindings::QueueReload) .SetMethod("queueReload", &TestRunnerBindings::QueueReload)
.SetMethod("removeOriginAccessWhitelistEntry", .SetMethod("removeOriginAccessWhitelistEntry",
&TestRunnerBindings::RemoveOriginAccessWhitelistEntry) &TestRunnerBindings::RemoveOriginAccessWhitelistEntry)
.SetMethod("removeSpellCheckResolvedCallback",
&TestRunnerBindings::RemoveSpellCheckResolvedCallback)
.SetMethod("removeWebPageOverlay", .SetMethod("removeWebPageOverlay",
&TestRunnerBindings::RemoveWebPageOverlay) &TestRunnerBindings::RemoveWebPageOverlay)
.SetMethod("resetDeviceLight", &TestRunnerBindings::ResetDeviceLight) .SetMethod("resetDeviceLight", &TestRunnerBindings::ResetDeviceLight)
...@@ -560,6 +564,8 @@ gin::ObjectTemplateBuilder TestRunnerBindings::GetObjectTemplateBuilder( ...@@ -560,6 +564,8 @@ gin::ObjectTemplateBuilder TestRunnerBindings::GetObjectTemplateBuilder(
.SetMethod( .SetMethod(
"setShouldStayOnPageAfterHandlingBeforeUnload", "setShouldStayOnPageAfterHandlingBeforeUnload",
&TestRunnerBindings::SetShouldStayOnPageAfterHandlingBeforeUnload) &TestRunnerBindings::SetShouldStayOnPageAfterHandlingBeforeUnload)
.SetMethod("setSpellCheckResolvedCallback",
&TestRunnerBindings::SetSpellCheckResolvedCallback)
.SetMethod("setStorageAllowed", &TestRunnerBindings::SetStorageAllowed) .SetMethod("setStorageAllowed", &TestRunnerBindings::SetStorageAllowed)
.SetMethod("setTabKeyCyclesThroughElements", .SetMethod("setTabKeyCyclesThroughElements",
&TestRunnerBindings::SetTabKeyCyclesThroughElements) &TestRunnerBindings::SetTabKeyCyclesThroughElements)
...@@ -748,6 +754,17 @@ void TestRunnerBindings::SetMockSpellCheckerEnabled(bool enabled) { ...@@ -748,6 +754,17 @@ void TestRunnerBindings::SetMockSpellCheckerEnabled(bool enabled) {
runner_->SetMockSpellCheckerEnabled(enabled); runner_->SetMockSpellCheckerEnabled(enabled);
} }
void TestRunnerBindings::SetSpellCheckResolvedCallback(
v8::Local<v8::Function> callback) {
if (runner_)
runner_->spellcheck_->SetSpellCheckResolvedCallback(callback);
}
void TestRunnerBindings::RemoveSpellCheckResolvedCallback() {
if (runner_)
runner_->spellcheck_->RemoveSpellCheckResolvedCallback();
}
v8::Local<v8::Value> v8::Local<v8::Value>
TestRunnerBindings::EvaluateScriptInIsolatedWorldAndReturnValue( TestRunnerBindings::EvaluateScriptInIsolatedWorldAndReturnValue(
int world_id, const std::string& script) { int world_id, const std::string& script) {
...@@ -1682,7 +1699,7 @@ void TestRunner::Reset() { ...@@ -1682,7 +1699,7 @@ void TestRunner::Reset() {
else else
close_remaining_windows_ = true; close_remaining_windows_ = true;
spellcheck_->SetEnabled(false); spellcheck_->Reset();
} }
void TestRunner::SetTestIsRunning(bool running) { void TestRunner::SetTestIsRunning(bool running) {
...@@ -1924,6 +1941,10 @@ WebFrame* TestRunner::topLoadingFrame() const { ...@@ -1924,6 +1941,10 @@ WebFrame* TestRunner::topLoadingFrame() const {
return top_loading_frame_; return top_loading_frame_;
} }
WebFrame* TestRunner::mainFrame() const {
return main_view_->mainFrame();
}
void TestRunner::policyDelegateDone() { void TestRunner::policyDelegateDone() {
DCHECK(layout_test_runtime_flags_.wait_until_done()); DCHECK(layout_test_runtime_flags_.wait_until_done());
delegate_->TestFinished(); delegate_->TestFinished();
......
...@@ -155,6 +155,7 @@ class TestRunner : public WebTestRunner { ...@@ -155,6 +155,7 @@ class TestRunner : public WebTestRunner {
// pending load requests in WorkQueue). // pending load requests in WorkQueue).
bool tryToClearTopLoadingFrame(blink::WebFrame*); bool tryToClearTopLoadingFrame(blink::WebFrame*);
blink::WebFrame* mainFrame() const;
blink::WebFrame* topLoadingFrame() const; blink::WebFrame* topLoadingFrame() const;
void policyDelegateDone(); void policyDelegateDone();
bool policyDelegateEnabled() const; bool policyDelegateEnabled() const;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment