/* * Copyright (C) 2016 Apple Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF * THE POSSIBILITY OF SUCH DAMAGE. */ #import "config.h" #import #if PLATFORM(MAC) #import "PlatformUtilities.h" #import "Test.h" #import "TestNavigationDelegate.h" #import "TestWKWebView.h" #import #import #import #import #import #import static bool didForceRepaint; using namespace TestWebKitAPI; @interface WKWebView () - (WKPageRef)_pageForTesting; @end @interface SnapshotTestWKWebView : TestWKWebView @end @implementation SnapshotTestWKWebView - (instancetype)init { self = [super initWithFrame:NSMakeRect(0, 0, 800, 600)]; if (self) { [self _disableBackForwardSnapshotVolatilityForTesting]; [self setAllowsBackForwardNavigationGestures:YES]; [self _setWindowOcclusionDetectionEnabled:NO]; [self.window orderBack:nil]; } return self; } static void forceRepaintCallback(WKErrorRef error, void*) { EXPECT_NULL(error); didForceRepaint = true; } - (void)synchronouslyForceRepaint { didForceRepaint = false; WKPageForceRepaint([self _pageForTesting], 0, forceRepaintCallback); TestWebKitAPI::Util::run(&didForceRepaint); } - (void)synchronouslyLoadTestPageAndForceRepaint:(NSString *)testPageName { [self synchronouslyLoadTestPageNamed:testPageName]; [self synchronouslyForceRepaint]; [[self window] display]; } @end static bool imagesAreEqual(CGImageRef first, CGImageRef second) { RetainPtr firstData = adoptNS((NSData *)CGDataProviderCopyData(CGImageGetDataProvider(first))); RetainPtr secondData = adoptNS((NSData *)CGDataProviderCopyData(CGImageGetDataProvider(second))); return [firstData isEqualTo:secondData.get()]; } TEST(SnapshotStore, SnapshotUponNavigation) { RetainPtr webView = adoptNS([[SnapshotTestWKWebView alloc] init]); [webView synchronouslyLoadTestPageAndForceRepaint:@"simple"]; RetainPtr firstItem = [[webView backForwardList] currentItem]; RetainPtr imageBeforeNavigation = adoptCF([firstItem _copySnapshotForTesting]); EXPECT_NULL(imageBeforeNavigation.get()); [webView synchronouslyLoadTestPageAndForceRepaint:@"lots-of-text"]; // After navigating, the first item should have a valid back-forward snapshot. RetainPtr imageAfterNavigation = adoptCF([firstItem _copySnapshotForTesting]); EXPECT_NOT_NULL(imageAfterNavigation.get()); EXPECT_EQ(CGImageGetWidth(imageAfterNavigation.get()), (unsigned long)(800 * [webView window].backingScaleFactor)); } TEST(SnapshotStore, SnapshotClearedWhenItemIsRemoved) { RetainPtr webView = adoptNS([[SnapshotTestWKWebView alloc] init]); [webView synchronouslyLoadTestPageAndForceRepaint:@"simple"]; [webView synchronouslyLoadTestPageAndForceRepaint:@"lots-of-text"]; RetainPtr item = [[webView backForwardList] currentItem]; [webView synchronouslyLoadTestPageAndForceRepaint:@"simple"]; EXPECT_NOT_NULL(adoptCF([item _copySnapshotForTesting]).get()); [webView goBack]; [webView _test_waitForDidFinishNavigation]; [webView goBack]; [webView _test_waitForDidFinishNavigation]; // The original second item is still in the forward list, and should still have a snapshot. EXPECT_NOT_NULL(adoptCF([item _copySnapshotForTesting]).get()); [webView synchronouslyLoadTestPageAndForceRepaint:@"lots-of-text"]; // The original second item shouldn't have an image anymore, because it was invalidated // by navigating back past it and then doing another load. EXPECT_NULL(adoptCF([item _copySnapshotForTesting]).get()); } static RetainPtr makeRedSquareView() { RetainPtr redSquare = adoptNS([[NSBox alloc] initWithFrame:NSMakeRect(0, 0, 200, 200)]); [redSquare setBoxType:NSBoxCustom]; [redSquare setFillColor:[NSColor redColor]]; return redSquare; } TEST(SnapshotStore, ExplicitSnapshotsChangeUponNavigation) { RetainPtr webView = adoptNS([[SnapshotTestWKWebView alloc] init]); RetainPtr redSquare = makeRedSquareView(); [[webView superview] addSubview:redSquare.get()]; [webView synchronouslyLoadTestPageAndForceRepaint:@"lots-of-text"]; RetainPtr firstItem = [[webView backForwardList] currentItem]; [webView _saveBackForwardSnapshotForItem:firstItem.get()]; [redSquare removeFromSuperview]; RetainPtr initialSnapshot = adoptCF([firstItem _copySnapshotForTesting]); EXPECT_NOT_NULL(initialSnapshot); [webView synchronouslyLoadTestPageAndForceRepaint:@"simple"]; // After navigating, the first item's snapshot should change. RetainPtr snapshotAfterNavigation = adoptCF([firstItem _copySnapshotForTesting]); EXPECT_NOT_NULL(snapshotAfterNavigation.get()); EXPECT_FALSE(imagesAreEqual(initialSnapshot.get(), snapshotAfterNavigation.get())); } TEST(SnapshotStore, SnapshotsForNeverLoadedPagesDoNotChangeUponNavigation) { RetainPtr webView = adoptNS([[SnapshotTestWKWebView alloc] init]); RetainPtr redSquare = makeRedSquareView(); [[webView superview] addSubview:redSquare.get()]; [webView synchronouslyLoadTestPageAndForceRepaint:@"simple"]; WKRetainPtr sessionState = adoptWK(static_cast(WKPageCopySessionState([webView _pageForTesting], nullptr, nullptr))); WKPageRestoreFromSessionStateWithoutNavigation([webView _pageForTesting], sessionState.get()); RetainPtr firstItem = [[webView backForwardList] currentItem]; [webView _saveBackForwardSnapshotForItem:firstItem.get()]; [redSquare removeFromSuperview]; [webView synchronouslyLoadTestPageAndForceRepaint:@"lots-of-text"]; RetainPtr initialSnapshot = adoptCF([firstItem _copySnapshotForTesting]); EXPECT_NOT_NULL(initialSnapshot); [webView synchronouslyLoadTestPageAndForceRepaint:@"simple"]; // After navigating, the first item's snapshot should not change, because it was // never loaded into the view. RetainPtr snapshotAfterNavigation = adoptCF([firstItem _copySnapshotForTesting]); EXPECT_NOT_NULL(snapshotAfterNavigation.get()); EXPECT_TRUE(imagesAreEqual(initialSnapshot.get(), snapshotAfterNavigation.get())); } TEST(SnapshotStore, SnapshottingNullBackForwardItemShouldNotCrash) { RetainPtr webView = adoptNS([[SnapshotTestWKWebView alloc] init]); [webView _saveBackForwardSnapshotForItem:nil]; } #endif // PLATFORM(MAC)