uikit_util_unittest.mm 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #import "ui/gfx/ios/uikit_util.h"
  5. #import <Foundation/Foundation.h>
  6. #include <stddef.h>
  7. #include "testing/platform_test.h"
  8. namespace {
  9. typedef PlatformTest UIKitUtilTest;
  10. TEST_F(UIKitUtilTest, AlignValueToUpperPixel) {
  11. CGFloat scale = [[UIScreen mainScreen] scale];
  12. // Pick a few interesting values: already aligned, aligned on retina, and
  13. // some unaligned values that would round differently. Ensure that all are
  14. // "integer" values within <1 of the original value in the scaled space.
  15. CGFloat test_values[] = { 10.0, 55.5, 3.1, 2.9 };
  16. const CGFloat kMaxAlignDelta = 0.9999;
  17. size_t value_count = std::size(test_values);
  18. for (unsigned int i = 0; i < value_count; ++i) {
  19. CGFloat aligned = ui::AlignValueToUpperPixel(test_values[i]);
  20. EXPECT_FLOAT_EQ(aligned * scale, floor(aligned * scale));
  21. EXPECT_NEAR(aligned * scale, test_values[i] * scale, kMaxAlignDelta);
  22. }
  23. }
  24. TEST_F(UIKitUtilTest, AlignSizeToUpperPixel) {
  25. CGFloat scale = [[UIScreen mainScreen] scale];
  26. // Pick a few interesting values: already aligned, aligned on retina, and
  27. // some unaligned values that would round differently. Ensure that all are
  28. // "integer" values within <1 of the original value in the scaled space.
  29. CGFloat test_values[] = { 10.0, 55.5, 3.1, 2.9 };
  30. const CGFloat kMaxAlignDelta = 0.9999;
  31. size_t value_count = std::size(test_values);
  32. for (unsigned int i = 0; i < value_count; ++i) {
  33. CGFloat width = test_values[i];
  34. CGFloat height = test_values[(i + 1) % value_count];
  35. CGSize alignedSize = ui::AlignSizeToUpperPixel(CGSizeMake(width, height));
  36. EXPECT_FLOAT_EQ(floor(alignedSize.width * scale),
  37. alignedSize.width * scale);
  38. EXPECT_FLOAT_EQ(floor(alignedSize.height * scale),
  39. alignedSize.height * scale);
  40. EXPECT_NEAR(width * scale, alignedSize.width * scale, kMaxAlignDelta);
  41. EXPECT_NEAR(height * scale, alignedSize.height * scale, kMaxAlignDelta);
  42. }
  43. }
  44. } // namespace