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() : text_field_(new views::Textfield) { |
| 37 text_field_->SetTextInputType(ui::TEXT_INPUT_TYPE_TEXT); |
| 38 AddChildView(text_field_); |
| 39 SetLayoutManager(new views::FillLayout); |
| 40 } |
| 41 |
| 42 virtual ~TextInputView() {} |
| 43 |
| 44 virtual gfx::Size GetPreferredSize() const override { |
| 45 return gfx::Size(kTextInputWindowWidth, kTextInputWindowHeight); |
| 46 } |
| 47 |
| 48 // Overridden from views::WidgetDelegate: |
| 49 virtual views::View* GetContentsView() override { return this; } |
| 50 |
| 51 void FocusOnTextInput() { GetFocusManager()->SetFocusedView(text_field_); } |
| 52 |
| 53 private: |
| 54 views::Textfield* text_field_; // owned by views hierarchy |
| 55 |
| 56 DISALLOW_COPY_AND_ASSIGN(TextInputView); |
| 57 }; |
| 58 |
25 } // namespace | 59 } // namespace |
26 | 60 |
27 class MagnificationControllerTest: public test::AshTestBase { | 61 class MagnificationControllerTest: public test::AshTestBase { |
28 public: | 62 public: |
29 MagnificationControllerTest() {} | 63 MagnificationControllerTest() : text_input_view_(NULL) {} |
30 virtual ~MagnificationControllerTest() {} | 64 virtual ~MagnificationControllerTest() {} |
31 | 65 |
32 virtual void SetUp() override { | 66 virtual void SetUp() override { |
33 AshTestBase::SetUp(); | 67 AshTestBase::SetUp(); |
34 UpdateDisplay(base::StringPrintf("%dx%d", kRootWidth, kRootHeight)); | 68 UpdateDisplay(base::StringPrintf("%dx%d", kRootWidth, kRootHeight)); |
35 | 69 |
36 aura::Window* root = GetRootWindow(); | 70 aura::Window* root = GetRootWindow(); |
37 gfx::Rect root_bounds(root->bounds()); | 71 gfx::Rect root_bounds(root->bounds()); |
38 | 72 |
39 #if defined(OS_WIN) | 73 #if defined(OS_WIN) |
(...skipping 28 matching lines...) Expand all Loading... |
68 gfx::RectF bounds(0, 0, kRootWidth, kRootHeight); | 102 gfx::RectF bounds(0, 0, kRootWidth, kRootHeight); |
69 GetRootWindow()->layer()->transform().TransformRectReverse(&bounds); | 103 GetRootWindow()->layer()->transform().TransformRectReverse(&bounds); |
70 return gfx::ToEnclosingRect(bounds); | 104 return gfx::ToEnclosingRect(bounds); |
71 } | 105 } |
72 | 106 |
73 std::string CurrentPointOfInterest() const { | 107 std::string CurrentPointOfInterest() const { |
74 return GetMagnificationController()-> | 108 return GetMagnificationController()-> |
75 GetPointOfInterestForTesting().ToString(); | 109 GetPointOfInterestForTesting().ToString(); |
76 } | 110 } |
77 | 111 |
| 112 void CreateAndShowTextInputView(const gfx::Rect& bounds) { |
| 113 text_input_view_ = new TextInputView; |
| 114 views::Widget* widget = views::Widget::CreateWindowWithContextAndBounds( |
| 115 text_input_view_, GetRootWindow(), bounds); |
| 116 widget->Show(); |
| 117 } |
| 118 |
| 119 // Returns the text input view's bounds in root window coordinates. |
| 120 gfx::Rect GetTextInputViewBounds() { |
| 121 DCHECK(text_input_view_); |
| 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() { |
| 142 DCHECK(text_input_view_); |
| 143 text_input_view_->FocusOnTextInput(); |
| 144 } |
| 145 |
78 private: | 146 private: |
| 147 TextInputView* text_input_view_; |
| 148 |
| 149 ui::InputMethod* GetInputMethod() { |
| 150 DCHECK(text_input_view_); |
| 151 return text_input_view_->GetWidget() |
| 152 ->GetNativeWindow() |
| 153 ->GetRootWindow() |
| 154 ->GetProperty(aura::client::kRootWindowInputMethodKey); |
| 155 } |
| 156 |
79 DISALLOW_COPY_AND_ASSIGN(MagnificationControllerTest); | 157 DISALLOW_COPY_AND_ASSIGN(MagnificationControllerTest); |
80 }; | 158 }; |
81 | 159 |
82 TEST_F(MagnificationControllerTest, EnableAndDisable) { | 160 TEST_F(MagnificationControllerTest, EnableAndDisable) { |
83 // Confirms the magnifier is disabled. | 161 // Confirms the magnifier is disabled. |
84 EXPECT_TRUE(GetRootWindow()->layer()->transform().IsIdentity()); | 162 EXPECT_TRUE(GetRootWindow()->layer()->transform().IsIdentity()); |
85 EXPECT_EQ(1.0f, GetMagnificationController()->GetScale()); | 163 EXPECT_EQ(1.0f, GetMagnificationController()->GetScale()); |
86 EXPECT_EQ("0,0 800x600", GetViewport().ToString()); | 164 EXPECT_EQ("0,0 800x600", GetViewport().ToString()); |
87 | 165 |
88 // Enables magnifier. | 166 // Enables magnifier. |
(...skipping 344 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
433 EXPECT_EQ("98,300", GetHostMouseLocation()); | 511 EXPECT_EQ("98,300", GetHostMouseLocation()); |
434 | 512 |
435 scale *= kMagnificationScaleFactor; | 513 scale *= kMagnificationScaleFactor; |
436 GetMagnificationController()->SetScale(scale, false); | 514 GetMagnificationController()->SetScale(scale, false); |
437 EXPECT_FLOAT_EQ(4.f, GetMagnificationController()->GetScale()); | 515 EXPECT_FLOAT_EQ(4.f, GetMagnificationController()->GetScale()); |
438 generator.MoveMouseToInHost(gfx::Point(0, 300)); | 516 generator.MoveMouseToInHost(gfx::Point(0, 300)); |
439 EXPECT_EQ("139,298", env->last_mouse_location().ToString()); | 517 EXPECT_EQ("139,298", env->last_mouse_location().ToString()); |
440 EXPECT_EQ("100,300", GetHostMouseLocation()); | 518 EXPECT_EQ("100,300", GetHostMouseLocation()); |
441 } | 519 } |
442 | 520 |
| 521 TEST_F(MagnificationControllerTest, FollowTextInputFieldFocus) { |
| 522 CreateAndShowTextInputView(gfx::Rect(500, 300, 80, 80)); |
| 523 gfx::Rect text_input_bounds = GetTextInputViewBounds(); |
| 524 |
| 525 // Enables magnifier and confirm the viewport is at center. |
| 526 GetMagnificationController()->SetEnabled(true); |
| 527 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale()); |
| 528 EXPECT_EQ("200,150 400x300", GetViewport().ToString()); |
| 529 |
| 530 // Move the viewport to (0, 0), so that text input field will be out of |
| 531 // the viewport region. |
| 532 GetMagnificationController()->MoveWindow(0, 0, false); |
| 533 EXPECT_EQ("0,0 400x300", GetViewport().ToString()); |
| 534 EXPECT_FALSE(GetViewport().Intersects(text_input_bounds)); |
| 535 |
| 536 // Focus on the text input field. |
| 537 FocusOnTextInputView(); |
| 538 |
| 539 // 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. |
| 541 gfx::Rect view_port = GetViewport(); |
| 542 EXPECT_TRUE(view_port.Contains(text_input_bounds)); |
| 543 gfx::Rect caret_bounds = GetCaretBounds(); |
| 544 EXPECT_TRUE(text_input_bounds.Contains(caret_bounds)); |
| 545 EXPECT_EQ(caret_bounds.origin(), view_port.CenterPoint()); |
| 546 } |
| 547 |
| 548 TEST_F(MagnificationControllerTest, FollowTextInputFieldKeyPress) { |
| 549 CreateAndShowTextInputView(gfx::Rect(385, 200, 80, 80)); |
| 550 gfx::Rect text_input_bounds = GetTextInputViewBounds(); |
| 551 |
| 552 // Enables magnifier and confirm the viewport is at center. |
| 553 GetMagnificationController()->SetEnabled(true); |
| 554 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale()); |
| 555 EXPECT_EQ("200,150 400x300", GetViewport().ToString()); |
| 556 |
| 557 // Move the viewport to (0, 0), so that text input field intersects the |
| 558 // view port at the right edge. |
| 559 GetMagnificationController()->MoveWindow(0, 0, false); |
| 560 EXPECT_EQ("0,0 400x300", GetViewport().ToString()); |
| 561 EXPECT_TRUE(GetViewport().Intersects(text_input_bounds)); |
| 562 |
| 563 // Focus on the text input field. |
| 564 FocusOnTextInputView(); |
| 565 |
| 566 // Verify the view port is not moved, and the caret is inside the view port. |
| 567 gfx::Rect view_port = GetViewport(); |
| 568 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())); |
| 571 |
| 572 // 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 |
| 574 // where caret's x coordinate is centered at the new view port. |
| 575 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow()); |
| 576 generator.PressKey(ui::VKEY_A, 0); |
| 577 generator.ReleaseKey(ui::VKEY_A, 0); |
| 578 gfx::Rect caret_bounds = GetCaretBounds(); |
| 579 EXPECT_FALSE(view_port.Intersects(caret_bounds)); |
| 580 |
| 581 gfx::Rect new_view_port = GetViewport(); |
| 582 EXPECT_TRUE(new_view_port.Contains(caret_bounds)); |
| 583 EXPECT_EQ(caret_bounds.x(), new_view_port.CenterPoint().x()); |
| 584 EXPECT_EQ(view_port.y(), new_view_port.y()); |
| 585 } |
| 586 |
443 } // namespace ash | 587 } // namespace ash |
OLD | NEW |