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 <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 Loading... | |
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 CaptureLostTrackingWidget(CaptureLostState* capture_lost_state) | |
tapted
2016/06/24 07:11:58
nit: explicit
karandeepb
2016/06/24 09:27:28
Done.
| |
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 // Fails on mus. http://crbug.com/611764 | |
tapted
2016/06/24 07:11:58
Can you add this to BUG= and give it a brief menti
karandeepb
2016/06/24 09:27:28
Looking at the Mus bug, It seems to me that the Mu
| |
1394 if (IsMus()) | |
1395 return; | |
1396 | |
1397 CaptureLostState capture_state; | |
1398 CaptureLostTrackingWidget* widget = | |
1399 new CaptureLostTrackingWidget(&capture_state); | |
1400 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_WINDOW); | |
1401 params.native_widget = CreateNativeWidget(params, true, widget); | |
1402 widget->Init(params); | |
1403 widget->Show(); | |
1404 | |
1405 widget->SetCapture(widget->GetRootView()); | |
1406 EXPECT_TRUE(widget->HasCapture()); | |
1407 EXPECT_FALSE(capture_state.GetAndClearGotCaptureLost()); | |
1408 widget->CloseNow(); | |
1409 EXPECT_TRUE(capture_state.GetAndClearGotCaptureLost()); | |
1410 } | |
1411 | |
1412 TEST_F(WidgetCaptureTest, DestroyWithCapture_Close) { | |
1413 CaptureLostState capture_state; | |
tapted
2016/06/24 07:11:59
ooh? so this one passes on mus - interesting (are
karandeepb
2016/06/24 09:27:28
The tests don't crash on Mus. They fail because Mu
tapted
2016/06/24 12:14:41
If there's no crash, it might be better to write t
karandeepb
2016/06/27 02:07:46
Done.
| |
1414 CaptureLostTrackingWidget* widget = | |
1415 new CaptureLostTrackingWidget(&capture_state); | |
1416 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_WINDOW); | |
1417 params.native_widget = CreateNativeWidget(params, true, widget); | |
1418 widget->Init(params); | |
1419 widget->Show(); | |
1420 | |
1421 widget->SetCapture(widget->GetRootView()); | |
1422 EXPECT_TRUE(widget->HasCapture()); | |
1423 EXPECT_FALSE(capture_state.GetAndClearGotCaptureLost()); | |
1424 widget->Close(); | |
1425 EXPECT_TRUE(capture_state.GetAndClearGotCaptureLost()); | |
1426 } | |
1427 | |
1428 TEST_F(WidgetCaptureTest, DestroyWithCapture_WidgetOwnsNativeWidget) { | |
1429 // Fails on mus. http://crbug.com/611764 | |
1430 if (IsMus()) | |
1431 return; | |
1432 | |
1433 CaptureLostState capture_state; | |
1434 CaptureLostTrackingWidget widget(&capture_state); | |
1435 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_WINDOW); | |
1436 params.native_widget = CreateNativeWidget(params, true, &widget); | |
1437 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | |
tapted
2016/06/24 07:11:59
it would be nice to avoid some of this boilerplate
karandeepb
2016/06/24 09:27:28
Yeah don't think it worth for just three tests. Co
| |
1438 widget.Init(params); | |
1439 widget.Show(); | |
1440 | |
1441 widget.SetCapture(widget.GetRootView()); | |
1442 EXPECT_TRUE(widget.HasCapture()); | |
1443 EXPECT_FALSE(capture_state.GetAndClearGotCaptureLost()); | |
1444 widget.CloseNow(); | |
tapted
2016/06/24 07:11:59
this should test |widget| being deleted / going ou
karandeepb
2016/06/24 09:27:28
Done. But then we won't be able to detect if Widge
tapted
2016/06/24 12:14:41
Ah, yeah that's a good point :/. But I think there
| |
1445 EXPECT_TRUE(capture_state.GetAndClearGotCaptureLost()); | |
1446 } | |
1447 | |
1373 #if !defined(OS_CHROMEOS) | 1448 #if !defined(OS_CHROMEOS) |
1374 // See description in TestCapture(). Creates DesktopNativeWidget. | 1449 // See description in TestCapture(). Creates DesktopNativeWidget. |
1375 TEST_F(WidgetCaptureTest, CaptureDesktopNativeWidget) { | 1450 TEST_F(WidgetCaptureTest, CaptureDesktopNativeWidget) { |
1376 // Fails on mus. http://crbug.com/611764 | 1451 // Fails on mus. http://crbug.com/611764 |
1377 if (IsMus()) | 1452 if (IsMus()) |
1378 return; | 1453 return; |
1379 | 1454 |
1380 TestCapture(true); | 1455 TestCapture(true); |
1381 } | 1456 } |
1382 #endif | 1457 #endif |
(...skipping 408 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1791 | 1866 |
1792 ui::KeyEvent key_event2(key_event); | 1867 ui::KeyEvent key_event2(key_event); |
1793 widget->OnKeyEvent(&key_event2); | 1868 widget->OnKeyEvent(&key_event2); |
1794 EXPECT_FALSE(key_event2.stopped_propagation()); | 1869 EXPECT_FALSE(key_event2.stopped_propagation()); |
1795 | 1870 |
1796 widget->CloseNow(); | 1871 widget->CloseNow(); |
1797 } | 1872 } |
1798 | 1873 |
1799 } // namespace test | 1874 } // namespace test |
1800 } // namespace views | 1875 } // namespace views |
OLD | NEW |