page_display_state_unittest.mm 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright 2017 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 "ios/web/public/ui/page_display_state.h"
  5. #include "testing/gtest/include/gtest/gtest.h"
  6. #define EXPECT_NAN(value) EXPECT_NE(value, value)
  7. #include "testing/platform_test.h"
  8. #if !defined(__has_feature) || !__has_feature(objc_arc)
  9. #error "This file requires ARC support."
  10. #endif
  11. using PageDisplayStateTest = PlatformTest;
  12. // Tests that the empty constructor creates an invalid PageDisplayState with all
  13. // NAN values.
  14. TEST_F(PageDisplayStateTest, EmptyConstructor) {
  15. web::PageDisplayState state;
  16. EXPECT_NAN(state.scroll_state().content_offset().y);
  17. EXPECT_NAN(state.scroll_state().content_offset().x);
  18. EXPECT_NAN(state.scroll_state().content_inset().top);
  19. EXPECT_NAN(state.scroll_state().content_inset().left);
  20. EXPECT_NAN(state.scroll_state().content_inset().bottom);
  21. EXPECT_NAN(state.scroll_state().content_inset().right);
  22. EXPECT_NAN(state.zoom_state().minimum_zoom_scale());
  23. EXPECT_NAN(state.zoom_state().maximum_zoom_scale());
  24. EXPECT_NAN(state.zoom_state().zoom_scale());
  25. EXPECT_FALSE(state.IsValid());
  26. }
  27. // Tests that the constructor with input states correctly populates the display
  28. // state.
  29. TEST_F(PageDisplayStateTest, StatesConstructor) {
  30. const CGPoint kContentOffset = CGPointMake(0.0, 1.0);
  31. const UIEdgeInsets kContentInset = UIEdgeInsetsMake(0.0, 1.0, 2.0, 3.0);
  32. web::PageScrollState scroll_state(kContentOffset, kContentInset);
  33. EXPECT_TRUE(
  34. CGPointEqualToPoint(scroll_state.content_offset(), kContentOffset));
  35. EXPECT_TRUE(UIEdgeInsetsEqualToEdgeInsets(scroll_state.content_inset(),
  36. kContentInset));
  37. EXPECT_TRUE(scroll_state.IsValid());
  38. web::PageZoomState zoom_state(1.0, 5.0, 1.0);
  39. EXPECT_EQ(1.0, zoom_state.minimum_zoom_scale());
  40. EXPECT_EQ(5.0, zoom_state.maximum_zoom_scale());
  41. EXPECT_EQ(1.0, zoom_state.zoom_scale());
  42. EXPECT_TRUE(zoom_state.IsValid());
  43. web::PageDisplayState state(scroll_state, zoom_state);
  44. EXPECT_EQ(scroll_state, state.scroll_state());
  45. EXPECT_EQ(zoom_state, state.zoom_state());
  46. EXPECT_TRUE(state.IsValid());
  47. }
  48. // Tests converting between a PageDisplayState, its serialization, and back.
  49. TEST_F(PageDisplayStateTest, Serialization) {
  50. web::PageDisplayState state(CGPointMake(0.0, 1.0),
  51. UIEdgeInsetsMake(0.0, 1.0, 2.0, 3.0), 1.0, 5.0,
  52. 1.0);
  53. web::PageDisplayState new_state(state.GetSerialization());
  54. EXPECT_EQ(state, new_state);
  55. }
  56. // Tests PageScrollState::GetEffectiveContentOffsetForContentInset().
  57. TEST_F(PageDisplayStateTest, EffectiveContentOffset) {
  58. // kContentOffset is chosen such that a page with kTopInset is scrolled to the
  59. // top.
  60. const CGFloat kTopInset = 100;
  61. const CGPoint kContentOffset = CGPointMake(0.0, -kTopInset);
  62. const UIEdgeInsets kContentInset = UIEdgeInsetsMake(kTopInset, 0.0, 0.0, 0.0);
  63. web::PageScrollState scroll_state(kContentOffset, kContentInset);
  64. // Tests that GetEffectiveContentOffsetForContentInset() returns the scrolled-
  65. // to-top content offset for kNewTopInset.
  66. const CGFloat kNewTopInset = 50.0;
  67. const UIEdgeInsets kNewContentInset =
  68. UIEdgeInsetsMake(kNewTopInset, 0.0, 0.0, 0.0);
  69. CGPoint effective_content_offset =
  70. scroll_state.GetEffectiveContentOffsetForContentInset(kNewContentInset);
  71. EXPECT_EQ(effective_content_offset.y, -kNewTopInset);
  72. }