| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <stddef.h> | 5 #include <stddef.h> |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 | 8 |
| 9 #include "base/i18n/rtl.h" | 9 #include "base/i18n/rtl.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| (...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 | 205 |
| 206 // A derived class for testing purpose. | 206 // A derived class for testing purpose. |
| 207 class TestView : public View { | 207 class TestView : public View { |
| 208 public: | 208 public: |
| 209 TestView() | 209 TestView() |
| 210 : View(), | 210 : View(), |
| 211 did_layout_(false), | 211 did_layout_(false), |
| 212 delete_on_pressed_(false), | 212 delete_on_pressed_(false), |
| 213 did_paint_(false), | 213 did_paint_(false), |
| 214 native_theme_(NULL), | 214 native_theme_(NULL), |
| 215 can_process_events_within_subtree_(true) {} | 215 can_process_events_within_subtree_(true), |
| 216 is_child_widget_(false), |
| 217 focus_in_child_widget_(false) {} |
| 216 ~TestView() override {} | 218 ~TestView() override {} |
| 217 | 219 |
| 218 // Reset all test state | 220 // Reset all test state |
| 219 void Reset() { | 221 void Reset() { |
| 220 did_change_bounds_ = false; | 222 did_change_bounds_ = false; |
| 221 did_layout_ = false; | 223 did_layout_ = false; |
| 222 last_mouse_event_type_ = 0; | 224 last_mouse_event_type_ = 0; |
| 223 location_.SetPoint(0, 0); | 225 location_.SetPoint(0, 0); |
| 224 received_mouse_enter_ = false; | 226 received_mouse_enter_ = false; |
| 225 received_mouse_exit_ = false; | 227 received_mouse_exit_ = false; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 258 void OnMouseReleased(const ui::MouseEvent& event) override; | 260 void OnMouseReleased(const ui::MouseEvent& event) override; |
| 259 void OnMouseEntered(const ui::MouseEvent& event) override; | 261 void OnMouseEntered(const ui::MouseEvent& event) override; |
| 260 void OnMouseExited(const ui::MouseEvent& event) override; | 262 void OnMouseExited(const ui::MouseEvent& event) override; |
| 261 | 263 |
| 262 void OnPaint(gfx::Canvas* canvas) override; | 264 void OnPaint(gfx::Canvas* canvas) override; |
| 263 void SchedulePaintInRect(const gfx::Rect& rect) override; | 265 void SchedulePaintInRect(const gfx::Rect& rect) override; |
| 264 bool AcceleratorPressed(const ui::Accelerator& accelerator) override; | 266 bool AcceleratorPressed(const ui::Accelerator& accelerator) override; |
| 265 | 267 |
| 266 void OnNativeThemeChanged(const ui::NativeTheme* native_theme) override; | 268 void OnNativeThemeChanged(const ui::NativeTheme* native_theme) override; |
| 267 | 269 |
| 270 bool IsChildWidget() const override { return is_child_widget_; } |
| 271 bool FocusInChildWidget() const override { return focus_in_child_widget_; } |
| 272 |
| 273 void set_child_widget(bool b) { is_child_widget_ = b; } |
| 274 void set_focus_in_child_widget(bool b) { focus_in_child_widget_ = b; } |
| 275 |
| 268 // OnBoundsChanged. | 276 // OnBoundsChanged. |
| 269 bool did_change_bounds_; | 277 bool did_change_bounds_; |
| 270 gfx::Rect new_bounds_; | 278 gfx::Rect new_bounds_; |
| 271 | 279 |
| 272 // Layout. | 280 // Layout. |
| 273 bool did_layout_; | 281 bool did_layout_; |
| 274 | 282 |
| 275 // MouseEvent. | 283 // MouseEvent. |
| 276 int last_mouse_event_type_; | 284 int last_mouse_event_type_; |
| 277 gfx::Point location_; | 285 gfx::Point location_; |
| 278 bool received_mouse_enter_; | 286 bool received_mouse_enter_; |
| 279 bool received_mouse_exit_; | 287 bool received_mouse_exit_; |
| 280 bool delete_on_pressed_; | 288 bool delete_on_pressed_; |
| 281 | 289 |
| 282 // Painting. | 290 // Painting. |
| 283 std::vector<gfx::Rect> scheduled_paint_rects_; | 291 std::vector<gfx::Rect> scheduled_paint_rects_; |
| 284 bool did_paint_; | 292 bool did_paint_; |
| 285 | 293 |
| 286 // Accelerators. | 294 // Accelerators. |
| 287 std::map<ui::Accelerator, int> accelerator_count_map_; | 295 std::map<ui::Accelerator, int> accelerator_count_map_; |
| 288 | 296 |
| 289 // Native theme. | 297 // Native theme. |
| 290 const ui::NativeTheme* native_theme_; | 298 const ui::NativeTheme* native_theme_; |
| 291 | 299 |
| 292 // Value to return from CanProcessEventsWithinSubtree(). | 300 // Value to return from CanProcessEventsWithinSubtree(). |
| 293 bool can_process_events_within_subtree_; | 301 bool can_process_events_within_subtree_; |
| 302 |
| 303 bool is_child_widget_; |
| 304 bool focus_in_child_widget_; |
| 294 }; | 305 }; |
| 295 | 306 |
| 296 //////////////////////////////////////////////////////////////////////////////// | 307 //////////////////////////////////////////////////////////////////////////////// |
| 297 // Layout | 308 // Layout |
| 298 //////////////////////////////////////////////////////////////////////////////// | 309 //////////////////////////////////////////////////////////////////////////////// |
| 299 | 310 |
| 300 TEST_F(ViewTest, LayoutCalledInvalidateAndOriginChanges) { | 311 TEST_F(ViewTest, LayoutCalledInvalidateAndOriginChanges) { |
| 301 TestView parent; | 312 TestView parent; |
| 302 TestView* child = new TestView; | 313 TestView* child = new TestView; |
| 303 gfx::Rect parent_rect(0, 0, 100, 100); | 314 gfx::Rect parent_rect(0, 0, 100, 100); |
| (...skipping 1790 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2094 } | 2105 } |
| 2095 | 2106 |
| 2096 //////////////////////////////////////////////////////////////////////////////// | 2107 //////////////////////////////////////////////////////////////////////////////// |
| 2097 // Accelerators | 2108 // Accelerators |
| 2098 //////////////////////////////////////////////////////////////////////////////// | 2109 //////////////////////////////////////////////////////////////////////////////// |
| 2099 bool TestView::AcceleratorPressed(const ui::Accelerator& accelerator) { | 2110 bool TestView::AcceleratorPressed(const ui::Accelerator& accelerator) { |
| 2100 accelerator_count_map_[accelerator]++; | 2111 accelerator_count_map_[accelerator]++; |
| 2101 return true; | 2112 return true; |
| 2102 } | 2113 } |
| 2103 | 2114 |
| 2115 class TestWidget : public Widget { |
| 2116 public: |
| 2117 TestWidget() : Widget() {} |
| 2118 |
| 2119 // Widget method: |
| 2120 bool IsActive() const override { return active_; } |
| 2121 |
| 2122 void set_active(bool active) { active_ = active; } |
| 2123 |
| 2124 private: |
| 2125 bool active_ = false; |
| 2126 |
| 2127 DISALLOW_COPY_AND_ASSIGN(TestWidget); |
| 2128 }; |
| 2129 |
| 2130 // ChromeOS handles accelerators correctly with regards to the activeness or top |
| 2131 // level status of the widget, so there's no extra logic there to test. |
| 2132 #if !defined(OS_CHROMEOS) |
| 2133 TEST_F(ViewTest, HandleAccelerator) { |
| 2134 ui::Accelerator return_accelerator(ui::VKEY_RETURN, ui::EF_NONE); |
| 2135 TestView* view = new TestView(); |
| 2136 view->Reset(); |
| 2137 view->AddAccelerator(return_accelerator); |
| 2138 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 0); |
| 2139 |
| 2140 // Create a window and add the view as its child. |
| 2141 scoped_ptr<TestWidget> widget(new TestWidget); |
| 2142 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); |
| 2143 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 2144 params.bounds = gfx::Rect(0, 0, 100, 100); |
| 2145 widget->Init(params); |
| 2146 View* root = widget->GetRootView(); |
| 2147 root->AddChildView(view); |
| 2148 widget->Show(); |
| 2149 |
| 2150 FocusManager* focus_manager = widget->GetFocusManager(); |
| 2151 ASSERT_TRUE(focus_manager); |
| 2152 |
| 2153 // Child widgets shouldn't handle accelerators when they are not focused. |
| 2154 EXPECT_FALSE(view->IsChildWidget()); |
| 2155 EXPECT_FALSE(view->FocusInChildWidget()); |
| 2156 EXPECT_FALSE(widget->IsActive()); |
| 2157 EXPECT_FALSE(focus_manager->ProcessAccelerator(return_accelerator)); |
| 2158 EXPECT_EQ(0, view->accelerator_count_map_[return_accelerator]); |
| 2159 |
| 2160 // Child without focus. |
| 2161 view->set_child_widget(true); |
| 2162 view->set_focus_in_child_widget(false); |
| 2163 EXPECT_FALSE(focus_manager->ProcessAccelerator(return_accelerator)); |
| 2164 EXPECT_EQ(0, view->accelerator_count_map_[return_accelerator]); |
| 2165 |
| 2166 // Child with focus. |
| 2167 view->set_child_widget(true); |
| 2168 view->set_focus_in_child_widget(true); |
| 2169 EXPECT_TRUE(focus_manager->ProcessAccelerator(return_accelerator)); |
| 2170 EXPECT_EQ(1, view->accelerator_count_map_[return_accelerator]); |
| 2171 |
| 2172 // Not a child, but active. |
| 2173 view->set_child_widget(false); |
| 2174 view->set_focus_in_child_widget(true); |
| 2175 widget->set_active(true); |
| 2176 EXPECT_TRUE(focus_manager->ProcessAccelerator(return_accelerator)); |
| 2177 EXPECT_EQ(2, view->accelerator_count_map_[return_accelerator]); |
| 2178 } |
| 2179 #endif |
| 2180 |
| 2104 // TODO: these tests were initially commented out when getting aura to | 2181 // TODO: these tests were initially commented out when getting aura to |
| 2105 // run. Figure out if still valuable and either nuke or fix. | 2182 // run. Figure out if still valuable and either nuke or fix. |
| 2106 #if 0 | 2183 #if 0 |
| 2107 TEST_F(ViewTest, ActivateAccelerator) { | 2184 TEST_F(ViewTest, ActivateAccelerator) { |
| 2108 // Register a keyboard accelerator before the view is added to a window. | 2185 // Register a keyboard accelerator before the view is added to a window. |
| 2109 ui::Accelerator return_accelerator(ui::VKEY_RETURN, ui::EF_NONE); | 2186 ui::Accelerator return_accelerator(ui::VKEY_RETURN, ui::EF_NONE); |
| 2110 TestView* view = new TestView(); | 2187 TestView* view = new TestView(); |
| 2111 view->Reset(); | 2188 view->Reset(); |
| 2112 view->AddAccelerator(return_accelerator); | 2189 view->AddAccelerator(return_accelerator); |
| 2113 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 0); | 2190 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 0); |
| (...skipping 2283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4397 | 4474 |
| 4398 // The View should continue receiving events after the |handler| is deleted. | 4475 // The View should continue receiving events after the |handler| is deleted. |
| 4399 v->Reset(); | 4476 v->Reset(); |
| 4400 ui::MouseEvent released(ui::ET_MOUSE_RELEASED, gfx::Point(), gfx::Point(), | 4477 ui::MouseEvent released(ui::ET_MOUSE_RELEASED, gfx::Point(), gfx::Point(), |
| 4401 ui::EventTimeForNow(), 0, 0); | 4478 ui::EventTimeForNow(), 0, 0); |
| 4402 root->OnMouseReleased(released); | 4479 root->OnMouseReleased(released); |
| 4403 EXPECT_EQ(ui::ET_MOUSE_RELEASED, v->last_mouse_event_type_); | 4480 EXPECT_EQ(ui::ET_MOUSE_RELEASED, v->last_mouse_event_type_); |
| 4404 } | 4481 } |
| 4405 | 4482 |
| 4406 } // namespace views | 4483 } // namespace views |
| OLD | NEW |