DeviceScaleFactorOnBack.mm 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright (C) 2011 Apple Inc. All rights reserved.
  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 APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
  14. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  15. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
  17. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  23. * THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "config.h"
  26. #include "WebKitAgnosticTest.h"
  27. #include "JavaScriptTest.h"
  28. #include "PlatformUtilities.h"
  29. #include "SyntheticBackingScaleFactorWindow.h"
  30. #include <WebKit2/WKViewPrivate.h>
  31. #include <wtf/RetainPtr.h>
  32. namespace TestWebKitAPI {
  33. class DeviceScaleFactorOnBack : public WebKitAgnosticTest {
  34. public:
  35. RetainPtr<SyntheticBackingScaleFactorWindow> createWindow();
  36. template <typename View> void runTest(View);
  37. // WebKitAgnosticTest
  38. virtual NSURL *url() const { return [[NSBundle mainBundle] URLForResource:@"devicePixelRatio" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]; }
  39. virtual void didLoadURL(WebView *webView) { runTest(webView); }
  40. virtual void didLoadURL(WKView *wkView) { runTest(wkView); }
  41. virtual void initializeView(WebView *);
  42. virtual void initializeView(WKView *);
  43. };
  44. RetainPtr<SyntheticBackingScaleFactorWindow> DeviceScaleFactorOnBack::createWindow()
  45. {
  46. RetainPtr<SyntheticBackingScaleFactorWindow> window = adoptNS([[SyntheticBackingScaleFactorWindow alloc] initWithContentRect:viewFrame styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:YES]);
  47. [window.get() setReleasedWhenClosed:NO];
  48. return window;
  49. }
  50. void DeviceScaleFactorOnBack::initializeView(WebView *view)
  51. {
  52. // The default cache model has a capacity of 0, so it is necessary to switch to a cache
  53. // model that actuall caches things.
  54. [[view preferences] setCacheModel:WebCacheModelDocumentBrowser];
  55. }
  56. void DeviceScaleFactorOnBack::initializeView(WKView *view)
  57. {
  58. // The default cache model has a capacity of 0, so it is necessary to switch to a cache
  59. // model that actuall caches things.
  60. WKContextSetCacheModel(WKPageGetContext([view pageRef]), kWKCacheModelDocumentBrowser);
  61. }
  62. template <typename View>
  63. void DeviceScaleFactorOnBack::runTest(View view)
  64. {
  65. EXPECT_JS_EQ(view, "window.devicePixelRatio", "1");
  66. EXPECT_JS_EQ(view, "devicePixelRatioFromStyle()", "1");
  67. // Navigate to new URL
  68. loadURL(view, [NSURL URLWithString:@"about:blank"]);
  69. waitForLoadToFinish();
  70. // Change the scale factor
  71. RetainPtr<SyntheticBackingScaleFactorWindow> window1 = createWindow();
  72. [window1.get() setBackingScaleFactor:3];
  73. [[window1.get() contentView] addSubview:view];
  74. // Navigate back to the first page
  75. goBack(view);
  76. waitForLoadToFinish();
  77. // Ensure that the cached page has updated its scale factor
  78. EXPECT_JS_EQ(view, "window.devicePixelRatio", "3");
  79. EXPECT_JS_EQ(view, "devicePixelRatioFromStyle()", "3");
  80. [view removeFromSuperview];
  81. }
  82. TEST_F(DeviceScaleFactorOnBack, WebKit)
  83. {
  84. runWebKit1Test();
  85. }
  86. TEST_F(DeviceScaleFactorOnBack, WebKit2)
  87. {
  88. runWebKit2Test();
  89. }
  90. } // namespace TestWebKitAPI