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

Side by Side Diff: ui/gfx/geometry/rect_unittest.cc

Issue 2354783004: Fix overflow/underflow in gfx geometry once and for all (Closed)
Patch Set: Remove accessibility test changes Created 4 years, 2 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/gfx/geometry/rect.cc ('k') | ui/gfx/geometry/safe_integer_conversions.h » ('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) 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 <limits> 5 #include <limits>
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include "base/macros.h" 9 #include "base/macros.h"
10 #include "build/build_config.h" 10 #include "build/build_config.h"
(...skipping 892 matching lines...) Expand 10 before | Expand all | Expand 10 after
903 0.0f, f.ManhattanInternalDistance(gfx::RectF(-1.0f, 0.0f, 1.1f, 1.0f))); 903 0.0f, f.ManhattanInternalDistance(gfx::RectF(-1.0f, 0.0f, 1.1f, 1.0f)));
904 EXPECT_FLOAT_EQ( 904 EXPECT_FLOAT_EQ(
905 0.1f + kEpsilon, 905 0.1f + kEpsilon,
906 f.ManhattanInternalDistance(gfx::RectF(-1.5f, 0.0f, 1.4f, 1.0f))); 906 f.ManhattanInternalDistance(gfx::RectF(-1.5f, 0.0f, 1.4f, 1.0f)));
907 EXPECT_FLOAT_EQ( 907 EXPECT_FLOAT_EQ(
908 kEpsilon, 908 kEpsilon,
909 f.ManhattanInternalDistance(gfx::RectF(-1.5f, 0.0f, 1.5f, 1.0f))); 909 f.ManhattanInternalDistance(gfx::RectF(-1.5f, 0.0f, 1.5f, 1.0f)));
910 } 910 }
911 911
912 TEST(RectTest, IntegerOverflow) { 912 TEST(RectTest, IntegerOverflow) {
913 int limit = std::numeric_limits<int>::max();
914 int min_limit = std::numeric_limits<int>::min();
913 int expected = 10; 915 int expected = 10;
914 int large_number = std::numeric_limits<int>::max() - expected; 916 int large_number = limit - expected;
915 917
916 Rect height_overflow(0, large_number, 100, 100); 918 Rect height_overflow(0, large_number, 100, 100);
917 EXPECT_EQ(large_number, height_overflow.y()); 919 EXPECT_EQ(large_number, height_overflow.y());
918 EXPECT_EQ(expected, height_overflow.height()); 920 EXPECT_EQ(expected, height_overflow.height());
919 921
920 Rect width_overflow(large_number, 0, 100, 100); 922 Rect width_overflow(large_number, 0, 100, 100);
921 EXPECT_EQ(large_number, width_overflow.x()); 923 EXPECT_EQ(large_number, width_overflow.x());
922 EXPECT_EQ(expected, width_overflow.width()); 924 EXPECT_EQ(expected, width_overflow.width());
923 925
924 Rect size_height_overflow(Point(0, large_number), Size(100, 100)); 926 Rect size_height_overflow(Point(0, large_number), Size(100, 100));
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
963 EXPECT_EQ(Size(5, 5), set_size_overflow.size()); 965 EXPECT_EQ(Size(5, 5), set_size_overflow.size());
964 set_size_overflow.set_size(size); 966 set_size_overflow.set_size(size);
965 EXPECT_EQ(large_offset, set_size_overflow.origin()); 967 EXPECT_EQ(large_offset, set_size_overflow.origin());
966 EXPECT_EQ(expected_size, set_size_overflow.size()); 968 EXPECT_EQ(expected_size, set_size_overflow.size());
967 969
968 Rect set_rect_overflow; 970 Rect set_rect_overflow;
969 set_rect_overflow.SetRect(large_number, large_number, 100, 100); 971 set_rect_overflow.SetRect(large_number, large_number, 100, 100);
970 EXPECT_EQ(large_offset, set_rect_overflow.origin()); 972 EXPECT_EQ(large_offset, set_rect_overflow.origin());
971 EXPECT_EQ(expected_size, set_rect_overflow.size()); 973 EXPECT_EQ(expected_size, set_rect_overflow.size());
972 974
975 // Insetting an empty rect, but the total inset (left + right) could overflow.
973 Rect inset_overflow; 976 Rect inset_overflow;
974 inset_overflow.Inset(large_number, large_number, 100, 100); 977 inset_overflow.Inset(large_number, large_number, 100, 100);
975 EXPECT_EQ(large_offset, inset_overflow.origin()); 978 EXPECT_EQ(large_offset, inset_overflow.origin());
976 EXPECT_EQ(expected_size, inset_overflow.size()); 979 EXPECT_EQ(gfx::Size(), inset_overflow.size());
980
981 // Insetting where the total inset (width - left - right) could overflow.
982 // Also, this insetting by the min limit in all directions cannot
983 // represent width() without overflow, so that will also clamp.
984 Rect inset_overflow2;
985 inset_overflow2.Inset(min_limit, min_limit, min_limit, min_limit);
986 EXPECT_EQ(inset_overflow2, gfx::Rect(min_limit, min_limit, limit, limit));
987
988 // Insetting where the width shouldn't change, but if the insets operations
989 // clamped in the wrong order, e.g. ((width - left) - right) vs (width - (left
990 // + right)) then this will not work properly. This is the proper order,
991 // as if left + right overflows, the width cannot be decreased by more than
992 // max int anyway. Additionally, if left + right underflows, it cannot be
993 // increased by more then max int.
994 Rect inset_overflow3(0, 0, limit, limit);
995 inset_overflow3.Inset(-100, -100, 100, 100);
996 EXPECT_EQ(inset_overflow3, gfx::Rect(-100, -100, limit, limit));
997
998 Rect inset_overflow4(-1000, -1000, limit, limit);
999 inset_overflow4.Inset(100, 100, -100, -100);
1000 EXPECT_EQ(inset_overflow4, gfx::Rect(-900, -900, limit, limit));
977 1001
978 Rect offset_overflow(0, 0, 100, 100); 1002 Rect offset_overflow(0, 0, 100, 100);
979 offset_overflow.Offset(large_number, large_number); 1003 offset_overflow.Offset(large_number, large_number);
980 EXPECT_EQ(large_offset, offset_overflow.origin()); 1004 EXPECT_EQ(large_offset, offset_overflow.origin());
981 EXPECT_EQ(expected_size, offset_overflow.size()); 1005 EXPECT_EQ(expected_size, offset_overflow.size());
982 1006
983 Rect operator_overflow(0, 0, 100, 100); 1007 Rect operator_overflow(0, 0, 100, 100);
984 operator_overflow += Vector2d(large_number, large_number); 1008 operator_overflow += Vector2d(large_number, large_number);
985 EXPECT_EQ(large_offset, operator_overflow.origin()); 1009 EXPECT_EQ(large_offset, operator_overflow.origin());
986 EXPECT_EQ(expected_size, operator_overflow.size()); 1010 EXPECT_EQ(expected_size, operator_overflow.size());
1011
1012 Rect origin_maxint(limit, limit, limit, limit);
1013 EXPECT_EQ(origin_maxint, Rect(gfx::Point(limit, limit), gfx::Size()));
1014
1015 // Expect a rect at the origin and a rect whose right/bottom is maxint
1016 // create a rect that extends from 0..maxint in both extents.
1017 {
1018 Rect origin_small(0, 0, 100, 100);
1019 Rect big_clamped(50, 50, limit, limit);
1020 EXPECT_EQ(big_clamped.right(), limit);
1021
1022 Rect unioned = UnionRects(origin_small, big_clamped);
1023 Rect rect_limit(0, 0, limit, limit);
1024 EXPECT_EQ(unioned, rect_limit);
1025 }
1026
1027 // Expect a rect that would overflow width (but not right) to be clamped
1028 // and to have maxint extents after unioning.
1029 {
1030 Rect small(-500, -400, 100, 100);
1031 Rect big(-400, -500, limit, limit);
1032 // Technically, this should be limit + 100 width, but will clamp to maxint.
1033 EXPECT_EQ(UnionRects(small, big), Rect(-500, -500, limit, limit));
1034 }
1035
1036 // Expect a rect that would overflow right *and* width to be clamped.
1037 {
1038 Rect clamped(500, 500, limit, limit);
1039 Rect positive_origin(100, 100, 500, 500);
1040
1041 // Ideally, this should be (100, 100, limit + 400, limit + 400).
1042 // However, width overflows and would be clamped to limit, but right
1043 // overflows too and so will be clamped to limit - 100.
1044 Rect expected(100, 100, limit - 100, limit - 100);
1045 EXPECT_EQ(UnionRects(clamped, positive_origin), expected);
1046 }
1047
1048 // Unioning a left=minint rect with a right=maxint rect.
1049 // Width is always clamped before adjusting position, so this
1050 // should taking the min left/top and then finding the max width.
1051 {
1052 Rect left_minint(min_limit, min_limit, 1, 1);
1053 Rect right_maxint(limit - 1, limit - 1, limit, limit);
1054 Rect expected(min_limit, min_limit, limit, limit);
1055 EXPECT_EQ(UnionRects(left_minint, right_maxint), expected);
1056 }
987 } 1057 }
988 1058
989 TEST(RectTest, ScaleToEnclosingRectSafe) { 1059 TEST(RectTest, ScaleToEnclosingRectSafe) {
990 const int max_int = std::numeric_limits<int>::max(); 1060 const int max_int = std::numeric_limits<int>::max();
991 const int min_int = std::numeric_limits<int>::min(); 1061 const int min_int = std::numeric_limits<int>::min();
992 1062
993 Rect xy_underflow(-100000, -123456, 10, 20); 1063 Rect xy_underflow(-100000, -123456, 10, 20);
994 EXPECT_EQ(ScaleToEnclosingRectSafe(xy_underflow, 100000, 100000), 1064 EXPECT_EQ(ScaleToEnclosingRectSafe(xy_underflow, 100000, 100000),
995 Rect(min_int, min_int, 1000000, 2000000)); 1065 Rect(min_int, min_int, 1000000, 2000000));
996 1066
(...skipping 23 matching lines...) Expand all
1020 1090
1021 Rect min_rect(min_int, min_int, max_int, max_int); 1091 Rect min_rect(min_int, min_int, max_int, max_int);
1022 // Min rect can't be scaled up any further in any dimension. 1092 // Min rect can't be scaled up any further in any dimension.
1023 EXPECT_EQ(ScaleToEnclosingRectSafe(min_rect, 2, 3.5), min_rect); 1093 EXPECT_EQ(ScaleToEnclosingRectSafe(min_rect, 2, 3.5), min_rect);
1024 EXPECT_EQ(ScaleToEnclosingRectSafe(min_rect, max_int, max_int), min_rect); 1094 EXPECT_EQ(ScaleToEnclosingRectSafe(min_rect, max_int, max_int), min_rect);
1025 // Min rect scaled by min is an empty rect at (max, max) 1095 // Min rect scaled by min is an empty rect at (max, max)
1026 EXPECT_EQ(ScaleToEnclosingRectSafe(min_rect, min_int, min_int), max_rect); 1096 EXPECT_EQ(ScaleToEnclosingRectSafe(min_rect, min_int, min_int), max_rect);
1027 } 1097 }
1028 1098
1029 } // namespace gfx 1099 } // namespace gfx
OLDNEW
« no previous file with comments | « ui/gfx/geometry/rect.cc ('k') | ui/gfx/geometry/safe_integer_conversions.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698