blob: de8e06b3aaec41a1b195f81ce37a3917bb08e735 [file] [log] [blame] [edit]
/*
* Copyright (C) 2022 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.
*/
#if !__has_feature(objc_arc)
#error This file requires ARC. Add the "-fobjc-arc" compiler flag for this file.
#endif
#import "config.h"
#import "WebExtensionUtilities.h"
@interface TestWebExtensionManager () <_WKWebExtensionControllerDelegatePrivate>
@end
@implementation TestWebExtensionManager {
bool _done;
}
- (instancetype)initForExtension:(_WKWebExtension *)extension
{
if (!(self = [super init]))
return nil;
_extension = extension;
_context = [[_WKWebExtensionContext alloc] initForExtension:extension];
_controller = [[_WKWebExtensionController alloc] initWithConfiguration:_WKWebExtensionControllerConfiguration.nonPersistentConfiguration];
_context._testingMode = YES;
// This should always be self. If you need the delegate, use the controllerDelegate property.
// Delegate method calls will be forwarded to the controllerDelegate.
_controller.delegate = self;
return self;
}
- (BOOL)respondsToSelector:(SEL)selector
{
return [_controllerDelegate respondsToSelector:selector] || [super respondsToSelector:selector];
}
- (id)forwardingTargetForSelector:(SEL)selector
{
return [_controllerDelegate respondsToSelector:selector] ? _controllerDelegate : [super forwardingTargetForSelector:selector];
}
- (void)load
{
NSError *error;
EXPECT_TRUE([_controller loadExtensionContext:_context error:&error]);
EXPECT_NULL(error);
}
- (void)run
{
_done = false;
TestWebKitAPI::Util::run(&_done);
}
- (void)loadAndRun
{
[self load];
[self run];
}
- (void)_webExtensionController:(_WKWebExtensionController *)controller recordTestAssertionResult:(BOOL)result withMessage:(NSString *)message andSourceURL:(NSString *)sourceURL lineNumber:(unsigned)lineNumber forExtensionContext:(_WKWebExtensionContext *)context
{
if (result)
return;
if (!message.length)
message = @"Assertion failed with no message.";
::testing::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure, sourceURL.UTF8String, lineNumber, message.UTF8String) = ::testing::Message();
}
- (void)_webExtensionController:(_WKWebExtensionController *)controller recordTestEqualityResult:(BOOL)result expectedValue:(NSString *)expectedValue actualValue:(NSString *)actualValue withMessage:(NSString *)message andSourceURL:(NSString *)sourceURL lineNumber:(unsigned)lineNumber forExtensionContext:(_WKWebExtensionContext *)context
{
if (result)
return;
if (!message.length)
message = @"Expected equality of these values";
::testing::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure, sourceURL.UTF8String, lineNumber, "") = ::testing::Message()
<< message.UTF8String << ":\n"
<< " " << actualValue.UTF8String << "\n"
<< " " << expectedValue.UTF8String;
}
- (void)_webExtensionController:(_WKWebExtensionController *)controller recordTestMessage:(NSString *)message andSourceURL:(NSString *)sourceURL lineNumber:(unsigned)lineNumber forExtensionContext:(_WKWebExtensionContext *)context
{
printf("\n%s:%u\n%s\n\n", sourceURL.UTF8String, lineNumber, message.UTF8String);
}
- (void)_webExtensionController:(_WKWebExtensionController *)controller recordTestYieldedWithMessage:(NSString *)message andSourceURL:(NSString *)sourceURL lineNumber:(unsigned)lineNumber forExtensionContext:(_WKWebExtensionContext *)context
{
_done = true;
_yieldMessage = [message copy];
}
- (void)_webExtensionController:(_WKWebExtensionController *)controller recordTestFinishedWithResult:(BOOL)result message:(NSString *)message andSourceURL:(NSString *)sourceURL lineNumber:(unsigned)lineNumber forExtensionContext:(_WKWebExtensionContext *)context
{
_done = true;
if (result)
return;
if (!message.length)
message = @"Test failed with no message.";
::testing::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure, sourceURL.UTF8String, lineNumber, message.UTF8String) = ::testing::Message();
}
@end
namespace TestWebKitAPI {
namespace Util {
RetainPtr<TestWebExtensionManager> loadAndRunExtension(_WKWebExtension *extension)
{
auto manager = adoptNS([[TestWebExtensionManager alloc] initForExtension:extension]);
[manager loadAndRun];
return manager;
}
RetainPtr<TestWebExtensionManager> loadAndRunExtension(NSDictionary *manifest, NSDictionary *resources)
{
return loadAndRunExtension([[_WKWebExtension alloc] _initWithManifestDictionary:manifest resources:resources]);
}
RetainPtr<TestWebExtensionManager> loadAndRunExtension(NSDictionary *resources)
{
return loadAndRunExtension([[_WKWebExtension alloc] _initWithResources:resources]);
}
RetainPtr<TestWebExtensionManager> loadAndRunExtension(NSURL *baseURL)
{
return loadAndRunExtension([[_WKWebExtension alloc] initWithResourceBaseURL:baseURL error:nullptr]);
}
} // namespace Util
} // namespace TestWebKitAPI