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/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
12 #include "content/public/test/test_utils.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" |
21 #include "ui/views/controls/textfield/textfield.h" | 22 #include "ui/views/controls/textfield/textfield.h" |
(...skipping 29 matching lines...) Expand all Loading... | |
51 views::View* GetContentsView() override { return this; } | 52 views::View* GetContentsView() override { return this; } |
52 | 53 |
53 void FocusOnTextInput() { GetFocusManager()->SetFocusedView(text_field_); } | 54 void FocusOnTextInput() { GetFocusManager()->SetFocusedView(text_field_); } |
54 | 55 |
55 private: | 56 private: |
56 views::Textfield* text_field_; // owned by views hierarchy | 57 views::Textfield* text_field_; // owned by views hierarchy |
57 | 58 |
58 DISALLOW_COPY_AND_ASSIGN(TextInputView); | 59 DISALLOW_COPY_AND_ASSIGN(TextInputView); |
59 }; | 60 }; |
60 | 61 |
62 class MagnifierAnimationWaiter { | |
63 public: | |
64 explicit MagnifierAnimationWaiter(ash::MagnificationController* controller) | |
65 : controller_(controller) {} | |
66 | |
67 void Wait() { | |
68 check_timer_.Stop(); | |
69 check_timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(10), this, | |
70 &MagnifierAnimationWaiter::OnTimer); | |
71 runner_ = new content::MessageLoopRunner; | |
72 runner_->Run(); | |
73 base::RunLoop().RunUntilIdle(); | |
74 } | |
oshima
2015/05/28 22:41:35
Can you comment why this is necessary?
jennyz
2015/05/29 21:41:24
Removed this class.
| |
75 | |
76 private: | |
77 void OnTimer() { | |
78 DCHECK(runner_.get()); | |
79 if (!controller_->IsOnAnimationForTesting()) { | |
oshima
2015/05/28 22:41:35
Animation is turned off in unittest by default. (h
jennyz
2015/05/29 21:41:24
Good catch. I am not aware the animation is disabl
| |
80 runner_->Quit(); | |
81 } | |
oshima
2015/05/28 22:41:35
nit: nuke{}
jennyz
2015/05/29 21:41:24
Acknowledged.
| |
82 } | |
83 | |
84 ash::MagnificationController* controller_; // not owned | |
85 base::RepeatingTimer<MagnifierAnimationWaiter> check_timer_; | |
86 scoped_refptr<content::MessageLoopRunner> runner_; | |
87 | |
88 DISALLOW_COPY_AND_ASSIGN(MagnifierAnimationWaiter); | |
89 }; | |
90 | |
61 } // namespace | 91 } // namespace |
62 | 92 |
63 class MagnificationControllerTest: public test::AshTestBase { | 93 class MagnificationControllerTest: public test::AshTestBase { |
64 public: | 94 public: |
65 MagnificationControllerTest() : text_input_view_(NULL) {} | 95 MagnificationControllerTest() : text_input_view_(NULL) {} |
66 ~MagnificationControllerTest() override {} | 96 ~MagnificationControllerTest() override {} |
67 | 97 |
68 void SetUp() override { | 98 void SetUp() override { |
69 AshTestBase::SetUp(); | 99 AshTestBase::SetUp(); |
70 UpdateDisplay(base::StringPrintf("%dx%d", kRootWidth, kRootHeight)); | 100 UpdateDisplay(base::StringPrintf("%dx%d", kRootWidth, kRootHeight)); |
(...skipping 448 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
519 } | 549 } |
520 | 550 |
521 TEST_F(MagnificationControllerTest, FollowTextInputFieldFocus) { | 551 TEST_F(MagnificationControllerTest, FollowTextInputFieldFocus) { |
522 CreateAndShowTextInputView(gfx::Rect(500, 300, 80, 80)); | 552 CreateAndShowTextInputView(gfx::Rect(500, 300, 80, 80)); |
523 gfx::Rect text_input_bounds = GetTextInputViewBounds(); | 553 gfx::Rect text_input_bounds = GetTextInputViewBounds(); |
524 | 554 |
525 // Enables magnifier and confirm the viewport is at center. | 555 // Enables magnifier and confirm the viewport is at center. |
526 GetMagnificationController()->SetEnabled(true); | 556 GetMagnificationController()->SetEnabled(true); |
527 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale()); | 557 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale()); |
528 EXPECT_EQ("200,150 400x300", GetViewport().ToString()); | 558 EXPECT_EQ("200,150 400x300", GetViewport().ToString()); |
559 EXPECT_FALSE(GetMagnificationController()->KeepFocusCentered()); | |
529 | 560 |
530 // Move the viewport to (0, 0), so that text input field will be out of | 561 // Move the viewport to (0, 0), so that text input field will be out of |
531 // the viewport region. | 562 // the viewport region. |
532 GetMagnificationController()->MoveWindow(0, 0, false); | 563 GetMagnificationController()->MoveWindow(0, 0, false); |
533 EXPECT_EQ("0,0 400x300", GetViewport().ToString()); | 564 EXPECT_EQ("0,0 400x300", GetViewport().ToString()); |
534 EXPECT_FALSE(GetViewport().Intersects(text_input_bounds)); | 565 EXPECT_FALSE(GetViewport().Intersects(text_input_bounds)); |
535 | 566 |
536 // Focus on the text input field. | 567 // Focus on the text input field. |
537 FocusOnTextInputView(); | 568 FocusOnTextInputView(); |
538 | 569 |
539 // Verify the view port has been moved to the place where the text field is | 570 // 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. | 571 // contained in the view port and the caret is at the center of the view port. |
541 gfx::Rect view_port = GetViewport(); | 572 gfx::Rect view_port = GetViewport(); |
542 EXPECT_TRUE(view_port.Contains(text_input_bounds)); | 573 EXPECT_TRUE(view_port.Contains(text_input_bounds)); |
543 gfx::Rect caret_bounds = GetCaretBounds(); | 574 gfx::Rect caret_bounds = GetCaretBounds(); |
544 EXPECT_TRUE(text_input_bounds.Contains(caret_bounds)); | 575 EXPECT_TRUE(text_input_bounds.Contains(caret_bounds)); |
545 EXPECT_EQ(caret_bounds.origin(), view_port.CenterPoint()); | 576 EXPECT_EQ(caret_bounds.CenterPoint(), view_port.CenterPoint()); |
546 } | 577 } |
547 | 578 |
579 // Tests the following case. First the text input field intersects on the right | |
580 // edge with the view port, with focus caret sitting just a little left to the | |
581 // caret panning margin, so that when it gets focus, the view port won't move. | |
582 // Then when user types a character, the caret moves beyond the right panning | |
583 // edge, the view port will be moved to center the caret horizontally. | |
548 TEST_F(MagnificationControllerTest, FollowTextInputFieldKeyPress) { | 584 TEST_F(MagnificationControllerTest, FollowTextInputFieldKeyPress) { |
549 CreateAndShowTextInputView(gfx::Rect(385, 200, 80, 80)); | 585 const int kCaretPanningMargin = 50; |
586 const int kScale = 2.0f; | |
587 const int kViewportWidth = 400; | |
588 // Add some extra distance horizontally from text caret to to left edge of | |
589 // the text input view. | |
590 int x = kViewportWidth - (kCaretPanningMargin + 20) / kScale; | |
591 CreateAndShowTextInputView(gfx::Rect(x, 200, 80, 80)); | |
550 gfx::Rect text_input_bounds = GetTextInputViewBounds(); | 592 gfx::Rect text_input_bounds = GetTextInputViewBounds(); |
551 | 593 |
552 // Enables magnifier and confirm the viewport is at center. | 594 // Enables magnifier and confirm the viewport is at center. |
553 GetMagnificationController()->SetEnabled(true); | 595 GetMagnificationController()->SetEnabled(true); |
554 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale()); | 596 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale()); |
555 EXPECT_EQ("200,150 400x300", GetViewport().ToString()); | 597 EXPECT_EQ("200,150 400x300", GetViewport().ToString()); |
598 EXPECT_FALSE(GetMagnificationController()->KeepFocusCentered()); | |
556 | 599 |
557 // Move the viewport to (0, 0), so that text input field intersects the | 600 // Move the viewport to (0, 0), so that text input field intersects the |
558 // view port at the right edge. | 601 // view port at the right edge. |
559 GetMagnificationController()->MoveWindow(0, 0, false); | 602 GetMagnificationController()->MoveWindow(0, 0, false); |
560 EXPECT_EQ("0,0 400x300", GetViewport().ToString()); | 603 EXPECT_EQ("0,0 400x300", GetViewport().ToString()); |
561 EXPECT_TRUE(GetViewport().Intersects(text_input_bounds)); | 604 EXPECT_TRUE(GetViewport().Intersects(text_input_bounds)); |
562 | 605 |
563 // Focus on the text input field. | 606 // Focus on the text input field. |
564 FocusOnTextInputView(); | 607 FocusOnTextInputView(); |
565 | 608 |
566 // Verify the view port is not moved, and the caret is inside the view port. | 609 // Verify the view port is not moved, and the caret is inside the view port |
610 // and not beyond the caret right panning margin. | |
567 gfx::Rect view_port = GetViewport(); | 611 gfx::Rect view_port = GetViewport(); |
568 EXPECT_EQ("0,0 400x300", view_port.ToString()); | 612 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())); | 613 EXPECT_TRUE(text_input_bounds.Contains(GetCaretBounds())); |
614 EXPECT_GT(view_port.right() - kCaretPanningMargin / kScale, | |
615 GetCaretBounds().x()); | |
571 | 616 |
572 // Press keys on text input simulate typing on text field and the caret | 617 // 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 | 618 // 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. | 619 // place where caret's x coordinate is centered at the new view port. |
575 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow()); | 620 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow()); |
576 generator.PressKey(ui::VKEY_A, 0); | 621 generator.PressKey(ui::VKEY_A, 0); |
577 generator.ReleaseKey(ui::VKEY_A, 0); | 622 generator.ReleaseKey(ui::VKEY_A, 0); |
578 gfx::Rect caret_bounds = GetCaretBounds(); | 623 gfx::Rect caret_bounds = GetCaretBounds(); |
579 EXPECT_FALSE(view_port.Intersects(caret_bounds)); | 624 EXPECT_LT(view_port.right() - kCaretPanningMargin / kScale, |
625 GetCaretBounds().x()); | |
580 | 626 |
581 gfx::Rect new_view_port = GetViewport(); | 627 gfx::Rect new_view_port = GetViewport(); |
582 EXPECT_TRUE(new_view_port.Contains(caret_bounds)); | 628 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 } | 629 } |
586 | 630 |
631 TEST_F(MagnificationControllerTest, CenterTextCaretNotInsideViewport) { | |
632 CreateAndShowTextInputView(gfx::Rect(500, 300, 50, 30)); | |
633 gfx::Rect text_input_bounds = GetTextInputViewBounds(); | |
634 | |
635 // Enables magnifier and confirm the viewport is at center. | |
636 GetMagnificationController()->SetKeepFocusCentered(true); | |
637 GetMagnificationController()->SetEnabled(true); | |
638 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale()); | |
639 EXPECT_EQ("200,150 400x300", GetViewport().ToString()); | |
640 EXPECT_TRUE(GetMagnificationController()->KeepFocusCentered()); | |
641 | |
642 // Move the viewport to (0, 0), so that text input field will be out of | |
643 // the viewport region. | |
644 GetMagnificationController()->MoveWindow(0, 0, false); | |
645 EXPECT_EQ("0,0 400x300", GetViewport().ToString()); | |
646 EXPECT_FALSE(GetViewport().Contains(text_input_bounds)); | |
647 | |
648 // Focus on the text input field. | |
649 FocusOnTextInputView(); | |
650 MagnifierAnimationWaiter waiter(GetMagnificationController()); | |
651 waiter.Wait(); | |
652 | |
653 // Verify the view port has been moved to the place where the text field is | |
654 // contained in the view port and the caret is at the center of the view port. | |
655 gfx::Rect view_port = GetViewport(); | |
656 EXPECT_TRUE(view_port.Contains(text_input_bounds)); | |
657 gfx::Rect caret_bounds = GetCaretBounds(); | |
658 EXPECT_EQ(caret_bounds.CenterPoint(), view_port.CenterPoint()); | |
659 | |
660 // Press keys on text input simulate typing on text field and the view port | |
661 // should be moved to keep the caret centered. | |
662 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow()); | |
663 generator.PressKey(ui::VKEY_A, 0); | |
664 generator.ReleaseKey(ui::VKEY_A, 0); | |
665 gfx::Rect new_caret_bounds = GetCaretBounds(); | |
666 EXPECT_NE(caret_bounds, new_caret_bounds); | |
667 waiter.Wait(); | |
668 | |
669 gfx::Rect new_view_port = GetViewport(); | |
670 EXPECT_NE(view_port, new_view_port); | |
671 EXPECT_TRUE(new_view_port.Contains(new_caret_bounds)); | |
672 EXPECT_EQ(new_caret_bounds.CenterPoint(), new_view_port.CenterPoint()); | |
673 } | |
674 | |
675 TEST_F(MagnificationControllerTest, CenterTextCaretInViewport) { | |
676 CreateAndShowTextInputView(gfx::Rect(250, 200, 50, 30)); | |
677 gfx::Rect text_input_bounds = GetTextInputViewBounds(); | |
678 | |
679 // Enables magnifier and confirm the viewport is at center. | |
680 GetMagnificationController()->SetKeepFocusCentered(true); | |
681 GetMagnificationController()->SetEnabled(true); | |
682 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale()); | |
683 EXPECT_EQ("200,150 400x300", GetViewport().ToString()); | |
684 EXPECT_TRUE(GetMagnificationController()->KeepFocusCentered()); | |
685 | |
686 // Verify the text input field is inside the view port. | |
687 gfx::Rect view_port = GetViewport(); | |
688 EXPECT_TRUE(view_port.Contains(text_input_bounds)); | |
689 | |
690 // Focus on the text input field. | |
691 FocusOnTextInputView(); | |
692 MagnifierAnimationWaiter waiter(GetMagnificationController()); | |
693 waiter.Wait(); | |
694 | |
695 // Verify the view port has been moved to the place where the text field is | |
696 // contained in the view port and the caret is at the center of the view port. | |
697 gfx::Rect new_view_port = GetViewport(); | |
698 EXPECT_NE(view_port, new_view_port); | |
699 EXPECT_TRUE(new_view_port.Contains(text_input_bounds)); | |
700 gfx::Rect caret_bounds = GetCaretBounds(); | |
701 EXPECT_EQ(caret_bounds.CenterPoint(), new_view_port.CenterPoint()); | |
702 } | |
703 | |
704 | |
587 // Make sure that unified desktop can enter magnified mode. | 705 // Make sure that unified desktop can enter magnified mode. |
588 TEST_F(MagnificationControllerTest, EnableMagnifierInUnifiedDesktop) { | 706 TEST_F(MagnificationControllerTest, EnableMagnifierInUnifiedDesktop) { |
589 if (!SupportsMultipleDisplays()) | 707 if (!SupportsMultipleDisplays()) |
590 return; | 708 return; |
591 test::DisplayManagerTestApi::EnableUnifiedDesktopForTest(); | 709 test::DisplayManagerTestApi::EnableUnifiedDesktopForTest(); |
592 | 710 |
593 DisplayManager* display_manager = Shell::GetInstance()->display_manager(); | 711 DisplayManager* display_manager = Shell::GetInstance()->display_manager(); |
594 display_manager->SetDefaultMultiDisplayMode(DisplayManager::UNIFIED); | 712 display_manager->SetDefaultMultiDisplayMode(DisplayManager::UNIFIED); |
595 display_manager->SetMultiDisplayMode(DisplayManager::UNIFIED); | 713 display_manager->SetMultiDisplayMode(DisplayManager::UNIFIED); |
596 | 714 |
(...skipping 18 matching lines...) Expand all Loading... | |
615 UpdateDisplay("500x500"); | 733 UpdateDisplay("500x500"); |
616 EXPECT_EQ("0,0 500x500", screen->GetPrimaryDisplay().bounds().ToString()); | 734 EXPECT_EQ("0,0 500x500", screen->GetPrimaryDisplay().bounds().ToString()); |
617 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale()); | 735 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale()); |
618 | 736 |
619 GetMagnificationController()->SetEnabled(false); | 737 GetMagnificationController()->SetEnabled(false); |
620 EXPECT_EQ("0,0 500x500", screen->GetPrimaryDisplay().bounds().ToString()); | 738 EXPECT_EQ("0,0 500x500", screen->GetPrimaryDisplay().bounds().ToString()); |
621 EXPECT_EQ(1.0f, GetMagnificationController()->GetScale()); | 739 EXPECT_EQ(1.0f, GetMagnificationController()->GetScale()); |
622 } | 740 } |
623 | 741 |
624 } // namespace ash | 742 } // namespace ash |
OLD | NEW |