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 "ui/views/view.h" | 5 #include "ui/views/view.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <map> | 9 #include <map> |
10 #include <memory> | 10 #include <memory> |
(...skipping 2085 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2096 } | 2096 } |
2097 | 2097 |
2098 //////////////////////////////////////////////////////////////////////////////// | 2098 //////////////////////////////////////////////////////////////////////////////// |
2099 // Accelerators | 2099 // Accelerators |
2100 //////////////////////////////////////////////////////////////////////////////// | 2100 //////////////////////////////////////////////////////////////////////////////// |
2101 bool TestView::AcceleratorPressed(const ui::Accelerator& accelerator) { | 2101 bool TestView::AcceleratorPressed(const ui::Accelerator& accelerator) { |
2102 accelerator_count_map_[accelerator]++; | 2102 accelerator_count_map_[accelerator]++; |
2103 return true; | 2103 return true; |
2104 } | 2104 } |
2105 | 2105 |
| 2106 namespace { |
| 2107 |
| 2108 class TestViewWidget { |
| 2109 public: |
| 2110 TestViewWidget(const Widget::InitParams& create_params, |
| 2111 ui::Accelerator* initial_accelerator, |
| 2112 bool show_after_init = true) |
| 2113 : view_(new TestView) { |
| 2114 view_->Reset(); |
| 2115 |
| 2116 // Register a keyboard accelerator before the view is added to a window. |
| 2117 if (initial_accelerator) { |
| 2118 view_->AddAccelerator(*initial_accelerator); |
| 2119 EXPECT_EQ(view_->accelerator_count_map_[*initial_accelerator], 0); |
| 2120 } |
| 2121 |
| 2122 // Create a window and add the view as its child. |
| 2123 Widget::InitParams params = create_params; |
| 2124 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 2125 params.bounds = gfx::Rect(0, 0, 100, 100); |
| 2126 widget_.Init(params); |
| 2127 View* root = widget_.GetRootView(); |
| 2128 root->AddChildView(view_); |
| 2129 if (show_after_init) |
| 2130 widget_.Show(); |
| 2131 |
| 2132 EXPECT_TRUE(widget_.GetFocusManager()); |
| 2133 } |
| 2134 |
| 2135 TestView* view() { return view_; } |
| 2136 Widget* widget() { return &widget_; } |
| 2137 |
| 2138 private: |
| 2139 TestView* view_; |
| 2140 Widget widget_; |
| 2141 |
| 2142 DISALLOW_COPY_AND_ASSIGN(TestViewWidget); |
| 2143 }; |
| 2144 |
| 2145 } // namespace |
| 2146 |
2106 // On non-ChromeOS aura there is extra logic to determine whether a view should | 2147 // On non-ChromeOS aura there is extra logic to determine whether a view should |
2107 // handle accelerators or not (see View::CanHandleAccelerators for details). | 2148 // handle accelerators or not (see View::CanHandleAccelerators for details). |
2108 // This test targets that extra logic, but should also work on other platforms. | 2149 // This test targets that extra logic, but should also work on other platforms. |
2109 TEST_F(ViewTest, HandleAccelerator) { | 2150 TEST_F(ViewTest, HandleAccelerator) { |
2110 ui::Accelerator return_accelerator(ui::VKEY_RETURN, ui::EF_NONE); | 2151 ui::Accelerator return_accelerator(ui::VKEY_RETURN, ui::EF_NONE); |
2111 TestView* view = new TestView(); | 2152 TestViewWidget test_widget(CreateParams(Widget::InitParams::TYPE_POPUP), |
2112 view->Reset(); | 2153 &return_accelerator); |
2113 view->AddAccelerator(return_accelerator); | 2154 TestView* view = test_widget.view(); |
2114 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 0); | 2155 Widget* widget = test_widget.widget(); |
2115 | |
2116 // Create a window and add the view as its child. | |
2117 std::unique_ptr<Widget> widget(new Widget); | |
2118 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); | |
2119 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | |
2120 params.bounds = gfx::Rect(0, 0, 100, 100); | |
2121 widget->Init(params); | |
2122 View* root = widget->GetRootView(); | |
2123 root->AddChildView(view); | |
2124 widget->Show(); | |
2125 | |
2126 FocusManager* focus_manager = widget->GetFocusManager(); | 2156 FocusManager* focus_manager = widget->GetFocusManager(); |
2127 ASSERT_TRUE(focus_manager); | |
2128 | 2157 |
2129 #if defined(USE_AURA) && !defined(OS_CHROMEOS) | 2158 #if defined(USE_AURA) && !defined(OS_CHROMEOS) |
2130 // When a non-child view is not active, it shouldn't handle accelerators. | 2159 // When a non-child view is not active, it shouldn't handle accelerators. |
2131 EXPECT_FALSE(widget->IsActive()); | 2160 EXPECT_FALSE(widget->IsActive()); |
2132 EXPECT_FALSE(focus_manager->ProcessAccelerator(return_accelerator)); | 2161 EXPECT_FALSE(focus_manager->ProcessAccelerator(return_accelerator)); |
2133 EXPECT_EQ(0, view->accelerator_count_map_[return_accelerator]); | 2162 EXPECT_EQ(0, view->accelerator_count_map_[return_accelerator]); |
2134 #endif | 2163 #endif |
2135 | 2164 |
2136 // TYPE_POPUP widgets default to non-activatable, so the Show() above wouldn't | 2165 // TYPE_POPUP widgets default to non-activatable, so the Show() above wouldn't |
2137 // have activated the Widget. First, allow activation. | 2166 // have activated the Widget. First, allow activation. |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2174 child_view->accelerator_count_map_[return_accelerator] = 0; | 2203 child_view->accelerator_count_map_[return_accelerator] = 0; |
2175 view->accelerator_count_map_[return_accelerator] = 0; | 2204 view->accelerator_count_map_[return_accelerator] = 0; |
2176 child_focus_manager->ClearFocus(); | 2205 child_focus_manager->ClearFocus(); |
2177 EXPECT_FALSE(child_view->GetWidget()->IsActive()); | 2206 EXPECT_FALSE(child_view->GetWidget()->IsActive()); |
2178 EXPECT_TRUE(child_focus_manager->ProcessAccelerator(return_accelerator)); | 2207 EXPECT_TRUE(child_focus_manager->ProcessAccelerator(return_accelerator)); |
2179 EXPECT_EQ(0, child_view->accelerator_count_map_[return_accelerator]); | 2208 EXPECT_EQ(0, child_view->accelerator_count_map_[return_accelerator]); |
2180 EXPECT_EQ(1, view->accelerator_count_map_[return_accelerator]); | 2209 EXPECT_EQ(1, view->accelerator_count_map_[return_accelerator]); |
2181 #endif | 2210 #endif |
2182 } | 2211 } |
2183 | 2212 |
2184 // TODO: these tests were initially commented out when getting aura to | |
2185 // run. Figure out if still valuable and either nuke or fix. | |
2186 #if 0 | |
2187 TEST_F(ViewTest, ActivateAccelerator) { | 2213 TEST_F(ViewTest, ActivateAccelerator) { |
2188 // Register a keyboard accelerator before the view is added to a window. | |
2189 ui::Accelerator return_accelerator(ui::VKEY_RETURN, ui::EF_NONE); | 2214 ui::Accelerator return_accelerator(ui::VKEY_RETURN, ui::EF_NONE); |
2190 TestView* view = new TestView(); | 2215 TestViewWidget test_widget(CreateParams(Widget::InitParams::TYPE_POPUP), |
2191 view->Reset(); | 2216 &return_accelerator); |
2192 view->AddAccelerator(return_accelerator); | 2217 TestView* view = test_widget.view(); |
2193 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 0); | 2218 FocusManager* focus_manager = test_widget.widget()->GetFocusManager(); |
2194 | |
2195 // Create a window and add the view as its child. | |
2196 std::unique_ptr<Widget> widget(new Widget); | |
2197 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); | |
2198 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | |
2199 params.bounds = gfx::Rect(0, 0, 100, 100); | |
2200 widget->Init(params); | |
2201 View* root = widget->GetRootView(); | |
2202 root->AddChildView(view); | |
2203 widget->Show(); | |
2204 | |
2205 // Get the focus manager. | |
2206 FocusManager* focus_manager = widget->GetFocusManager(); | |
2207 ASSERT_TRUE(focus_manager); | |
2208 | 2219 |
2209 // Hit the return key and see if it takes effect. | 2220 // Hit the return key and see if it takes effect. |
2210 EXPECT_TRUE(focus_manager->ProcessAccelerator(return_accelerator)); | 2221 EXPECT_TRUE(focus_manager->ProcessAccelerator(return_accelerator)); |
2211 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 1); | 2222 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 1); |
2212 | 2223 |
2213 // Hit the escape key. Nothing should happen. | 2224 // Hit the escape key. Nothing should happen. |
2214 ui::Accelerator escape_accelerator(ui::VKEY_ESCAPE, ui::EF_NONE); | 2225 ui::Accelerator escape_accelerator(ui::VKEY_ESCAPE, ui::EF_NONE); |
2215 EXPECT_FALSE(focus_manager->ProcessAccelerator(escape_accelerator)); | 2226 EXPECT_FALSE(focus_manager->ProcessAccelerator(escape_accelerator)); |
2216 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 1); | 2227 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 1); |
2217 EXPECT_EQ(view->accelerator_count_map_[escape_accelerator], 0); | 2228 EXPECT_EQ(view->accelerator_count_map_[escape_accelerator], 0); |
(...skipping 20 matching lines...) Expand all Loading... |
2238 EXPECT_EQ(view->accelerator_count_map_[escape_accelerator], 2); | 2249 EXPECT_EQ(view->accelerator_count_map_[escape_accelerator], 2); |
2239 | 2250 |
2240 // Remove all the accelerators. | 2251 // Remove all the accelerators. |
2241 view->ResetAccelerators(); | 2252 view->ResetAccelerators(); |
2242 EXPECT_FALSE(focus_manager->ProcessAccelerator(return_accelerator)); | 2253 EXPECT_FALSE(focus_manager->ProcessAccelerator(return_accelerator)); |
2243 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 2); | 2254 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 2); |
2244 EXPECT_EQ(view->accelerator_count_map_[escape_accelerator], 2); | 2255 EXPECT_EQ(view->accelerator_count_map_[escape_accelerator], 2); |
2245 EXPECT_FALSE(focus_manager->ProcessAccelerator(escape_accelerator)); | 2256 EXPECT_FALSE(focus_manager->ProcessAccelerator(escape_accelerator)); |
2246 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 2); | 2257 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 2); |
2247 EXPECT_EQ(view->accelerator_count_map_[escape_accelerator], 2); | 2258 EXPECT_EQ(view->accelerator_count_map_[escape_accelerator], 2); |
2248 | |
2249 widget->CloseNow(); | |
2250 } | 2259 } |
2251 | 2260 |
2252 TEST_F(ViewTest, HiddenViewWithAccelerator) { | 2261 TEST_F(ViewTest, HiddenViewWithAccelerator) { |
2253 ui::Accelerator return_accelerator(ui::VKEY_RETURN, ui::EF_NONE); | 2262 ui::Accelerator return_accelerator(ui::VKEY_RETURN, ui::EF_NONE); |
2254 TestView* view = new TestView(); | 2263 TestViewWidget test_widget(CreateParams(Widget::InitParams::TYPE_POPUP), |
2255 view->Reset(); | 2264 &return_accelerator); |
2256 view->AddAccelerator(return_accelerator); | 2265 TestView* view = test_widget.view(); |
2257 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 0); | 2266 FocusManager* focus_manager = test_widget.widget()->GetFocusManager(); |
2258 | |
2259 std::unique_ptr<Widget> widget(new Widget); | |
2260 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); | |
2261 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | |
2262 params.bounds = gfx::Rect(0, 0, 100, 100); | |
2263 widget->Init(params); | |
2264 View* root = widget->GetRootView(); | |
2265 root->AddChildView(view); | |
2266 widget->Show(); | |
2267 | |
2268 FocusManager* focus_manager = widget->GetFocusManager(); | |
2269 ASSERT_TRUE(focus_manager); | |
2270 | 2267 |
2271 view->SetVisible(false); | 2268 view->SetVisible(false); |
2272 EXPECT_FALSE(focus_manager->ProcessAccelerator(return_accelerator)); | 2269 EXPECT_FALSE(focus_manager->ProcessAccelerator(return_accelerator)); |
2273 | 2270 |
2274 view->SetVisible(true); | 2271 view->SetVisible(true); |
2275 EXPECT_TRUE(focus_manager->ProcessAccelerator(return_accelerator)); | 2272 EXPECT_TRUE(focus_manager->ProcessAccelerator(return_accelerator)); |
2276 | |
2277 widget->CloseNow(); | |
2278 } | 2273 } |
2279 | 2274 |
2280 TEST_F(ViewTest, ViewInHiddenWidgetWithAccelerator) { | 2275 TEST_F(ViewTest, ViewInHiddenWidgetWithAccelerator) { |
2281 ui::Accelerator return_accelerator(ui::VKEY_RETURN, ui::EF_NONE); | 2276 ui::Accelerator return_accelerator(ui::VKEY_RETURN, ui::EF_NONE); |
2282 TestView* view = new TestView(); | 2277 TestViewWidget test_widget(CreateParams(Widget::InitParams::TYPE_POPUP), |
2283 view->Reset(); | 2278 &return_accelerator, false); |
2284 view->AddAccelerator(return_accelerator); | 2279 TestView* view = test_widget.view(); |
2285 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 0); | 2280 Widget* widget = test_widget.widget(); |
2286 | 2281 FocusManager* focus_manager = test_widget.widget()->GetFocusManager(); |
2287 std::unique_ptr<Widget> widget(new Widget); | |
2288 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); | |
2289 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | |
2290 params.bounds = gfx::Rect(0, 0, 100, 100); | |
2291 widget->Init(params); | |
2292 View* root = widget->GetRootView(); | |
2293 root->AddChildView(view); | |
2294 | |
2295 FocusManager* focus_manager = widget->GetFocusManager(); | |
2296 ASSERT_TRUE(focus_manager); | |
2297 | 2282 |
2298 EXPECT_FALSE(focus_manager->ProcessAccelerator(return_accelerator)); | 2283 EXPECT_FALSE(focus_manager->ProcessAccelerator(return_accelerator)); |
2299 EXPECT_EQ(0, view->accelerator_count_map_[return_accelerator]); | 2284 EXPECT_EQ(0, view->accelerator_count_map_[return_accelerator]); |
2300 | 2285 |
2301 widget->Show(); | 2286 widget->Show(); |
2302 EXPECT_TRUE(focus_manager->ProcessAccelerator(return_accelerator)); | 2287 EXPECT_TRUE(focus_manager->ProcessAccelerator(return_accelerator)); |
2303 EXPECT_EQ(1, view->accelerator_count_map_[return_accelerator]); | 2288 EXPECT_EQ(1, view->accelerator_count_map_[return_accelerator]); |
2304 | 2289 |
2305 widget->Hide(); | 2290 widget->Hide(); |
2306 EXPECT_FALSE(focus_manager->ProcessAccelerator(return_accelerator)); | 2291 EXPECT_FALSE(focus_manager->ProcessAccelerator(return_accelerator)); |
2307 EXPECT_EQ(1, view->accelerator_count_map_[return_accelerator]); | 2292 EXPECT_EQ(1, view->accelerator_count_map_[return_accelerator]); |
| 2293 } |
2308 | 2294 |
2309 widget->CloseNow(); | 2295 #if 0 |
2310 } | |
2311 | 2296 |
2312 //////////////////////////////////////////////////////////////////////////////// | 2297 //////////////////////////////////////////////////////////////////////////////// |
2313 // Mouse-wheel message rerouting | 2298 // Mouse-wheel message rerouting |
2314 //////////////////////////////////////////////////////////////////////////////// | 2299 //////////////////////////////////////////////////////////////////////////////// |
2315 class ScrollableTestView : public View { | 2300 class ScrollableTestView : public View { |
2316 public: | 2301 public: |
2317 ScrollableTestView() { } | 2302 ScrollableTestView() { } |
2318 | 2303 |
2319 virtual gfx::Size GetPreferredSize() { | 2304 virtual gfx::Size GetPreferredSize() { |
2320 return gfx::Size(100, 10000); | 2305 return gfx::Size(100, 10000); |
(...skipping 2250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4571 widget.Init(params); | 4556 widget.Init(params); |
4572 | 4557 |
4573 AddViewWithChildLayer(widget.GetRootView()); | 4558 AddViewWithChildLayer(widget.GetRootView()); |
4574 ViewThatAddsViewInOnNativeThemeChanged* v = | 4559 ViewThatAddsViewInOnNativeThemeChanged* v = |
4575 new ViewThatAddsViewInOnNativeThemeChanged; | 4560 new ViewThatAddsViewInOnNativeThemeChanged; |
4576 widget.GetRootView()->AddChildView(v); | 4561 widget.GetRootView()->AddChildView(v); |
4577 EXPECT_TRUE(v->on_native_theme_changed_called()); | 4562 EXPECT_TRUE(v->on_native_theme_changed_called()); |
4578 } | 4563 } |
4579 | 4564 |
4580 } // namespace views | 4565 } // namespace views |
OLD | NEW |