Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "ash/magnifier/magnification_controller.h" | 5 #include "ash/magnifier/magnification_controller.h" |
| 6 | 6 |
| 7 #include "ash/display/display_manager.h" | 7 #include "ash/display/display_manager.h" |
| 8 #include "ash/shell.h" | 8 #include "ash/shell.h" |
| 9 #include "ash/test/ash_test_base.h" | 9 #include "ash/test/ash_test_base.h" |
| 10 #include "ash/test/display_manager_test_api.h" | 10 #include "ash/test/display_manager_test_api.h" |
| 11 #include "base/run_loop.h" | |
| 11 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
| 12 #include "ui/aura/client/aura_constants.h" | 13 #include "ui/aura/client/aura_constants.h" |
| 13 #include "ui/aura/env.h" | 14 #include "ui/aura/env.h" |
| 14 #include "ui/aura/test/aura_test_utils.h" | 15 #include "ui/aura/test/aura_test_utils.h" |
| 15 #include "ui/aura/window_tree_host.h" | 16 #include "ui/aura/window_tree_host.h" |
| 16 #include "ui/base/ime/input_method.h" | 17 #include "ui/base/ime/input_method.h" |
| 17 #include "ui/chromeos/accessibility_types.h" | 18 #include "ui/chromeos/accessibility_types.h" |
| 18 #include "ui/events/test/event_generator.h" | 19 #include "ui/events/test/event_generator.h" |
| 19 #include "ui/gfx/geometry/rect_conversions.h" | 20 #include "ui/gfx/geometry/rect_conversions.h" |
| 20 #include "ui/gfx/screen.h" | 21 #include "ui/gfx/screen.h" |
| (...skipping 498 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 519 } | 520 } |
| 520 | 521 |
| 521 TEST_F(MagnificationControllerTest, FollowTextInputFieldFocus) { | 522 TEST_F(MagnificationControllerTest, FollowTextInputFieldFocus) { |
| 522 CreateAndShowTextInputView(gfx::Rect(500, 300, 80, 80)); | 523 CreateAndShowTextInputView(gfx::Rect(500, 300, 80, 80)); |
| 523 gfx::Rect text_input_bounds = GetTextInputViewBounds(); | 524 gfx::Rect text_input_bounds = GetTextInputViewBounds(); |
| 524 | 525 |
| 525 // Enables magnifier and confirm the viewport is at center. | 526 // Enables magnifier and confirm the viewport is at center. |
| 526 GetMagnificationController()->SetEnabled(true); | 527 GetMagnificationController()->SetEnabled(true); |
| 527 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale()); | 528 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale()); |
| 528 EXPECT_EQ("200,150 400x300", GetViewport().ToString()); | 529 EXPECT_EQ("200,150 400x300", GetViewport().ToString()); |
| 530 EXPECT_FALSE(GetMagnificationController()->KeepFocusCentered()); | |
| 529 | 531 |
| 530 // Move the viewport to (0, 0), so that text input field will be out of | 532 // Move the viewport to (0, 0), so that text input field will be out of |
| 531 // the viewport region. | 533 // the viewport region. |
| 532 GetMagnificationController()->MoveWindow(0, 0, false); | 534 GetMagnificationController()->MoveWindow(0, 0, false); |
| 533 EXPECT_EQ("0,0 400x300", GetViewport().ToString()); | 535 EXPECT_EQ("0,0 400x300", GetViewport().ToString()); |
| 534 EXPECT_FALSE(GetViewport().Intersects(text_input_bounds)); | 536 EXPECT_FALSE(GetViewport().Intersects(text_input_bounds)); |
| 535 | 537 |
| 536 // Focus on the text input field. | 538 // Focus on the text input field. |
| 537 FocusOnTextInputView(); | 539 FocusOnTextInputView(); |
| 538 | 540 |
| 539 // Verify the view port has been moved to the place where the text field is | 541 // Verify the view port has been moved to the place where the text field is |
| 540 // contained in the view port and the caret is at the center of the view port. | 542 // contained in the view port and the caret is at the center of the view port. |
| 541 gfx::Rect view_port = GetViewport(); | 543 gfx::Rect view_port = GetViewport(); |
| 542 EXPECT_TRUE(view_port.Contains(text_input_bounds)); | 544 EXPECT_TRUE(view_port.Contains(text_input_bounds)); |
| 543 gfx::Rect caret_bounds = GetCaretBounds(); | 545 gfx::Rect caret_bounds = GetCaretBounds(); |
| 544 EXPECT_TRUE(text_input_bounds.Contains(caret_bounds)); | 546 EXPECT_TRUE(text_input_bounds.Contains(caret_bounds)); |
| 545 EXPECT_EQ(caret_bounds.origin(), view_port.CenterPoint()); | 547 EXPECT_EQ(caret_bounds.CenterPoint(), view_port.CenterPoint()); |
| 546 } | 548 } |
| 547 | 549 |
| 550 // Tests the following case. First the text input field intersects on the right | |
| 551 // edge with the view port, with focus caret sitting just a little left to the | |
| 552 // caret panning margin, so that when it gets focus, the view port won't move. | |
| 553 // Then when user types a character, the caret moves beyond the right panning | |
| 554 // edge, the view port will be moved to center the caret horizontally. | |
| 548 TEST_F(MagnificationControllerTest, FollowTextInputFieldKeyPress) { | 555 TEST_F(MagnificationControllerTest, FollowTextInputFieldKeyPress) { |
| 549 CreateAndShowTextInputView(gfx::Rect(385, 200, 80, 80)); | 556 const int kCaretPanningMargin = 50; |
| 557 const int kScale = 2.0f; | |
| 558 const int kViewportWidth = 400; | |
| 559 // Add some extra distance horizontally from text caret to to left edge of | |
| 560 // the text input view. | |
| 561 int x = kViewportWidth - (kCaretPanningMargin + 20) / kScale; | |
| 562 CreateAndShowTextInputView(gfx::Rect(x, 200, 80, 80)); | |
| 550 gfx::Rect text_input_bounds = GetTextInputViewBounds(); | 563 gfx::Rect text_input_bounds = GetTextInputViewBounds(); |
| 551 | 564 |
| 552 // Enables magnifier and confirm the viewport is at center. | 565 // Enables magnifier and confirm the viewport is at center. |
| 553 GetMagnificationController()->SetEnabled(true); | 566 GetMagnificationController()->SetEnabled(true); |
| 554 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale()); | 567 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale()); |
| 555 EXPECT_EQ("200,150 400x300", GetViewport().ToString()); | 568 EXPECT_EQ("200,150 400x300", GetViewport().ToString()); |
| 569 EXPECT_FALSE(GetMagnificationController()->KeepFocusCentered()); | |
| 556 | 570 |
| 557 // Move the viewport to (0, 0), so that text input field intersects the | 571 // Move the viewport to (0, 0), so that text input field intersects the |
| 558 // view port at the right edge. | 572 // view port at the right edge. |
| 559 GetMagnificationController()->MoveWindow(0, 0, false); | 573 GetMagnificationController()->MoveWindow(0, 0, false); |
| 560 EXPECT_EQ("0,0 400x300", GetViewport().ToString()); | 574 EXPECT_EQ("0,0 400x300", GetViewport().ToString()); |
| 561 EXPECT_TRUE(GetViewport().Intersects(text_input_bounds)); | 575 EXPECT_TRUE(GetViewport().Intersects(text_input_bounds)); |
| 562 | 576 |
| 563 // Focus on the text input field. | 577 // Focus on the text input field. |
| 564 FocusOnTextInputView(); | 578 FocusOnTextInputView(); |
| 565 | 579 |
| 566 // Verify the view port is not moved, and the caret is inside the view port. | 580 // Verify the view port is not moved, and the caret is inside the view port |
| 581 // and not beyond the caret right panning margin. | |
| 567 gfx::Rect view_port = GetViewport(); | 582 gfx::Rect view_port = GetViewport(); |
| 568 EXPECT_EQ("0,0 400x300", view_port.ToString()); | 583 EXPECT_EQ("0,0 400x300", view_port.ToString()); |
| 569 EXPECT_TRUE(view_port.Intersects(text_input_bounds)); | |
| 570 EXPECT_TRUE(text_input_bounds.Contains(GetCaretBounds())); | 584 EXPECT_TRUE(text_input_bounds.Contains(GetCaretBounds())); |
| 585 EXPECT_GT(view_port.right() - kCaretPanningMargin / kScale, | |
| 586 GetCaretBounds().x()); | |
| 571 | 587 |
| 572 // Press keys on text input simulate typing on text field and the caret | 588 // Press keys on text input simulate typing on text field and the caret |
| 573 // moves out of the old view port region. The view port is moved to the place | 589 // moves beyond the caret right panning margin. The view port is moved to the |
| 574 // where caret's x coordinate is centered at the new view port. | 590 // place where caret's x coordinate is centered at the new view port. |
| 575 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow()); | 591 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow()); |
| 576 generator.PressKey(ui::VKEY_A, 0); | 592 generator.PressKey(ui::VKEY_A, 0); |
| 577 generator.ReleaseKey(ui::VKEY_A, 0); | 593 generator.ReleaseKey(ui::VKEY_A, 0); |
| 578 gfx::Rect caret_bounds = GetCaretBounds(); | 594 gfx::Rect caret_bounds = GetCaretBounds(); |
| 579 EXPECT_FALSE(view_port.Intersects(caret_bounds)); | 595 EXPECT_LT(view_port.right() - kCaretPanningMargin / kScale, |
| 596 GetCaretBounds().x()); | |
| 580 | 597 |
| 581 gfx::Rect new_view_port = GetViewport(); | 598 gfx::Rect new_view_port = GetViewport(); |
| 582 EXPECT_TRUE(new_view_port.Contains(caret_bounds)); | 599 EXPECT_EQ(caret_bounds.CenterPoint().x(), new_view_port.CenterPoint().x()); |
| 583 EXPECT_EQ(caret_bounds.x(), new_view_port.CenterPoint().x()); | |
| 584 EXPECT_EQ(view_port.y(), new_view_port.y()); | |
| 585 } | 600 } |
| 586 | 601 |
| 602 TEST_F(MagnificationControllerTest, CenterTextCaretNotInsideViewport) { | |
| 603 CreateAndShowTextInputView(gfx::Rect(500, 300, 50, 30)); | |
| 604 gfx::Rect text_input_bounds = GetTextInputViewBounds(); | |
| 605 | |
| 606 // Enables magnifier and confirm the viewport is at center. | |
| 607 GetMagnificationController()->SetKeepFocusCentered(true); | |
| 608 GetMagnificationController()->SetEnabled(true); | |
| 609 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale()); | |
| 610 EXPECT_EQ("200,150 400x300", GetViewport().ToString()); | |
| 611 EXPECT_TRUE(GetMagnificationController()->KeepFocusCentered()); | |
| 612 | |
| 613 // Move the viewport to (0, 0), so that text input field will be out of | |
| 614 // the viewport region. | |
| 615 GetMagnificationController()->MoveWindow(0, 0, false); | |
| 616 EXPECT_EQ("0,0 400x300", GetViewport().ToString()); | |
| 617 EXPECT_FALSE(GetViewport().Contains(text_input_bounds)); | |
| 618 | |
| 619 // Focus on the text input field. | |
| 620 FocusOnTextInputView(); | |
| 621 base::RunLoop().RunUntilIdle(); | |
|
oshima
2015/05/29 22:34:26
nit: you can just call RunAllPendingInMessageLoop(
jennyz
2015/05/30 00:12:58
Done.
| |
| 622 // Verify the view port has been moved to the place where the text field is | |
| 623 // contained in the view port and the caret is at the center of the view port. | |
| 624 gfx::Rect view_port = GetViewport(); | |
| 625 EXPECT_TRUE(view_port.Contains(text_input_bounds)); | |
| 626 gfx::Rect caret_bounds = GetCaretBounds(); | |
| 627 EXPECT_EQ(caret_bounds.CenterPoint(), view_port.CenterPoint()); | |
| 628 | |
| 629 // Press keys on text input simulate typing on text field and the view port | |
| 630 // should be moved to keep the caret centered. | |
| 631 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow()); | |
| 632 generator.PressKey(ui::VKEY_A, 0); | |
| 633 generator.ReleaseKey(ui::VKEY_A, 0); | |
| 634 base::RunLoop().RunUntilIdle(); | |
| 635 gfx::Rect new_caret_bounds = GetCaretBounds(); | |
| 636 EXPECT_NE(caret_bounds, new_caret_bounds); | |
| 637 | |
| 638 gfx::Rect new_view_port = GetViewport(); | |
| 639 EXPECT_NE(view_port, new_view_port); | |
| 640 EXPECT_TRUE(new_view_port.Contains(new_caret_bounds)); | |
| 641 EXPECT_EQ(new_caret_bounds.CenterPoint(), new_view_port.CenterPoint()); | |
| 642 } | |
| 643 | |
| 644 TEST_F(MagnificationControllerTest, CenterTextCaretInViewport) { | |
| 645 CreateAndShowTextInputView(gfx::Rect(250, 200, 50, 30)); | |
| 646 gfx::Rect text_input_bounds = GetTextInputViewBounds(); | |
| 647 | |
| 648 // Enables magnifier and confirm the viewport is at center. | |
| 649 GetMagnificationController()->SetKeepFocusCentered(true); | |
| 650 GetMagnificationController()->SetEnabled(true); | |
| 651 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale()); | |
| 652 EXPECT_EQ("200,150 400x300", GetViewport().ToString()); | |
| 653 EXPECT_TRUE(GetMagnificationController()->KeepFocusCentered()); | |
| 654 | |
| 655 // Verify the text input field is inside the view port. | |
| 656 gfx::Rect view_port = GetViewport(); | |
| 657 EXPECT_TRUE(view_port.Contains(text_input_bounds)); | |
| 658 | |
| 659 // Focus on the text input field. | |
| 660 FocusOnTextInputView(); | |
| 661 base::RunLoop().RunUntilIdle(); | |
| 662 | |
| 663 // Verify the view port has been moved to the place where the text field is | |
| 664 // contained in the view port and the caret is at the center of the view port. | |
| 665 gfx::Rect new_view_port = GetViewport(); | |
| 666 EXPECT_NE(view_port, new_view_port); | |
| 667 EXPECT_TRUE(new_view_port.Contains(text_input_bounds)); | |
| 668 gfx::Rect caret_bounds = GetCaretBounds(); | |
| 669 EXPECT_EQ(caret_bounds.CenterPoint(), new_view_port.CenterPoint()); | |
| 670 } | |
| 671 | |
| 672 | |
| 587 // Make sure that unified desktop can enter magnified mode. | 673 // Make sure that unified desktop can enter magnified mode. |
| 588 TEST_F(MagnificationControllerTest, EnableMagnifierInUnifiedDesktop) { | 674 TEST_F(MagnificationControllerTest, EnableMagnifierInUnifiedDesktop) { |
| 589 if (!SupportsMultipleDisplays()) | 675 if (!SupportsMultipleDisplays()) |
| 590 return; | 676 return; |
| 591 test::DisplayManagerTestApi::EnableUnifiedDesktopForTest(); | 677 test::DisplayManagerTestApi::EnableUnifiedDesktopForTest(); |
| 592 | 678 |
| 593 DisplayManager* display_manager = Shell::GetInstance()->display_manager(); | 679 DisplayManager* display_manager = Shell::GetInstance()->display_manager(); |
| 594 display_manager->SetDefaultMultiDisplayMode(DisplayManager::UNIFIED); | 680 display_manager->SetDefaultMultiDisplayMode(DisplayManager::UNIFIED); |
| 595 display_manager->SetMultiDisplayMode(DisplayManager::UNIFIED); | 681 display_manager->SetMultiDisplayMode(DisplayManager::UNIFIED); |
| 596 | 682 |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 615 UpdateDisplay("500x500"); | 701 UpdateDisplay("500x500"); |
| 616 EXPECT_EQ("0,0 500x500", screen->GetPrimaryDisplay().bounds().ToString()); | 702 EXPECT_EQ("0,0 500x500", screen->GetPrimaryDisplay().bounds().ToString()); |
| 617 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale()); | 703 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale()); |
| 618 | 704 |
| 619 GetMagnificationController()->SetEnabled(false); | 705 GetMagnificationController()->SetEnabled(false); |
| 620 EXPECT_EQ("0,0 500x500", screen->GetPrimaryDisplay().bounds().ToString()); | 706 EXPECT_EQ("0,0 500x500", screen->GetPrimaryDisplay().bounds().ToString()); |
| 621 EXPECT_EQ(1.0f, GetMagnificationController()->GetScale()); | 707 EXPECT_EQ(1.0f, GetMagnificationController()->GetScale()); |
| 622 } | 708 } |
| 623 | 709 |
| 624 } // namespace ash | 710 } // namespace ash |
| OLD | NEW |