Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1002)

Side by Side Diff: ui/views/widget/widget_interactive_uitest.cc

Issue 2090003002: MacViews: Explicitly release mouse capture in BridgedNativeWidget::OnWindowWillClose(). (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase. Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/views/cocoa/bridged_native_widget.mm ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <stddef.h> 5 #include <stddef.h>
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/command_line.h" 8 #include "base/command_line.h"
9 #include "base/location.h" 9 #include "base/location.h"
10 #include "base/macros.h" 10 #include "base/macros.h"
(...skipping 1246 matching lines...) Expand 10 before | Expand all | Expand 10 after
1257 1257
1258 Widget* widget = delegate.GetWidget(); 1258 Widget* widget = delegate.GetWidget();
1259 ShowSync(widget); 1259 ShowSync(widget);
1260 widget->Show(); 1260 widget->Show();
1261 EXPECT_TRUE(delegate.view()->HasFocus()); 1261 EXPECT_TRUE(delegate.view()->HasFocus());
1262 EXPECT_EQ(delegate.view(), widget->GetFocusManager()->GetStoredFocusView()); 1262 EXPECT_EQ(delegate.view(), widget->GetFocusManager()->GetStoredFocusView());
1263 } 1263 }
1264 1264
1265 namespace { 1265 namespace {
1266 1266
1267 // Used to verify OnMouseCaptureLost() has been invoked. 1267 // Helper class for CaptureLostTrackingWidget to store whether
1268 class CaptureLostTrackingWidget : public Widget { 1268 // OnMouseCaptureLost has been invoked for a widget.
1269 class CaptureLostState {
1269 public: 1270 public:
1270 CaptureLostTrackingWidget() : got_capture_lost_(false) {} 1271 CaptureLostState() : got_capture_lost_(false) {}
1271 ~CaptureLostTrackingWidget() override {}
1272 1272
1273 bool GetAndClearGotCaptureLost() { 1273 bool GetAndClearGotCaptureLost() {
1274 bool value = got_capture_lost_; 1274 bool value = got_capture_lost_;
1275 got_capture_lost_ = false; 1275 got_capture_lost_ = false;
1276 return value; 1276 return value;
1277 } 1277 }
1278 1278
1279 void OnMouseCaptureLost() { got_capture_lost_ = true; }
1280
1281 private:
1282 bool got_capture_lost_;
1283
1284 DISALLOW_COPY_AND_ASSIGN(CaptureLostState);
1285 };
1286
1287 // Used to verify OnMouseCaptureLost() has been invoked.
1288 class CaptureLostTrackingWidget : public Widget {
1289 public:
1290 explicit CaptureLostTrackingWidget(CaptureLostState* capture_lost_state)
1291 : capture_lost_state_(capture_lost_state) {}
1292
1279 // Widget: 1293 // Widget:
1280 void OnMouseCaptureLost() override { 1294 void OnMouseCaptureLost() override {
1281 got_capture_lost_ = true; 1295 capture_lost_state_->OnMouseCaptureLost();
1282 Widget::OnMouseCaptureLost(); 1296 Widget::OnMouseCaptureLost();
1283 } 1297 }
1284 1298
1285 private: 1299 private:
1286 bool got_capture_lost_; 1300 // Weak. Stores whether OnMouseCaptureLost has been invoked for this widget.
1301 CaptureLostState* capture_lost_state_;
1287 1302
1288 DISALLOW_COPY_AND_ASSIGN(CaptureLostTrackingWidget); 1303 DISALLOW_COPY_AND_ASSIGN(CaptureLostTrackingWidget);
1289 }; 1304 };
1290 1305
1291 } // namespace 1306 } // namespace
1292 1307
1293 class WidgetCaptureTest : public ViewsTestBase { 1308 class WidgetCaptureTest : public ViewsTestBase {
1294 public: 1309 public:
1295 WidgetCaptureTest() { 1310 WidgetCaptureTest() {
1296 } 1311 }
1297 1312
1298 ~WidgetCaptureTest() override {} 1313 ~WidgetCaptureTest() override {}
1299 1314
1300 void SetUp() override { 1315 void SetUp() override {
1301 // On mus these tests run as part of views::ViewsTestSuite which already 1316 // On mus these tests run as part of views::ViewsTestSuite which already
1302 // does this initialization. 1317 // does this initialization.
1303 if (!IsMus()) { 1318 if (!IsMus()) {
1304 gl::GLSurfaceTestSupport::InitializeOneOff(); 1319 gl::GLSurfaceTestSupport::InitializeOneOff();
1305 ui::RegisterPathProvider(); 1320 ui::RegisterPathProvider();
1306 base::FilePath ui_test_pak_path; 1321 base::FilePath ui_test_pak_path;
1307 ASSERT_TRUE(PathService::Get(ui::UI_TEST_PAK, &ui_test_pak_path)); 1322 ASSERT_TRUE(PathService::Get(ui::UI_TEST_PAK, &ui_test_pak_path));
1308 ui::ResourceBundle::InitSharedInstanceWithPakPath(ui_test_pak_path); 1323 ui::ResourceBundle::InitSharedInstanceWithPakPath(ui_test_pak_path);
1309 } 1324 }
1310 ViewsTestBase::SetUp(); 1325 ViewsTestBase::SetUp();
1311 } 1326 }
1312 1327
1313 // Verifies Widget::SetCapture() results in updating native capture along with 1328 // Verifies Widget::SetCapture() results in updating native capture along with
1314 // invoking the right Widget function. 1329 // invoking the right Widget function.
1315 void TestCapture(bool use_desktop_native_widget) { 1330 void TestCapture(bool use_desktop_native_widget) {
1316 CaptureLostTrackingWidget widget1; 1331 CaptureLostState capture_state1;
1332 CaptureLostTrackingWidget widget1(&capture_state1);
1317 Widget::InitParams params1 = 1333 Widget::InitParams params1 =
1318 CreateParams(views::Widget::InitParams::TYPE_WINDOW); 1334 CreateParams(views::Widget::InitParams::TYPE_WINDOW);
1319 params1.native_widget = 1335 params1.native_widget =
1320 CreateNativeWidget(params1, use_desktop_native_widget, &widget1); 1336 CreateNativeWidget(params1, use_desktop_native_widget, &widget1);
1321 params1.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; 1337 params1.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
1322 widget1.Init(params1); 1338 widget1.Init(params1);
1323 widget1.Show(); 1339 widget1.Show();
1324 1340
1325 CaptureLostTrackingWidget widget2; 1341 CaptureLostState capture_state2;
1342 CaptureLostTrackingWidget widget2(&capture_state2);
1326 Widget::InitParams params2 = 1343 Widget::InitParams params2 =
1327 CreateParams(views::Widget::InitParams::TYPE_WINDOW); 1344 CreateParams(views::Widget::InitParams::TYPE_WINDOW);
1328 params2.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; 1345 params2.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
1329 params2.native_widget = 1346 params2.native_widget =
1330 CreateNativeWidget(params2, use_desktop_native_widget, &widget2); 1347 CreateNativeWidget(params2, use_desktop_native_widget, &widget2);
1331 widget2.Init(params2); 1348 widget2.Init(params2);
1332 widget2.Show(); 1349 widget2.Show();
1333 1350
1334 // Set capture to widget2 and verity it gets it. 1351 // Set capture to widget2 and verity it gets it.
1335 widget2.SetCapture(widget2.GetRootView()); 1352 widget2.SetCapture(widget2.GetRootView());
1336 EXPECT_FALSE(widget1.HasCapture()); 1353 EXPECT_FALSE(widget1.HasCapture());
1337 EXPECT_TRUE(widget2.HasCapture()); 1354 EXPECT_TRUE(widget2.HasCapture());
1338 EXPECT_FALSE(widget1.GetAndClearGotCaptureLost()); 1355 EXPECT_FALSE(capture_state1.GetAndClearGotCaptureLost());
1339 EXPECT_FALSE(widget2.GetAndClearGotCaptureLost()); 1356 EXPECT_FALSE(capture_state2.GetAndClearGotCaptureLost());
1340 1357
1341 // Set capture to widget1 and verify it gets it. 1358 // Set capture to widget1 and verify it gets it.
1342 widget1.SetCapture(widget1.GetRootView()); 1359 widget1.SetCapture(widget1.GetRootView());
1343 EXPECT_TRUE(widget1.HasCapture()); 1360 EXPECT_TRUE(widget1.HasCapture());
1344 EXPECT_FALSE(widget2.HasCapture()); 1361 EXPECT_FALSE(widget2.HasCapture());
1345 EXPECT_FALSE(widget1.GetAndClearGotCaptureLost()); 1362 EXPECT_FALSE(capture_state1.GetAndClearGotCaptureLost());
1346 EXPECT_TRUE(widget2.GetAndClearGotCaptureLost()); 1363 EXPECT_TRUE(capture_state2.GetAndClearGotCaptureLost());
1347 1364
1348 // Release and verify no one has it. 1365 // Release and verify no one has it.
1349 widget1.ReleaseCapture(); 1366 widget1.ReleaseCapture();
1350 EXPECT_FALSE(widget1.HasCapture()); 1367 EXPECT_FALSE(widget1.HasCapture());
1351 EXPECT_FALSE(widget2.HasCapture()); 1368 EXPECT_FALSE(widget2.HasCapture());
1352 EXPECT_TRUE(widget1.GetAndClearGotCaptureLost()); 1369 EXPECT_TRUE(capture_state1.GetAndClearGotCaptureLost());
1353 EXPECT_FALSE(widget2.GetAndClearGotCaptureLost()); 1370 EXPECT_FALSE(capture_state2.GetAndClearGotCaptureLost());
1354 } 1371 }
1355 1372
1356 NativeWidget* CreateNativeWidget(const Widget::InitParams& params, 1373 NativeWidget* CreateNativeWidget(const Widget::InitParams& params,
1357 bool create_desktop_native_widget, 1374 bool create_desktop_native_widget,
1358 Widget* widget) { 1375 Widget* widget) {
1359 if (create_desktop_native_widget) 1376 if (create_desktop_native_widget)
1360 return CreatePlatformDesktopNativeWidgetImpl(params, widget, nullptr); 1377 return CreatePlatformDesktopNativeWidgetImpl(params, widget, nullptr);
1361 return CreatePlatformNativeWidgetImpl(params, widget, kDefault, nullptr); 1378 return CreatePlatformNativeWidgetImpl(params, widget, kDefault, nullptr);
1362 } 1379 }
1363 1380
1364 private: 1381 private:
1365 DISALLOW_COPY_AND_ASSIGN(WidgetCaptureTest); 1382 DISALLOW_COPY_AND_ASSIGN(WidgetCaptureTest);
1366 }; 1383 };
1367 1384
1368 // See description in TestCapture(). 1385 // See description in TestCapture().
1369 TEST_F(WidgetCaptureTest, Capture) { 1386 TEST_F(WidgetCaptureTest, Capture) {
1370 TestCapture(false); 1387 TestCapture(false);
1371 } 1388 }
1372 1389
1390 // Tests to ensure capture is correctly released from a Widget with capture when
1391 // it is destroyed. Test for crbug.com/622201.
1392 TEST_F(WidgetCaptureTest, DestroyWithCapture_CloseNow) {
1393 CaptureLostState capture_state;
1394 CaptureLostTrackingWidget* widget =
1395 new CaptureLostTrackingWidget(&capture_state);
1396 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_WINDOW);
1397 params.native_widget = CreateNativeWidget(params, true, widget);
1398 widget->Init(params);
1399 widget->Show();
1400
1401 widget->SetCapture(widget->GetRootView());
1402 EXPECT_TRUE(widget->HasCapture());
1403 EXPECT_FALSE(capture_state.GetAndClearGotCaptureLost());
1404 widget->CloseNow();
1405
1406 // Fails on mus. http://crbug.com/611764
1407 if (IsMus())
1408 EXPECT_FALSE(capture_state.GetAndClearGotCaptureLost());
1409 else
1410 EXPECT_TRUE(capture_state.GetAndClearGotCaptureLost());
1411 }
1412
1413 TEST_F(WidgetCaptureTest, DestroyWithCapture_Close) {
1414 CaptureLostState capture_state;
1415 CaptureLostTrackingWidget* widget =
1416 new CaptureLostTrackingWidget(&capture_state);
1417 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_WINDOW);
1418 params.native_widget = CreateNativeWidget(params, true, widget);
1419 widget->Init(params);
1420 widget->Show();
1421
1422 widget->SetCapture(widget->GetRootView());
1423 EXPECT_TRUE(widget->HasCapture());
1424 EXPECT_FALSE(capture_state.GetAndClearGotCaptureLost());
1425 widget->Close();
1426 EXPECT_TRUE(capture_state.GetAndClearGotCaptureLost());
1427 }
1428
1429 TEST_F(WidgetCaptureTest, DestroyWithCapture_WidgetOwnsNativeWidget) {
1430 Widget widget;
1431 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_WINDOW);
1432 params.native_widget = CreateNativeWidget(params, true, &widget);
1433 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
1434 widget.Init(params);
1435 widget.Show();
1436
1437 widget.SetCapture(widget.GetRootView());
1438 EXPECT_TRUE(widget.HasCapture());
1439 }
1440
1373 #if !defined(OS_CHROMEOS) 1441 #if !defined(OS_CHROMEOS)
1374 // See description in TestCapture(). Creates DesktopNativeWidget. 1442 // See description in TestCapture(). Creates DesktopNativeWidget.
1375 TEST_F(WidgetCaptureTest, CaptureDesktopNativeWidget) { 1443 TEST_F(WidgetCaptureTest, CaptureDesktopNativeWidget) {
1376 // Fails on mus. http://crbug.com/611764 1444 // Fails on mus. http://crbug.com/611764
1377 if (IsMus()) 1445 if (IsMus())
1378 return; 1446 return;
1379 1447
1380 TestCapture(true); 1448 TestCapture(true);
1381 } 1449 }
1382 #endif 1450 #endif
(...skipping 408 matching lines...) Expand 10 before | Expand all | Expand 10 after
1791 1859
1792 ui::KeyEvent key_event2(key_event); 1860 ui::KeyEvent key_event2(key_event);
1793 widget->OnKeyEvent(&key_event2); 1861 widget->OnKeyEvent(&key_event2);
1794 EXPECT_FALSE(key_event2.stopped_propagation()); 1862 EXPECT_FALSE(key_event2.stopped_propagation());
1795 1863
1796 widget->CloseNow(); 1864 widget->CloseNow();
1797 } 1865 }
1798 1866
1799 } // namespace test 1867 } // namespace test
1800 } // namespace views 1868 } // namespace views
OLDNEW
« no previous file with comments | « ui/views/cocoa/bridged_native_widget.mm ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698