Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / html / tests_html_media.lucb

#==============================================================================================
#
#   tests_html_media - tests of HTMLMediaElement, HTMLVideoElement, HTMLAudioElement,
#                      HTMLTrackElement and MediaControls (region r27)
#
#   DESCRIPTION:
#       Ladybird has no unit tests for these files (media is tested through Tests/LibWeb/Text
#       and Screenshot, which need scripting and media decoding), so these pin the C++
#       behaviour that phase 1 reaches.
#
#       The elements here are allocated and constructed without initialize (realm.create's
#       first two steps), which is all that the state, the reflections, canPlayType, the
#       intrinsic and natural dimensions and adjust_computed_style need; tests_html_media_2
#       makes them through the ElementFactory, whose initialize makes the track lists.
#
#==============================================================================================

# mark: Helpers =================================================================================

## A media element of class `info`, constructed by `construct` and not initialized.
func test_media_allocate(document: DomDocument*, info: const ak.ClassInfo*) -> gc.Cell*:
    return web_allocate_cell(bind_platform_object_realm(document.bind_platform_object()), info)

## A <video> constructed (HTMLVideoElement::HTMLVideoElement) and not initialized.
func test_media_video(document: DomDocument*) -> HtmlVideoElement*:
    let video = (HtmlVideoElement*)test_media_allocate(document, &html_video_element_class)
    html_video_element_construct(video, document, test_html_qualified_name("video"))
    return video

## An <audio> constructed (HTMLAudioElement::HTMLAudioElement) and not initialized.
func test_media_audio(document: DomDocument*) -> HtmlAudioElement*:
    let audio = (HtmlAudioElement*)test_media_allocate(document, &html_audio_element_class)
    html_audio_element_construct(audio, document, test_html_qualified_name("audio"))
    return audio

## A <track> constructed (HTMLTrackElement::HTMLTrackElement) and not initialized.
func test_media_track(document: DomDocument*) -> HtmlTrackElement*:
    let track = (HtmlTrackElement*)test_media_allocate(document, &html_track_element_class)
    html_track_element_construct(track, document, test_html_qualified_name("track"))
    return track

## HTMLMediaElement::finalize unregisters the element from its page (Page::unregister_media_element,
## region r28's stub); before a test destroys its VM, whose collection finalizes every cell, the
## media elements it made get HTMLElement's table, whose finalize is Node's.
func test_media_forget(element: HtmlMediaElement*):
    element.cell().vtable = (const gc.CellVTable*)&html_element_vtable

## ComputedProperties whose display is `short_`.
func test_media_style(realm: Realm*, short_: CssDisplayShort) -> CssComputedProperties*:
    let style = heap_allocate_css_computed_properties(vm_heap(realm_vm(realm)))
    css_computed_properties_set_property(style, css_data.PropertyId.display, css_display_style_value_create(css_display_from_short(short_)).css_style_value())
    return style

## Whether the style's display is `short_`.
func test_media_display_is(style: CssComputedProperties*, short_: CssDisplayShort) -> bool:
    let display = css_computed_properties_display(style)
    let expected = css_display_from_short(short_)
    return css_display_eq(&display, expected)

## The IDL exception a failed setter stored, as its DOMException name.
func test_media_exception_name() -> ak.FlyString:
    match idl_take_pending_exception():
        .idl_dom_exception(exception):
            return idl_dom_exception_name(exception)
        _:
            trap("not a DOMException")

# mark: Construction ============================================================================

test "media elements: construction moves the mixin tables; the virtuals; the members' defaults":
    let (vm, realm, document) = test_html_setup()
    defer web_test_destroy_vm(vm)
    let video = test_media_video(document)
    defer test_media_forget(video.html_media_element())
    let element = video.html_element()
    assert(video.cell().class_id == html_video_element_class.first_id)
    assert(element.element.aria_mixin.vtable == &html_video_element_aria_mixin_vtable)
    assert(element.element.slottable_mixin.vtable == &html_video_element_slottable_mixin_vtable)
    assert(element.element.animatable.vtable == &html_video_element_animatable_vtable)
    assert(element.global_event_handlers.vtable == &html_video_element_global_event_handlers_vtable)
    assert(element.form_associated_element.vtable == &html_video_element_form_associated_element_vtable)
    assert(is_html_media_element(video) and is_html_video_element(video) and not is_html_audio_element(video))
    assert(dom_node_is_html_media_element(video.dom_node()))
    assert(dom_event_target_is_focusable(video.dom_event_target()))
    assert(dom_element_supports_dimension_attributes(video.dom_element()))

    let media = video.html_media_element()
    assert(html_media_element_paused(media) and html_media_element_show_poster(media))
    assert(not html_media_element_seeking(media) and not html_media_element_ended(media) and not html_media_element_muted(media))
    assert(math.is_nan(html_media_element_duration(media)))
    assert(html_media_element_volume(media) == 1.0 and html_media_element_playback_rate(media) == 1.0 and html_media_element_default_playback_rate(media) == 1.0)
    assert(html_media_element_network_state(media) == HtmlMediaElementNetworkState.empty)
    assert(html_media_element_ready_state(media) == HtmlMediaElementReadyState.have_nothing)
    assert(html_media_element_crossorigin(media) == HtmlCorsSettingAttribute.no_cors)
    assert(html_media_element_error(media) == none and html_media_element_selected_video_track_sink(media) == none)
    assert(ak.string_is_empty(&media.m_current_src))
    assert(html_media_element_current_time(media) == 0.0 and html_media_element_current_playback_position(media) == 0.0)
    assert(media.m_can_autoplay and media.m_controls == none)

    # Every media element has its own media element event task source (UniqueTaskSource).
    let audio = test_media_audio(document)
    defer test_media_forget(audio.html_media_element())
    assert(audio.cell().class_id == html_audio_element_class.first_id)
    assert(audio.html_element().form_associated_element.vtable == &html_audio_element_form_associated_element_vtable)
    assert(html_media_element_media_element_event_task_source(audio.html_media_element()) != html_media_element_media_element_event_task_source(media))

    let track = test_media_track(document)
    assert(track.cell().class_id == html_track_element_class.first_id)
    assert(track.html_element().element.aria_mixin.vtable == &html_track_element_aria_mixin_vtable)
    assert(ak.string_is_empty(&track.m_track_url) and not track.m_loading and not track.m_awaiting_track_url_change)

# mark: canPlayType =============================================================================

test "HTMLMediaElement::can_play_type: video and audio subtypes, mpeg, octet-stream and failures":
    let (vm, realm, document) = test_html_setup()
    defer web_test_destroy_vm(vm)
    let media = test_media_video(document).html_media_element()
    defer test_media_forget(media)
    let cases: (str, BindCanPlayTypeResult)[16] = [
        ("application/octet-stream", BindCanPlayTypeResult.empty),
        ("video/mp4", BindCanPlayTypeResult.probably),
        ("video/webm", BindCanPlayTypeResult.probably),
        ("video/ogg", BindCanPlayTypeResult.probably),
        ("video/mpeg", BindCanPlayTypeResult.probably),
        ("VIDEO/WebM", BindCanPlayTypeResult.probably),
        ("video/mp4; codecs=\"avc1.42E01E\"", BindCanPlayTypeResult.probably),
        ("video/x-matroska", BindCanPlayTypeResult.maybe),
        ("audio/mpeg", BindCanPlayTypeResult.maybe),
        ("audio/mp3", BindCanPlayTypeResult.probably),
        ("audio/flac", BindCanPlayTypeResult.probably),
        ("audio/wav", BindCanPlayTypeResult.probably),
        ("audio/aac", BindCanPlayTypeResult.maybe),
        ("text/plain", BindCanPlayTypeResult.empty),
        ("", BindCanPlayTypeResult.empty),
        ("not a mime type", BindCanPlayTypeResult.empty),
    ]
    for case in cases:
        let (type_, expected) = case
        assert(html_media_element_can_play_type(media, ak.sv(type_)) == expected, type_)

# mark: Time, volume and rates ==================================================================

test "HTMLMediaElement: currentTime before metadata, blocked, potentially playing, direction and ended":
    let (vm, realm, document) = test_html_setup()
    defer web_test_destroy_vm(vm)
    let media = test_media_video(document).html_media_element()
    defer test_media_forget(media)

    # readyState HAVE_NOTHING: the default playback start position is set, and currentTime answers it.
    html_media_element_set_current_time(media, 5.0)
    assert(media.m_default_playback_start_position == 5.0 and media.m_official_playback_position == 0.0)
    assert(html_media_element_current_time(media) == 5.0)
    # fastSeek with HAVE_NOTHING returns after clearing the show poster flag.
    html_media_element_fast_seek(media, 2.0)
    assert(not html_media_element_show_poster(media) and not html_media_element_seeking(media))

    assert(html_media_element_blocked(media) and not html_media_element_stalled(media))
    assert(not html_media_element_potentially_playing(media))
    assert(html_media_element_direction_of_playback(media) == HtmlMediaElementPlaybackDirection.forwards)
    assert(not html_media_element_has_ended_playback(media))

    media.m_ready_state = HtmlMediaElementReadyState.have_enough_data
    assert(not html_media_element_blocked(media))
    media.m_duration = 10.0
    media.m_current_playback_position = 10.0
    assert(html_media_element_has_ended_playback(media))
    media.m_loop_was_specified_when_reaching_end_of_media_resource = true
    assert(not html_media_element_has_ended_playback(media))
    media.m_paused = false
    assert(html_media_element_potentially_playing(media))
    media.m_ready_state = HtmlMediaElementReadyState.have_current_data
    assert(not html_media_element_potentially_playing(media))

    # A playback rate below zero plays backwards; at the earliest position that has ended playback.
    media.m_playback_rate = -1.0
    media.m_current_playback_position = 0.0
    assert(html_media_element_direction_of_playback(media) == HtmlMediaElementPlaybackDirection.backwards)
    assert(html_media_element_has_ended_playback(media))

test "HTMLMediaElement: volume, muted and playbackRate setters":
    let (vm, realm, document) = test_html_setup()
    defer web_test_destroy_vm(vm)
    let media = test_media_video(document).html_media_element()
    defer test_media_forget(media)

    # The same volume is not a change; outside 0..1 throws an IndexSizeError.
    html_media_element_set_volume(media, 1.0) else trap("set_volume(1.0)")
    var threw = false
    html_media_element_set_volume(media, 1.5) catch:
        threw = true
    assert(threw)
    let name = test_media_exception_name()
    assert(ak.fly_string_eq_string_view(&name, ak.sv("IndexSizeError")))
    assert(html_media_element_volume(media) == 1.0)

    # Playback rates other than 1 are not supported.
    threw = false
    html_media_element_set_playback_rate(media, 2.0) catch:
        threw = true
    assert(threw)
    let rate_name = test_media_exception_name()
    assert(ak.fly_string_eq_string_view(&rate_name, ak.sv("NotSupportedError")))
    html_media_element_set_playback_rate(media, 1.0) else trap("set_playback_rate(1.0)")
    assert(html_media_element_playback_rate(media) == 1.0)

    # The same muted value is not a change.
    html_media_element_set_muted(media, false)
    assert(not html_media_element_muted(media))

# mark: The video element's dimensions ==========================================================

test "HTMLVideoElement: intrinsic video dimensions, videoWidth / videoHeight and the natural dimensions":
    let (vm, realm, document) = test_html_setup()
    defer web_test_destroy_vm(vm)
    let video = test_media_video(document)
    defer test_media_forget(video.html_media_element())
    let media = video.html_media_element()

    assert(html_video_element_video_width(video) == 0 and html_video_element_video_height(video) == 0)
    assert(html_video_element_natural_media_size(video) == none and html_video_element_natural_element_size(video) == none)
    assert(html_video_element_current_representation(video) == HtmlVideoElementRepresentation.transparent_black)
    assert(html_video_element_poster_frame(video) == none and html_video_element_bitmap(video) == none)

    # No dimensions: no resize event is queued and nothing changes.
    html_video_element_set_intrinsic_video_dimensions(video, none)
    assert(html_video_element_natural_element_size(video) == none)

    # The natural dimensions follow the video's (no poster frame); videoWidth answers 0 until there is metadata.
    html_video_element_set_intrinsic_video_dimensions(video, gfx.SizeOfU32(m_width = 640, m_height = 360))
    let natural = html_video_element_natural_element_size(video) else trap("no natural size")
    assert(natural.m_width == gfx.css_pixels_from_int(640) and natural.m_height == gfx.css_pixels_from_int(360))
    assert(html_video_element_natural_media_size(video) == gfx.SizeOfU32(m_width = 640, m_height = 360))
    assert(html_video_element_video_width(video) == 0)
    media.m_ready_state = HtmlMediaElementReadyState.have_metadata
    assert(html_video_element_video_width(video) == 640 and html_video_element_video_height(video) == 360)

    # Forgetting the dimensions (no selected video track sink) clears the natural size.
    media.m_ready_state = HtmlMediaElementReadyState.have_nothing
    html_video_element_update_intrinsic_video_dimensions_impl(media)
    assert(html_video_element_natural_media_size(video) == none and html_video_element_natural_element_size(video) == none)

# mark: Computed style ==========================================================================

test "adjust_computed_style: display contents becomes none, inline becomes inline-block, audio without controls is not displayed":
    let (vm, realm, document) = test_html_setup()
    defer web_test_destroy_vm(vm)
    let video = test_media_video(document)
    defer test_media_forget(video.html_media_element())

    let contents = test_media_style(realm, CssDisplayShort.contents)
    html_media_element_adjust_computed_style_impl(video.dom_element(), contents)
    assert(test_media_display_is(contents, CssDisplayShort.none_))

    let inline = test_media_style(realm, CssDisplayShort.inline)
    html_media_element_adjust_computed_style_impl(video.dom_element(), inline)
    assert(test_media_display_is(inline, CssDisplayShort.inline_block))

    let block = test_media_style(realm, CssDisplayShort.block)
    html_media_element_adjust_computed_style_impl(video.dom_element(), block)
    assert(test_media_display_is(block, CssDisplayShort.block))

    let audio = test_media_audio(document)
    defer test_media_forget(audio.html_media_element())
    let audio_inline = test_media_style(realm, CssDisplayShort.inline)
    html_audio_element_adjust_computed_style_impl(audio.dom_element(), audio_inline)
    assert(test_media_display_is(audio_inline, CssDisplayShort.none_))

    _ = test_html_put(audio.html_element(), html_attribute_names_controls, "")
    let audio_controls = test_media_style(realm, CssDisplayShort.inline)
    html_audio_element_adjust_computed_style_impl(audio.dom_element(), audio_controls)
    assert(test_media_display_is(audio_controls, CssDisplayShort.inline_block))

    # Scripting is disabled for a document without a browsing context, so an audio element paints.
    assert(html_audio_element_should_paint(audio))
    assert(html_audio_element_layout_node(audio) == none and html_video_element_layout_node(video) == none)

# mark: Play promises and the source element selector ===========================================

test "HTMLMediaElement: taking pending play promises; the source element selector waits at the end of the list":
    let (vm, realm, document) = test_html_setup()
    defer web_test_destroy_vm(vm)
    let video = test_media_video(document)
    defer test_media_forget(video.html_media_element())
    let media = video.html_media_element()

    var promises = html_media_element_take_pending_play_promises(media)
    assert(ak.vector_size(&promises) == 0 and ak.vector_size(&media.m_pending_play_promises) == 0)

    # A selector whose candidate is the last child records it as previously failed, and its
    # children-changed step then waits again.
    # (HTMLSourceElement's own constructor is region r26's; HTMLElement's is enough here.)
    let source = (HtmlSourceElement*)test_media_allocate(document, &html_source_element_class)
    html_element_construct(source.html_element(), document, test_html_qualified_name("source"))
    test_html_append(video.html_element(), source.html_element())
    let selector = realm_create_html_source_element_selector(realm, media, source)
    html_source_element_selector_wait_for_next_candidate(selector, source.dom_node())
    assert(selector.m_previously_failed_candidate == (DomNode*?)source.dom_node())
    html_source_element_selector_process_next_candidate(selector)
    assert(selector.m_previously_failed_candidate == (DomNode*?)source.dom_node())