Commit 825430af authored by Sebastian Kaspari's avatar Sebastian Kaspari
Browse files

Closes #3188: Uplift recording devices API to browser-engine-gecko-beta.

parent 711673b8
......@@ -7,8 +7,10 @@ package mozilla.components.browser.engine.gecko.media
import mozilla.components.browser.engine.gecko.GeckoEngineSession
import mozilla.components.concept.engine.EngineSession
import mozilla.components.concept.engine.media.Media
import mozilla.components.concept.engine.media.RecordingDevice
import org.mozilla.geckoview.GeckoSession
import org.mozilla.geckoview.MediaElement
import org.mozilla.geckoview.GeckoSession.MediaDelegate.RecordingDevice as GeckoRecordingDevice
/**
* [GeckoSession.MediaDelegate] implementation for wrapping [MediaElement] instances in [GeckoMedia] ([Media]) and
......@@ -34,4 +36,30 @@ internal class GeckoMediaDelegate(
engineSession.notifyObservers { onMediaRemoved(media) }
}
}
override fun onRecordingStatusChanged(
session: GeckoSession,
devices: Array<out GeckoRecordingDevice>
) {
val genericDevices = devices.map { it.toRecordingDevice() }
engineSession.notifyObservers {
onRecordingStateChanged(genericDevices)
}
}
}
private fun GeckoRecordingDevice.toRecordingDevice(): RecordingDevice {
val type = when (type) {
GeckoRecordingDevice.Type.CAMERA -> RecordingDevice.Type.CAMERA
GeckoRecordingDevice.Type.MICROPHONE -> RecordingDevice.Type.MICROPHONE
else -> throw IllegalStateException("Unknown device type: $type")
}
val status = when (status) {
GeckoRecordingDevice.Status.INACTIVE -> RecordingDevice.Status.INACTIVE
GeckoRecordingDevice.Status.RECORDING -> RecordingDevice.Status.RECORDING
else -> throw IllegalStateException("Unknown device status: $status")
}
return RecordingDevice(type, status)
}
......@@ -3,16 +3,20 @@ package mozilla.components.browser.engine.gecko.media
import mozilla.components.browser.engine.gecko.GeckoEngineSession
import mozilla.components.concept.engine.EngineSession
import mozilla.components.concept.engine.media.Media
import mozilla.components.concept.engine.media.RecordingDevice
import mozilla.components.support.test.mock
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertTrue
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mockito
import org.mockito.Mockito.verify
import org.mozilla.geckoview.GeckoRuntime
import org.mozilla.geckoview.GeckoSession
import org.mozilla.geckoview.MediaElement
import org.mozilla.geckoview.MockRecordingDevice
import org.robolectric.RobolectricTestRunner
@RunWith(RobolectricTestRunner::class)
......@@ -81,4 +85,45 @@ class GeckoMediaDelegateTest {
assertFalse(onMediaRemovedExecuted)
}
@Test
fun `WHEN recording state changes THEN observers are notified`() {
val engineSession = GeckoEngineSession(mock())
var observedDevices: List<RecordingDevice> = listOf()
engineSession.register(object : EngineSession.Observer {
override fun onRecordingStateChanged(devices: List<RecordingDevice>) {
observedDevices = devices
}
})
assertTrue(observedDevices.isEmpty())
engineSession.geckoSession.mediaDelegate!!.onRecordingStatusChanged(mock(), arrayOf(
MockRecordingDevice(
GeckoSession.MediaDelegate.RecordingDevice.Type.CAMERA,
GeckoSession.MediaDelegate.RecordingDevice.Status.RECORDING)
))
assertEquals(1, observedDevices.size)
assertEquals(RecordingDevice(RecordingDevice.Type.CAMERA, RecordingDevice.Status.RECORDING), observedDevices[0])
engineSession.geckoSession.mediaDelegate!!.onRecordingStatusChanged(mock(), arrayOf(
MockRecordingDevice(
GeckoSession.MediaDelegate.RecordingDevice.Type.CAMERA,
GeckoSession.MediaDelegate.RecordingDevice.Status.RECORDING),
MockRecordingDevice(
GeckoSession.MediaDelegate.RecordingDevice.Type.MICROPHONE,
GeckoSession.MediaDelegate.RecordingDevice.Status.INACTIVE)
))
assertEquals(2, observedDevices.size)
assertEquals(RecordingDevice(RecordingDevice.Type.CAMERA, RecordingDevice.Status.RECORDING), observedDevices[0])
assertEquals(RecordingDevice(RecordingDevice.Type.MICROPHONE, RecordingDevice.Status.INACTIVE), observedDevices[1])
engineSession.geckoSession.mediaDelegate!!.onRecordingStatusChanged(mock(), emptyArray())
assertTrue(observedDevices.isEmpty())
}
}
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.geckoview
import mozilla.components.test.ReflectionUtils
internal class MockRecordingDevice(
type: Long,
status: Long
) : GeckoSession.MediaDelegate.RecordingDevice() {
init {
ReflectionUtils.setField(this, "type", type)
ReflectionUtils.setField(this, "status", status)
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment