2Tests for the <chives-image> web component.
7from playwright.sync_api import expect, Page
10from chives.media import create_video_entity
13@pytest.fixture(autouse=True)
14def setup_chives_video_component(page: Page) -> None:
16 Intercept the page, open a blank state, and load the chives-video
19 page.goto("about:blank")
21 with open("javascript/chives-video.js") as f:
22 component_js = f.read()
24 page.evaluate(component_js)
27def test_render_basic_video(page: Page) -> None:
29 The <chives-image> component can render a basic video entity.
31 e = create_video_entity(
32 video_path="tests/fixtures/media/wings_tracking_shot.mp4",
33 poster_path="tests/fixtures/media/wings_tracking_shot.jpg",
37 const elem = document.createElement("chives-video");
38 elem.videoEntity = %s;
39 document.body.appendChild(elem);
44 chives_elem = page.locator("chives-video")
46 video_elem = chives_elem.locator("video")
47 expect(video_elem).to_be_visible()
49 video_elem.get_attribute("src")
50 == "tests/fixtures/media/wings_tracking_shot.mp4"
52 assert video_elem.get_attribute("autoplay") is None
53 assert video_elem.get_attribute("preload") == "none"
56def test_video_with_autoplay(page: Page) -> None:
58 If a video entity has autoplay, then <chives-video> sets the autoplay
59 attribute on the <video> element.
61 e = create_video_entity(
62 video_path="tests/fixtures/media/wings_tracking_shot.mp4",
63 poster_path="tests/fixtures/media/wings_tracking_shot.jpg",
68 const elem = document.createElement("chives-video");
69 elem.videoEntity = %s;
70 document.body.appendChild(elem);
75 chives_elem = page.locator("chives-video")
77 video_elem = chives_elem.locator("video")
78 expect(video_elem).to_be_visible()
79 assert video_elem.get_attribute("autoplay") == ""
82@pytest.mark.parametrize(
83 "width, height, orientation",
86 (200, 100, "landscape"),
87 (100, 200, "portrait"),
90def test_sets_orientation(
91 page: Page, width: int, height: int, orientation: str
94 The <chives-video> element sets a data-orientation attribute.
96 e = create_video_entity(
97 video_path="tests/fixtures/media/wings_tracking_shot.mp4",
98 poster_path="tests/fixtures/media/wings_tracking_shot.jpg",
105 const elem = document.createElement("chives-video");
106 elem.videoEntity = %s;
107 document.body.appendChild(elem);
112 chives_elem = page.locator("chives-video")
114 wrapper_elem = chives_elem.locator("div")
115 expect(wrapper_elem).to_be_visible()
116 assert (wrapper_elem).get_attribute("data-orientation") == orientation
119def test_sets_subtitles(page: Page) -> None:
121 If a video entity has subtitles, then <chives-video> adds a <track>
122 element for every subtitle.
124 e = create_video_entity(
125 video_path="tests/fixtures/media/wings_tracking_shot.mp4",
126 poster_path="tests/fixtures/media/wings_tracking_shot.jpg",
129 {"label": "English", "path": "wings_tracking_shot.en.vtt"},
130 {"label": "French", "path": "wings_tracking_shot.FR.vtt"},
135 const elem = document.createElement("chives-video");
136 elem.videoEntity = %s;
137 document.body.appendChild(elem);
142 chives_elem = page.locator("chives-video")
144 video_elem = chives_elem.locator("video")
145 assert video_elem.locator("track").count() == 2
147 first_track = video_elem.locator("track").first
148 assert first_track.get_attribute("label") == "English"
149 assert first_track.get_attribute("src") == "wings_tracking_shot.en.vtt"
152@pytest.mark.parametrize("subtitle_label", ["English (CC)"])
153def test_subtitle_labels(page: Page, subtitle_label: str) -> None:
155 A video can have subtitles whose labels don't neatly map to
156 HTML attribute names.
158 e = create_video_entity(
159 video_path="tests/fixtures/media/wings_tracking_shot.mp4",
160 poster_path="tests/fixtures/media/wings_tracking_shot.jpg",
163 {"label": subtitle_label, "path": "wings_tracking_shot.en.vtt"},
168 const elem = document.createElement("chives-video");
169 elem.videoEntity = %s;
170 document.body.appendChild(elem);
175 chives_elem = page.locator("chives-video")
177 video_elem = chives_elem.locator("video")
178 track_elem = video_elem.locator("track")
179 assert track_elem.get_attribute("label") == subtitle_label