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

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

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

Powered by Google App Engine
This is Rietveld 408576698