2023-11-16 22:45:48 +08:00
import { test , expect , drag_and_drop_file } from "@gradio/tootils" ;
import fs from "fs" ;
2023-12-20 08:07:52 +08:00
test ( "Image click-to-upload uploads image successfuly. Clear button dispatches event correctly. Downloading the file works and has the correct name." , async ( {
2023-11-16 22:45:48 +08:00
page
} ) = > {
await page . getByRole ( "button" , { name : "Drop Image Here" } ) . click ( ) ;
const uploader = await page . locator ( "input[type=file]" ) ;
2023-12-15 04:40:36 +08:00
const change_counter = await page . getByLabel ( "# Change Events" , {
exact : true
} ) ;
const clear_counter = await page . getByLabel ( "# Clear Events" ) ;
const upload_counter = await page . getByLabel ( "# Upload Events" ) ;
const change_output_counter = await page . getByLabel ( "# Change Events Output" ) ;
2023-11-16 22:45:48 +08:00
2023-12-15 04:40:36 +08:00
await uploader . setInputFiles ( "./test/files/cheetah1.jpg" ) ;
await expect ( change_counter ) . toHaveValue ( "1" ) ;
await expect ( upload_counter ) . toHaveValue ( "1" ) ;
await expect ( change_output_counter ) . toHaveValue ( "1" ) ;
2023-11-16 22:45:48 +08:00
const downloadPromise = page . waitForEvent ( "download" ) ;
await page . getByLabel ( "Download" ) . click ( ) ;
const download = await downloadPromise ;
2023-12-15 04:40:36 +08:00
// PIL converts from .jpg to .jpeg
2023-11-17 06:47:47 +08:00
await expect ( download . suggestedFilename ( ) ) . toBe ( "cheetah1.jpeg" ) ;
2023-11-16 22:45:48 +08:00
await page . getByLabel ( "Remove Image" ) . click ( ) ;
2023-12-15 04:40:36 +08:00
await expect ( clear_counter ) . toHaveValue ( "1" ) ;
await expect ( change_counter ) . toHaveValue ( "2" ) ;
await expect ( upload_counter ) . toHaveValue ( "1" ) ;
2023-11-22 07:50:40 +08:00
2023-12-15 04:40:36 +08:00
await uploader . setInputFiles ( "./test/files/gradio-logo.svg" ) ;
await expect ( change_counter ) . toHaveValue ( "3" ) ;
await expect ( upload_counter ) . toHaveValue ( "2" ) ;
await expect ( change_output_counter ) . toHaveValue ( "2" ) ;
2023-11-22 07:50:40 +08:00
const SVGdownloadPromise = page . waitForEvent ( "download" ) ;
await page . getByLabel ( "Download" ) . click ( ) ;
const SVGdownload = await SVGdownloadPromise ;
expect ( SVGdownload . suggestedFilename ( ) ) . toBe ( "gradio-logo.svg" ) ;
2023-11-16 22:45:48 +08:00
} ) ;
test ( "Image drag-to-upload uploads image successfuly." , async ( { page } ) = > {
2023-12-15 04:40:36 +08:00
await drag_and_drop_file (
page ,
"input[type=file]" ,
"./test/files/cheetah1.jpg" ,
"cheetag1.jpg" ,
"image/*"
) ;
2023-11-16 22:45:48 +08:00
await expect ( page . getByLabel ( "# Change Events" ) . first ( ) ) . toHaveValue ( "1" ) ;
await expect ( page . getByLabel ( "# Upload Events" ) ) . toHaveValue ( "1" ) ;
} ) ;
2023-12-20 08:07:52 +08:00
test . skip ( "Image copy from clipboard dispatches upload event." , async ( {
page
} ) = > {
2023-11-16 22:45:48 +08:00
// Need to make request from inside browser for blob to be formatted correctly
// tried lots of different things
await page . evaluate ( async ( ) = > {
const blob = await (
await fetch (
` https://gradio-builds.s3.amazonaws.com/assets/PDFDisplay.png `
)
) . blob ( ) ;
navigator . clipboard . write ( [ new ClipboardItem ( { [ blob . type ] : blob } ) ] ) ;
} ) ;
2023-12-20 03:24:08 +08:00
await page . getByLabel ( "Paste from clipboard" ) . click ( ) ;
await Promise . all ( [
page . waitForResponse (
( resp ) = > resp . url ( ) . includes ( "/clipboard.png" ) && resp . status ( ) === 200
)
] ) ;
2023-11-16 22:45:48 +08:00
await expect ( page . getByLabel ( "# Change Events" ) . first ( ) ) . toHaveValue ( "1" ) ;
await expect ( page . getByLabel ( "# Upload Events" ) ) . toHaveValue ( "1" ) ;
} ) ;
2023-12-20 03:24:08 +08:00
test ( "Image paste to clipboard via the Upload component works" , async ( {
page
} ) = > {
await page . evaluate ( async ( ) = > {
navigator . clipboard . writeText ( "123" ) ;
} ) ;
await page . getByLabel ( "Paste from clipboard" ) . click ( ) ;
await page . evaluate ( async ( ) = > {
const blob = await (
await fetch (
` https://gradio-builds.s3.amazonaws.com/assets/PDFDisplay.png `
)
) . blob ( ) ;
navigator . clipboard . write ( [ new ClipboardItem ( { [ blob . type ] : blob } ) ] ) ;
} ) ;
await page . getByText ( "Paste from clipboard" ) . click ( ) ;
await expect ( page . getByLabel ( "# Upload Events" ) ) . toHaveValue ( "1" ) ;
} ) ;