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