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/magnifier/magnifier_constants.h" | 7 #include "ash/magnifier/magnifier_constants.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 "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
11 #include "ui/aura/client/aura_constants.h" | 11 #include "ui/aura/client/aura_constants.h" |
12 #include "ui/aura/env.h" | 12 #include "ui/aura/env.h" |
13 #include "ui/aura/test/aura_test_utils.h" | 13 #include "ui/aura/test/aura_test_utils.h" |
14 #include "ui/aura/window_tree_host.h" | 14 #include "ui/aura/window_tree_host.h" |
| 15 #include "ui/base/ime/input_method.h" |
15 #include "ui/events/test/event_generator.h" | 16 #include "ui/events/test/event_generator.h" |
16 #include "ui/gfx/rect_conversions.h" | 17 #include "ui/gfx/rect_conversions.h" |
17 #include "ui/gfx/screen.h" | 18 #include "ui/gfx/screen.h" |
| 19 #include "ui/views/controls/textfield/textfield.h" |
| 20 #include "ui/views/layout/fill_layout.h" |
| 21 #include "ui/views/widget/widget.h" |
| 22 #include "ui/views/widget/widget_delegate.h" |
| 23 #include "ui/wm/core/coordinate_conversion.h" |
18 | 24 |
19 namespace ash { | 25 namespace ash { |
20 namespace { | 26 namespace { |
21 | 27 |
22 const int kRootHeight = 600; | 28 const int kRootHeight = 600; |
23 const int kRootWidth = 800; | 29 const int kRootWidth = 800; |
24 | 30 |
| 31 const int kTextInputWindowWidth = 50; |
| 32 const int kTextInputWindowHeight = 50; |
| 33 |
| 34 class TextInputView : public views::WidgetDelegateView { |
| 35 public: |
| 36 TextInputView() { |
| 37 text_field_ = new views::Textfield; |
| 38 text_field_->SetTextInputType(ui::TEXT_INPUT_TYPE_TEXT); |
| 39 AddChildView(text_field_); |
| 40 SetLayoutManager(new views::FillLayout); |
| 41 } |
| 42 |
| 43 virtual ~TextInputView() {} |
| 44 |
| 45 virtual gfx::Size GetPreferredSize() const override { |
| 46 return gfx::Size(kTextInputWindowWidth, kTextInputWindowHeight); |
| 47 } |
| 48 |
| 49 // Overridden from views::WidgetDelegate: |
| 50 virtual views::View* GetContentsView() override { return this; } |
| 51 |
| 52 void FocusOnTextInput() { GetFocusManager()->SetFocusedView(text_field_); } |
| 53 |
| 54 private: |
| 55 views::Textfield* text_field_; // owned by views hierarchy |
| 56 |
| 57 DISALLOW_COPY_AND_ASSIGN(TextInputView); |
| 58 }; |
| 59 |
25 } // namespace | 60 } // namespace |
26 | 61 |
27 class MagnificationControllerTest: public test::AshTestBase { | 62 class MagnificationControllerTest: public test::AshTestBase { |
28 public: | 63 public: |
29 MagnificationControllerTest() {} | 64 MagnificationControllerTest() {} |
30 virtual ~MagnificationControllerTest() {} | 65 virtual ~MagnificationControllerTest() {} |
31 | 66 |
32 virtual void SetUp() override { | 67 virtual void SetUp() override { |
33 AshTestBase::SetUp(); | 68 AshTestBase::SetUp(); |
34 UpdateDisplay(base::StringPrintf("%dx%d", kRootWidth, kRootHeight)); | 69 UpdateDisplay(base::StringPrintf("%dx%d", kRootWidth, kRootHeight)); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 gfx::RectF bounds(0, 0, kRootWidth, kRootHeight); | 103 gfx::RectF bounds(0, 0, kRootWidth, kRootHeight); |
69 GetRootWindow()->layer()->transform().TransformRectReverse(&bounds); | 104 GetRootWindow()->layer()->transform().TransformRectReverse(&bounds); |
70 return gfx::ToEnclosingRect(bounds); | 105 return gfx::ToEnclosingRect(bounds); |
71 } | 106 } |
72 | 107 |
73 std::string CurrentPointOfInterest() const { | 108 std::string CurrentPointOfInterest() const { |
74 return GetMagnificationController()-> | 109 return GetMagnificationController()-> |
75 GetPointOfInterestForTesting().ToString(); | 110 GetPointOfInterestForTesting().ToString(); |
76 } | 111 } |
77 | 112 |
| 113 void CreateAndShowTextInputView(const gfx::Rect& bounds) { |
| 114 text_input_view_ = new TextInputView; |
| 115 views::Widget* widget = views::Widget::CreateWindowWithContextAndBounds( |
| 116 text_input_view_, GetRootWindow(), bounds); |
| 117 widget->Show(); |
| 118 } |
| 119 |
| 120 // Returns the text input view's bounds in root window coordinates. |
| 121 gfx::Rect GetTextInputViewBounds() { |
| 122 gfx::Rect bounds = text_input_view_->bounds(); |
| 123 gfx::Point origin = bounds.origin(); |
| 124 // Convert origin to screen coordinates. |
| 125 views::View::ConvertPointToScreen(text_input_view_, &origin); |
| 126 // Convert origin to root_window_ coordinates. |
| 127 wm::ConvertPointFromScreen(GetRootWindow(), &origin); |
| 128 return gfx::Rect(origin.x(), origin.y(), bounds.width(), bounds.height()); |
| 129 } |
| 130 |
| 131 // Returns the caret bounds in root window coordinates. |
| 132 gfx::Rect GetCaretBounds() { |
| 133 gfx::Rect caret_bounds = |
| 134 GetInputMethod()->GetTextInputClient()->GetCaretBounds(); |
| 135 gfx::Point origin = caret_bounds.origin(); |
| 136 wm::ConvertPointFromScreen(GetRootWindow(), &origin); |
| 137 return gfx::Rect( |
| 138 origin.x(), origin.y(), caret_bounds.width(), caret_bounds.height()); |
| 139 } |
| 140 |
| 141 void FocusOnTextInputView() { text_input_view_->FocusOnTextInput(); } |
| 142 |
78 private: | 143 private: |
| 144 TextInputView* text_input_view_; |
| 145 |
| 146 ui::InputMethod* GetInputMethod() { |
| 147 return Shell::GetPrimaryRootWindow()->GetProperty( |
| 148 aura::client::kRootWindowInputMethodKey); |
| 149 } |
| 150 |
79 DISALLOW_COPY_AND_ASSIGN(MagnificationControllerTest); | 151 DISALLOW_COPY_AND_ASSIGN(MagnificationControllerTest); |
80 }; | 152 }; |
81 | 153 |
82 TEST_F(MagnificationControllerTest, EnableAndDisable) { | 154 TEST_F(MagnificationControllerTest, EnableAndDisable) { |
83 // Confirms the magnifier is disabled. | 155 // Confirms the magnifier is disabled. |
84 EXPECT_TRUE(GetRootWindow()->layer()->transform().IsIdentity()); | 156 EXPECT_TRUE(GetRootWindow()->layer()->transform().IsIdentity()); |
85 EXPECT_EQ(1.0f, GetMagnificationController()->GetScale()); | 157 EXPECT_EQ(1.0f, GetMagnificationController()->GetScale()); |
86 EXPECT_EQ("0,0 800x600", GetViewport().ToString()); | 158 EXPECT_EQ("0,0 800x600", GetViewport().ToString()); |
87 | 159 |
88 // Enables magnifier. | 160 // Enables magnifier. |
(...skipping 344 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
433 EXPECT_EQ("98,300", GetHostMouseLocation()); | 505 EXPECT_EQ("98,300", GetHostMouseLocation()); |
434 | 506 |
435 scale *= kMagnificationScaleFactor; | 507 scale *= kMagnificationScaleFactor; |
436 GetMagnificationController()->SetScale(scale, false); | 508 GetMagnificationController()->SetScale(scale, false); |
437 EXPECT_FLOAT_EQ(4.f, GetMagnificationController()->GetScale()); | 509 EXPECT_FLOAT_EQ(4.f, GetMagnificationController()->GetScale()); |
438 generator.MoveMouseToInHost(gfx::Point(0, 300)); | 510 generator.MoveMouseToInHost(gfx::Point(0, 300)); |
439 EXPECT_EQ("139,298", env->last_mouse_location().ToString()); | 511 EXPECT_EQ("139,298", env->last_mouse_location().ToString()); |
440 EXPECT_EQ("100,300", GetHostMouseLocation()); | 512 EXPECT_EQ("100,300", GetHostMouseLocation()); |
441 } | 513 } |
442 | 514 |
| 515 TEST_F(MagnificationControllerTest, FollowTextInputFieldFocus) { |
| 516 CreateAndShowTextInputView(gfx::Rect(500, 300, 80, 80)); |
| 517 gfx::Rect text_input_bounds = GetTextInputViewBounds(); |
| 518 |
| 519 // Enables magnifier and confirm the viewport is at center. |
| 520 GetMagnificationController()->SetEnabled(true); |
| 521 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale()); |
| 522 EXPECT_EQ("200,150 400x300", GetViewport().ToString()); |
| 523 |
| 524 // Move the viewport to (0, 0), so that text input field will be out of |
| 525 // the viewport region. |
| 526 GetMagnificationController()->MoveWindow(0, 0, false); |
| 527 EXPECT_EQ("0,0 400x300", GetViewport().ToString()); |
| 528 EXPECT_FALSE(GetViewport().Intersects(text_input_bounds)); |
| 529 |
| 530 // Focus on the text input field. |
| 531 FocusOnTextInputView(); |
| 532 |
| 533 // Verify the view port has been moved to the place where the text field is |
| 534 // contained in the view port and the caret is at the center of the view port. |
| 535 gfx::Rect view_port = GetViewport(); |
| 536 EXPECT_TRUE(view_port.Contains(text_input_bounds)); |
| 537 gfx::Rect caret_bounds = GetCaretBounds(); |
| 538 EXPECT_TRUE(text_input_bounds.Contains(caret_bounds)); |
| 539 EXPECT_EQ(caret_bounds.origin(), view_port.CenterPoint()); |
| 540 } |
| 541 |
| 542 TEST_F(MagnificationControllerTest, FollowTextInputFieldKeyPress) { |
| 543 CreateAndShowTextInputView(gfx::Rect(385, 200, 80, 80)); |
| 544 gfx::Rect text_input_bounds = GetTextInputViewBounds(); |
| 545 |
| 546 // Enables magnifier and confirm the viewport is at center. |
| 547 GetMagnificationController()->SetEnabled(true); |
| 548 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale()); |
| 549 EXPECT_EQ("200,150 400x300", GetViewport().ToString()); |
| 550 |
| 551 // Move the viewport to (0, 0), so that text input field intersects the |
| 552 // view port at the right edge. |
| 553 GetMagnificationController()->MoveWindow(0, 0, false); |
| 554 EXPECT_EQ("0,0 400x300", GetViewport().ToString()); |
| 555 EXPECT_TRUE(GetViewport().Intersects(text_input_bounds)); |
| 556 |
| 557 // Focus on the text input field. |
| 558 FocusOnTextInputView(); |
| 559 |
| 560 // Verify the view port is not moved, and the caret is inside the view port. |
| 561 gfx::Rect view_port = GetViewport(); |
| 562 EXPECT_EQ("0,0 400x300", view_port.ToString()); |
| 563 EXPECT_TRUE(view_port.Intersects(text_input_bounds)); |
| 564 EXPECT_TRUE(text_input_bounds.Contains(GetCaretBounds())); |
| 565 |
| 566 // Press keys on text input simulate typing on text field and the caret |
| 567 // moves out of the old view port region. The view port is moved to the place |
| 568 // where caret's x coordinate is centered at the new view port. |
| 569 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow()); |
| 570 generator.PressKey(ui::VKEY_A, 0); |
| 571 generator.ReleaseKey(ui::VKEY_A, 0); |
| 572 gfx::Rect caret_bounds = GetCaretBounds(); |
| 573 EXPECT_FALSE(view_port.Intersects(caret_bounds)); |
| 574 |
| 575 gfx::Rect new_view_port = GetViewport(); |
| 576 EXPECT_TRUE(new_view_port.Contains(caret_bounds)); |
| 577 EXPECT_EQ(caret_bounds.x(), new_view_port.CenterPoint().x()); |
| 578 EXPECT_EQ(view_port.y(), new_view_port.y()); |
| 579 } |
| 580 |
443 } // namespace ash | 581 } // namespace ash |
OLD | NEW |