| OLD | NEW |
| 1 // Copyright 2014 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 "chrome/browser/ui/autofill/popup_controller_common.h" | 5 #include "chrome/browser/ui/autofill/popup_view_utils.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "chrome/browser/ui/autofill/test_popup_controller_common.h" | |
| 12 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 11 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 12 #include "content/public/browser/web_contents.h" |
| 13 #include "ui/gfx/display.h" | 13 #include "ui/gfx/display.h" |
| 14 #include "ui/gfx/geometry/rect.h" | 14 #include "ui/gfx/geometry/rect.h" |
| 15 | 15 |
| 16 namespace autofill { | 16 namespace autofill { |
| 17 namespace view_utils { |
| 17 | 18 |
| 18 class PopupControllerBaseTest : public ChromeRenderViewHostTestHarness { | 19 namespace { |
| 20 |
| 21 // Test class which overrides specific behavior for testing. |
| 22 class TestPopupViewUtils : public PopupViewUtils { |
| 19 public: | 23 public: |
| 20 PopupControllerBaseTest() {} | 24 explicit TestPopupViewUtils(const gfx::Display& display) |
| 21 ~PopupControllerBaseTest() override {} | 25 : display_(display) {} |
| 26 |
| 27 gfx::Display GetDisplayNearestPoint(const gfx::Point& point, |
| 28 gfx::NativeView container_view) override { |
| 29 return display_; |
| 30 } |
| 22 | 31 |
| 23 private: | 32 private: |
| 24 DISALLOW_COPY_AND_ASSIGN(PopupControllerBaseTest); | 33 gfx::Display display_; |
| 25 }; | 34 }; |
| 26 | 35 |
| 27 TEST_F(PopupControllerBaseTest, GetPopupBoundsTest) { | 36 } // namespace |
| 37 |
| 38 class PopupViewUtilsTest : public ChromeRenderViewHostTestHarness { |
| 39 public: |
| 40 PopupViewUtilsTest() {} |
| 41 ~PopupViewUtilsTest() override {} |
| 42 |
| 43 private: |
| 44 DISALLOW_COPY_AND_ASSIGN(PopupViewUtilsTest); |
| 45 }; |
| 46 |
| 47 TEST_F(PopupViewUtilsTest, CalculatePopupBounds) { |
| 28 int desired_width = 40; | 48 int desired_width = 40; |
| 29 int desired_height = 16; | 49 int desired_height = 16; |
| 30 | 50 |
| 31 // Set up the visible screen space. | 51 // Set up the visible screen space. |
| 32 gfx::Display display(0, | 52 gfx::Display display(0, |
| 33 gfx::Rect(0, 0, 2 * desired_width, 2 * desired_height)); | 53 gfx::Rect(0, 0, 2 * desired_width, 2 * desired_height)); |
| 54 TestPopupViewUtils view_utils(display); |
| 34 | 55 |
| 35 struct { | 56 struct { |
| 36 gfx::RectF element_bounds; | 57 gfx::Rect element_bounds; |
| 37 gfx::Rect expected_popup_bounds_ltr; | 58 gfx::Rect expected_popup_bounds_ltr; |
| 38 // Non-empty only when it differs from the ltr expectation. | 59 // Non-empty only when it differs from the ltr expectation. |
| 39 gfx::Rect expected_popup_bounds_rtl; | 60 gfx::Rect expected_popup_bounds_rtl; |
| 40 } test_cases[] = { | 61 } test_cases[] = { |
| 41 // The popup grows down and to the end. | 62 // The popup grows down and to the end. |
| 42 {gfx::RectF(38, 0, 5, 0), | 63 {gfx::Rect(38, 0, 5, 0), gfx::Rect(38, 0, desired_width, desired_height), |
| 43 gfx::Rect(38, 0, desired_width, desired_height), | |
| 44 gfx::Rect(3, 0, desired_width, desired_height)}, | 64 gfx::Rect(3, 0, desired_width, desired_height)}, |
| 45 | 65 |
| 46 // The popup grows down and to the left when there's no room on the right. | 66 // The popup grows down and to the left when there's no room on the right. |
| 47 {gfx::RectF(2 * desired_width, 0, 5, 0), | 67 {gfx::Rect(2 * desired_width, 0, 5, 0), |
| 48 gfx::Rect(desired_width, 0, desired_width, desired_height)}, | 68 gfx::Rect(desired_width, 0, desired_width, desired_height)}, |
| 49 | 69 |
| 50 // The popup grows up and to the right. | 70 // The popup grows up and to the right. |
| 51 {gfx::RectF(0, 2 * desired_height, 5, 0), | 71 {gfx::Rect(0, 2 * desired_height, 5, 0), |
| 52 gfx::Rect(0, desired_height, desired_width, desired_height)}, | 72 gfx::Rect(0, desired_height, desired_width, desired_height)}, |
| 53 | 73 |
| 54 // The popup grows up and to the left. | 74 // The popup grows up and to the left. |
| 55 {gfx::RectF(2 * desired_width, 2 * desired_height, 5, 0), | 75 {gfx::Rect(2 * desired_width, 2 * desired_height, 5, 0), |
| 56 gfx::Rect(desired_width, desired_height, desired_width, desired_height)}, | 76 gfx::Rect(desired_width, desired_height, desired_width, desired_height)}, |
| 57 | 77 |
| 58 // The popup would be partial off the top and left side of the screen. | 78 // The popup would be partial off the top and left side of the screen. |
| 59 {gfx::RectF(-desired_width / 2, -desired_height / 2, 5, 0), | 79 {gfx::Rect(-desired_width / 2, -desired_height / 2, 5, 0), |
| 60 gfx::Rect(0, 0, desired_width, desired_height)}, | 80 gfx::Rect(0, 0, desired_width, desired_height)}, |
| 61 | 81 |
| 62 // The popup would be partially off the bottom and the right side of | 82 // The popup would be partially off the bottom and the right side of |
| 63 // the screen. | 83 // the screen. |
| 64 {gfx::RectF(desired_width * 1.5, desired_height * 1.5, 5, 0), | 84 {gfx::Rect(desired_width * 1.5, desired_height * 1.5, 5, 0), |
| 65 gfx::Rect((desired_width * 1.5 + 5 - desired_width), | 85 gfx::Rect((desired_width * 1.5 + 5 - desired_width), |
| 66 (desired_height * 1.5 - desired_height), desired_width, | 86 (desired_height * 1.5 - desired_height), desired_width, |
| 67 desired_height)}, | 87 desired_height)}, |
| 68 }; | 88 }; |
| 69 | 89 |
| 70 for (size_t i = 0; i < arraysize(test_cases); ++i) { | 90 for (size_t i = 0; i < arraysize(test_cases); ++i) { |
| 71 scoped_ptr<TestPopupControllerCommon> popup_controller( | 91 gfx::Rect actual_popup_bounds = view_utils.CalculatePopupBounds( |
| 72 new TestPopupControllerCommon(test_cases[i].element_bounds, | 92 desired_width, desired_height, test_cases[i].element_bounds, |
| 73 base::i18n::LEFT_TO_RIGHT)); | 93 web_contents()->GetNativeView(), /* is_rtl= */ false); |
| 74 popup_controller->set_display(display); | |
| 75 gfx::Rect actual_popup_bounds = | |
| 76 popup_controller->GetPopupBounds(desired_width, desired_height); | |
| 77 EXPECT_EQ(test_cases[i].expected_popup_bounds_ltr.ToString(), | 94 EXPECT_EQ(test_cases[i].expected_popup_bounds_ltr.ToString(), |
| 78 actual_popup_bounds.ToString()) | 95 actual_popup_bounds.ToString()) |
| 79 << "Popup bounds failed to match for ltr test " << i; | 96 << "Popup bounds failed to match for ltr test " << i; |
| 80 | 97 |
| 81 popup_controller.reset(new TestPopupControllerCommon( | 98 actual_popup_bounds = view_utils.CalculatePopupBounds( |
| 82 test_cases[i].element_bounds, base::i18n::RIGHT_TO_LEFT)); | 99 desired_width, desired_height, test_cases[i].element_bounds, |
| 83 popup_controller->set_display(display); | 100 web_contents()->GetNativeView(), /* is_rtl= */ true); |
| 84 actual_popup_bounds = | |
| 85 popup_controller->GetPopupBounds(desired_width, desired_height); | |
| 86 gfx::Rect expected_popup_bounds = test_cases[i].expected_popup_bounds_rtl; | 101 gfx::Rect expected_popup_bounds = test_cases[i].expected_popup_bounds_rtl; |
| 87 if (expected_popup_bounds.IsEmpty()) | 102 if (expected_popup_bounds.IsEmpty()) |
| 88 expected_popup_bounds = test_cases[i].expected_popup_bounds_ltr; | 103 expected_popup_bounds = test_cases[i].expected_popup_bounds_ltr; |
| 89 EXPECT_EQ(expected_popup_bounds.ToString(), actual_popup_bounds.ToString()) | 104 EXPECT_EQ(expected_popup_bounds.ToString(), actual_popup_bounds.ToString()) |
| 90 << "Popup bounds failed to match for rtl test " << i; | 105 << "Popup bounds failed to match for rtl test " << i; |
| 91 } | 106 } |
| 92 } | 107 } |
| 93 | 108 |
| 109 } // namespace view_utils |
| 94 } // namespace autofill | 110 } // namespace autofill |
| OLD | NEW |