Chromium Code Reviews| 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 | |
|
Charlie Reis
2016/05/18 20:46:03
Given the number of helper classes (and the expect
EhsanK
2016/05/24 20:42:44
Yes, I was thinking within the same lines. I have
Charlie Reis
2016/05/26 06:22:03
Ok, a site_per_process style test will do.
| |
| 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_) | |
| 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 observes 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()->SetUpdateCalledCallback(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 observes 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 // The following class observers |TextInputState.type| for a specific RWHV. | |
| 359 class ViewTextInputTypeObserver : public TextInputManagerObserverBase { | |
| 360 public: | |
| 361 explicit ViewTextInputTypeObserver(content::WebContents* web_contents, | |
| 362 content::RenderWidgetHostView* rwhv, | |
| 363 ui::TextInputType expected_type) | |
| 364 : TextInputManagerObserverBase(web_contents), | |
| 365 web_contents_(web_contents), | |
| 366 view_(rwhv), | |
| 367 expected_type_(expected_type) { | |
| 368 observer()->SetUpdateCalledCallback(base::Bind( | |
| 369 &ViewTextInputTypeObserver::VerifyType, base::Unretained(this))); | |
| 370 } | |
| 371 | |
| 372 private: | |
| 373 void VerifyType(content::TestTextInputManagerObserver* observer) { | |
| 374 std::unordered_map<const content::RenderWidgetHostView*, ui::TextInputType> | |
| 375 type_map = content::TestTextInputManagerObserver::GetTextInputTypeMap( | |
| 376 web_contents_); | |
| 377 if (expected_type_ == type_map[view_]) | |
| 378 on_success().Run(); | |
| 379 } | |
| 380 | |
| 381 content::WebContents* web_contents_; | |
| 382 content::RenderWidgetHostView* view_; | |
| 383 ui::TextInputType expected_type_; | |
| 384 }; | |
| 385 | |
| 386 // Main class for all TextInputState and IME related tests. | |
| 387 class TextInputInteractiveBrowserTest | |
| 388 : public SitePerProcessInteractiveBrowserTest { | |
| 389 protected: | |
| 390 content::WebContents* active_contents() { | |
| 391 return browser()->tab_strip_model()->GetActiveWebContents(); | |
| 392 } | |
| 393 | |
| 394 // static | |
| 395 // Adds an <input> field to a given frame by executing javascript code. | |
| 396 // The input can be added as the first element or the last element of | |
| 397 // |document.body|. | |
| 398 static void AddInputFieldToFrame(content::RenderFrameHost* rfh, | |
| 399 const std::string& type, | |
| 400 const std::string& value, | |
| 401 bool append_as_first_child) { | |
| 402 std::string script = base::StringPrintf( | |
| 403 "var input = document.createElement('input');" | |
| 404 "input.setAttribute('type', '%s');" | |
| 405 "input.setAttribute('value', '%s');" | |
| 406 "if (%s && !!document.body.firstChild) {" | |
| 407 " document.body.insertBefore(input, document.body.firstChild);" | |
| 408 "} else {" | |
| 409 " document.body.appendChild(input);" | |
| 410 "}", | |
| 411 type.c_str(), value.c_str(), append_as_first_child ? "true" : "false"); | |
| 412 EXPECT_TRUE(ExecuteScript(rfh, script)); | |
| 413 } | |
| 414 | |
| 415 // Uses 'cross_site_iframe_factory.html'. The main frame's domain is | |
| 416 // 'a.com'. | |
| 417 void CreateIframePage(const std::string& structure) { | |
| 418 std::string path = base::StringPrintf("/cross_site_iframe_factory.html?%s", | |
| 419 structure.c_str()); | |
| 420 GURL main_url(embedded_test_server()->GetURL("a.com", path)); | |
| 421 ui_test_utils::NavigateToURL(browser(), main_url); | |
| 422 } | |
| 423 | |
| 424 // Recrusively uses ChildFrameAt(frame, i) to get the i-th child frame | |
| 425 // inside frame. For example, for 'a(b(c, d(e)))', [0] returns b, and | |
| 426 // [0, 1, 0] returns e; | |
| 427 content::RenderFrameHost* GetFrame(const std::vector<size_t>& indices) { | |
| 428 content::RenderFrameHost* current = active_contents()->GetMainFrame(); | |
| 429 for (size_t index : indices) | |
| 430 current = ChildFrameAt(current, index); | |
| 431 return current; | |
| 432 } | |
| 433 }; | |
| 434 | |
| 435 // The following test loads a page with multiple nested <iframe> elements which | |
| 436 // are in or out of process with the main frame. Then an <input> field is added | |
| 437 // to every single frame on the frame tree where the input's value is distinctly | |
| 438 // selected for each frame. The test then creates a sequence of tab presses and | |
|
Charlie Reis
2016/05/18 20:46:03
What do you mean by distinctly selected?
EhsanK
2016/05/24 20:42:44
What I was trying to explain here was that each <i
| |
| 439 // verifies that after eahc key press, the right TextInputState is observed. | |
|
Charlie Reis
2016/05/18 20:46:03
nit: each
EhsanK
2016/05/24 20:42:44
Done.
| |
| 440 IN_PROC_BROWSER_TEST_F(TextInputInteractiveBrowserTest, | |
| 441 TrackStateWhenSwitchingFocusedFrames) { | |
| 442 CreateIframePage("a(a,b,c(a,b,d(e, f)),g)"); | |
| 443 std::vector<std::string> values{ | |
| 444 "main", "node_a", "node_b", "node_c", "node_c_a", | |
| 445 "node_c_b", "node_c_d", "node_c_d_e", "node_c_d_f", "node_g"}; | |
| 446 | |
| 447 // TODO(ekaramad): This should not be needed and uniform initialization should | |
| 448 // work. However, in some bots, this is failing. | |
| 449 using vector = std::vector<size_t>; | |
| 450 std::vector<content::RenderFrameHost*> frames{ | |
| 451 GetFrame(vector{}), GetFrame(vector{0}), | |
| 452 GetFrame(vector{1}), GetFrame(vector{2}), | |
| 453 GetFrame(vector{2, 0}), GetFrame(vector{2, 1}), | |
| 454 GetFrame(vector{2, 2}), GetFrame(vector{2, 2, 0}), | |
| 455 GetFrame(vector{2, 2, 1}), GetFrame(vector{3})}; | |
| 456 | |
| 457 for (size_t i = 0; i < frames.size(); ++i) | |
| 458 AddInputFieldToFrame(frames[i], "text", values[i], true); | |
| 459 | |
| 460 for (size_t i = 0; i < frames.size(); ++i) { | |
| 461 TextInputManagerValueObserver observer(active_contents(), values[i]); | |
| 462 SimulateKeyPress(active_contents(), ui::VKEY_TAB, false, false, false, | |
| 463 false); | |
| 464 observer.Wait(); | |
| 465 } | |
| 466 } | |
| 467 | |
| 468 // The following test loads a page with two OOPIFs. An <input> is added to | |
| 469 // both frames and tab key is faked until the one in the second OOPIF is | |
| 470 // focused. Then, the renderer process for both frames are crashed. The test | |
| 471 // verifies that the TextInputManager stop tracking the RWHVs as well as | |
| 472 // properly resetting the TextInputState after the second (active) RWHV goes | |
| 473 // away. | |
| 474 IN_PROC_BROWSER_TEST_F(TextInputInteractiveBrowserTest, | |
| 475 StopTrackingCrashedChildFrame) { | |
| 476 CreateIframePage("a(b, c)"); | |
| 477 std::vector<std::string> values{"node_b", "node_c"}; | |
| 478 using vector = std::vector<size_t>; | |
| 479 std::vector<content::RenderFrameHost*> frames{GetFrame(vector{0}), | |
| 480 GetFrame(vector{1})}; | |
| 481 | |
| 482 for (size_t i = 0; i < frames.size(); ++i) | |
| 483 AddInputFieldToFrame(frames[i], "text", values[i], true); | |
| 484 | |
| 485 // Tab into both inputs. And make sure we correctly receive their | |
| 486 // TextInputState. For the second tab two IPC's arrive one from the first | |
| 487 // frame to set the state to none, and another one from the second frame to | |
| 488 // set it to TEXT. To avoid the race between them, we shall also observe the | |
| 489 // first frame setting its state to NONE after the second tab. | |
| 490 ViewTextInputTypeObserver view_type_observer( | |
| 491 active_contents(), frames[0]->GetView(), ui::TEXT_INPUT_TYPE_NONE); | |
| 492 | |
| 493 for (size_t i = 0; i < frames.size(); ++i) { | |
| 494 TextInputManagerValueObserver observer(active_contents(), values[i]); | |
| 495 SimulateKeyPress(active_contents(), ui::VKEY_TAB, false, false, false, | |
| 496 false); | |
| 497 observer.Wait(); | |
| 498 } | |
| 499 | |
| 500 // Make sure that the first view has set its TextInputState.type to NONE. | |
| 501 view_type_observer.Wait(); | |
| 502 | |
| 503 // Verify that we are tracking the TextInputState from the first frame. | |
| 504 content::RenderWidgetHostView* first_view = frames[0]->GetView(); | |
| 505 std::unordered_map<const content::RenderWidgetHostView*, ui::TextInputType> | |
| 506 type_map = content::TestTextInputManagerObserver::GetTextInputTypeMap( | |
| 507 active_contents()); | |
| 508 EXPECT_EQ(1UL, type_map.count(first_view)); | |
| 509 EXPECT_EQ(ui::TEXT_INPUT_TYPE_NONE, type_map[first_view]); | |
| 510 | |
| 511 // Now that the second frame's <input> is focused, we crash the first frame | |
| 512 // and observe that text input state is updated for the view. | |
| 513 std::unique_ptr<content::RenderWidgetHostViewDestructionObserver> | |
| 514 destruction_observer = | |
| 515 content::RenderWidgetHostViewDestructionObserver::Create(first_view); | |
| 516 frames[0]->GetProcess()->Shutdown(0, false); | |
| 517 destruction_observer->Wait(); | |
| 518 | |
| 519 // Verifying that the TextInputManager has smaller cleaned the memory | |
| 520 // allocated to the view. | |
| 521 type_map = content::TestTextInputManagerObserver::GetTextInputTypeMap( | |
| 522 active_contents()); | |
| 523 EXPECT_EQ(0UL, type_map.count(first_view)); | |
| 524 | |
| 525 // Now crash the second <iframe> which has an active view. | |
| 526 content::RenderWidgetHostView* second_view = frames[1]->GetView(); | |
| 527 TextInputManagerChangeObserver change_observer(active_contents()); | |
| 528 frames[1]->GetProcess()->Shutdown(0, false); | |
| 529 change_observer.Wait(); | |
| 530 EXPECT_EQ(ui::TEXT_INPUT_TYPE_NONE, | |
| 531 content::GetTextInputTypeFromWebContents(active_contents())); | |
| 532 EXPECT_FALSE(!!content::GetActiveViewFromWebContents(active_contents())); | |
| 533 type_map = content::TestTextInputManagerObserver::GetTextInputTypeMap( | |
| 534 active_contents()); | |
| 535 EXPECT_EQ(0UL, type_map.count(second_view)); | |
| 536 } | |
| 537 | |
| 538 // The following test loads a page with two child frames; one in process and one | |
| 539 // out of process with main frame. The test inserts an <input> inside each frame | |
| 540 // and focuses the first frame and observes the TextInputManager setting the | |
| 541 // state to ui::TEXT_INPUT_TYPE_TEXT. Then, the frame is detached and the test | |
| 542 // observes that the state type is reset to ui::TEXT_INPUT_TYPE_NONE. The same | |
| 543 // sequence of actions is then performed on the out of process frame. | |
| 544 IN_PROC_BROWSER_TEST_F(TextInputInteractiveBrowserTest, | |
| 545 ResetStateAfterFrameDetached) { | |
| 546 CreateIframePage("a(a, b)"); | |
| 547 using vector = std::vector<size_t>; | |
| 548 std::vector<content::RenderFrameHost*> frames{GetFrame(vector{0}), | |
| 549 GetFrame(vector{1})}; | |
| 550 | |
| 551 for (size_t i = 0; i < frames.size(); ++i) | |
| 552 AddInputFieldToFrame(frames[i], "text", "", true); | |
| 553 | |
| 554 // Press tab key to focus the <input> in the first frame. | |
| 555 TextInputManagerTypeObserver type_observer_text_a(active_contents(), | |
| 556 ui::TEXT_INPUT_TYPE_TEXT); | |
| 557 SimulateKeyPress(active_contents(), ui::VKEY_TAB, false, false, false, false); | |
| 558 type_observer_text_a.Wait(); | |
| 559 | |
| 560 // Detach first frame and observe |TextInputState.type| resetting to | |
| 561 // ui::TEXT_INPUT_TYPE_NONE. | |
| 562 TextInputManagerTypeObserver type_observer_none_a(active_contents(), | |
| 563 ui::TEXT_INPUT_TYPE_NONE); | |
| 564 EXPECT_TRUE(ExecuteScript( | |
| 565 frames[0], "document.body.removeChild(document.body.firstChild);")); | |
| 566 type_observer_none_a.Wait(); | |
| 567 | |
| 568 // Press tab to focus the <input> in the second frame. | |
| 569 TextInputManagerTypeObserver type_observer_text_b(active_contents(), | |
| 570 ui::TEXT_INPUT_TYPE_TEXT); | |
| 571 SimulateKeyPress(active_contents(), ui::VKEY_TAB, false, false, false, false); | |
| 572 type_observer_text_b.Wait(); | |
| 573 | |
| 574 // Detach first frame and observe |TextInputState.type| resetting to | |
| 575 // ui::TEXT_INPUT_TYPE_NONE. | |
| 576 TextInputManagerTypeObserver type_observer_none_b(active_contents(), | |
| 577 ui::TEXT_INPUT_TYPE_NONE); | |
| 578 EXPECT_TRUE(ExecuteScript( | |
| 579 frames[1], "document.body.removeChild(document.body.firstChild);")); | |
| 580 type_observer_none_b.Wait(); | |
| 581 } | |
| 582 | |
| 583 // This test creates a page with one OOPIF and adds an <input> to it. Then, the | |
| 584 // <input> is focused and the test verfies that the |TextInputState.type| is set | |
| 585 // to ui::TEXT_INPUT_TYPE_TEXT. Next, the child frame is navigated away and the | |
| 586 // test verifies that |TextInputState.type| resets to ui::TEXT_INPUT_TYPE_NONE. | |
| 587 IN_PROC_BROWSER_TEST_F(TextInputInteractiveBrowserTest, | |
| 588 ResetStateAfterChildNavigation) { | |
| 589 CreateIframePage("a(b)"); | |
| 590 using vector = std::vector<size_t>; | |
| 591 content::RenderFrameHost* main_frame = GetFrame(vector{}); | |
| 592 content::RenderFrameHost* child_frame = GetFrame(vector{0}); | |
| 593 | |
| 594 AddInputFieldToFrame(child_frame, "text", "child", false); | |
| 595 | |
| 596 // Focus <input> in child frame and verify the |TextInputState.value|. | |
| 597 TextInputManagerValueObserver child_set_state_observer(active_contents(), | |
| 598 "child"); | |
| 599 SimulateKeyPress(active_contents(), ui::VKEY_TAB, false, false, false, false); | |
| 600 child_set_state_observer.Wait(); | |
| 601 | |
| 602 // Navigate the child frame to about:blank and verify that TextInputManager | |
| 603 // correctly sets its |TextInputState.type| to ui::TEXT_INPUT_TYPE_NONE. | |
| 604 TextInputManagerTypeObserver child_reset_state_observer( | |
| 605 active_contents(), ui::TEXT_INPUT_TYPE_NONE); | |
| 606 EXPECT_TRUE(ExecuteScript( | |
| 607 main_frame, "document.querySelector('iframe').src = 'about:blank'")); | |
| 608 child_reset_state_observer.Wait(); | |
| 609 } | |
| 610 | |
| 611 // This test creates a blank page and adds an <input> to it. Then, the <input> | |
| 612 // is focused and the test verfies that the |TextInputState.type| is set to | |
| 613 // ui::TEXT_INPUT_TYPE_TEXT. Next, the browser is navigated away and the test | |
| 614 // verifies that |TextInputState.type| resets to ui::TEXT_INPUT_TYPE_NONE. | |
| 615 IN_PROC_BROWSER_TEST_F(TextInputInteractiveBrowserTest, | |
| 616 ResetStateAfterBrowserNavigation) { | |
| 617 CreateIframePage("a()"); | |
| 618 content::RenderFrameHost* main_frame = GetFrame(std::vector<size_t>{}); | |
| 619 AddInputFieldToFrame(main_frame, "text", "", false); | |
| 620 | |
| 621 TextInputManagerTypeObserver set_state_observer(active_contents(), | |
| 622 ui::TEXT_INPUT_TYPE_TEXT); | |
| 623 SimulateKeyPress(active_contents(), ui::VKEY_TAB, false, false, false, false); | |
| 624 set_state_observer.Wait(); | |
| 625 | |
| 626 TextInputManagerTypeObserver reset_state_observer(active_contents(), | |
| 627 ui::TEXT_INPUT_TYPE_NONE); | |
| 628 ui_test_utils::NavigateToURL(browser(), GURL("about:blank")); | |
| 629 reset_state_observer.Wait(); | |
| 630 } | |
| 631 #endif // USE_AURA | |
| 632 | |
| 633 // TODO(ekaramad): The following tests are specifically written for Aura and are | |
| 634 // based on InputMethodObserver. Write similar tests for Mac/Android/Mus | |
| 635 // (crbug.com/602723). | |
| 636 #ifdef USE_AURA | |
| 637 // Observes current input method for state changes. | |
| 638 class InputMethodObserverBase { | |
| 639 public: | |
| 640 explicit InputMethodObserverBase(content::WebContents* web_contents) | |
| 641 : success_(false), | |
| 642 test_observer_(content::TestInputMethodObserver::Create(web_contents)) { | |
| 643 } | |
| 644 | |
| 645 void Wait() { | |
| 646 if (success_) | |
| 647 return; | |
| 648 message_loop_runner_ = new content::MessageLoopRunner(); | |
| 649 message_loop_runner_->Run(); | |
| 650 } | |
| 651 | |
| 652 bool success() const { return success_; } | |
| 653 | |
| 654 protected: | |
| 655 content::TestInputMethodObserver* test_observer() { | |
| 656 return test_observer_.get(); | |
| 657 } | |
| 658 | |
| 659 const base::Closure success_closure() { | |
| 660 return base::Bind(&InputMethodObserverBase::OnSuccess, | |
| 661 base::Unretained(this)); | |
| 662 } | |
| 663 | |
| 664 private: | |
| 665 void OnSuccess() { | |
| 666 success_ = true; | |
| 667 if (message_loop_runner_) | |
| 668 message_loop_runner_->Quit(); | |
| 669 } | |
| 670 | |
| 671 bool success_; | |
| 672 std::unique_ptr<content::TestInputMethodObserver> test_observer_; | |
| 673 scoped_refptr<content::MessageLoopRunner> message_loop_runner_; | |
| 674 | |
| 675 DISALLOW_COPY_AND_ASSIGN(InputMethodObserverBase); | |
| 676 }; | |
| 677 | |
| 678 class InputMethodObserverForShowIme : public InputMethodObserverBase { | |
| 679 public: | |
| 680 explicit InputMethodObserverForShowIme(content::WebContents* web_contents) | |
| 681 : InputMethodObserverBase(web_contents) { | |
| 682 test_observer()->SetOnShowImeIfNeededCallback(success_closure()); | |
| 683 } | |
| 684 }; | |
| 685 | |
| 686 // This test verifies that the IME for Aura is shown if and only if the current | |
| 687 // client's |TextInputState.type| is not ui::TEXT_INPUT_TYPE_NONE and the flag | |
| 688 // |TextInputState.show_ime_if_needed| is true. This should happen even. | |
| 689 // TODO(ekaramad): This test is actually a unit test and should be moved to some | |
| 690 // place more appropriate. | |
| 691 IN_PROC_BROWSER_TEST_F(TextInputInteractiveBrowserTest, | |
| 692 CorrectlyShowImeIfNeeded) { | |
| 693 // We only need the <iframe> page to create RWHV. | |
| 694 CreateIframePage("a()"); | |
| 695 content::RenderFrameHost* main_frame = GetFrame(std::vector<size_t>{}); | |
| 696 content::RenderWidgetHostView* view = main_frame->GetView(); | |
| 697 content::WebContents* web_contents = active_contents(); | |
| 698 | |
| 699 content::TextInputStateSender sender(view); | |
| 700 | |
| 701 auto send_and_check_show_ime = [&sender, &web_contents]() { | |
| 702 InputMethodObserverForShowIme observer(web_contents); | |
| 703 sender.Send(); | |
| 704 return observer.success(); | |
| 705 }; | |
| 706 | |
| 707 // Sending an empty state should not trigger ime. | |
| 708 EXPECT_FALSE(send_and_check_show_ime()); | |
| 709 | |
| 710 // Set |TextInputState.type| to text. Expect no IME. | |
| 711 sender.SetType(ui::TEXT_INPUT_TYPE_TEXT); | |
| 712 EXPECT_FALSE(send_and_check_show_ime()); | |
| 713 | |
| 714 // Set |TextInputState.show_ime_if_needed| to true. Expect IME. | |
| 715 sender.SetShowImeIfNeeded(true); | |
| 716 EXPECT_TRUE(send_and_check_show_ime()); | |
| 717 | |
| 718 // Send the same message. Expect IME (no change). | |
| 719 EXPECT_TRUE(send_and_check_show_ime()); | |
| 720 | |
| 721 // Reset |TextInputState.show_ime_if_needed|. Expect no IME. | |
| 722 sender.SetShowImeIfNeeded(false); | |
| 723 EXPECT_FALSE(send_and_check_show_ime()); | |
| 724 | |
| 725 // Setting an irrelevant field. Expect no IME. | |
| 726 sender.SetMode(ui::TEXT_INPUT_MODE_LATIN); | |
| 727 EXPECT_FALSE(send_and_check_show_ime()); | |
| 728 | |
| 729 // Set |TextInputState.show_ime_if_needed|. Expect IME. | |
| 730 sender.SetShowImeIfNeeded(true); | |
| 731 EXPECT_TRUE(send_and_check_show_ime()); | |
| 732 | |
| 733 // Set |TextInputState.type| to ui::TEXT_INPUT_TYPE_NONE. Expect no IME. | |
| 734 sender.SetType(ui::TEXT_INPUT_TYPE_NONE); | |
| 735 EXPECT_FALSE(send_and_check_show_ime()); | |
| 736 } | |
| 737 #endif // USE_AURA | |
| OLD | NEW |