| 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 <map> | 5 #include <map> |
| 6 | 6 |
| 7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/rand_util.h" | 8 #include "base/rand_util.h" |
| 9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| (...skipping 1040 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1051 EXPECT_EQ(v2, root_view->GetTooltipHandlerForPoint(v2_centerpoint)); | 1051 EXPECT_EQ(v2, root_view->GetTooltipHandlerForPoint(v2_centerpoint)); |
| 1052 | 1052 |
| 1053 EXPECT_EQ(v1, root_view->GetTooltipHandlerForPoint(v1_origin)); | 1053 EXPECT_EQ(v1, root_view->GetTooltipHandlerForPoint(v1_origin)); |
| 1054 EXPECT_EQ(root_view, root_view->GetTooltipHandlerForPoint(v2_origin)); | 1054 EXPECT_EQ(root_view, root_view->GetTooltipHandlerForPoint(v2_origin)); |
| 1055 | 1055 |
| 1056 EXPECT_FALSE(v1->GetTooltipHandlerForPoint(v2_origin)); | 1056 EXPECT_FALSE(v1->GetTooltipHandlerForPoint(v2_origin)); |
| 1057 | 1057 |
| 1058 widget->CloseNow(); | 1058 widget->CloseNow(); |
| 1059 } | 1059 } |
| 1060 | 1060 |
| 1061 // Tests the correctness of the rect-based targeting algorithm implemented in |
| 1062 // View::GetEventHandlerForRect(). See http://goo.gl/3Jp2BD for a description |
| 1063 // of rect-based targeting. |
| 1061 TEST_F(ViewTest, GetEventHandlerForRect) { | 1064 TEST_F(ViewTest, GetEventHandlerForRect) { |
| 1062 Widget* widget = new Widget; | 1065 Widget* widget = new Widget; |
| 1063 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); | 1066 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); |
| 1064 widget->Init(params); | 1067 widget->Init(params); |
| 1065 View* root_view = widget->GetRootView(); | 1068 View* root_view = widget->GetRootView(); |
| 1066 root_view->SetBoundsRect(gfx::Rect(0, 0, 500, 500)); | 1069 root_view->SetBoundsRect(gfx::Rect(0, 0, 500, 500)); |
| 1067 | 1070 |
| 1068 // Have this hierarchy of views (the coordinates here are all in | 1071 // Have this hierarchy of views (the coordinates here are all in |
| 1069 // the root view's coordinate space): | 1072 // the root view's coordinate space): |
| 1070 // v1 (0, 0, 100, 100) | 1073 // v1 (0, 0, 100, 100) |
| 1071 // v2 (150, 0, 250, 100) | 1074 // v2 (150, 0, 250, 100) |
| 1072 // v3 (0, 200, 150, 100) | 1075 // v3 (0, 200, 150, 100) |
| 1073 // v31 (10, 210, 80, 80) | 1076 // v31 (10, 210, 80, 80) |
| 1074 // v32 (110, 210, 30, 80) | 1077 // v32 (110, 210, 30, 80) |
| 1075 // v4 (300, 200, 100, 100) | 1078 // v4 (300, 200, 100, 100) |
| 1076 // v41 (310, 210, 80, 80) | 1079 // v41 (310, 210, 80, 80) |
| 1077 // v411 (370, 275, 10, 5) | 1080 // v411 (370, 275, 10, 5) |
| 1081 // v5 (450, 197, 30, 36) |
| 1082 // v51 (450, 200, 30, 30) |
| 1078 | 1083 |
| 1079 // The coordinates used for SetBounds are in parent coordinates. | 1084 // The coordinates used for SetBounds are in parent coordinates. |
| 1080 | 1085 |
| 1081 TestView* v1 = new TestView; | 1086 TestView* v1 = new TestView; |
| 1082 v1->SetBounds(0, 0, 100, 100); | 1087 v1->SetBounds(0, 0, 100, 100); |
| 1083 root_view->AddChildView(v1); | 1088 root_view->AddChildView(v1); |
| 1084 | 1089 |
| 1085 TestView* v2 = new TestView; | 1090 TestView* v2 = new TestView; |
| 1086 v2->SetBounds(150, 0, 250, 100); | 1091 v2->SetBounds(150, 0, 250, 100); |
| 1087 root_view->AddChildView(v2); | 1092 root_view->AddChildView(v2); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 1103 v3->AddChildView(v32); | 1108 v3->AddChildView(v32); |
| 1104 | 1109 |
| 1105 TestView* v41 = new TestView; | 1110 TestView* v41 = new TestView; |
| 1106 v41->SetBounds(10, 10, 80, 80); | 1111 v41->SetBounds(10, 10, 80, 80); |
| 1107 v4->AddChildView(v41); | 1112 v4->AddChildView(v41); |
| 1108 | 1113 |
| 1109 TestView* v411 = new TestView; | 1114 TestView* v411 = new TestView; |
| 1110 v411->SetBounds(60, 65, 10, 5); | 1115 v411->SetBounds(60, 65, 10, 5); |
| 1111 v41->AddChildView(v411); | 1116 v41->AddChildView(v411); |
| 1112 | 1117 |
| 1118 TestView* v5 = new TestView; |
| 1119 v5->SetBounds(450, 197, 30, 36); |
| 1120 root_view->AddChildView(v5); |
| 1121 |
| 1122 TestView* v51 = new TestView; |
| 1123 v51->SetBounds(0, 3, 30, 30); |
| 1124 v5->AddChildView(v51); |
| 1125 |
| 1113 // |touch_rect| does not intersect any descendant view of |root_view|. | 1126 // |touch_rect| does not intersect any descendant view of |root_view|. |
| 1114 gfx::Rect touch_rect(105, 105, 30, 45); | 1127 gfx::Rect touch_rect(105, 105, 30, 45); |
| 1115 View* result_view = root_view->GetEventHandlerForRect(touch_rect); | 1128 View* result_view = root_view->GetEventHandlerForRect(touch_rect); |
| 1116 EXPECT_EQ(root_view, result_view); | 1129 EXPECT_EQ(root_view, result_view); |
| 1117 result_view = NULL; | 1130 result_view = NULL; |
| 1118 | 1131 |
| 1119 // Covers |v1| by at least 60%. | 1132 // Covers |v1| by at least 60%. |
| 1120 touch_rect.SetRect(15, 15, 100, 100); | 1133 touch_rect.SetRect(15, 15, 100, 100); |
| 1121 result_view = root_view->GetEventHandlerForRect(touch_rect); | 1134 result_view = root_view->GetEventHandlerForRect(touch_rect); |
| 1122 EXPECT_EQ(v1, result_view); | 1135 EXPECT_EQ(v1, result_view); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 1136 EXPECT_EQ(root_view, result_view); | 1149 EXPECT_EQ(root_view, result_view); |
| 1137 result_view = NULL; | 1150 result_view = NULL; |
| 1138 | 1151 |
| 1139 // Intersects |v1| and |v2|, but only covers |v2| by at least 60%. | 1152 // Intersects |v1| and |v2|, but only covers |v2| by at least 60%. |
| 1140 touch_rect.SetRect(95, 10, 300, 120); | 1153 touch_rect.SetRect(95, 10, 300, 120); |
| 1141 result_view = root_view->GetEventHandlerForRect(touch_rect); | 1154 result_view = root_view->GetEventHandlerForRect(touch_rect); |
| 1142 EXPECT_EQ(v2, result_view); | 1155 EXPECT_EQ(v2, result_view); |
| 1143 result_view = NULL; | 1156 result_view = NULL; |
| 1144 | 1157 |
| 1145 // Covers both |v1| and |v2| by at least 60%, but the center point | 1158 // Covers both |v1| and |v2| by at least 60%, but the center point |
| 1146 // of |touch_rect| is closer to the center line of |v2|. | 1159 // of |touch_rect| is closer to the center point of |v2|. |
| 1147 touch_rect.SetRect(20, 20, 400, 100); | 1160 touch_rect.SetRect(20, 20, 400, 100); |
| 1148 result_view = root_view->GetEventHandlerForRect(touch_rect); | 1161 result_view = root_view->GetEventHandlerForRect(touch_rect); |
| 1149 EXPECT_EQ(v2, result_view); | 1162 EXPECT_EQ(v2, result_view); |
| 1150 result_view = NULL; | 1163 result_view = NULL; |
| 1151 | 1164 |
| 1152 // Covers both |v1| and |v2| by at least 60%, but the center point | 1165 // Covers both |v1| and |v2| by at least 60%, but the center point |
| 1153 // of |touch_rect| is closer to the center line (point) of |v1|. | 1166 // of |touch_rect| is closer to the center point of |v1|. |
| 1154 touch_rect.SetRect(-700, -15, 1050, 110); | 1167 touch_rect.SetRect(-700, -15, 1050, 110); |
| 1155 result_view = root_view->GetEventHandlerForRect(touch_rect); | 1168 result_view = root_view->GetEventHandlerForRect(touch_rect); |
| 1156 EXPECT_EQ(v1, result_view); | 1169 EXPECT_EQ(v1, result_view); |
| 1157 result_view = NULL; | 1170 result_view = NULL; |
| 1158 | 1171 |
| 1159 // A mouse click within |v1| will target |v1|. | 1172 // A mouse click within |v1| will target |v1|. |
| 1160 touch_rect.SetRect(15, 15, 1, 1); | 1173 touch_rect.SetRect(15, 15, 1, 1); |
| 1161 result_view = root_view->GetEventHandlerForRect(touch_rect); | 1174 result_view = root_view->GetEventHandlerForRect(touch_rect); |
| 1162 EXPECT_EQ(v1, result_view); | 1175 EXPECT_EQ(v1, result_view); |
| 1163 result_view = NULL; | 1176 result_view = NULL; |
| 1164 | 1177 |
| 1165 // Intersects |v3| and |v31| by at least 60% and the center point | 1178 // Intersects |v3| and |v31| by at least 60% and the center point |
| 1166 // of |touch_rect| is closer to the center line of |v3|. | 1179 // of |touch_rect| is closer to the center point of |v31|. |
| 1167 touch_rect.SetRect(0, 200, 110, 100); | 1180 touch_rect.SetRect(0, 200, 110, 100); |
| 1168 result_view = root_view->GetEventHandlerForRect(touch_rect); | 1181 result_view = root_view->GetEventHandlerForRect(touch_rect); |
| 1169 EXPECT_EQ(v3, result_view); | |
| 1170 result_view = NULL; | |
| 1171 | |
| 1172 // Intersects |v3| and |v31| by at least 60% and the center point | |
| 1173 // of |touch_rect| is equally close to the center lines of both. | |
| 1174 touch_rect.SetRect(-60, 140, 200, 200); | |
| 1175 result_view = root_view->GetEventHandlerForRect(touch_rect); | |
| 1176 EXPECT_EQ(v31, result_view); | 1182 EXPECT_EQ(v31, result_view); |
| 1177 result_view = NULL; | 1183 result_view = NULL; |
| 1178 | 1184 |
| 1179 // Intersects |v3| and |v31|, but neither by at least 60%. The | 1185 // Intersects |v3| and |v31|, but neither by at least 60%. The |
| 1180 // center point of |touch_rect| lies within |v31|. | 1186 // center point of |touch_rect| lies within |v31|. |
| 1181 touch_rect.SetRect(80, 280, 15, 15); | 1187 touch_rect.SetRect(80, 280, 15, 15); |
| 1182 result_view = root_view->GetEventHandlerForRect(touch_rect); | 1188 result_view = root_view->GetEventHandlerForRect(touch_rect); |
| 1183 EXPECT_EQ(v31, result_view); | 1189 EXPECT_EQ(v31, result_view); |
| 1184 result_view = NULL; | 1190 result_view = NULL; |
| 1185 | 1191 |
| 1186 // Covers |v3|, |v31|, and |v32| all by at least 60%, and the | 1192 // Covers |v3|, |v31|, and |v32| all by at least 60%, and the |
| 1187 // center point of |touch_rect| is closest to the center line | 1193 // center point of |touch_rect| is closest to the center point |
| 1188 // of |v3|. | 1194 // of |v32|. |
| 1189 touch_rect.SetRect(0, 200, 200, 100); | 1195 touch_rect.SetRect(0, 200, 200, 100); |
| 1190 result_view = root_view->GetEventHandlerForRect(touch_rect); | 1196 result_view = root_view->GetEventHandlerForRect(touch_rect); |
| 1191 EXPECT_EQ(v3, result_view); | 1197 EXPECT_EQ(v32, result_view); |
| 1192 result_view = NULL; | 1198 result_view = NULL; |
| 1193 | 1199 |
| 1194 // Intersects all of |v3|, |v31|, and |v32|, but only covers | 1200 // Intersects all of |v3|, |v31|, and |v32|, but only covers |
| 1195 // |v31| and |v32| by at least 60%. The center point of | 1201 // |v31| and |v32| by at least 60%. The center point of |
| 1196 // |touch_rect| is closest to the center line of |v32|. | 1202 // |touch_rect| is closest to the center point of |v32|. |
| 1197 touch_rect.SetRect(30, 225, 180, 115); | 1203 touch_rect.SetRect(30, 225, 180, 115); |
| 1198 result_view = root_view->GetEventHandlerForRect(touch_rect); | 1204 result_view = root_view->GetEventHandlerForRect(touch_rect); |
| 1199 EXPECT_EQ(v32, result_view); | 1205 EXPECT_EQ(v32, result_view); |
| 1200 result_view = NULL; | 1206 result_view = NULL; |
| 1201 | 1207 |
| 1202 // A mouse click at the corner of |v3| will target |v3|. | 1208 // A mouse click at the corner of |v3| will target |v3|. |
| 1203 touch_rect.SetRect(0, 200, 1, 1); | 1209 touch_rect.SetRect(0, 200, 1, 1); |
| 1204 result_view = root_view->GetEventHandlerForRect(touch_rect); | 1210 result_view = root_view->GetEventHandlerForRect(touch_rect); |
| 1205 EXPECT_EQ(v3, result_view); | 1211 EXPECT_EQ(v3, result_view); |
| 1206 result_view = NULL; | 1212 result_view = NULL; |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1272 result_view = NULL; | 1278 result_view = NULL; |
| 1273 | 1279 |
| 1274 // A mouse click within |v41| will target |v41|. | 1280 // A mouse click within |v41| will target |v41|. |
| 1275 touch_rect.SetRect(350, 215, 1, 1); | 1281 touch_rect.SetRect(350, 215, 1, 1); |
| 1276 result_view = root_view->GetEventHandlerForRect(touch_rect); | 1282 result_view = root_view->GetEventHandlerForRect(touch_rect); |
| 1277 EXPECT_EQ(v41, result_view); | 1283 EXPECT_EQ(v41, result_view); |
| 1278 result_view = NULL; | 1284 result_view = NULL; |
| 1279 | 1285 |
| 1280 // Covers |v3|, |v4|, and all of their descendants by at | 1286 // Covers |v3|, |v4|, and all of their descendants by at |
| 1281 // least 60%. The center point of |touch_rect| is closest | 1287 // least 60%. The center point of |touch_rect| is closest |
| 1282 // to the center line of |v32|. | 1288 // to the center point of |v32|. |
| 1283 touch_rect.SetRect(0, 200, 400, 100); | 1289 touch_rect.SetRect(0, 200, 400, 100); |
| 1284 result_view = root_view->GetEventHandlerForRect(touch_rect); | 1290 result_view = root_view->GetEventHandlerForRect(touch_rect); |
| 1285 EXPECT_EQ(v32, result_view); | 1291 EXPECT_EQ(v32, result_view); |
| 1286 result_view = NULL; | 1292 result_view = NULL; |
| 1287 | 1293 |
| 1288 // Intersects all of |v2|, |v3|, |v32|, |v4|, |v41|, and |v411|. | 1294 // Intersects all of |v2|, |v3|, |v32|, |v4|, |v41|, and |v411|. |
| 1289 // Covers |v2|, |v32|, |v4|, |v41|, and |v411| by at least 60%. | 1295 // Covers |v2|, |v32|, |v4|, |v41|, and |v411| by at least 60%. |
| 1290 // The center point of |touch_rect| is closest to the center | 1296 // The center point of |touch_rect| is closest to the center |
| 1291 // point of |root_view|. | 1297 // point of |root_view|. |
| 1292 touch_rect.SetRect(110, 15, 375, 450); | 1298 touch_rect.SetRect(110, 15, 375, 450); |
| 1293 result_view = root_view->GetEventHandlerForRect(touch_rect); | 1299 result_view = root_view->GetEventHandlerForRect(touch_rect); |
| 1294 EXPECT_EQ(root_view, result_view); | 1300 EXPECT_EQ(root_view, result_view); |
| 1295 result_view = NULL; | 1301 result_view = NULL; |
| 1296 | 1302 |
| 1297 // Covers all views by at least 60%. The center point of | 1303 // Covers all views (except |v5| and |v51|) by at least 60%. The |
| 1298 // |touch_rect| is closest to the center line of |v2|. | 1304 // center point of |touch_rect| is equally close to the center |
| 1305 // points of |v2| and |v32|. One is not a descendant of the other, |
| 1306 // so in this case the view selected is arbitrary (i.e., |
| 1307 // it depends only on the ordering of nodes in the views |
| 1308 // hierarchy). |
| 1299 touch_rect.SetRect(0, 0, 400, 300); | 1309 touch_rect.SetRect(0, 0, 400, 300); |
| 1300 result_view = root_view->GetEventHandlerForRect(touch_rect); | 1310 result_view = root_view->GetEventHandlerForRect(touch_rect); |
| 1301 EXPECT_EQ(v2, result_view); | 1311 EXPECT_EQ(v32, result_view); |
| 1312 result_view = NULL; |
| 1313 |
| 1314 // Covers |v5| and |v51| by at least 60%, and the center point of |
| 1315 // the touch is located within both views. Since both views share |
| 1316 // the same center point, the child view should be selected. |
| 1317 touch_rect.SetRect(440, 190, 40, 40); |
| 1318 result_view = root_view->GetEventHandlerForRect(touch_rect); |
| 1319 EXPECT_EQ(v51, result_view); |
| 1320 result_view = NULL; |
| 1321 |
| 1322 // Covers |v5| and |v51| by at least 60%, but the center point of |
| 1323 // the touch is not located within either view. Since both views |
| 1324 // share the same center point, the child view should be selected. |
| 1325 touch_rect.SetRect(455, 187, 60, 60); |
| 1326 result_view = root_view->GetEventHandlerForRect(touch_rect); |
| 1327 EXPECT_EQ(v51, result_view); |
| 1328 result_view = NULL; |
| 1329 |
| 1330 // Covers neither |v5| nor |v51| by at least 60%, but the center |
| 1331 // of the touch is located within |v51|. |
| 1332 touch_rect.SetRect(450, 197, 10, 10); |
| 1333 result_view = root_view->GetEventHandlerForRect(touch_rect); |
| 1334 EXPECT_EQ(v51, result_view); |
| 1335 result_view = NULL; |
| 1336 |
| 1337 // Covers neither |v5| nor |v51| by at least 60% but intersects both. |
| 1338 // The center point is located outside of both views. |
| 1339 touch_rect.SetRect(433, 180, 24, 24); |
| 1340 result_view = root_view->GetEventHandlerForRect(touch_rect); |
| 1341 EXPECT_EQ(root_view, result_view); |
| 1342 result_view = NULL; |
| 1343 |
| 1344 // Only intersects |v5| but does not cover it by at least 60%. The |
| 1345 // center point of the touch region is located within |v5|. |
| 1346 touch_rect.SetRect(449, 196, 3, 3); |
| 1347 result_view = root_view->GetEventHandlerForRect(touch_rect); |
| 1348 EXPECT_EQ(v5, result_view); |
| 1349 result_view = NULL; |
| 1350 |
| 1351 // A mouse click within |v5| (but not |v51|) should target |v5|. |
| 1352 touch_rect.SetRect(462, 199, 1, 1); |
| 1353 result_view = root_view->GetEventHandlerForRect(touch_rect); |
| 1354 EXPECT_EQ(v5, result_view); |
| 1355 result_view = NULL; |
| 1356 |
| 1357 // A mouse click |v5| and |v51| should target the child view. |
| 1358 touch_rect.SetRect(452, 226, 1, 1); |
| 1359 result_view = root_view->GetEventHandlerForRect(touch_rect); |
| 1360 EXPECT_EQ(v51, result_view); |
| 1361 result_view = NULL; |
| 1362 |
| 1363 // A mouse click on the center of |v5| and |v51| should target |
| 1364 // the child view. |
| 1365 touch_rect.SetRect(465, 215, 1, 1); |
| 1366 result_view = root_view->GetEventHandlerForRect(touch_rect); |
| 1367 EXPECT_EQ(v51, result_view); |
| 1302 result_view = NULL; | 1368 result_view = NULL; |
| 1303 | 1369 |
| 1304 widget->CloseNow(); | 1370 widget->CloseNow(); |
| 1305 } | 1371 } |
| 1306 | 1372 |
| 1307 TEST_F(ViewTest, NotifyEnterExitOnChild) { | 1373 TEST_F(ViewTest, NotifyEnterExitOnChild) { |
| 1308 Widget* widget = new Widget; | 1374 Widget* widget = new Widget; |
| 1309 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); | 1375 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); |
| 1310 widget->Init(params); | 1376 widget->Init(params); |
| 1311 View* root_view = widget->GetRootView(); | 1377 View* root_view = widget->GetRootView(); |
| (...skipping 2196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3508 const std::vector<ui::Layer*>& child_layers_post = root_layer->children(); | 3574 const std::vector<ui::Layer*>& child_layers_post = root_layer->children(); |
| 3509 ASSERT_EQ(3u, child_layers_post.size()); | 3575 ASSERT_EQ(3u, child_layers_post.size()); |
| 3510 EXPECT_EQ(v1->layer(), child_layers_post[0]); | 3576 EXPECT_EQ(v1->layer(), child_layers_post[0]); |
| 3511 EXPECT_EQ(v2->layer(), child_layers_post[1]); | 3577 EXPECT_EQ(v2->layer(), child_layers_post[1]); |
| 3512 EXPECT_EQ(v1_old_layer, child_layers_post[2]); | 3578 EXPECT_EQ(v1_old_layer, child_layers_post[2]); |
| 3513 } | 3579 } |
| 3514 | 3580 |
| 3515 #endif // USE_AURA | 3581 #endif // USE_AURA |
| 3516 | 3582 |
| 3517 } // namespace views | 3583 } // namespace views |
| OLD | NEW |