1/*
2 * Copyright (C) 2017 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
26#import "config.h"
27
28#if WK_API_ENABLED
29
30#if ENABLE(MEDIA_STREAM)
31
32#import "PlatformUtilities.h"
33#import "Test.h"
34#import "TestWKWebView.h"
35#import <WebKit/WKPreferencesPrivate.h>
36#import <WebKit/WKUIDelegatePrivate.h>
37#import <WebKit/WKWebViewConfiguration.h>
38#import <WebKit/_WKProcessPoolConfiguration.h>
39
40static bool hasRecievedCorrectCaptureState = false;
41
42@interface MediaStreamTrackDetachedUIDelegate : NSObject<WKUIDelegate>
43- (void)_webView:(WKWebView *)webView requestUserMediaAuthorizationForDevices:(_WKCaptureDevices)devices url:(NSURL *)url mainFrameURL:(NSURL *)mainFrameURL decisionHandler:(void (^)(BOOL authorized))decisionHandler;
44- (void)_webView:(WKWebView *)webView checkUserMediaPermissionForURL:(NSURL *)url mainFrameURL:(NSURL *)mainFrameURL frameIdentifier:(NSUInteger)frameIdentifier decisionHandler:(void (^)(NSString *salt, BOOL authorized))decisionHandler;
45- (void)_webView:(WKWebView *)webView mediaCaptureStateDidChange:(_WKMediaCaptureState)state;
46@end
47
48@implementation MediaStreamTrackDetachedUIDelegate
49- (void)_webView:(WKWebView *)webView requestUserMediaAuthorizationForDevices:(_WKCaptureDevices)devices url:(NSURL *)url mainFrameURL:(NSURL *)mainFrameURL decisionHandler:(void (^)(BOOL authorized))decisionHandler
50{
51 decisionHandler(YES);
52}
53
54- (void)_webView:(WKWebView *)webView checkUserMediaPermissionForURL:(NSURL *)url mainFrameURL:(NSURL *)mainFrameURL frameIdentifier:(NSUInteger)frameIdentifier decisionHandler:(void (^)(NSString *salt, BOOL authorized))decisionHandler
55{
56 decisionHandler(@"0x987654321", YES);
57}
58- (void)_webView:(WKWebView *)webView mediaCaptureStateDidChange:(_WKMediaCaptureState)state
59{
60 if (state == _WKMediaCaptureStateActiveMicrophone)
61 hasRecievedCorrectCaptureState = true;
62}
63@end
64
65namespace TestWebKitAPI {
66
67TEST(WebKit2, MediaStreamTrackDetached)
68{
69 auto configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
70 auto processPoolConfig = adoptNS([[_WKProcessPoolConfiguration alloc] init]);
71 auto preferences = [configuration preferences];
72 preferences._mediaCaptureRequiresSecureConnection = NO;
73 preferences._mediaDevicesEnabled = YES;
74 auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500) configuration:configuration.get() processPoolConfiguration:processPoolConfig.get()]);
75 auto delegate = adoptNS([[MediaStreamTrackDetachedUIDelegate alloc] init]);
76 webView.get().UIDelegate = delegate.get();
77
78 hasRecievedCorrectCaptureState = false;
79 [webView loadTestPageNamed:@"mediastreamtrack-detached"];
80
81 TestWebKitAPI::Util::run(&hasRecievedCorrectCaptureState);
82
83
84}
85
86} // namespace TestWebKitAPI
87
88#endif // ENABLE(MEDIA_STREAM)
89
90#endif // WK_API_ENABLED