tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_1956165_m_youtube_com.py (3168B)


      1 import asyncio
      2 
      3 import pytest
      4 
      5 URL = "https://m.youtube.com/results?search_query=trailer"
      6 FIRST_VIDEO_CSS = "a[href*=watch]"
      7 FULLSCREEN_ICON_CSS = "button.fullscreen-icon"
      8 PLAY_BUTTON_CSS = ".ytp-large-play-button.ytp-button"
      9 VIDEO_UI_CSS = ".player-controls-background"
     10 
     11 
     12 async def pip_activates_properly(client, also_test_fullscreen_button=False):
     13    await client.make_preload_script("delete navigator.__proto__.webdriver")
     14 
     15    await client.navigate(URL)
     16    client.await_css(FIRST_VIDEO_CSS, is_displayed=True).click()
     17 
     18    # wait for the video to start playing
     19    client.execute_async_script(
     20        """
     21            const done = arguments[0];
     22            const i = setInterval(() => {
     23                const vid = document.querySelector("video");
     24                if (vid && !vid.paused) {
     25                    done();
     26                }
     27            }, 100);
     28        """
     29    )
     30 
     31    def get_video_dims(video):
     32        return client.execute_script(
     33            """
     34            const { width, height } = arguments[0].getBoundingClientRect();
     35            return `${width}:${height}`;
     36        """,
     37            video,
     38        )
     39 
     40    # give ads up to 30 seconds to play
     41    fs = client.await_css(FULLSCREEN_ICON_CSS, timeout=30)
     42 
     43    video = client.await_css("video", is_displayed=True)
     44    last_known_dims = get_video_dims(video)
     45 
     46    async def toggle_fullscreen():
     47        nonlocal last_known_dims, video
     48 
     49        for _ in range(5):
     50            await asyncio.sleep(0.5)
     51 
     52            if client.find_css(FULLSCREEN_ICON_CSS, is_displayed=True):
     53                await client.apz_click(element=fs, offset=[5, 5])
     54            else:
     55                await client.apz_click(element=video, offset=[50, 50])
     56                await asyncio.sleep(0.25)
     57                await client.apz_click(element=fs, offset=[5, 5])
     58 
     59            await asyncio.sleep(0.5)
     60 
     61            # confirm that the video went into fullscreen mode
     62            dims = get_video_dims(video)
     63            if dims != last_known_dims:
     64                last_known_dims = dims
     65                return True
     66 
     67        return False
     68 
     69    assert await toggle_fullscreen()
     70 
     71    if also_test_fullscreen_button:
     72        # test that we can toggle back out of fullscreen
     73        assert await toggle_fullscreen()
     74 
     75        await asyncio.sleep(0.5)
     76 
     77        # go back into fullscreen mode for the rest of the test
     78        assert await toggle_fullscreen()
     79 
     80    await asyncio.sleep(1)
     81 
     82    with client.using_context("chrome"):
     83        client.execute_script(
     84            """
     85            ChromeUtils.androidMoveTaskToBack();
     86        """
     87        )
     88 
     89    await asyncio.sleep(1)
     90    return client.execute_script(
     91        """
     92            return !(arguments[0]?.paused);
     93        """,
     94        video,
     95    )
     96 
     97 
     98 @pytest.mark.only_platforms("android")
     99 @pytest.mark.actual_platform_required
    100 @pytest.mark.asyncio
    101 @pytest.mark.with_interventions
    102 async def test_enabled(client):
    103    assert await pip_activates_properly(client, True)
    104 
    105 
    106 @pytest.mark.only_platforms("android")
    107 @pytest.mark.actual_platform_required
    108 @pytest.mark.asyncio
    109 @pytest.mark.without_interventions
    110 async def test_disabled(client):
    111    assert not await pip_activates_properly(client)