DynamicDeviceScaleFactor.mm 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 <wtf/RetainPtr.h>
  31. namespace TestWebKitAPI {
  32. class DynamicDeviceScaleFactor : public WebKitAgnosticTest {
  33. public:
  34. RetainPtr<SyntheticBackingScaleFactorWindow> createWindow();
  35. template <typename View> void runTest(View);
  36. // WebKitAgnosticTest
  37. virtual NSURL *url() const { return [[NSBundle mainBundle] URLForResource:@"devicePixelRatio" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]; }
  38. virtual void didLoadURL(WebView *webView) { runTest(webView); }
  39. virtual void didLoadURL(WKView *wkView) { runTest(wkView); }
  40. };
  41. RetainPtr<SyntheticBackingScaleFactorWindow> DynamicDeviceScaleFactor::createWindow()
  42. {
  43. RetainPtr<SyntheticBackingScaleFactorWindow> window = adoptNS([[SyntheticBackingScaleFactorWindow alloc] initWithContentRect:viewFrame styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:YES]);
  44. [window.get() setReleasedWhenClosed:NO];
  45. return window;
  46. }
  47. template <typename View>
  48. void DynamicDeviceScaleFactor::runTest(View view)
  49. {
  50. EXPECT_JS_EQ(view, "window.devicePixelRatio", "1");
  51. EXPECT_JS_EQ(view, "devicePixelRatioFromStyle()", "1");
  52. RetainPtr<SyntheticBackingScaleFactorWindow> window1 = createWindow();
  53. [window1.get() setBackingScaleFactor:3];
  54. [[window1.get() contentView] addSubview:view];
  55. EXPECT_JS_EQ(view, "window.devicePixelRatio", "3");
  56. EXPECT_JS_EQ(view, "devicePixelRatioFromStyle()", "3");
  57. RetainPtr<SyntheticBackingScaleFactorWindow> window2 = createWindow();
  58. [window2.get() setBackingScaleFactor:4];
  59. [[window2.get() contentView] addSubview:view];
  60. EXPECT_JS_EQ(view, "window.devicePixelRatio", "4");
  61. EXPECT_JS_EQ(view, "devicePixelRatioFromStyle()", "4");
  62. [view removeFromSuperview];
  63. EXPECT_JS_EQ(view, "window.devicePixelRatio", "1");
  64. EXPECT_JS_EQ(view, "devicePixelRatioFromStyle()", "1");
  65. }
  66. TEST_F(DynamicDeviceScaleFactor, WebKit)
  67. {
  68. runWebKit1Test();
  69. }
  70. TEST_F(DynamicDeviceScaleFactor, WebKit2)
  71. {
  72. runWebKit2Test();
  73. }
  74. } // namespace TestWebKitAPI