ResizeViewWhileHidden.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 "PlatformUtilities.h"
  27. #include "PlatformWebView.h"
  28. #include <WebKit2/WKRetainPtr.h>
  29. namespace TestWebKitAPI {
  30. static bool didFinishLoad;
  31. static void didFinishLoadForFrame(WKPageRef, WKFrameRef, WKTypeRef, const void*)
  32. {
  33. didFinishLoad = true;
  34. }
  35. static void setPageLoaderClient(WKPageRef page)
  36. {
  37. WKPageLoaderClient loaderClient;
  38. memset(&loaderClient, 0, sizeof(loaderClient));
  39. loaderClient.version = 0;
  40. loaderClient.didFinishLoadForFrame = didFinishLoadForFrame;
  41. WKPageSetPageLoaderClient(page, &loaderClient);
  42. }
  43. static void flushMessages(WKPageRef page)
  44. {
  45. // In order to ensure all pending messages have been handled by the UI and web processes, we
  46. // load a URL and wait for the load to finish.
  47. setPageLoaderClient(page);
  48. WKPageLoadURL(page, adoptWK(Util::createURLForResource("simple", "html")).get());
  49. Util::run(&didFinishLoad);
  50. didFinishLoad = false;
  51. WKPageSetPageLoaderClient(page, 0);
  52. }
  53. static bool timerFired;
  54. static void CALLBACK timerCallback(HWND hwnd, UINT, UINT_PTR timerID, DWORD)
  55. {
  56. ::KillTimer(hwnd, timerID);
  57. timerFired = true;
  58. }
  59. static void runForDuration(double seconds)
  60. {
  61. ::SetTimer(0, 0, seconds * 1000, timerCallback);
  62. Util::run(&timerFired);
  63. timerFired = false;
  64. }
  65. static void waitForBackingStoreUpdate(WKPageRef page)
  66. {
  67. // Wait for the web process to handle the changes we just made, to perform a display (which
  68. // happens on a timer), and to tell the UI process about the display (which updates the backing
  69. // store).
  70. // FIXME: It would be much less fragile (and maybe faster) to have an explicit way to wait
  71. // until the backing store is updated.
  72. runForDuration(0.5);
  73. flushMessages(page);
  74. }
  75. TEST(WebKit2, ResizeViewWhileHidden)
  76. {
  77. WKRetainPtr<WKContextRef> context(AdoptWK, WKContextCreate());
  78. PlatformWebView webView(context.get());
  79. HWND window = WKViewGetWindow(webView.platformView());
  80. RECT originalRect;
  81. ::GetClientRect(window, &originalRect);
  82. RECT newRect = originalRect;
  83. ::InflateRect(&newRect, 1, 1);
  84. // Show the WKView and resize it so that the WKView's backing store will be created. Ideally
  85. // we'd have some more explicit way of forcing the backing store to be created.
  86. ::ShowWindow(window, SW_SHOW);
  87. webView.resizeTo(newRect.right - newRect.left, newRect.bottom - newRect.top);
  88. waitForBackingStoreUpdate(webView.page());
  89. // Resize the window while hidden and show it again so that it will update its backing store at
  90. // the new size.
  91. ::ShowWindow(window, SW_HIDE);
  92. webView.resizeTo(originalRect.right - originalRect.left, originalRect.bottom - originalRect.top);
  93. ::ShowWindow(window, SW_SHOW);
  94. // Force the WKView to paint to try to trigger <http://webkit.org/b/54142>.
  95. ::SendMessage(window, WM_PAINT, 0, 0);
  96. // In Debug builds without the fix for <http://webkit.org/b/54141>, the web process will assert
  97. // at this point.
  98. // FIXME: It would be good to have a way to check that our behavior is correct in Release
  99. // builds, too!
  100. waitForBackingStoreUpdate(webView.page());
  101. }
  102. } // namespace TestWebKitAPI