Bug 1601067 - Use installBuiltIn in tests instead of registerWebExtension. r=snorp

Differential Revision: https://phabricator.services.mozilla.com/D72979
This commit is contained in:
Agi Sferro 2020-05-05 22:19:28 +00:00
Родитель 859bdae8a8
Коммит 699cc8cf70
2 изменённых файлов: 88 добавлений и 92 удалений

Просмотреть файл

@ -39,9 +39,12 @@ class ExtensionActionTest : BaseSessionTest() {
@field:Parameterized.Parameter(0) @JvmField var id: String = ""
private val controller
get() = sessionRule.runtime.webExtensionController
@Before
fun setup() {
sessionRule.runtime.webExtensionController.setTabActive(mainSession, true)
controller.setTabActive(mainSession, true)
// This method installs the extension, opens up ports with the background script and the
// content script and captures the default action definition from the manifest
@ -51,9 +54,8 @@ class ExtensionActionTest : BaseSessionTest() {
val windowPortResult = GeckoResult<WebExtension.Port>()
val backgroundPortResult = GeckoResult<WebExtension.Port>()
extension = WebExtension("resource://android/assets/web_extensions/actions/",
"actions", WebExtension.Flags.ALLOW_CONTENT_MESSAGING,
sessionRule.runtime.webExtensionController)
extension = sessionRule.waitForResult(
controller.installBuiltIn("resource://android/assets/web_extensions/actions/"));
sessionRule.session.webExtensionController.setMessageDelegate(
extension!!,
@ -83,14 +85,15 @@ class ExtensionActionTest : BaseSessionTest() {
}
})
sessionRule.waitForResult(sessionRule.runtime.registerWebExtension(extension!!))
sessionRule.session.loadUri("http://example.com")
sessionRule.waitForPageStop()
val pageAction = sessionRule.waitForResult(pageActionDefaultResult)
val browserAction = sessionRule.waitForResult(browserActionDefaultResult)
default = when (id) {
"#pageAction" -> sessionRule.waitForResult(pageActionDefaultResult)
"#browserAction" -> sessionRule.waitForResult(browserActionDefaultResult)
"#pageAction" -> pageAction
"#browserAction" -> browserAction
else -> throw IllegalArgumentException()
}
@ -114,7 +117,11 @@ class ExtensionActionTest : BaseSessionTest() {
@After
fun tearDown() {
sessionRule.waitForResult(sessionRule.runtime.unregisterWebExtension(extension!!))
if (extension != null) {
extension!!.setMessageDelegate(null, "browser")
extension!!.setActionDelegate(null)
sessionRule.waitForResult(controller.uninstall(extension!!))
}
}
private fun testBackgroundActionApi(message: String, tester: (WebExtension.Action) -> Unit) {

Просмотреть файл

@ -59,7 +59,7 @@ class WebExtensionTest : BaseSessionTest() {
}
@Test
fun registerWebExtension() {
fun installBuiltIn() {
mainSession.loadUri("example.com")
sessionRule.waitForPageStop()
@ -67,11 +67,10 @@ class WebExtensionTest : BaseSessionTest() {
// the WebExtension
assertBodyBorderEqualTo("")
val borderify = WebExtension("resource://android/assets/web_extensions/borderify/",
controller)
// Load the WebExtension that will add a border to the body
sessionRule.waitForResult(sessionRule.runtime.registerWebExtension(borderify))
val borderify = sessionRule.waitForResult(controller.installBuiltIn(
"resource://android/assets/web_extensions/borderify/"
))
mainSession.reload()
sessionRule.waitForPageStop()
@ -80,7 +79,7 @@ class WebExtensionTest : BaseSessionTest() {
assertBodyBorderEqualTo("red")
// Unregister WebExtension and check again
sessionRule.waitForResult(sessionRule.runtime.unregisterWebExtension(borderify))
sessionRule.waitForResult(controller.uninstall(borderify))
mainSession.reload()
sessionRule.waitForPageStop()
@ -486,11 +485,10 @@ class WebExtensionTest : BaseSessionTest() {
}
}
val messaging = createWebExtension(background, messageDelegate)
sessionRule.waitForResult(sessionRule.runtime.registerWebExtension(messaging))
val messaging = installWebExtension(background, messageDelegate)
sessionRule.waitForResult(messageResult)
sessionRule.waitForResult(sessionRule.runtime.unregisterWebExtension(messaging))
sessionRule.waitForResult(controller.uninstall(messaging))
}
// This test
@ -501,23 +499,22 @@ class WebExtensionTest : BaseSessionTest() {
@Test
fun testBrowserTabsCreate() {
val tabsCreateResult = GeckoResult<Void>()
val tabsExtension = WebExtension(TABS_CREATE_BACKGROUND, controller)
var tabsExtension: WebExtension? = null
val tabDelegate = object : WebExtension.TabDelegate {
override fun onNewTab(source: WebExtension, details: WebExtension.CreateTabDetails): GeckoResult<GeckoSession> {
assertEquals(details.url, "https://www.mozilla.org/en-US/")
assertEquals(details.active, true)
assertEquals(tabsExtension, source)
assertEquals(tabsExtension!!, source)
tabsCreateResult.complete(null)
return GeckoResult.fromValue(null)
}
}
tabsExtension.setTabDelegate(tabDelegate)
sessionRule.waitForResult(sessionRule.runtime.registerWebExtension(tabsExtension))
tabsExtension = sessionRule.waitForResult(controller.installBuiltIn(TABS_CREATE_BACKGROUND))
tabsExtension.setTabDelegate(tabDelegate)
sessionRule.waitForResult(tabsCreateResult)
sessionRule.waitForResult(sessionRule.runtime.unregisterWebExtension(tabsExtension))
sessionRule.waitForResult(controller.uninstall(tabsExtension))
}
// This test
@ -531,13 +528,10 @@ class WebExtensionTest : BaseSessionTest() {
@Test
fun testBrowserTabsCreateBrowserTabsRemove() {
val onCloseRequestResult = GeckoResult<Void>()
val tabsExtension = WebExtension(TABS_CREATE_REMOVE_BACKGROUND, controller)
val tabsExtension = sessionRule.waitForResult(
controller.installBuiltIn(TABS_CREATE_REMOVE_BACKGROUND))
sessionRule.addExternalDelegateUntilTestEnd(
WebExtension.TabDelegate::class,
tabsExtension::setTabDelegate,
{ tabsExtension.setTabDelegate(null) },
object : WebExtension.TabDelegate {
tabsExtension.tabDelegate = object : WebExtension.TabDelegate {
override fun onNewTab(source: WebExtension, details: WebExtension.CreateTabDetails): GeckoResult<GeckoSession> {
val extensionCreatedSession = sessionRule.createClosedSession(sessionRule.session.settings)
@ -554,13 +548,10 @@ class WebExtensionTest : BaseSessionTest() {
return GeckoResult.fromValue(extensionCreatedSession)
}
})
};
sessionRule.waitForResult(sessionRule.runtime.registerWebExtension(tabsExtension))
sessionRule.waitForResult(onCloseRequestResult)
sessionRule.waitForResult(sessionRule.runtime.unregisterWebExtension(tabsExtension))
sessionRule.waitForResult(controller.uninstall(tabsExtension))
}
// This test
@ -578,7 +569,8 @@ class WebExtensionTest : BaseSessionTest() {
@Test
fun testSetTabActive() {
val onCloseRequestResult = GeckoResult<Void>()
val tabsExtension = WebExtension(TABS_ACTIVATE_REMOVE_BACKGROUND, controller)
val tabsExtension = sessionRule.waitForResult(
controller.installBuiltIn(TABS_ACTIVATE_REMOVE_BACKGROUND))
val newTabSession = sessionRule.createOpenSession(sessionRule.session.settings)
sessionRule.addExternalDelegateUntilTestEnd(
@ -595,13 +587,11 @@ class WebExtensionTest : BaseSessionTest() {
}
})
sessionRule.waitForResult(sessionRule.runtime.registerWebExtension(tabsExtension))
controller.setTabActive(sessionRule.session, false)
controller.setTabActive(newTabSession, true)
sessionRule.waitForResult(onCloseRequestResult)
sessionRule.waitForResult(sessionRule.runtime.unregisterWebExtension(tabsExtension))
sessionRule.waitForResult(controller.uninstall(tabsExtension))
}
// Same as testSetTabActive when the extension is not allowed in private browsing
@ -715,7 +705,8 @@ class WebExtensionTest : BaseSessionTest() {
existingSession.loadTestPath("$HELLO_HTML_PATH?tabToClose")
existingSession.waitForPageStop()
val tabsExtension = WebExtension(TABS_REMOVE_BACKGROUND, controller)
val tabsExtension = sessionRule.waitForResult(
controller.installBuiltIn(TABS_REMOVE_BACKGROUND))
sessionRule.addExternalDelegateUntilTestEnd(
WebExtension.SessionTabDelegate::class,
@ -729,24 +720,21 @@ class WebExtensionTest : BaseSessionTest() {
}
})
sessionRule.waitForResult(sessionRule.runtime.registerWebExtension(tabsExtension))
sessionRule.waitForResult(onCloseRequestResult)
sessionRule.waitForResult(sessionRule.runtime.unregisterWebExtension(tabsExtension))
sessionRule.waitForResult(controller.uninstall(tabsExtension))
}
private fun createWebExtension(background: Boolean,
private fun installWebExtension(background: Boolean,
messageDelegate: WebExtension.MessageDelegate): WebExtension {
val webExtension: WebExtension
val uuid = "{${UUID.randomUUID()}}"
if (background) {
webExtension = WebExtension(MESSAGING_BACKGROUND, uuid, WebExtension.Flags.NONE,
controller)
webExtension = sessionRule.waitForResult(
controller.installBuiltIn(MESSAGING_BACKGROUND))
webExtension.setMessageDelegate(messageDelegate, "browser")
} else {
webExtension = WebExtension(MESSAGING_CONTENT, uuid,
WebExtension.Flags.ALLOW_CONTENT_MESSAGING, controller)
webExtension = sessionRule.waitForResult(
controller.installBuiltIn(MESSAGING_CONTENT))
sessionRule.session.webExtensionController
.setMessageDelegate(webExtension, messageDelegate, "browser")
}
@ -818,11 +806,9 @@ class WebExtensionTest : BaseSessionTest() {
}
}
val messaging = createWebExtension(background, messageDelegate)
sessionRule.waitForResult(sessionRule.runtime.registerWebExtension(messaging))
val messaging = installWebExtension(background, messageDelegate)
sessionRule.waitForResult(result)
sessionRule.waitForResult(sessionRule.runtime.unregisterWebExtension(messaging))
sessionRule.waitForResult(controller.uninstall(messaging))
}
@Test
@ -861,7 +847,7 @@ class WebExtensionTest : BaseSessionTest() {
}
override fun onDisconnect(port: WebExtension.Port) {
assertEquals(messaging, port.sender.webExtension)
assertEquals(messaging!!.id, port.sender.webExtension.id)
assertEquals(port, messagingPort)
// We successfully received a disconnection
result.complete(null)
@ -870,7 +856,7 @@ class WebExtensionTest : BaseSessionTest() {
val messageDelegate = object : WebExtension.MessageDelegate {
override fun onConnect(port: WebExtension.Port) {
assertEquals(messaging, port.sender.webExtension)
assertEquals(messaging!!.id, port.sender.webExtension.id)
checkSender(port.name, port.sender, background)
assertEquals(port.name, "browser")
@ -891,18 +877,16 @@ class WebExtensionTest : BaseSessionTest() {
override fun onMessage(nativeApp: String, message: Any,
sender: WebExtension.MessageSender): GeckoResult<Any>? {
assertEquals(messaging, sender.webExtension)
assertEquals(messaging!!.id, sender.webExtension.id)
// Ignored for this test
return null
}
}
messaging = createWebExtension(background, messageDelegate)
sessionRule.waitForResult(sessionRule.runtime.registerWebExtension(messaging))
messaging = installWebExtension(background, messageDelegate)
sessionRule.waitForResult(result)
sessionRule.waitForResult(sessionRule.runtime.unregisterWebExtension(messaging))
sessionRule.waitForResult(controller.uninstall(messaging))
}
@Test
@ -957,7 +941,7 @@ class WebExtensionTest : BaseSessionTest() {
val messageDelegate = object : WebExtension.MessageDelegate {
override fun onConnect(port: WebExtension.Port) {
assertEquals(messaging, port.sender.webExtension)
assertEquals(messaging!!.id, port.sender.webExtension.id)
checkSender(port.name, port.sender, background)
port.disconnect()
@ -965,7 +949,7 @@ class WebExtensionTest : BaseSessionTest() {
override fun onMessage(nativeApp: String, message: Any,
sender: WebExtension.MessageSender): GeckoResult<Any>? {
assertEquals(messaging, sender.webExtension)
assertEquals(messaging!!.id, sender.webExtension.id)
checkSender(nativeApp, sender, background)
if (message is JSONObject) {
@ -978,11 +962,9 @@ class WebExtensionTest : BaseSessionTest() {
}
}
messaging = createWebExtension(background, messageDelegate)
sessionRule.waitForResult(sessionRule.runtime.registerWebExtension(messaging))
messaging = installWebExtension(background, messageDelegate)
sessionRule.waitForResult(result)
sessionRule.waitForResult(sessionRule.runtime.unregisterWebExtension(messaging))
sessionRule.waitForResult(controller.uninstall(messaging))
}
@Test
@ -1008,7 +990,7 @@ class WebExtensionTest : BaseSessionTest() {
val messageDelegate = object : WebExtension.MessageDelegate {
override fun onConnect(port: WebExtension.Port) {
assertEquals(messaging, port.sender.webExtension)
assertEquals(messaging!!.id, port.sender.webExtension.id)
assertEquals(WebExtension.MessageSender.ENV_TYPE_CONTENT_SCRIPT,
port.sender.environmentType)
when (port.sender.url) {
@ -1029,7 +1011,7 @@ class WebExtensionTest : BaseSessionTest() {
override fun onMessage(nativeApp: String, message: Any,
sender: WebExtension.MessageSender): GeckoResult<Any>? {
assertEquals(messaging, sender.webExtension)
assertEquals(messaging!!.id, sender.webExtension.id)
assertEquals(WebExtension.MessageSender.ENV_TYPE_CONTENT_SCRIPT,
sender.environmentType)
when (sender.url) {
@ -1049,17 +1031,15 @@ class WebExtensionTest : BaseSessionTest() {
}
}
messaging = WebExtension("resource://android/assets/web_extensions/messaging-iframe/",
"{${UUID.randomUUID()}}", WebExtension.Flags.ALLOW_CONTENT_MESSAGING, controller)
messaging = sessionRule.waitForResult(controller.installBuiltIn(
"resource://android/assets/web_extensions/messaging-iframe/"))
sessionRule.session.webExtensionController
.setMessageDelegate(messaging, messageDelegate, "browser")
sessionRule.waitForResult(sessionRule.runtime.registerWebExtension(messaging))
sessionRule.waitForResult(portTopLevel)
sessionRule.waitForResult(portIframe)
sessionRule.waitForResult(messageTopLevel)
sessionRule.waitForResult(messageIframe)
sessionRule.waitForResult(sessionRule.runtime.unregisterWebExtension(messaging))
sessionRule.waitForResult(controller.uninstall(messaging))
}
@Test
@ -1086,10 +1066,9 @@ class WebExtensionTest : BaseSessionTest() {
}
}
extension = WebExtension("resource://android/assets/web_extensions/extension-page-update/",
controller)
extension = sessionRule.waitForResult(controller.installBuiltIn(
"resource://android/assets/web_extensions/extension-page-update/"))
sessionRule.waitForResult(sessionRule.runtime.registerWebExtension(extension))
val sessionController = mainSession.webExtensionController
sessionController.setMessageDelegate(extension, messageDelegate, "browser")
sessionController.setTabDelegate(extension, object: WebExtension.SessionTabDelegate {
@ -1147,7 +1126,7 @@ class WebExtensionTest : BaseSessionTest() {
{ mainSession.webExtensionController.setTabDelegate(extension, null) },
object : WebExtension.SessionTabDelegate {})
val unregister = sessionRule.runtime.unregisterWebExtension(extension)
val unregister = controller.uninstall(extension)
sessionRule.waitUntilCalled(object : WebExtension.SessionTabDelegate {
@AssertCalled
@ -1162,34 +1141,44 @@ class WebExtensionTest : BaseSessionTest() {
sessionRule.waitForResult(unregister)
}
@Test
fun badUrl() {
testInstallBuiltInError("invalid url", "Could not parse uri")
}
@Test
fun badHost() {
testInstallBuiltInError("resource://gre/", "Only resource://android")
}
@Test
fun dontAllowRemoteUris() {
testInstallBuiltInError("https://example.com/extension/", "Only resource://android")
}
@Test
fun badFileType() {
testRegisterError("resource://android/bad/location/error",
"does not point to a folder or an .xpi")
testInstallBuiltInError("resource://android/bad/location/error",
"does not point to a folder")
}
@Test
fun badLocationXpi() {
testRegisterError("resource://android/bad/location/error.xpi",
"NS_ERROR_FILE_NOT_FOUND")
testInstallBuiltInError("resource://android/bad/location/error.xpi",
"does not point to a folder")
}
@Test
fun badLocationFolder() {
testRegisterError("resource://android/bad/location/error/",
"NS_ERROR_FILE_NOT_FOUND")
fun testInstallBuiltInError() {
testInstallBuiltInError("resource://android/bad/location/error/",
"does not contain a valid manifest")
}
private fun testRegisterError(location: String, expectedError: String) {
private fun testInstallBuiltInError(location: String, expectedError: String) {
try {
sessionRule.waitForResult(sessionRule.runtime.registerWebExtension(
WebExtension(location, controller)
))
sessionRule.waitForResult(controller.installBuiltIn(location))
} catch (ex: Exception) {
// Let's make sure the error message contains the WebExtension URL
assertTrue(ex.message!!.contains(location))
// and it contains the expected error message
// Let's make sure the error message contains the expected error message
assertTrue(ex.message!!.contains(expectedError))
return