Changeset 203905 in webkit


Ignore:
Timestamp:
Jul 29, 2016 10:49:03 AM (8 years ago)
Author:
Wenson Hsieh
Message:

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):

Location:
trunk/Tools
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/Tools/ChangeLog

    r203902 r203905  
     12016-07-29  Wenson Hsieh  <wenson_hsieh@apple.com>
     2
     3        Add TestWebKitAPI support for interacting with media controls
     4        https://bugs.webkit.org/show_bug.cgi?id=160342
     5        <rdar://problem/27610246>
     6
     7        Reviewed by Beth Dakin.
     8
     9        Adds support for testing interaction with some media controls, as well as a basic test
     10        verifying that media control teardown after interaction does not result in a crash.
     11
     12        * TestWebKitAPI/Tests/WebKit2Cocoa/VideoControlsManager.mm:
     13        (-[WKWebView mouseDownAtPoint:]):
     14        (-[WKWebView performAfterLoading:]):
     15        (TestWebKitAPI::TEST):
     16
    1172016-07-29  Jonathan Bedard  <jbedard@apple.com>
    218
  • trunk/Tools/TestWebKitAPI/Tests/WebKit2Cocoa/VideoControlsManager.mm

    r203698 r203905  
    4141static bool receivedScriptMessage;
    4242
    43 @interface WKWebView (UserInteraction)
    44 
    45 - (void)mouseDownAtPoint:(NSPoint)point;
    46 
    47 @end
    48 
    49 @implementation WKWebView (UserInteraction)
    50 
    51 - (void)mouseDownAtPoint:(NSPoint)point {
    52     [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]];
    53 }
    54 
    55 @end
    56 
    5743@interface MediaPlaybackMessageHandler : NSObject <WKScriptMessageHandler> {
    5844    RetainPtr<WKWebView> _webView;
     
    128114@end
    129115
     116@interface WKWebView (WKWebViewAdditions)
     117
     118- (void)_interactWithMediaControlsForTesting;
     119
     120@end
     121
     122@interface WKWebView (TestingAdditions)
     123
     124- (void)mouseDownAtPoint:(NSPoint)point;
     125- (void)performAfterLoading:(dispatch_block_t)actions;
     126
     127@end
     128
     129@implementation WKWebView (TestingAdditions)
     130
     131- (void)mouseDownAtPoint:(NSPoint)point {
     132    [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]];
     133}
     134
     135- (void)performAfterLoading:(dispatch_block_t)actions {
     136    OnLoadMessageHandler *handler = [[OnLoadMessageHandler alloc] initWithWKWebView:self handler:actions];
     137    NSString *onloadScript = @"window.onload = function() { window.webkit.messageHandlers.onloadHandler.postMessage('loaded'); }";
     138    WKUserScript *script = [[WKUserScript alloc] initWithSource:onloadScript injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:NO];
     139    [[[self configuration] userContentController] addUserScript:script];
     140    [[[self configuration] userContentController] addScriptMessageHandler:handler name:@"onloadHandler"];
     141}
     142
     143@end
     144
    130145namespace TestWebKitAPI {
    131146
     
    351366}
    352367
     368TEST(VideoControlsManager, VideoControlsManagerTearsDownMediaControlsOnDealloc)
     369{
     370    RetainPtr<WKWebViewConfiguration> configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
     371    configuration.get().mediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypeNone;
     372    RetainPtr<WKWebView> webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 100, 100) configuration:configuration.get()]);
     373
     374    RetainPtr<NSWindow> window = adoptNS([[NSWindow alloc] initWithContentRect:[webView frame] styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO]);
     375    [[window contentView] addSubview:webView.get()];
     376
     377    NSURL *urlOfVideo = [[NSBundle mainBundle] URLForResource:@"video-with-audio" withExtension:@"mp4" subdirectory:@"TestWebKitAPI.resources"];
     378    [webView loadFileURL:urlOfVideo allowingReadAccessToURL:[urlOfVideo URLByDeletingLastPathComponent]];
     379
     380    __block bool finishedTest = false;
     381    [webView performAfterLoading:^()
     382    {
     383        // Verify that we tear down the media controls properly, such that we don't crash when the web view is released.
     384        if ([webView respondsToSelector:@selector(_interactWithMediaControlsForTesting)])
     385            [webView _interactWithMediaControlsForTesting];
     386
     387        [webView release];
     388        finishedTest = true;
     389    }];
     390
     391    TestWebKitAPI::Util::run(&finishedTest);
     392}
     393
    353394TEST(VideoControlsManager, VideoControlsManagerSmallVideoInMediaDocument)
    354395{
     
    361402   
    362403    __block bool finishedLoad = false;
    363     dispatch_block_t handleFinishedLoad = ^()
     404    [webView performAfterLoading:^()
    364405    {
    365406        finishedLoad = true;
    366     };
    367     OnLoadMessageHandler *handler = [[OnLoadMessageHandler alloc] initWithWKWebView:webView.get() handler:handleFinishedLoad];
    368    
    369     NSString *onloadScript = @"window.onload = function() { window.webkit.messageHandlers.onloadHandler.postMessage('loaded'); }";
    370     WKUserScript *script = [[WKUserScript alloc] initWithSource:onloadScript injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:NO];
    371     [[configuration userContentController] addUserScript:script];
    372     [[configuration userContentController] addScriptMessageHandler:handler name:@"onloadHandler"];
     407    }];
    373408   
    374409    NSURL *urlOfVideo = [[NSBundle mainBundle] URLForResource:@"video-with-audio" withExtension:@"mp4" subdirectory:@"TestWebKitAPI.resources"];
Note: See TracChangeset for help on using the changeset viewer.