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

Side by Side Diff: ui/views/view_unittest.cc

Issue 22891016: Add support for rect-based event targeting in views (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: patch for landing Created 7 years, 1 month 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 | Annotate | Revision Log
« no previous file with comments | « ui/views/view.cc ('k') | ui/views/views.gyp » ('j') | 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) 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 1031 matching lines...) Expand 10 before | Expand all | Expand 10 after
1042 EXPECT_EQ(v2, root_view->GetTooltipHandlerForPoint(v2_centerpoint)); 1042 EXPECT_EQ(v2, root_view->GetTooltipHandlerForPoint(v2_centerpoint));
1043 1043
1044 EXPECT_EQ(v1, root_view->GetTooltipHandlerForPoint(v1_origin)); 1044 EXPECT_EQ(v1, root_view->GetTooltipHandlerForPoint(v1_origin));
1045 EXPECT_EQ(root_view, root_view->GetTooltipHandlerForPoint(v2_origin)); 1045 EXPECT_EQ(root_view, root_view->GetTooltipHandlerForPoint(v2_origin));
1046 1046
1047 EXPECT_FALSE(v1->GetTooltipHandlerForPoint(v2_origin)); 1047 EXPECT_FALSE(v1->GetTooltipHandlerForPoint(v2_origin));
1048 1048
1049 widget->CloseNow(); 1049 widget->CloseNow();
1050 } 1050 }
1051 1051
1052 TEST_F(ViewTest, GetEventHandlerForRect) {
1053 Widget* widget = new Widget;
1054 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
1055 widget->Init(params);
1056 View* root_view = widget->GetRootView();
1057 root_view->SetBoundsRect(gfx::Rect(0, 0, 500, 500));
1058
1059 // Have this hierarchy of views (the coordinates here are all in
1060 // the root view's coordinate space):
1061 // v1 (0, 0, 100, 100)
1062 // v2 (150, 0, 250, 100)
1063 // v3 (0, 200, 150, 100)
1064 // v31 (10, 210, 80, 80)
1065 // v32 (110, 210, 30, 80)
1066 // v4 (300, 200, 100, 100)
1067 // v41 (310, 210, 80, 80)
1068 // v411 (370, 275, 10, 5)
1069
1070 // The coordinates used for SetBounds are in parent coordinates.
1071
1072 TestView* v1 = new TestView;
1073 v1->SetBounds(0, 0, 100, 100);
1074 root_view->AddChildView(v1);
1075
1076 TestView* v2 = new TestView;
1077 v2->SetBounds(150, 0, 250, 100);
1078 root_view->AddChildView(v2);
1079
1080 TestView* v3 = new TestView;
1081 v3->SetBounds(0, 200, 150, 100);
1082 root_view->AddChildView(v3);
1083
1084 TestView* v4 = new TestView;
1085 v4->SetBounds(300, 200, 100, 100);
1086 root_view->AddChildView(v4);
1087
1088 TestView* v31 = new TestView;
1089 v31->SetBounds(10, 10, 80, 80);
1090 v3->AddChildView(v31);
1091
1092 TestView* v32 = new TestView;
1093 v32->SetBounds(110, 10, 30, 80);
1094 v3->AddChildView(v32);
1095
1096 TestView* v41 = new TestView;
1097 v41->SetBounds(10, 10, 80, 80);
1098 v4->AddChildView(v41);
1099
1100 TestView* v411 = new TestView;
1101 v411->SetBounds(60, 65, 10, 5);
1102 v41->AddChildView(v411);
1103
1104 // |touch_rect| does not intersect any descendant view of |root_view|.
1105 gfx::Rect touch_rect(105, 105, 30, 45);
1106 View* result_view = root_view->GetEventHandlerForRect(touch_rect);
1107 EXPECT_EQ(root_view, result_view);
1108 result_view = NULL;
1109
1110 // Covers |v1| by at least 60%.
1111 touch_rect.SetRect(15, 15, 100, 100);
1112 result_view = root_view->GetEventHandlerForRect(touch_rect);
1113 EXPECT_EQ(v1, result_view);
1114 result_view = NULL;
1115
1116 // Intersects |v1| but does not cover it by at least 60%. The center
1117 // of |touch_rect| is within |v1|.
1118 touch_rect.SetRect(50, 50, 5, 10);
1119 result_view = root_view->GetEventHandlerForRect(touch_rect);
1120 EXPECT_EQ(v1, result_view);
1121 result_view = NULL;
1122
1123 // Intersects |v1| but does not cover it by at least 60%. The center
1124 // of |touch_rect| is not within |v1|.
1125 touch_rect.SetRect(95, 96, 21, 22);
1126 result_view = root_view->GetEventHandlerForRect(touch_rect);
1127 EXPECT_EQ(root_view, result_view);
1128 result_view = NULL;
1129
1130 // Intersects |v1| and |v2|, but only covers |v2| by at least 60%.
1131 touch_rect.SetRect(95, 10, 300, 120);
1132 result_view = root_view->GetEventHandlerForRect(touch_rect);
1133 EXPECT_EQ(v2, result_view);
1134 result_view = NULL;
1135
1136 // Covers both |v1| and |v2| by at least 60%, but the center point
1137 // of |touch_rect| is closer to the center line of |v2|.
1138 touch_rect.SetRect(20, 20, 400, 100);
1139 result_view = root_view->GetEventHandlerForRect(touch_rect);
1140 EXPECT_EQ(v2, result_view);
1141 result_view = NULL;
1142
1143 // Covers both |v1| and |v2| by at least 60%, but the center point
1144 // of |touch_rect| is closer to the center line (point) of |v1|.
1145 touch_rect.SetRect(-700, -15, 1050, 110);
1146 result_view = root_view->GetEventHandlerForRect(touch_rect);
1147 EXPECT_EQ(v1, result_view);
1148 result_view = NULL;
1149
1150 // A mouse click within |v1| will target |v1|.
1151 touch_rect.SetRect(15, 15, 1, 1);
1152 result_view = root_view->GetEventHandlerForRect(touch_rect);
1153 EXPECT_EQ(v1, result_view);
1154 result_view = NULL;
1155
1156 // Intersects |v3| and |v31| by at least 60% and the center point
1157 // of |touch_rect| is closer to the center line of |v3|.
1158 touch_rect.SetRect(0, 200, 110, 100);
1159 result_view = root_view->GetEventHandlerForRect(touch_rect);
1160 EXPECT_EQ(v3, result_view);
1161 result_view = NULL;
1162
1163 // Intersects |v3| and |v31| by at least 60% and the center point
1164 // of |touch_rect| is equally close to the center lines of both.
1165 touch_rect.SetRect(-60, 140, 200, 200);
1166 result_view = root_view->GetEventHandlerForRect(touch_rect);
1167 EXPECT_EQ(v31, result_view);
1168 result_view = NULL;
1169
1170 // Intersects |v3| and |v31|, but neither by at least 60%. The
1171 // center point of |touch_rect| lies within |v31|.
1172 touch_rect.SetRect(80, 280, 15, 15);
1173 result_view = root_view->GetEventHandlerForRect(touch_rect);
1174 EXPECT_EQ(v31, result_view);
1175 result_view = NULL;
1176
1177 // Covers |v3|, |v31|, and |v32| all by at least 60%, and the
1178 // center point of |touch_rect| is closest to the center line
1179 // of |v3|.
1180 touch_rect.SetRect(0, 200, 200, 100);
1181 result_view = root_view->GetEventHandlerForRect(touch_rect);
1182 EXPECT_EQ(v3, result_view);
1183 result_view = NULL;
1184
1185 // Intersects all of |v3|, |v31|, and |v32|, but only covers
1186 // |v31| and |v32| by at least 60%. The center point of
1187 // |touch_rect| is closest to the center line of |v32|.
1188 touch_rect.SetRect(30, 225, 180, 115);
1189 result_view = root_view->GetEventHandlerForRect(touch_rect);
1190 EXPECT_EQ(v32, result_view);
1191 result_view = NULL;
1192
1193 // A mouse click at the corner of |v3| will target |v3|.
1194 touch_rect.SetRect(0, 200, 1, 1);
1195 result_view = root_view->GetEventHandlerForRect(touch_rect);
1196 EXPECT_EQ(v3, result_view);
1197 result_view = NULL;
1198
1199 // A mouse click within |v32| will target |v32|.
1200 touch_rect.SetRect(112, 211, 1, 1);
1201 result_view = root_view->GetEventHandlerForRect(touch_rect);
1202 EXPECT_EQ(v32, result_view);
1203 result_view = NULL;
1204
1205 // Covers all of |v4|, |v41|, and |v411| by at least 60%.
1206 // The center point of |touch_rect| is equally close to
1207 // the center points of |v4| and |v41|.
1208 touch_rect.SetRect(310, 210, 80, 80);
1209 result_view = root_view->GetEventHandlerForRect(touch_rect);
1210 EXPECT_EQ(v41, result_view);
1211 result_view = NULL;
1212
1213 // Intersects all of |v4|, |v41|, and |v411| but only covers
1214 // |v411| by at least 60%.
1215 touch_rect.SetRect(370, 275, 7, 5);
1216 result_view = root_view->GetEventHandlerForRect(touch_rect);
1217 EXPECT_EQ(v411, result_view);
1218 result_view = NULL;
1219
1220 // Intersects |v4| and |v41| but covers neither by at least 60%.
1221 // The center point of |touch_rect| is equally close to the center
1222 // points of |v4| and |v41|.
1223 touch_rect.SetRect(345, 245, 7, 7);
1224 result_view = root_view->GetEventHandlerForRect(touch_rect);
1225 EXPECT_EQ(v41, result_view);
1226 result_view = NULL;
1227
1228 // Intersects all of |v4|, |v41|, and |v411| and covers none of
1229 // them by at least 60%. The center point of |touch_rect| lies
1230 // within |v411|.
1231 touch_rect.SetRect(368, 272, 4, 6);
1232 result_view = root_view->GetEventHandlerForRect(touch_rect);
1233 EXPECT_EQ(v411, result_view);
1234 result_view = NULL;
1235
1236 // Intersects all of |v4|, |v41|, and |v411| and covers none of
1237 // them by at least 60%. The center point of |touch_rect| lies
1238 // within |v41|.
1239 touch_rect.SetRect(365, 270, 7, 7);
1240 result_view = root_view->GetEventHandlerForRect(touch_rect);
1241 EXPECT_EQ(v41, result_view);
1242 result_view = NULL;
1243
1244 // Intersects all of |v4|, |v41|, and |v411| and covers none of
1245 // them by at least 60%. The center point of |touch_rect| lies
1246 // within |v4|.
1247 touch_rect.SetRect(205, 275, 200, 2);
1248 result_view = root_view->GetEventHandlerForRect(touch_rect);
1249 EXPECT_EQ(v4, result_view);
1250 result_view = NULL;
1251
1252 // Intersects all of |v4|, |v41|, and |v411| but only covers
1253 // |v41| by at least 60%.
1254 touch_rect.SetRect(310, 210, 61, 66);
1255 result_view = root_view->GetEventHandlerForRect(touch_rect);
1256 EXPECT_EQ(v41, result_view);
1257 result_view = NULL;
1258
1259 // A mouse click within |v411| will target |v411|.
1260 touch_rect.SetRect(372, 275, 1, 1);
1261 result_view = root_view->GetEventHandlerForRect(touch_rect);
1262 EXPECT_EQ(v411, result_view);
1263 result_view = NULL;
1264
1265 // A mouse click within |v41| will target |v41|.
1266 touch_rect.SetRect(350, 215, 1, 1);
1267 result_view = root_view->GetEventHandlerForRect(touch_rect);
1268 EXPECT_EQ(v41, result_view);
1269 result_view = NULL;
1270
1271 // Covers |v3|, |v4|, and all of their descendants by at
1272 // least 60%. The center point of |touch_rect| is closest
1273 // to the center line of |v32|.
1274 touch_rect.SetRect(0, 200, 400, 100);
1275 result_view = root_view->GetEventHandlerForRect(touch_rect);
1276 EXPECT_EQ(v32, result_view);
1277 result_view = NULL;
1278
1279 // Intersects all of |v2|, |v3|, |v32|, |v4|, |v41|, and |v411|.
1280 // Covers |v2|, |v32|, |v4|, |v41|, and |v411| by at least 60%.
1281 // The center point of |touch_rect| is closest to the center
1282 // point of |root_view|.
1283 touch_rect.SetRect(110, 15, 375, 450);
1284 result_view = root_view->GetEventHandlerForRect(touch_rect);
1285 EXPECT_EQ(root_view, result_view);
1286 result_view = NULL;
1287
1288 // Covers all views by at least 60%. The center point of
1289 // |touch_rect| is closest to the center line of |v2|.
1290 touch_rect.SetRect(0, 0, 400, 300);
1291 result_view = root_view->GetEventHandlerForRect(touch_rect);
1292 EXPECT_EQ(v2, result_view);
1293 result_view = NULL;
1294
1295 widget->CloseNow();
1296 }
1297
1052 TEST_F(ViewTest, NotifyEnterExitOnChild) { 1298 TEST_F(ViewTest, NotifyEnterExitOnChild) {
1053 Widget* widget = new Widget; 1299 Widget* widget = new Widget;
1054 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); 1300 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
1055 widget->Init(params); 1301 widget->Init(params);
1056 View* root_view = widget->GetRootView(); 1302 View* root_view = widget->GetRootView();
1057 root_view->SetBoundsRect(gfx::Rect(0, 0, 500, 500)); 1303 root_view->SetBoundsRect(gfx::Rect(0, 0, 500, 500));
1058 1304
1059 // Have this hierarchy of views (the coords here are in root coord): 1305 // Have this hierarchy of views (the coords here are in root coord):
1060 // v1 (0, 0, 100, 100) 1306 // v1 (0, 0, 100, 100)
1061 // - v11 (0, 0, 20, 30) 1307 // - v11 (0, 0, 20, 30)
(...skipping 2133 matching lines...) Expand 10 before | Expand all | Expand 10 after
3195 const std::vector<ui::Layer*>& child_layers_post = root_layer->children(); 3441 const std::vector<ui::Layer*>& child_layers_post = root_layer->children();
3196 ASSERT_EQ(3u, child_layers_post.size()); 3442 ASSERT_EQ(3u, child_layers_post.size());
3197 EXPECT_EQ(v1->layer(), child_layers_post[0]); 3443 EXPECT_EQ(v1->layer(), child_layers_post[0]);
3198 EXPECT_EQ(v2->layer(), child_layers_post[1]); 3444 EXPECT_EQ(v2->layer(), child_layers_post[1]);
3199 EXPECT_EQ(v1_old_layer, child_layers_post[2]); 3445 EXPECT_EQ(v1_old_layer, child_layers_post[2]);
3200 } 3446 }
3201 3447
3202 #endif // USE_AURA 3448 #endif // USE_AURA
3203 3449
3204 } // namespace views 3450 } // namespace views
OLDNEW
« no previous file with comments | « ui/views/view.cc ('k') | ui/views/views.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698