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

Side by Side Diff: content/browser/site_per_process_browsertest.cc

Issue 2653283002: Route IME Events to Focused RenderWidgets (Android) (Closed)
Patch Set: Minor fixes Created 3 years, 10 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
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 "content/browser/site_per_process_browsertest.h" 5 #include "content/browser/site_per_process_browsertest.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <algorithm> 10 #include <algorithm>
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
84 #include "ui/native_theme/native_theme_switches.h" 84 #include "ui/native_theme/native_theme_switches.h"
85 85
86 #if defined(USE_AURA) 86 #if defined(USE_AURA)
87 #include "content/browser/renderer_host/render_widget_host_view_aura.h" 87 #include "content/browser/renderer_host/render_widget_host_view_aura.h"
88 #endif 88 #endif
89 89
90 #if defined(OS_MACOSX) 90 #if defined(OS_MACOSX)
91 #include "ui/base/test/scoped_preferred_scroller_style_mac.h" 91 #include "ui/base/test/scoped_preferred_scroller_style_mac.h"
92 #endif 92 #endif
93 93
94 #if defined(OS_ANDROID)
95 #include "base/android/jni_android.h"
96 #include "base/android/jni_string.h"
97 #include "base/android/scoped_java_ref.h"
98 #include "content/browser/renderer_host/ime_adapter_android.h"
99 #include "content/browser/renderer_host/render_widget_host_view_android.h"
100 #endif
101
94 namespace content { 102 namespace content {
95 103
96 namespace { 104 namespace {
97 105
98 // Helper function to send a postMessage and wait for a reply message. The 106 // Helper function to send a postMessage and wait for a reply message. The
99 // |post_message_script| is executed on the |sender_ftn| frame, and the sender 107 // |post_message_script| is executed on the |sender_ftn| frame, and the sender
100 // frame is expected to post |reply_status| from the DOMAutomationController 108 // frame is expected to post |reply_status| from the DOMAutomationController
101 // when it receives a reply. 109 // when it receives a reply.
102 void PostMessageAndWaitForReply(FrameTreeNode* sender_ftn, 110 void PostMessageAndWaitForReply(FrameTreeNode* sender_ftn,
103 const std::string& post_message_script, 111 const std::string& post_message_script,
(...skipping 9229 matching lines...) Expand 10 before | Expand all | Expand 10 after
9333 "a.com", base::StringPrintf("/site_isolation/" 9341 "a.com", base::StringPrintf("/site_isolation/"
9334 "subframes_with_resources.html?urls=%s,%s&" 9342 "subframes_with_resources.html?urls=%s,%s&"
9335 "numSubresources=3", 9343 "numSubresources=3",
9336 path0.c_str(), path1.c_str()))); 9344 path0.c_str(), path1.c_str())));
9337 EXPECT_TRUE(NavigateToURL(shell(), url)); 9345 EXPECT_TRUE(NavigateToURL(shell(), url));
9338 bool result; 9346 bool result;
9339 EXPECT_TRUE(ExecuteScriptAndExtractBool(shell(), "createFrames()", &result)); 9347 EXPECT_TRUE(ExecuteScriptAndExtractBool(shell(), "createFrames()", &result));
9340 EXPECT_TRUE(result); 9348 EXPECT_TRUE(result);
9341 } 9349 }
9342 9350
9351 #if defined(OS_ANDROID)
9352 class TextSelectionObserver : public TextInputManager::Observer {
9353 public:
9354 explicit TextSelectionObserver(TextInputManager* text_input_manager)
9355 : text_input_manager_(text_input_manager) {
9356 text_input_manager->AddObserver(this);
9357 }
9358
9359 ~TextSelectionObserver() { text_input_manager_->RemoveObserver(this); }
9360
9361 void WaitForSelectedText(const std::string& expected_text) {
9362 if (last_selected_text_ == expected_text)
9363 return;
9364 expected_text_ = expected_text;
9365 loop_runner_ = new MessageLoopRunner();
9366 loop_runner_->Run();
9367 }
9368
9369 private:
9370 void OnTextSelectionChanged(TextInputManager* text_input_manager,
9371 RenderWidgetHostViewBase* updated_view) override {
9372 base::string16 text;
9373 if (text_input_manager->GetTextSelection(updated_view)
9374 ->GetSelectedText(&text)) {
9375 last_selected_text_ = base::UTF16ToUTF8(text);
9376 if (last_selected_text_ == expected_text_ && loop_runner_)
9377 loop_runner_->Quit();
9378 }
9379 }
9380 TextInputManager* const text_input_manager_;
9381 std::string last_selected_text_;
9382 std::string expected_text_;
9383 scoped_refptr<MessageLoopRunner> loop_runner_;
9384
9385 DISALLOW_COPY_AND_ASSIGN(TextSelectionObserver);
9386 };
9387
9388 class SitePerProcessAndroidImeTest : public SitePerProcessBrowserTest {
9389 public:
9390 SitePerProcessAndroidImeTest() : SitePerProcessBrowserTest() {}
9391 ~SitePerProcessAndroidImeTest() override {}
9392
9393 protected:
9394 ImeAdapterAndroid* ime_adapter() {
9395 return static_cast<RenderWidgetHostViewAndroid*>(
9396 web_contents()->GetRenderWidgetHostView())
9397 ->get_ime_adapter_for_testing();
9398 }
9399
9400 std::string GetInputValue(RenderFrameHostImpl* frame) {
9401 std::string result;
9402 EXPECT_TRUE(ExecuteScriptAndExtractString(
9403 frame, "window.domAutomationController.send(input.value);", &result));
9404 return result;
9405 }
9406
9407 void FocusInputInFrame(RenderFrameHostImpl* frame) {
9408 ASSERT_TRUE(ExecuteScript(frame, "window.focus(); input.focus();"));
9409 }
9410
9411 // Creates a page with multiple (nested) OOPIFs and populates all of them
9412 // with an <input> element along with the required handlers for the test.
9413 void LoadPage() {
9414 ASSERT_TRUE(NavigateToURL(
9415 shell(),
9416 GURL(embedded_test_server()->GetURL(
9417 "a.com", "/cross_site_iframe_factory.html?a(b,c(a(b)))"))));
9418 FrameTreeNode* root = web_contents()->GetFrameTree()->root();
9419 frames_.push_back(root->current_frame_host());
9420 frames_.push_back(root->child_at(0)->current_frame_host());
9421 frames_.push_back(root->child_at(1)->current_frame_host());
9422 frames_.push_back(root->child_at(1)->child_at(0)->current_frame_host());
9423 frames_.push_back(
9424 root->child_at(1)->child_at(0)->child_at(0)->current_frame_host());
9425
9426 // Adds an <input> to frame and sets up a handler for |window.oninput|. When
9427 // the input event is fired (by changing the value of <input> element), the
9428 // handler will select all the text so that the corresponding text selection
9429 // update on the browser side notifies the test about input insertion.
9430 std::string add_input_script =
9431 "var input = document.createElement('input');"
9432 "document.body.appendChild(input);"
9433 "window.oninput = function() {"
9434 " input.select();"
9435 "};";
9436
9437 for (auto* frame : frames_)
9438 ASSERT_TRUE(ExecuteScript(frame, add_input_script));
9439 }
9440
9441 // This methods tries to commit |text| by simulating a native call from Java.
9442 void CommitText(const char* text) {
9443 JNIEnv* env = base::android::AttachCurrentThread();
9444
9445 // A valid caller is needed for ImeAdapterAndroid::GetUnerlinesFromSpans.
Charlie Reis 2017/02/21 19:30:58 typo: Underlines
EhsanK 2017/02/21 20:41:38 Done.
9446 base::android::ScopedJavaLocalRef<jobject> caller =
9447 ime_adapter()->java_ime_adapter_for_testing(env);
9448
9449 // Input string from Java side.
9450 base::android::ScopedJavaLocalRef<jstring> jtext =
9451 base::android::ConvertUTF8ToJavaString(env, text);
9452
9453 // Simulating a native call from Java side.
9454 ime_adapter()->CommitText(
9455 env, base::android::JavaParamRef<jobject>(env, caller.obj()),
9456 base::android::JavaParamRef<jobject>(env, jtext.obj()),
9457 base::android::JavaParamRef<jstring>(env, jtext.obj()), 0);
9458 }
9459
9460 std::vector<RenderFrameHostImpl*> frames_;
9461
9462 private:
9463 DISALLOW_COPY_AND_ASSIGN(SitePerProcessAndroidImeTest);
9464 };
9465
9466 // This test verifies that committing text will be applied on the focused
9467 // RenderWidgetHost.
9468 IN_PROC_BROWSER_TEST_F(SitePerProcessAndroidImeTest,
9469 CommitTextForFocusedWidget) {
9470 LoadPage();
9471 TextSelectionObserver selection_observer(
9472 web_contents()->GetTextInputManager());
9473 for (size_t index = 0; index < frames_.size(); ++index) {
9474 std::string text = base::StringPrintf("text%zu", index);
9475 FocusInputInFrame(frames_[index]);
9476 CommitText(text.c_str());
9477 selection_observer.WaitForSelectedText(text);
9478 }
9479 }
9480 #endif // OS_ANDROID
9481
9343 } // namespace content 9482 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698