Add TestWebKitAPI support for interacting with media controls
authorwenson_hsieh@apple.com <wenson_hsieh@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 29 Jul 2016 17:49:03 +0000 (17:49 +0000)
committerwenson_hsieh@apple.com <wenson_hsieh@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 29 Jul 2016 17:49:03 +0000 (17:49 +0000)
https://bugs.webkit.org/show_bug.cgi?id=160342
<rdar://problem/27610246>

Reviewed by Beth Dakin.

Adds support for testing interaction with some media controls, as well as a basic test
verifying that media control teardown after interaction does not result in a crash.

* TestWebKitAPI/Tests/WebKit2Cocoa/VideoControlsManager.mm:
(-[WKWebView mouseDownAtPoint:]):
(-[WKWebView performAfterLoading:]):
(TestWebKitAPI::TEST):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@203905 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Tools/ChangeLog
Tools/TestWebKitAPI/Tests/WebKit2Cocoa/VideoControlsManager.mm

index 9c4d356..3f95464 100644 (file)
@@ -1,3 +1,19 @@
+2016-07-29  Wenson Hsieh  <wenson_hsieh@apple.com>
+
+        Add TestWebKitAPI support for interacting with media controls
+        https://bugs.webkit.org/show_bug.cgi?id=160342
+        <rdar://problem/27610246>
+
+        Reviewed by Beth Dakin.
+
+        Adds support for testing interaction with some media controls, as well as a basic test
+        verifying that media control teardown after interaction does not result in a crash.
+
+        * TestWebKitAPI/Tests/WebKit2Cocoa/VideoControlsManager.mm:
+        (-[WKWebView mouseDownAtPoint:]):
+        (-[WKWebView performAfterLoading:]):
+        (TestWebKitAPI::TEST):
+
 2016-07-29  Jonathan Bedard  <jbedard@apple.com>
 
         Crash Log Clobbering on Unbound Crashes
index 74afc25..de7a127 100644 (file)
 static bool testedControlsManagerAfterPlaying;
 static bool receivedScriptMessage;
 
-@interface WKWebView (UserInteraction)
-
-- (void)mouseDownAtPoint:(NSPoint)point;
-
-@end
-
-@implementation WKWebView (UserInteraction)
-
-- (void)mouseDownAtPoint:(NSPoint)point {
-    [self mouseDown:[NSEvent mouseEventWithType:NSEventTypeLeftMouseDown location:NSMakePoint(point.x, point.y) modifierFlags:0 timestamp:GetCurrentEventTime() windowNumber:0 context:[NSGraphicsContext currentContext] eventNumber:0 clickCount:0 pressure:0]];
-}
-
-@end
-
 @interface MediaPlaybackMessageHandler : NSObject <WKScriptMessageHandler> {
     RetainPtr<WKWebView> _webView;
 }
@@ -127,6 +113,35 @@ static bool receivedScriptMessage;
 }
 @end
 
+@interface WKWebView (WKWebViewAdditions)
+
+- (void)_interactWithMediaControlsForTesting;
+
+@end
+
+@interface WKWebView (TestingAdditions)
+
+- (void)mouseDownAtPoint:(NSPoint)point;
+- (void)performAfterLoading:(dispatch_block_t)actions;
+
+@end
+
+@implementation WKWebView (TestingAdditions)
+
+- (void)mouseDownAtPoint:(NSPoint)point {
+    [self mouseDown:[NSEvent mouseEventWithType:NSEventTypeLeftMouseDown location:NSMakePoint(point.x, point.y) modifierFlags:0 timestamp:GetCurrentEventTime() windowNumber:0 context:[NSGraphicsContext currentContext] eventNumber:0 clickCount:0 pressure:0]];
+}
+
+- (void)performAfterLoading:(dispatch_block_t)actions {
+    OnLoadMessageHandler *handler = [[OnLoadMessageHandler alloc] initWithWKWebView:self handler:actions];
+    NSString *onloadScript = @"window.onload = function() { window.webkit.messageHandlers.onloadHandler.postMessage('loaded'); }";
+    WKUserScript *script = [[WKUserScript alloc] initWithSource:onloadScript injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:NO];
+    [[[self configuration] userContentController] addUserScript:script];
+    [[[self configuration] userContentController] addScriptMessageHandler:handler name:@"onloadHandler"];
+}
+
+@end
+
 namespace TestWebKitAPI {
 
 TEST(VideoControlsManager, VideoControlsManagerSingleLargeVideo)
@@ -350,6 +365,32 @@ TEST(VideoControlsManager, VideoControlsManagerAudioElementStartedWithScript)
     TestWebKitAPI::Util::run(&receivedScriptMessage);
 }
 
+TEST(VideoControlsManager, VideoControlsManagerTearsDownMediaControlsOnDealloc)
+{
+    RetainPtr<WKWebViewConfiguration> configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
+    configuration.get().mediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypeNone;
+    RetainPtr<WKWebView> webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 100, 100) configuration:configuration.get()]);
+
+    RetainPtr<NSWindow> window = adoptNS([[NSWindow alloc] initWithContentRect:[webView frame] styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO]);
+    [[window contentView] addSubview:webView.get()];
+
+    NSURL *urlOfVideo = [[NSBundle mainBundle] URLForResource:@"video-with-audio" withExtension:@"mp4" subdirectory:@"TestWebKitAPI.resources"];
+    [webView loadFileURL:urlOfVideo allowingReadAccessToURL:[urlOfVideo URLByDeletingLastPathComponent]];
+
+    __block bool finishedTest = false;
+    [webView performAfterLoading:^()
+    {
+        // Verify that we tear down the media controls properly, such that we don't crash when the web view is released.
+        if ([webView respondsToSelector:@selector(_interactWithMediaControlsForTesting)])
+            [webView _interactWithMediaControlsForTesting];
+
+        [webView release];
+        finishedTest = true;
+    }];
+
+    TestWebKitAPI::Util::run(&finishedTest);
+}
+
 TEST(VideoControlsManager, VideoControlsManagerSmallVideoInMediaDocument)
 {
     RetainPtr<WKWebViewConfiguration> configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
@@ -360,16 +401,10 @@ TEST(VideoControlsManager, VideoControlsManagerSmallVideoInMediaDocument)
     [[window contentView] addSubview:webView.get()];
     
     __block bool finishedLoad = false;
-    dispatch_block_t handleFinishedLoad = ^()
+    [webView performAfterLoading:^()
     {
         finishedLoad = true;
-    };
-    OnLoadMessageHandler *handler = [[OnLoadMessageHandler alloc] initWithWKWebView:webView.get() handler:handleFinishedLoad];
-    
-    NSString *onloadScript = @"window.onload = function() { window.webkit.messageHandlers.onloadHandler.postMessage('loaded'); }";
-    WKUserScript *script = [[WKUserScript alloc] initWithSource:onloadScript injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:NO];
-    [[configuration userContentController] addUserScript:script];
-    [[configuration userContentController] addScriptMessageHandler:handler name:@"onloadHandler"];
+    }];
     
     NSURL *urlOfVideo = [[NSBundle mainBundle] URLForResource:@"video-with-audio" withExtension:@"mp4" subdirectory:@"TestWebKitAPI.resources"];
     [webView loadFileURL:urlOfVideo allowingReadAccessToURL:[urlOfVideo URLByDeletingLastPathComponent]];