| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 <vector> |
| 6 |
| 5 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 6 #include "chrome/browser/ui/browser.h" | 8 #include "chrome/browser/ui/browser.h" |
| 7 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 9 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 8 #include "chrome/test/base/in_process_browser_test.h" | 10 #include "chrome/test/base/in_process_browser_test.h" |
| 9 #include "chrome/test/base/ui_test_utils.h" | 11 #include "chrome/test/base/ui_test_utils.h" |
| 10 #include "content/public/browser/render_frame_host.h" | 12 #include "content/public/browser/render_frame_host.h" |
| 13 #include "content/public/browser/render_process_host.h" |
| 11 #include "content/public/browser/web_contents.h" | 14 #include "content/public/browser/web_contents.h" |
| 12 #include "content/public/test/browser_test_utils.h" | 15 #include "content/public/test/browser_test_utils.h" |
| 13 #include "content/public/test/content_browser_test_utils.h" | 16 #include "content/public/test/content_browser_test_utils.h" |
| 14 #include "content/public/test/test_utils.h" | 17 #include "content/public/test/test_utils.h" |
| 18 #include "content/public/test/text_input_test_utils.h" |
| 15 #include "net/dns/mock_host_resolver.h" | 19 #include "net/dns/mock_host_resolver.h" |
| 16 #include "net/test/embedded_test_server/embedded_test_server.h" | 20 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 21 #include "ui/base/ime/text_input_client.h" |
| 22 #include "ui/base/ime/text_input_mode.h" |
| 23 #include "ui/base/ime/text_input_type.h" |
| 24 |
| 17 #include "url/gurl.h" | 25 #include "url/gurl.h" |
| 18 | 26 |
| 19 class SitePerProcessInteractiveBrowserTest : public InProcessBrowserTest { | 27 class SitePerProcessInteractiveBrowserTest : public InProcessBrowserTest { |
| 20 public: | 28 public: |
| 21 SitePerProcessInteractiveBrowserTest() {} | 29 SitePerProcessInteractiveBrowserTest() {} |
| 22 ~SitePerProcessInteractiveBrowserTest() override {} | 30 ~SitePerProcessInteractiveBrowserTest() override {} |
| 23 | 31 |
| 24 void SetUpCommandLine(base::CommandLine* command_line) override { | 32 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 25 content::IsolateAllSitesForTesting(command_line); | 33 content::IsolateAllSitesForTesting(command_line); |
| 26 } | 34 } |
| (...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 225 EXPECT_EQ("\"child2-focused-input2\"", press_tab_and_wait_for_message(true)); | 233 EXPECT_EQ("\"child2-focused-input2\"", press_tab_and_wait_for_message(true)); |
| 226 EXPECT_EQ(child2, web_contents->GetFocusedFrame()); | 234 EXPECT_EQ(child2, web_contents->GetFocusedFrame()); |
| 227 EXPECT_EQ("\"child2-focused-input1\"", press_tab_and_wait_for_message(true)); | 235 EXPECT_EQ("\"child2-focused-input1\"", press_tab_and_wait_for_message(true)); |
| 228 EXPECT_EQ("\"child1-focused-input2\"", press_tab_and_wait_for_message(true)); | 236 EXPECT_EQ("\"child1-focused-input2\"", press_tab_and_wait_for_message(true)); |
| 229 EXPECT_EQ(child1, web_contents->GetFocusedFrame()); | 237 EXPECT_EQ(child1, web_contents->GetFocusedFrame()); |
| 230 EXPECT_EQ("\"child1-focused-input1\"", press_tab_and_wait_for_message(true)); | 238 EXPECT_EQ("\"child1-focused-input1\"", press_tab_and_wait_for_message(true)); |
| 231 EXPECT_EQ("\"root-focused-input1\"", press_tab_and_wait_for_message(true)); | 239 EXPECT_EQ("\"root-focused-input1\"", press_tab_and_wait_for_message(true)); |
| 232 EXPECT_EQ(main_frame, web_contents->GetFocusedFrame()); | 240 EXPECT_EQ(main_frame, web_contents->GetFocusedFrame()); |
| 233 } | 241 } |
| 234 | 242 |
| 243 // TODO(ekaramad): After fixing crbug.com/578168 for all platforms, the |
| 244 // following tests should be activated for other platforms, e.g., Mac and |
| 245 // Android (crbug.com/602723). |
| 246 #ifdef USE_AURA |
| 247 /////////////////////////////////////////////////////////////////////////////// |
| 248 // TextInputManager and IME Tests |
| 249 // |
| 250 // The following tests verify the correctness of TextInputState tracking on the |
| 251 // browser side. They also make sure the IME logic works correctly. The baseline |
| 252 // for comparison is the default functionality in the non-OOPIF case (i.e., the |
| 253 // legacy implementation in RWHV's other than RWHVCF.). |
| 254 |
| 255 // TextInputManager Observers |
| 256 // Observing the |TextInputState.value|. |
| 257 class TextInputManagerObserverBase { |
| 258 public: |
| 259 explicit TextInputManagerObserverBase(content::WebContents* web_contents) |
| 260 : success_(false) { |
| 261 test_observer_ = |
| 262 content::TestTextInputManagerObserver::Create(web_contents); |
| 263 } |
| 264 |
| 265 virtual ~TextInputManagerObserverBase() {} |
| 266 |
| 267 void Wait() { |
| 268 if (success_) |
| 269 return; |
| 270 message_loop_runner_ = new content::MessageLoopRunner(); |
| 271 message_loop_runner_->Run(); |
| 272 } |
| 273 |
| 274 bool success() const { return success_; } |
| 275 |
| 276 protected: |
| 277 base::Closure on_success() { |
| 278 return base::Bind(&TextInputManagerObserverBase::OnSuccess, |
| 279 base::Unretained(this)); |
| 280 } |
| 281 |
| 282 content::TestTextInputManagerObserver* observer() { |
| 283 return test_observer_.get(); |
| 284 } |
| 285 |
| 286 private: |
| 287 void OnSuccess() { |
| 288 success_ = true; |
| 289 if (message_loop_runner_ && message_loop_runner_->loop_running()) |
| 290 message_loop_runner_->Quit(); |
| 291 } |
| 292 |
| 293 std::unique_ptr<content::TestTextInputManagerObserver> test_observer_; |
| 294 bool success_; |
| 295 scoped_refptr<content::MessageLoopRunner> message_loop_runner_; |
| 296 |
| 297 DISALLOW_COPY_AND_ASSIGN(TextInputManagerObserverBase); |
| 298 }; |
| 299 |
| 300 // This class observers the TextInputManager for updated |TextInputState.value|. |
| 301 class TextInputManagerValueObserver : public TextInputManagerObserverBase { |
| 302 public: |
| 303 explicit TextInputManagerValueObserver(content::WebContents* web_contents, |
| 304 const std::string& expected_value) |
| 305 : TextInputManagerObserverBase(web_contents), |
| 306 expected_value_(expected_value) { |
| 307 observer()->SetUpdateCallback(base::Bind( |
| 308 &TextInputManagerValueObserver::VerifyValue, base::Unretained(this))); |
| 309 } |
| 310 |
| 311 private: |
| 312 void VerifyValue(content::TestTextInputManagerObserver* observer) { |
| 313 std::string value; |
| 314 if (observer->GetTextInputValue(value) && expected_value_ == value) |
| 315 on_success().Run(); |
| 316 } |
| 317 |
| 318 std::string expected_value_; |
| 319 }; |
| 320 |
| 321 // This class observers the TextInputManager for updated |TextInputState.type|. |
| 322 class TextInputManagerTypeObserver : public TextInputManagerObserverBase { |
| 323 public: |
| 324 explicit TextInputManagerTypeObserver(content::WebContents* web_contents, |
| 325 ui::TextInputType expected_type) |
| 326 : TextInputManagerObserverBase(web_contents), |
| 327 expected_type_(expected_type) { |
| 328 observer()->SetUpdateCallback(base::Bind( |
| 329 &TextInputManagerTypeObserver::VerifyType, base::Unretained(this))); |
| 330 } |
| 331 |
| 332 private: |
| 333 void VerifyType(content::TestTextInputManagerObserver* observer) { |
| 334 if (expected_type_ == observer->GetTextInputType()) |
| 335 on_success().Run(); |
| 336 } |
| 337 |
| 338 ui::TextInputType expected_type_; |
| 339 }; |
| 340 |
| 341 // An observer class which observes the TextInputManager until the first time |
| 342 // the TextInputManager detects a change in TextInputState. |
| 343 class TextInputManagerChangeObserver : public TextInputManagerObserverBase { |
| 344 public: |
| 345 explicit TextInputManagerChangeObserver(content::WebContents* web_contents) |
| 346 : TextInputManagerObserverBase(web_contents) { |
| 347 observer()->SetUpdateCallback(base::Bind( |
| 348 &TextInputManagerChangeObserver::VerifyChange, base::Unretained(this))); |
| 349 } |
| 350 |
| 351 private: |
| 352 void VerifyChange(content::TestTextInputManagerObserver* observer) { |
| 353 if (observer->IsTextInputStateChanged()) |
| 354 on_success().Run(); |
| 355 } |
| 356 }; |
| 357 |
| 358 // Main class for all TextInputState and IME related tests. |
| 359 class TextInputInteractiveBrowserTest |
| 360 : public SitePerProcessInteractiveBrowserTest { |
| 361 protected: |
| 362 content::WebContents* active_contents() { |
| 363 return browser()->tab_strip_model()->GetActiveWebContents(); |
| 364 } |
| 365 |
| 366 // static |
| 367 // Adds an <input> field to a given frame by executing javascript code. |
| 368 // The input can be added as the first element or the last element of |
| 369 // |document.body|. |
| 370 static void AddInputFieldToFrame(content::RenderFrameHost* rfh, |
| 371 const std::string& type, |
| 372 const std::string& value, |
| 373 bool append_as_first_child) { |
| 374 std::string script = base::StringPrintf( |
| 375 "var input = document.createElement('input');" |
| 376 "input.setAttribute('type', '%s');" |
| 377 "input.setAttribute('value', '%s');" |
| 378 "if (%s && !!document.body.firstChild) {" |
| 379 " document.body.insertBefore(input, document.body.firstChild);" |
| 380 "} else {" |
| 381 " document.body.appendChild(input);" |
| 382 "}", |
| 383 type.c_str(), value.c_str(), append_as_first_child ? "true" : "false"); |
| 384 EXPECT_TRUE(ExecuteScript(rfh, script)); |
| 385 } |
| 386 |
| 387 // Uses 'cross_site_iframe_factory.html'. The main frame's domain is |
| 388 // 'a.com'. |
| 389 void CreateIframePage(const std::string& structure) { |
| 390 std::string path = base::StringPrintf("/cross_site_iframe_factory.html?%s", |
| 391 structure.c_str()); |
| 392 GURL main_url(embedded_test_server()->GetURL("a.com", path)); |
| 393 ui_test_utils::NavigateToURL(browser(), main_url); |
| 394 } |
| 395 |
| 396 // Recrusively uses ChildFrameAt(frame, i) to get the i-th child frame |
| 397 // inside |
| 398 // frame. |
| 399 // For example, for 'a(b(c, d(e)))', [0] returns b, and [0, 1, 0] returns e; |
| 400 content::RenderFrameHost* GetFrame(const std::vector<size_t>& indices) { |
| 401 content::RenderFrameHost* current = active_contents()->GetMainFrame(); |
| 402 for (size_t index : indices) |
| 403 current = ChildFrameAt(current, index); |
| 404 return current; |
| 405 } |
| 406 }; |
| 407 |
| 408 // The following test loads a page with multiple nested <iframe> elements which |
| 409 // are in or out of process with the main frame. Then an <input> field is added |
| 410 // to every single frame on the frame tree where the input's value is distinctly |
| 411 // selected for each frame. The test then creates a sequence of tab presses and |
| 412 // verifies that after eahc key press, the right TextInputState is observed. |
| 413 IN_PROC_BROWSER_TEST_F(TextInputInteractiveBrowserTest, |
| 414 TrackStateWhenSwitchingFocusedFrames) { |
| 415 CreateIframePage("a(a,b,c(a,b,d(e, f)),g)"); |
| 416 std::vector<std::string> values{ |
| 417 "main", "node_a", "node_b", "node_c", "node_c_a", |
| 418 "node_c_b", "node_c_d", "node_c_d_e", "node_c_d_f", "node_g"}; |
| 419 |
| 420 // TODO(ekaramad): This should not be needed and uniform initialization should |
| 421 // work. However, in some bots, this is failing. |
| 422 using vector = std::vector<size_t>; |
| 423 std::vector<content::RenderFrameHost*> frames{ |
| 424 GetFrame(vector{}), GetFrame(vector{0}), |
| 425 GetFrame(vector{1}), GetFrame(vector{2}), |
| 426 GetFrame(vector{2, 0}), GetFrame(vector{2, 1}), |
| 427 GetFrame(vector{2, 2}), GetFrame(vector{2, 2, 0}), |
| 428 GetFrame(vector{2, 2, 1}), GetFrame(vector{3})}; |
| 429 |
| 430 for (size_t i = 0; i < frames.size(); ++i) |
| 431 AddInputFieldToFrame(frames[i], "text", values[i], true); |
| 432 |
| 433 for (size_t i = 0; i < frames.size(); ++i) { |
| 434 TextInputManagerValueObserver observer(active_contents(), values[i]); |
| 435 SimulateKeyPress(active_contents(), ui::VKEY_TAB, false, false, false, |
| 436 false); |
| 437 observer.Wait(); |
| 438 } |
| 439 } |
| 440 |
| 441 // The following test loads a page with two OOPIFs. An <input> is added to |
| 442 // both frames and tab key is faked until the one in the second OOPIF is |
| 443 // focused. Then, the renderer process for both frames are crashed. The test |
| 444 // verifies that the TextInputManager stop tracking the RWHVs as well as |
| 445 // properly resetting the TextInputState after the second (active) RWHV goes |
| 446 // away. |
| 447 IN_PROC_BROWSER_TEST_F(TextInputInteractiveBrowserTest, |
| 448 StopTrackingCrashedChildFrame) { |
| 449 CreateIframePage("a(b, c)"); |
| 450 std::vector<std::string> values{"node_b", "node_c"}; |
| 451 using vector = std::vector<size_t>; |
| 452 std::vector<content::RenderFrameHost*> frames{GetFrame(vector{0}), |
| 453 GetFrame(vector{1})}; |
| 454 |
| 455 for (size_t i = 0; i < frames.size(); ++i) |
| 456 AddInputFieldToFrame(frames[i], "text", values[i], true); |
| 457 |
| 458 for (size_t i = 0; i < frames.size(); ++i) { |
| 459 TextInputManagerValueObserver observer(active_contents(), values[i]); |
| 460 SimulateKeyPress(active_contents(), ui::VKEY_TAB, false, false, false, |
| 461 false); |
| 462 observer.Wait(); |
| 463 } |
| 464 |
| 465 // Verify that we are tracking the TextInputState from the first frame. |
| 466 content::RenderWidgetHostView* first_view = frames[0]->GetView(); |
| 467 std::unordered_map<const content::RenderWidgetHostView*, ui::TextInputType> |
| 468 type_map = content::TestTextInputManagerObserver::GetTextInputTypeMap( |
| 469 active_contents()); |
| 470 EXPECT_EQ(1UL, type_map.count(first_view)); |
| 471 EXPECT_EQ(ui::TEXT_INPUT_TYPE_NONE, type_map[first_view]); |
| 472 |
| 473 // Now that the second frame's <input> is focused, we crash the first frame |
| 474 // and observe that text input state is updated for the view. |
| 475 std::unique_ptr<content::RenderWidgetHostViewDestructionObserver> |
| 476 destruction_observer = |
| 477 content::RenderWidgetHostViewDestructionObserver::Create(first_view); |
| 478 frames[0]->GetProcess()->Shutdown(0, false); |
| 479 destruction_observer->Wait(); |
| 480 |
| 481 // Verifying that the TextInputManager has smaller cleaned the memory |
| 482 // allocated to the view. |
| 483 type_map = content::TestTextInputManagerObserver::GetTextInputTypeMap( |
| 484 active_contents()); |
| 485 EXPECT_EQ(0UL, type_map.count(first_view)); |
| 486 |
| 487 // Now crash the second <iframe> which has an active view. |
| 488 content::RenderWidgetHostView* second_view = frames[1]->GetView(); |
| 489 TextInputManagerChangeObserver change_observer(active_contents()); |
| 490 frames[1]->GetProcess()->Shutdown(0, false); |
| 491 change_observer.Wait(); |
| 492 EXPECT_EQ(ui::TEXT_INPUT_TYPE_NONE, |
| 493 content::GetTextInputTypeFromWebContents(active_contents())); |
| 494 EXPECT_FALSE(!!content::GetActiveViewFromWebContents(active_contents())); |
| 495 type_map = content::TestTextInputManagerObserver::GetTextInputTypeMap( |
| 496 active_contents()); |
| 497 EXPECT_EQ(0UL, type_map.count(second_view)); |
| 498 } |
| 499 |
| 500 // The following test loads a page with two child frames; one in process and one |
| 501 // out of process with main frame. The test inserts an <input> inside each frame |
| 502 // and focuses the first frame and observes the TextInputManager setting the |
| 503 // state to ui::TEXT_INPUT_TYPE_TEXT. Then, the frame is detached and the test |
| 504 // observes that the state type is reset to ui::TEXT_INPUT_TYPE_NONE. The same |
| 505 // sequence of actions is then performed on the out of process frame. |
| 506 IN_PROC_BROWSER_TEST_F(TextInputInteractiveBrowserTest, |
| 507 ResetStateAfterFrameDetached) { |
| 508 CreateIframePage("a(a, b)"); |
| 509 using vector = std::vector<size_t>; |
| 510 std::vector<content::RenderFrameHost*> frames{GetFrame(vector{0}), |
| 511 GetFrame(vector{1})}; |
| 512 |
| 513 for (size_t i = 0; i < frames.size(); ++i) |
| 514 AddInputFieldToFrame(frames[i], "text", "", true); |
| 515 |
| 516 // Press tab key to focus the <input> in the first frame. |
| 517 TextInputManagerTypeObserver type_observer_text_a(active_contents(), |
| 518 ui::TEXT_INPUT_TYPE_TEXT); |
| 519 SimulateKeyPress(active_contents(), ui::VKEY_TAB, false, false, false, false); |
| 520 type_observer_text_a.Wait(); |
| 521 |
| 522 // Detach first frame and observe |TextInputState.type| resetting to |
| 523 // ui::TEXT_INPUT_TYPE_NONE. |
| 524 TextInputManagerTypeObserver type_observer_none_a(active_contents(), |
| 525 ui::TEXT_INPUT_TYPE_NONE); |
| 526 EXPECT_TRUE(ExecuteScript( |
| 527 frames[0], "document.body.removeChild(document.body.firstChild);")); |
| 528 type_observer_none_a.Wait(); |
| 529 |
| 530 // Press tab to focus the <input> in the second frame. |
| 531 TextInputManagerTypeObserver type_observer_text_b(active_contents(), |
| 532 ui::TEXT_INPUT_TYPE_TEXT); |
| 533 SimulateKeyPress(active_contents(), ui::VKEY_TAB, false, false, false, false); |
| 534 type_observer_text_b.Wait(); |
| 535 |
| 536 // Detach first frame and observe |TextInputState.type| resetting to |
| 537 // ui::TEXT_INPUT_TYPE_NONE. |
| 538 TextInputManagerTypeObserver type_observer_none_b(active_contents(), |
| 539 ui::TEXT_INPUT_TYPE_NONE); |
| 540 EXPECT_TRUE(ExecuteScript( |
| 541 frames[1], "document.body.removeChild(document.body.firstChild);")); |
| 542 type_observer_none_b.Wait(); |
| 543 } |
| 544 |
| 545 // This test creates a page with one OOPIF and adds an <input> to it. Then, the |
| 546 // <input> is focused and the test verfies that the |TextInputState.type| is set |
| 547 // to ui::TEXT_INPUT_TYPE_TEXT. Next, the child frame is navigated away and the |
| 548 // test verifies that |TextInputState.type| resets to ui::TEXT_INPUT_TYPE_NONE. |
| 549 IN_PROC_BROWSER_TEST_F(TextInputInteractiveBrowserTest, |
| 550 ResetStateAfterChildNavigation) { |
| 551 CreateIframePage("a(b)"); |
| 552 using vector = std::vector<size_t>; |
| 553 content::RenderFrameHost* main_frame = GetFrame(vector{}); |
| 554 content::RenderFrameHost* child_frame = GetFrame(vector{0}); |
| 555 |
| 556 AddInputFieldToFrame(child_frame, "text", "child", false); |
| 557 |
| 558 // Focus <input> in child frame and verify the |TextInputState.value|. |
| 559 TextInputManagerValueObserver child_set_state_observer(active_contents(), |
| 560 "child"); |
| 561 SimulateKeyPress(active_contents(), ui::VKEY_TAB, false, false, false, false); |
| 562 child_set_state_observer.Wait(); |
| 563 |
| 564 // Navigate the child frame to about:blank and verify that TextInputManager |
| 565 // correctly sets its |TextInputState.type| to ui::TEXT_INPUT_TYPE_NONE. |
| 566 TextInputManagerTypeObserver child_reset_state_observer( |
| 567 active_contents(), ui::TEXT_INPUT_TYPE_NONE); |
| 568 EXPECT_TRUE(ExecuteScript( |
| 569 main_frame, "document.querySelector('iframe').src = 'about:blank'")); |
| 570 child_reset_state_observer.Wait(); |
| 571 } |
| 572 |
| 573 // This test creates a blank page and adds an <input> to it. Then, the <input> |
| 574 // is focused and the test verfies that the |TextInputState.type| is set to |
| 575 // ui::TEXT_INPUT_TYPE_TEXT. Next, the browser is navigated away and the test |
| 576 // verifies that |TextInputState.type| resets to ui::TEXT_INPUT_TYPE_NONE. |
| 577 IN_PROC_BROWSER_TEST_F(TextInputInteractiveBrowserTest, |
| 578 ResetStateAfterBrowserNavigation) { |
| 579 CreateIframePage("a()"); |
| 580 content::RenderFrameHost* main_frame = GetFrame(std::vector<size_t>{}); |
| 581 AddInputFieldToFrame(main_frame, "text", "", false); |
| 582 |
| 583 TextInputManagerTypeObserver set_state_observer(active_contents(), |
| 584 ui::TEXT_INPUT_TYPE_TEXT); |
| 585 SimulateKeyPress(active_contents(), ui::VKEY_TAB, false, false, false, false); |
| 586 set_state_observer.Wait(); |
| 587 |
| 588 TextInputManagerTypeObserver reset_state_observer(active_contents(), |
| 589 ui::TEXT_INPUT_TYPE_NONE); |
| 590 ui_test_utils::NavigateToURL(browser(), GURL("about:blank")); |
| 591 reset_state_observer.Wait(); |
| 592 } |
| 593 #endif // USE_AURA |
| 594 |
| 595 // TODO(ekaramad): The following tests are specifically written for Aura and are |
| 596 // based on InputMethodObserver. Write similar tests for Mac/Android/Mus |
| 597 // (crbug.com/602723). |
| 598 #ifdef USE_AURA |
| 599 // Observes current input method for state changes. |
| 600 class InputMethodObserverBase { |
| 601 public: |
| 602 explicit InputMethodObserverBase(content::WebContents* web_contents) |
| 603 : success_(false), |
| 604 test_observer_(content::TestInputMethodObserver::Create(web_contents)) { |
| 605 } |
| 606 |
| 607 void Wait() { |
| 608 if (success_) |
| 609 return; |
| 610 message_loop_runner_ = new content::MessageLoopRunner(); |
| 611 message_loop_runner_->Run(); |
| 612 } |
| 613 |
| 614 bool success() const { return success_; } |
| 615 |
| 616 protected: |
| 617 content::TestInputMethodObserver* test_observer() { |
| 618 return test_observer_.get(); |
| 619 } |
| 620 |
| 621 const base::Closure success_closure() { |
| 622 return base::Bind(&InputMethodObserverBase::OnSuccess, |
| 623 base::Unretained(this)); |
| 624 } |
| 625 |
| 626 private: |
| 627 void OnSuccess() { |
| 628 success_ = true; |
| 629 if (message_loop_runner_ && message_loop_runner_->loop_running()) |
| 630 message_loop_runner_->Quit(); |
| 631 } |
| 632 |
| 633 bool success_; |
| 634 std::unique_ptr<content::TestInputMethodObserver> test_observer_; |
| 635 scoped_refptr<content::MessageLoopRunner> message_loop_runner_; |
| 636 |
| 637 DISALLOW_COPY_AND_ASSIGN(InputMethodObserverBase); |
| 638 }; |
| 639 |
| 640 class InputMethodObserverForShowIme : public InputMethodObserverBase { |
| 641 public: |
| 642 explicit InputMethodObserverForShowIme(content::WebContents* web_contents) |
| 643 : InputMethodObserverBase(web_contents) { |
| 644 test_observer()->SetOnShowImeIfNeededCallback(success_closure()); |
| 645 } |
| 646 }; |
| 647 |
| 648 // This test verifies that the IME for Aura is shown if and only if the current |
| 649 // client's |TextInputState.type| is not ui::TEXT_INPUT_TYPE_NONE and the flag |
| 650 // |TextInputState.show_ime_if_needed| is true. This should happen even. |
| 651 // TODO(ekaramad): This test is actually a unit test and should be moved to some |
| 652 // place more appropriate. |
| 653 IN_PROC_BROWSER_TEST_F(TextInputInteractiveBrowserTest, |
| 654 CorrectlyShowImeIfNeeded) { |
| 655 // We only need the <iframe> page to create RWHV. |
| 656 CreateIframePage("a()"); |
| 657 content::RenderFrameHost* main_frame = GetFrame(std::vector<size_t>{}); |
| 658 content::RenderWidgetHostView* view = main_frame->GetView(); |
| 659 content::WebContents* web_contents = active_contents(); |
| 660 |
| 661 content::TextInputStateSender sender(view); |
| 662 |
| 663 auto send_and_check_show_ime = [&sender, &web_contents]() { |
| 664 InputMethodObserverForShowIme observer(web_contents); |
| 665 sender.Send(); |
| 666 return observer.success(); |
| 667 }; |
| 668 |
| 669 // Sending an empty state should not trigger ime. |
| 670 EXPECT_FALSE(send_and_check_show_ime()); |
| 671 |
| 672 // Set |TextInputState.type| to text. Expect no IME. |
| 673 sender.SetType(ui::TEXT_INPUT_TYPE_TEXT); |
| 674 EXPECT_FALSE(send_and_check_show_ime()); |
| 675 |
| 676 // Set |TextInputState.show_ime_if_needed| to true. Expect IME. |
| 677 sender.SetShowImeIfNeeded(true); |
| 678 EXPECT_TRUE(send_and_check_show_ime()); |
| 679 |
| 680 // Send the same message. Expect IME (no change). |
| 681 EXPECT_TRUE(send_and_check_show_ime()); |
| 682 |
| 683 // Reset |TextInputState.show_ime_if_needed|. Expect no IME. |
| 684 sender.SetShowImeIfNeeded(false); |
| 685 EXPECT_FALSE(send_and_check_show_ime()); |
| 686 |
| 687 // Setting an irrelevant field. Expect no IME. |
| 688 sender.SetMode(ui::TEXT_INPUT_MODE_LATIN); |
| 689 EXPECT_FALSE(send_and_check_show_ime()); |
| 690 |
| 691 // Set |TextInputState.show_ime_if_needed|. Expect IME. |
| 692 sender.SetShowImeIfNeeded(true); |
| 693 EXPECT_TRUE(send_and_check_show_ime()); |
| 694 |
| 695 // Set |TextInputState.type| to ui::TEXT_INPUT_TYPE_NONE. Expect no IME. |
| 696 sender.SetType(ui::TEXT_INPUT_TYPE_NONE); |
| 697 EXPECT_FALSE(send_and_check_show_ime()); |
| 698 } |
| 699 #endif // USE_AURA |
| OLD | NEW |