WKViewUserViewportToContents.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright (C) 2013 Nokia Corporation and/or its subsidiary(-ies).
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
  17. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  21. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "config.h"
  26. #include "WebKit2/WKView.h"
  27. #include "WebKit2/WKRetainPtr.h"
  28. namespace TestWebKitAPI {
  29. TEST(WebKit2, WKViewUserViewportToContents)
  30. {
  31. // This test creates a WKView and uses the WKViewUserViewportToContents
  32. // function to convert viewport coordinates to contents (page) coordinates.
  33. // It scrolls and scales around the contents and check if the coordinates
  34. // conversion math is right.
  35. WKRetainPtr<WKContextRef> context(AdoptWK, WKContextCreate());
  36. WKRetainPtr<WKViewRef> webView(AdoptWK, WKViewCreate(context.get(), 0));
  37. WKViewInitialize(webView.get());
  38. WKPageSetUseFixedLayout(WKViewGetPage(webView.get()), false);
  39. WKPoint out;
  40. // At scale 1.0 the viewport and contents coordinates should match.
  41. WKViewSetContentScaleFactor(webView.get(), 1.0);
  42. WKViewSetContentPosition(webView.get(), WKPointMake(0, 0));
  43. out = WKViewUserViewportToContents(webView.get(), WKPointMake(0, 0));
  44. EXPECT_EQ(out.x, 0);
  45. EXPECT_EQ(out.y, 0);
  46. out = WKViewUserViewportToContents(webView.get(), WKPointMake(10, 10));
  47. EXPECT_EQ(out.x, 10);
  48. EXPECT_EQ(out.y, 10);
  49. WKViewSetContentPosition(webView.get(), WKPointMake(20, 20));
  50. out = WKViewUserViewportToContents(webView.get(), WKPointMake(0, 0));
  51. EXPECT_EQ(out.x, 20);
  52. EXPECT_EQ(out.y, 20);
  53. out = WKViewUserViewportToContents(webView.get(), WKPointMake(10, 10));
  54. EXPECT_EQ(out.x, 30);
  55. EXPECT_EQ(out.y, 30);
  56. // At scale 2.0 the viewport distance values will be half
  57. // the ones seem in the contents.
  58. WKViewSetContentScaleFactor(webView.get(), 2.0);
  59. WKViewSetContentPosition(webView.get(), WKPointMake(0, 0));
  60. out = WKViewUserViewportToContents(webView.get(), WKPointMake(0, 0));
  61. EXPECT_EQ(out.x, 0);
  62. EXPECT_EQ(out.y, 0);
  63. out = WKViewUserViewportToContents(webView.get(), WKPointMake(10, 10));
  64. EXPECT_EQ(out.x, 5);
  65. EXPECT_EQ(out.y, 5);
  66. WKViewSetContentPosition(webView.get(), WKPointMake(20, 20));
  67. out = WKViewUserViewportToContents(webView.get(), WKPointMake(0, 0));
  68. EXPECT_EQ(out.x, 20);
  69. EXPECT_EQ(out.y, 20);
  70. out = WKViewUserViewportToContents(webView.get(), WKPointMake(10, 10));
  71. EXPECT_EQ(out.x, 25);
  72. EXPECT_EQ(out.y, 25);
  73. // At scale 0.5 the viewport distance values will be twice
  74. // the ones seem in the contents.
  75. WKViewSetContentScaleFactor(webView.get(), 0.5);
  76. WKViewSetContentPosition(webView.get(), WKPointMake(0, 0));
  77. out = WKViewUserViewportToContents(webView.get(), WKPointMake(0, 0));
  78. EXPECT_EQ(out.x, 0);
  79. EXPECT_EQ(out.y, 0);
  80. out = WKViewUserViewportToContents(webView.get(), WKPointMake(10, 10));
  81. EXPECT_EQ(out.x, 20);
  82. EXPECT_EQ(out.y, 20);
  83. WKViewSetContentPosition(webView.get(), WKPointMake(20, 20));
  84. out = WKViewUserViewportToContents(webView.get(), WKPointMake(0, 0));
  85. EXPECT_EQ(out.x, 20);
  86. EXPECT_EQ(out.y, 20);
  87. out = WKViewUserViewportToContents(webView.get(), WKPointMake(10, 10));
  88. EXPECT_EQ(out.x, 40);
  89. EXPECT_EQ(out.y, 40);
  90. // Let's add translation to the viewport.
  91. const int delta = 10;
  92. WKViewSetUserViewportTranslation(webView.get(), delta, delta);
  93. WKViewSetContentPosition(webView.get(), WKPointMake(0, 0));
  94. out = WKViewUserViewportToContents(webView.get(), WKPointMake(0, 0));
  95. EXPECT_EQ(out.x, 0 - delta / 0.5);
  96. EXPECT_EQ(out.y, 0 - delta / 0.5);
  97. out = WKViewUserViewportToContents(webView.get(), WKPointMake(10, 10));
  98. EXPECT_EQ(out.x, 20 - delta / 0.5);
  99. EXPECT_EQ(out.y, 20 - delta / 0.5);
  100. WKViewSetContentPosition(webView.get(), WKPointMake(20, 20));
  101. out = WKViewUserViewportToContents(webView.get(), WKPointMake(0, 0));
  102. EXPECT_EQ(out.x, 20 - delta / 0.5);
  103. EXPECT_EQ(out.y, 20 - delta / 0.5);
  104. out = WKViewUserViewportToContents(webView.get(), WKPointMake(10, 10));
  105. EXPECT_EQ(out.x, 40 - delta / 0.5);
  106. EXPECT_EQ(out.y, 40 - delta / 0.5);
  107. }
  108. }