| /* |
| * 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. |
| */ |
| |
| #import "config.h" |
| |
| #if ENABLE(WK_WEB_EXTENSIONS) |
| |
| #import "WebExtensionUtilities.h" |
| |
| namespace TestWebKitAPI { |
| |
| static auto *manifest = @{ |
| @"manifest_version": @3, |
| @"background": @{ |
| @"scripts": @[ @"background.js" ], |
| @"type": @"module", |
| @"persistent": @NO |
| }, |
| @"unused" : [NSNull null] |
| }; |
| |
| TEST(WKWebExtensionAPIRuntime, GetURL) |
| { |
| auto *baseURLString = @"test-extension://76C788B8-3374-400D-8259-40E5B9DF79D3"; |
| |
| auto *backgroundScript = Util::constructScript(@[ |
| // Variable Setup |
| [NSString stringWithFormat:@"const baseURL = '%@'", baseURLString], |
| |
| // Error Cases |
| @"browser.test.assertThrows(() => browser.runtime.getURL(), /A required argument is missing./)", |
| @"browser.test.assertThrows(() => browser.runtime.getURL(null), /Expected a string./)", |
| @"browser.test.assertThrows(() => browser.runtime.getURL(undefined), /Expected a string./)", |
| |
| // Normal Cases |
| @"browser.test.assertEq(browser.runtime.getURL(''), `${baseURL}/`)", |
| @"browser.test.assertEq(browser.runtime.getURL('test.js'), `${baseURL}/test.js`)", |
| @"browser.test.assertEq(browser.runtime.getURL('/test.js'), `${baseURL}/test.js`)", |
| @"browser.test.assertEq(browser.runtime.getURL('../../test.js'), `${baseURL}/test.js`)", |
| @"browser.test.assertEq(browser.runtime.getURL('./test.js'), `${baseURL}/test.js`)", |
| @"browser.test.assertEq(browser.runtime.getURL('././/example'), `${baseURL}//example`)", |
| @"browser.test.assertEq(browser.runtime.getURL('../../example/..//test/'), `${baseURL}//test/`)", |
| @"browser.test.assertEq(browser.runtime.getURL('.'), `${baseURL}/`)", |
| @"browser.test.assertEq(browser.runtime.getURL('..//../'), `${baseURL}/`)", |
| @"browser.test.assertEq(browser.runtime.getURL('.././..'), `${baseURL}/`)", |
| @"browser.test.assertEq(browser.runtime.getURL('/.././.'), `${baseURL}/`)", |
| |
| // Unexpected Cases |
| // FIXME: <https://webkit.org/b/248154> browser.runtime.getURL() has some edge cases that should be failures or return different results |
| @"browser.test.assertEq(browser.runtime.getURL(42), `${baseURL}/42`)", |
| @"browser.test.assertEq(browser.runtime.getURL(/test/), `${baseURL}/test/`)", |
| @"browser.test.assertEq(browser.runtime.getURL('//'), 'test-extension://')", |
| @"browser.test.assertEq(browser.runtime.getURL('//example'), `test-extension://example`)", |
| @"browser.test.assertEq(browser.runtime.getURL('///'), 'test-extension:///')", |
| |
| // Finish |
| @"browser.test.notifyPass()" |
| ]); |
| |
| auto extension = adoptNS([[_WKWebExtension alloc] _initWithManifestDictionary:manifest resources:@{ @"background.js": backgroundScript }]); |
| auto manager = adoptNS([[TestWebExtensionManager alloc] initForExtension:extension.get()]); |
| |
| // Set a base URL so it is a known value and not the default random one. |
| manager.get().context.baseURL = [NSURL URLWithString:baseURLString]; |
| |
| [manager loadAndRun]; |
| } |
| |
| TEST(WKWebExtensionAPIRuntime, Id) |
| { |
| auto *uniqueIdentifier = @"org.webkit.test.extension (76C788B8)"; |
| |
| auto *backgroundScript = Util::constructScript(@[ |
| [NSString stringWithFormat:@"browser.test.assertEq(browser.runtime.id, '%@')", uniqueIdentifier], |
| |
| // Finish |
| @"browser.test.notifyPass()" |
| ]); |
| |
| auto extension = adoptNS([[_WKWebExtension alloc] _initWithManifestDictionary:manifest resources:@{ @"background.js": backgroundScript }]); |
| auto manager = adoptNS([[TestWebExtensionManager alloc] initForExtension:extension.get()]); |
| |
| // Set an uniqueIdentifier so it is a known value and not the default random one. |
| manager.get().context.uniqueIdentifier = uniqueIdentifier; |
| |
| [manager loadAndRun]; |
| } |
| |
| TEST(WKWebExtensionAPIRuntime, GetManifest) |
| { |
| auto *backgroundScript = Util::constructScript(@[ |
| @"browser.test.assertDeepEq(browser.runtime.getManifest(), { manifest_version: 3, background: { persistent: false, scripts: [ 'background.js' ], type: 'module' }, unused: null })", |
| |
| // Finish |
| @"browser.test.notifyPass()" |
| ]); |
| |
| Util::loadAndRunExtension(manifest, @{ @"background.js": backgroundScript }); |
| } |
| |
| TEST(WKWebExtensionAPIRuntime, GetPlatformInfo) |
| { |
| #if PLATFORM(MAC) && (CPU(ARM) || CPU(ARM64)) |
| auto *expectedInfo = @"{ os: 'mac', arch: 'arm' }"; |
| #elif PLATFORM(MAC) && (CPU(X86_64)) |
| auto *expectedInfo = @"{ os: 'mac', arch: 'x86-64' }"; |
| #elif PLATFORM(IOS_FAMILY) && (CPU(ARM) || CPU(ARM64)) |
| auto *expectedInfo = @"{ os: 'ios', arch: 'arm' }"; |
| #elif PLATFORM(IOS_FAMILY) && (CPU(X86_64)) |
| auto *expectedInfo = @"{ os: 'ios', arch: 'x86-64' }"; |
| #else |
| auto *expectedInfo = @"{ os: 'unknown', arch: 'unknown' }"; |
| #endif |
| |
| auto *backgroundScript = Util::constructScript(@[ |
| @"browser.test.assertTrue(browser.runtime.getPlatformInfo() instanceof Promise)", |
| [NSString stringWithFormat:@"browser.test.assertDeepEq(await browser.runtime.getPlatformInfo(), %@)", expectedInfo], |
| [NSString stringWithFormat:@"browser.test.assertEq(browser.runtime.getPlatformInfo((info) => browser.test.assertDeepEq(info, %@)), undefined)", expectedInfo], |
| |
| // Finish |
| @"browser.test.notifyPass()" |
| ]); |
| |
| Util::loadAndRunExtension(manifest, @{ @"background.js": backgroundScript }); |
| } |
| |
| TEST(WKWebExtensionAPIRuntime, LastError) |
| { |
| auto *backgroundScript = Util::constructScript(@[ |
| @"browser.test.assertEq(typeof browser.runtime.lastError, 'object')", |
| @"browser.test.assertEq(browser.runtime.lastError, null)", |
| |
| // FIXME: <https://webkit.org/b/248156> Need test cases for lastError |
| |
| // Finish |
| @"browser.test.notifyPass()" |
| ]); |
| |
| Util::loadAndRunExtension(manifest, @{ @"background.js": backgroundScript }); |
| } |
| |
| } // namespace TestWebKitAPI |
| |
| #endif // ENABLE(WK_WEB_EXTENSIONS) |