2023-06-29 02:37:21 +08:00
|
|
|
import {
|
|
|
|
test,
|
|
|
|
describe,
|
|
|
|
assert,
|
|
|
|
afterEach,
|
|
|
|
vi,
|
|
|
|
beforeAll,
|
|
|
|
beforeEach,
|
|
|
|
expect
|
|
|
|
} from "vitest";
|
2023-07-11 23:56:46 +08:00
|
|
|
import { spy, spyOn } from "tinyspy";
|
2023-06-29 02:37:21 +08:00
|
|
|
import { cleanup, render } from "@gradio/tootils";
|
2023-08-04 06:01:18 +08:00
|
|
|
import { setupi18n } from "../app/src/i18n";
|
2023-06-29 02:37:21 +08:00
|
|
|
|
2023-08-18 23:33:07 +08:00
|
|
|
import InteractiveVideo from "./interactive";
|
|
|
|
import StaticVideo from "./static";
|
|
|
|
|
2023-08-16 02:21:41 +08:00
|
|
|
import type { LoadingStatus } from "@gradio/statustracker";
|
2023-06-29 02:37:21 +08:00
|
|
|
|
|
|
|
const loading_status = {
|
|
|
|
eta: 0,
|
|
|
|
queue_position: 1,
|
|
|
|
queue_size: 1,
|
|
|
|
status: "complete" as LoadingStatus["status"],
|
|
|
|
scroll_to_output: false,
|
|
|
|
visible: true,
|
|
|
|
fn_index: 0,
|
|
|
|
show_progress: "full" as LoadingStatus["show_progress"]
|
|
|
|
};
|
|
|
|
|
|
|
|
describe("Video", () => {
|
|
|
|
beforeAll(() => {
|
|
|
|
window.HTMLMediaElement.prototype.play = vi.fn();
|
|
|
|
window.HTMLMediaElement.prototype.pause = vi.fn();
|
|
|
|
});
|
|
|
|
beforeEach(setupi18n);
|
|
|
|
afterEach(() => cleanup());
|
|
|
|
|
|
|
|
test("renders provided value and label", async () => {
|
2023-08-18 23:33:07 +08:00
|
|
|
const { getByTestId, queryAllByText } = await render(InteractiveVideo, {
|
2023-06-29 02:37:21 +08:00
|
|
|
show_label: true,
|
|
|
|
loading_status,
|
|
|
|
value: [
|
|
|
|
{
|
|
|
|
name: "https://gradio-builds.s3.amazonaws.com/demo-files/audio_sample.wav",
|
|
|
|
data: null,
|
|
|
|
is_file: true
|
|
|
|
}
|
|
|
|
],
|
|
|
|
label: "Test Label",
|
|
|
|
root: "foo",
|
|
|
|
root_url: null,
|
|
|
|
streaming: false,
|
|
|
|
pending: false,
|
|
|
|
name: "bar",
|
|
|
|
source: "upload"
|
|
|
|
});
|
2023-08-23 06:05:22 +08:00
|
|
|
let vid = getByTestId("Test Label-player") as HTMLVideoElement
|
|
|
|
assert.equal(
|
|
|
|
vid.src,
|
|
|
|
"https://gradio-builds.s3.amazonaws.com/demo-files/audio_sample.wav"
|
2023-06-29 02:37:21 +08:00
|
|
|
);
|
2023-08-18 23:33:07 +08:00
|
|
|
assert.equal(queryAllByText("Test Label").length, 1);
|
2023-06-29 02:37:21 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
test("hides label", async () => {
|
2023-08-18 23:33:07 +08:00
|
|
|
const { queryAllByText } = await render(InteractiveVideo, {
|
2023-06-29 02:37:21 +08:00
|
|
|
show_label: false,
|
|
|
|
loading_status,
|
|
|
|
value: {
|
|
|
|
name: "https://gradio-builds.s3.amazonaws.com/demo-files/audio_sample.wav",
|
|
|
|
data: null,
|
|
|
|
is_file: true
|
|
|
|
},
|
|
|
|
label: "Video Component",
|
|
|
|
root: "foo",
|
|
|
|
root_url: null,
|
|
|
|
streaming: false,
|
|
|
|
pending: false,
|
|
|
|
name: "bar",
|
|
|
|
source: "upload"
|
|
|
|
});
|
2023-08-18 23:33:07 +08:00
|
|
|
assert.equal(queryAllByText("Video Component").length, 1);
|
2023-06-29 02:37:21 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
test("static Video sets value", async () => {
|
2023-08-18 23:33:07 +08:00
|
|
|
const { getByTestId } = await render(StaticVideo, {
|
2023-06-29 02:37:21 +08:00
|
|
|
show_label: true,
|
|
|
|
loading_status,
|
|
|
|
value: [
|
|
|
|
{
|
|
|
|
name: "https://gradio-builds.s3.amazonaws.com/demo-files/audio_sample.wav",
|
|
|
|
data: null,
|
|
|
|
is_file: true
|
|
|
|
}
|
|
|
|
],
|
|
|
|
root: "foo",
|
|
|
|
root_url: null,
|
|
|
|
streaming: false,
|
|
|
|
pending: false,
|
|
|
|
name: "bar",
|
|
|
|
source: "upload"
|
|
|
|
});
|
2023-08-23 06:05:22 +08:00
|
|
|
let vid = getByTestId("test-player") as HTMLVideoElement
|
|
|
|
assert.equal(
|
|
|
|
vid.src,
|
|
|
|
"https://gradio-builds.s3.amazonaws.com/demo-files/audio_sample.wav"
|
2023-06-29 02:37:21 +08:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("when autoplay is true `media.play` should be called in static mode", async () => {
|
2023-08-18 23:33:07 +08:00
|
|
|
const { getByTestId } = await render(StaticVideo, {
|
2023-06-29 02:37:21 +08:00
|
|
|
show_label: true,
|
|
|
|
loading_status,
|
|
|
|
mode: "static",
|
|
|
|
value: [
|
|
|
|
{
|
|
|
|
name: "https://gradio-builds.s3.amazonaws.com/demo-files/audio_sample.wav",
|
|
|
|
data: null,
|
|
|
|
is_file: true
|
|
|
|
}
|
|
|
|
],
|
|
|
|
root: "foo",
|
|
|
|
root_url: null,
|
|
|
|
streaming: false,
|
|
|
|
pending: false,
|
2023-07-11 23:56:46 +08:00
|
|
|
source: "upload",
|
2023-06-29 02:37:21 +08:00
|
|
|
autoplay: true
|
|
|
|
});
|
2023-08-18 23:33:07 +08:00
|
|
|
const startButton = getByTestId("test-player") as HTMLVideoElement;
|
2023-06-29 02:37:21 +08:00
|
|
|
const fn = spyOn(startButton, "play");
|
|
|
|
startButton.dispatchEvent(new Event("loadeddata"));
|
|
|
|
assert.equal(fn.callCount, 1);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("when autoplay is true `media.play` should be called in dynamic mode", async () => {
|
2023-08-18 23:33:07 +08:00
|
|
|
const { getByTestId } = await render(InteractiveVideo, {
|
2023-06-29 02:37:21 +08:00
|
|
|
show_label: true,
|
|
|
|
loading_status,
|
|
|
|
value: [
|
|
|
|
{
|
|
|
|
name: "https://gradio-builds.s3.amazonaws.com/demo-files/audio_sample.wav",
|
|
|
|
data: null,
|
|
|
|
is_file: true
|
|
|
|
}
|
|
|
|
],
|
|
|
|
root: "foo",
|
|
|
|
root_url: null,
|
|
|
|
streaming: false,
|
|
|
|
pending: false,
|
2023-07-11 23:56:46 +08:00
|
|
|
source: "upload",
|
2023-06-29 02:37:21 +08:00
|
|
|
autoplay: true
|
|
|
|
});
|
2023-08-18 23:33:07 +08:00
|
|
|
const startButton = getByTestId("test-player") as HTMLVideoElement;
|
2023-06-29 02:37:21 +08:00
|
|
|
const fn = spyOn(startButton, "play");
|
|
|
|
startButton.dispatchEvent(new Event("loadeddata"));
|
|
|
|
assert.equal(fn.callCount, 1);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("when autoplay is true `media.play` should be called in static mode when the Video data is updated", async () => {
|
2023-08-18 23:33:07 +08:00
|
|
|
const { component, getByTestId } = await render(StaticVideo, {
|
2023-06-29 02:37:21 +08:00
|
|
|
show_label: true,
|
|
|
|
loading_status,
|
|
|
|
mode: "static",
|
|
|
|
value: [
|
|
|
|
{
|
|
|
|
name: "https://gradio-builds.s3.amazonaws.com/demo-files/audio_sample.wav",
|
|
|
|
data: null,
|
|
|
|
is_file: true
|
|
|
|
}
|
|
|
|
],
|
|
|
|
root: "foo",
|
|
|
|
root_url: null,
|
|
|
|
streaming: false,
|
|
|
|
pending: false,
|
2023-07-11 23:56:46 +08:00
|
|
|
source: "upload",
|
2023-06-29 02:37:21 +08:00
|
|
|
autoplay: true
|
|
|
|
});
|
2023-08-18 23:33:07 +08:00
|
|
|
const startButton = getByTestId("test-player") as HTMLVideoElement;
|
2023-06-29 02:37:21 +08:00
|
|
|
const fn = spyOn(startButton, "play");
|
|
|
|
startButton.dispatchEvent(new Event("loadeddata"));
|
|
|
|
component.$set({
|
2023-08-18 23:33:07 +08:00
|
|
|
value: [
|
|
|
|
{
|
|
|
|
name: "https://gradio-builds.s3.amazonaws.com/demo-files/audio_sample.wav",
|
|
|
|
data: null,
|
|
|
|
is_file: true
|
|
|
|
}
|
|
|
|
]
|
2023-06-29 02:37:21 +08:00
|
|
|
});
|
|
|
|
startButton.dispatchEvent(new Event("loadeddata"));
|
|
|
|
assert.equal(fn.callCount, 2);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("when autoplay is true `media.play` should be called in dynamic mode when the Video data is updated", async () => {
|
2023-08-18 23:33:07 +08:00
|
|
|
const { component, getByTestId } = await render(InteractiveVideo, {
|
2023-06-29 02:37:21 +08:00
|
|
|
show_label: true,
|
|
|
|
loading_status,
|
|
|
|
mode: "dynamic",
|
|
|
|
value: [
|
|
|
|
{
|
|
|
|
name: "https://gradio-builds.s3.amazonaws.com/demo-files/audio_sample.wav",
|
|
|
|
data: null,
|
|
|
|
is_file: true
|
|
|
|
}
|
|
|
|
],
|
|
|
|
root: "foo",
|
|
|
|
root_url: null,
|
|
|
|
streaming: false,
|
|
|
|
pending: false,
|
2023-07-11 23:56:46 +08:00
|
|
|
source: "upload",
|
2023-06-29 02:37:21 +08:00
|
|
|
autoplay: true
|
|
|
|
});
|
2023-08-18 23:33:07 +08:00
|
|
|
const startButton = getByTestId("test-player") as HTMLVideoElement;
|
2023-06-29 02:37:21 +08:00
|
|
|
const fn = spyOn(startButton, "play");
|
|
|
|
startButton.dispatchEvent(new Event("loadeddata"));
|
|
|
|
component.$set({
|
2023-08-18 23:33:07 +08:00
|
|
|
value: [
|
|
|
|
{
|
|
|
|
name: "https://gradio-builds.s3.amazonaws.com/demo-files/audio_sample.wav",
|
|
|
|
data: null,
|
|
|
|
is_file: true
|
|
|
|
}
|
|
|
|
]
|
2023-06-29 02:37:21 +08:00
|
|
|
});
|
|
|
|
startButton.dispatchEvent(new Event("loadeddata"));
|
|
|
|
assert.equal(fn.callCount, 2);
|
|
|
|
});
|
|
|
|
test("renders video and download button", async () => {
|
|
|
|
const data = [
|
|
|
|
{
|
|
|
|
data: null,
|
|
|
|
name: "https://raw.githubusercontent.com/gradio-app/gradio/main/gradio/demo/video_component/files/a.mp4",
|
|
|
|
is_file: true
|
|
|
|
}
|
|
|
|
];
|
2023-08-18 23:33:07 +08:00
|
|
|
const results = await render(StaticVideo, {
|
2023-06-29 02:37:21 +08:00
|
|
|
mode: "static",
|
|
|
|
label: "video",
|
|
|
|
show_label: true,
|
|
|
|
value: data,
|
|
|
|
root: "foo"
|
|
|
|
});
|
|
|
|
|
|
|
|
const downloadButton = results.getAllByTestId("download-div")[0];
|
|
|
|
expect(
|
|
|
|
downloadButton.getElementsByTagName("a")[0].getAttribute("href")
|
2023-08-23 06:05:22 +08:00
|
|
|
).toBe(data[0].name);
|
2023-06-29 02:37:21 +08:00
|
|
|
expect(
|
|
|
|
downloadButton.getElementsByTagName("button").length
|
|
|
|
).toBeGreaterThan(0);
|
|
|
|
});
|
2023-07-11 23:56:46 +08:00
|
|
|
|
|
|
|
test("video change event trigger fires when value is changed and only fires once", async () => {
|
2023-08-18 23:33:07 +08:00
|
|
|
const { component } = await render(InteractiveVideo, {
|
2023-07-11 23:56:46 +08:00
|
|
|
show_label: true,
|
|
|
|
loading_status,
|
|
|
|
mode: "dynamic",
|
|
|
|
value: [
|
|
|
|
{
|
|
|
|
name: "https://raw.githubusercontent.com/gradio-app/gradio/main/gradio/demo/video_component/files/a.mp4",
|
|
|
|
data: null,
|
|
|
|
is_file: true
|
|
|
|
}
|
|
|
|
],
|
|
|
|
root: "foo",
|
|
|
|
root_url: null,
|
|
|
|
streaming: false,
|
|
|
|
pending: false,
|
|
|
|
source: "upload",
|
|
|
|
autoplay: true
|
|
|
|
});
|
|
|
|
|
|
|
|
const mock = spy();
|
|
|
|
component.$on("change", mock);
|
|
|
|
|
|
|
|
(component.value = [
|
|
|
|
{
|
|
|
|
name: "https://raw.githubusercontent.com/gradio-app/gradio/main/gradio/demo/video_component/files/b.mp4",
|
|
|
|
data: null,
|
|
|
|
is_file: true
|
|
|
|
}
|
|
|
|
]),
|
|
|
|
assert.equal(mock.callCount, 1);
|
|
|
|
});
|
2023-06-29 02:37:21 +08:00
|
|
|
});
|