Skip to content

Commit

Permalink
Added CGFloat comparison functions.
Browse files Browse the repository at this point in the history
Comparing CGFloat values using direct equality was causing flakes in
CRWWebController's PageScrollState unittests, so compare using CGFloat's
epsilon.

BUG=474290

Review URL: https://codereview.chromium.org/1155623002

Cr-Commit-Position: refs/heads/master@{#332223}
  • Loading branch information
kkhorimoto authored and Commit bot committed Jun 1, 2015
1 parent f57b91f commit 53ce056
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 12 deletions.
19 changes: 12 additions & 7 deletions ios/web/web_state/ui/crw_web_controller_unittest.mm
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,11 @@ void SimulateLoadRequest(NSURLRequest* request) const override {
}
};

// Helper function for comparing two CGFloats.
bool CGFloatsAreEqual(CGFloat val1, CGFloat val2) {
return std::fabs(val1 - val2) < std::numeric_limits<CGFloat>::epsilon();
}

WEB_TEST_F(CRWUIWebControllerPageScrollStateTest,
CRWWKWebControllerPageScrollStateTest,
SetPageStateWithUserScalableDisabled) {
Expand Down Expand Up @@ -945,12 +950,12 @@ void SimulateLoadRequest(NSURLRequest* request) const override {
// setPageState: is async; wait for its completion.
scrollView = [[[this->webController_ view] subviews][0] scrollView];
base::test::ios::WaitUntilCondition(^bool() {
return [scrollView contentOffset].x == 1.0f;
return CGFloatsAreEqual(scrollView.contentOffset.x, 1.0f);
});

ASSERT_EQ(originZoomScale, scrollView.zoomScale);
ASSERT_EQ(originMinimumZoomScale, scrollView.minimumZoomScale);
ASSERT_EQ(originMaximumZoomScale, scrollView.maximumZoomScale);
ASSERT_CGFLOAT_EQ(originZoomScale, scrollView.zoomScale);
ASSERT_CGFLOAT_EQ(originMinimumZoomScale, scrollView.minimumZoomScale);
ASSERT_CGFLOAT_EQ(originMaximumZoomScale, scrollView.maximumZoomScale);
};

WEB_TEST_F(CRWUIWebControllerPageScrollStateTest,
Expand All @@ -974,10 +979,10 @@ void SimulateLoadRequest(NSURLRequest* request) const override {
id webView = [[this->webController_ view] subviews][0];
UIScrollView* scrollView = [webView scrollView];
base::test::ios::WaitUntilCondition(^bool() {
return [scrollView contentOffset].x == 1.0f;
return CGFloatsAreEqual(scrollView.contentOffset.x, 1.0f);
});

EXPECT_FLOAT_EQ(3, scrollView.zoomScale / scrollView.minimumZoomScale);
ASSERT_FLOAT_EQ(3, scrollView.zoomScale / scrollView.minimumZoomScale);
};

WEB_TEST_F(CRWUIWebControllerPageScrollStateTest,
Expand Down Expand Up @@ -1006,7 +1011,7 @@ void SimulateLoadRequest(NSURLRequest* request) const override {
// setPageState: is async; wait for its completion.
id webView = [[this->webController_ view] subviews][0];
base::test::ios::WaitUntilCondition(^bool() {
return [[webView scrollView] contentOffset].y == 30.0f;
return CGFloatsAreEqual([webView scrollView].contentOffset.y, 30.0f);
});

ASSERT_FALSE([this->webController_ atTop]);
Expand Down
9 changes: 9 additions & 0 deletions testing/gtest_mac.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ GTEST_API_ AssertionResult CmpHelperNSNE(const char* expected_expression,
#define ASSERT_NSNE(val1, val2) \
ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperNSNE, val1, val2)

// Tests CGFloat equality.
#if CGFLOAT_IS_DOUBLE
#define EXPECT_CGFLOAT_EQ(expected, actual) EXPECT_DOUBLE_EQ(expected, actual)
#define ASSERT_CGFLOAT_EQ(expected, actual) ASSERT_DOUBLE_EQ(expected, actual)
#else
#define EXPECT_CGFLOAT_EQ(expected, actual) EXPECT_FLOAT_EQ(expected, actual)
#define ASSERT_CGFLOAT_EQ(expected, actual) ASSERT_FLOAT_EQ(expected, actual)
#endif // CGFLOAT_IS_DOUBLE

#endif // GTEST_OS_MAC

#endif // TESTING_GTEST_MAC_H_
5 changes: 0 additions & 5 deletions ui/gfx/test/ui_cocoa_test_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,6 @@ class CocoaTest : public PlatformTest {
(actual >= (expected - CGFLOAT_EPSILON) && \
actual <= (expected + CGFLOAT_EPSILON))

// A test support macro which ascertains if two CGFloats are equal.
#define EXPECT_CGFLOAT_EQ(expected, actual) \
EXPECT_TRUE(CGFLOAT_EQ(expected, actual)) << \
expected << " != " << actual

// A test support macro which compares two NSRects for equality taking
// the float epsilon into consideration.
#define EXPECT_NSRECT_EQ(expected, actual) \
Expand Down
1 change: 1 addition & 0 deletions ui/message_center/cocoa/tray_view_controller_unittest.mm
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#import "testing/gtest_mac.h"
#import "ui/gfx/test/ui_cocoa_test_helper.h"
#include "ui/message_center/fake_notifier_settings_provider.h"
#include "ui/message_center/message_center.h"
Expand Down

0 comments on commit 53ce056

Please sign in to comment.