-
-
Notifications
You must be signed in to change notification settings - Fork 207
Adds AV Tests and Caption Functionality #1796
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from all commits
f3ee2b2
9bfeb29
b8d2c21
ffa22cd
69f1a6d
4407f1a
1c63ced
1d55302
d550cee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| const puppeteer = require("puppeteer"); | ||
| const { BASE_URL } = require("../scripts/testBaseUrl"); | ||
|
|
||
| // PDF manifest for PDF-specific behaviour | ||
| const PDF_MULTI_FILE_MANIFEST = | ||
| "https://digital.library.villanova.edu/Item/vudl:294631/Manifest"; | ||
|
|
||
| const viewerUrl = (manifestUrl) => { | ||
| //const separator = BASE_URL.includes("#?") ? "&" : "#?"; | ||
| return `${BASE_URL}#?manifest=${encodeURIComponent(manifestUrl)}`; | ||
| }; | ||
|
|
||
| describe("Universal Viewer", () => { | ||
| let browser; | ||
| let page; | ||
|
|
||
| beforeAll(async () => { | ||
| browser = await puppeteer.launch({ | ||
| headless: true, | ||
| args: ["--no-sandbox", "--disable-setuid-sandbox"], | ||
| }); | ||
| page = await browser.newPage(); | ||
| }); | ||
|
|
||
| afterAll(async () => { | ||
| await browser.close(); | ||
| }); | ||
|
|
||
| // PDF MANIFEST TEST | ||
| describe("PDF manifest", () => { | ||
| beforeEach(async () => { | ||
| await page.goto(viewerUrl(PDF_MULTI_FILE_MANIFEST), { | ||
| waitUntil: "domcontentloaded", | ||
| }); | ||
| }); | ||
|
|
||
| it("loads PDF manifest successfully", async () => { | ||
| expect(page.url()).toContain(encodeURIComponent(PDF_MULTI_FILE_MANIFEST)); | ||
|
|
||
| await page.waitForSelector(".uv", { visible: true }); | ||
|
|
||
| await page.waitForFunction(() => { | ||
| return document.querySelectorAll("iframe").length > 0; | ||
| }); | ||
|
|
||
| const viewerFrame = page.frames().find((f) => { | ||
| const url = f.url(); | ||
|
|
||
| return ( | ||
| url.includes("uv.html") || | ||
| url.includes("viewer") || | ||
| url.includes("manifest") | ||
| ); | ||
| }); | ||
|
|
||
| expect(viewerFrame).toBeTruthy(); | ||
|
|
||
| await viewerFrame.waitForSelector("canvas", { visible: true }); | ||
|
|
||
| const canvasInfo = await viewerFrame.evaluate(() => { | ||
| const canvas = document.querySelector("canvas"); | ||
|
|
||
| if (!canvas) return null; | ||
| return { | ||
| width: canvas.width, | ||
| height: canvas.height, | ||
| }; | ||
| }); | ||
| expect(canvasInfo).not.toBeNull(); | ||
| expect(canvasInfo.width).toBeGreaterThan(0); | ||
| expect(canvasInfo.height).toBeGreaterThan(0); | ||
|
|
||
| const pageText = await viewerFrame.evaluate( | ||
| () => document.body.innerText | ||
| ); | ||
|
|
||
| expect(pageText).not.toContain("Unable to load"); | ||
| expect(pageText).not.toContain("Error loading"); | ||
| }); | ||
|
|
||
| it("shows multiple PDF files in the sidebar and allows navigation", async () => { | ||
| // In a fresh browser session the left panel opens automatically | ||
| // (panelOpen defaults to true), so wait for it rather than clicking | ||
| // the expand button, which is only visible while the panel is closed. | ||
| await page.waitForSelector(".leftPanel.open", { visible: true }); | ||
|
|
||
| await page.waitForSelector(".thumb", { visible: true }); | ||
|
|
||
| const thumbs = await page.$$(".thumb"); | ||
|
|
||
| expect(thumbs.length).toBeGreaterThan(1); | ||
| await thumbs[1].click(); | ||
|
|
||
| await page.waitForFunction(() => window.location.href.includes("cv=1")); | ||
|
|
||
| expect(page.url()).toContain("cv=1"); | ||
| }); | ||
|
|
||
| it("can collapse and re-expand the sidebar with the expand button", async () => { | ||
| // The sidebar opens automatically, so collapse it first to make the | ||
| // expand button available. The open-finished class is toggled once | ||
| // the panel animation completes. | ||
| await page.waitForSelector(".leftPanel.open-finished", { | ||
| visible: true, | ||
| }); | ||
| await page.click(".leftPanel button.collapseButton"); | ||
|
|
||
| // Collapsed: the panel content hides and the expand button appears. | ||
| await page.waitForFunction(() => { | ||
| const panel = document.querySelector(".leftPanel"); | ||
| return panel && !panel.classList.contains("open-finished"); | ||
| }); | ||
| await page.waitForSelector(".leftPanel button.expandButton", { | ||
| visible: true, | ||
| }); | ||
| await page.waitForSelector(".leftPanel .tabs", { hidden: true }); | ||
|
|
||
| expect( | ||
| await page.$eval(".leftPanel button.expandButton", (btn) => | ||
| btn.getAttribute("aria-expanded") | ||
| ) | ||
| ).toBe("false"); | ||
|
|
||
| await page.click(".leftPanel button.expandButton"); | ||
|
|
||
| // Expanded again: the panel reopens and the thumbnails are visible. | ||
| await page.waitForSelector(".leftPanel.open-finished", { | ||
| visible: true, | ||
| }); | ||
| await page.waitForSelector(".thumb", { visible: true }); | ||
|
|
||
| expect( | ||
| await page.$eval(".leftPanel", (el) => el.classList.contains("open")) | ||
| ).toBe(true); | ||
| expect( | ||
| await page.$eval(".leftPanel button.expandButton", (btn) => | ||
| btn.getAttribute("aria-expanded") | ||
| ) | ||
| ).toBe("true"); | ||
| }); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,6 +27,17 @@ type TextTrackDescriptor = { | |
| id: string; | ||
| }; | ||
|
|
||
| const captionTypes = new Set<String>(["text/vtt", "text/srt"]); | ||
|
|
||
| // A label in raw annotation JSON may be a plain string or a language map. | ||
| const captionLabel = (label: any): string | undefined => { | ||
| if (!label || typeof label === "string") { | ||
| return label || undefined; | ||
| } | ||
| const values = label[Object.keys(label)[0]]; | ||
| return Array.isArray(values) ? values[0] : undefined; | ||
| }; | ||
|
|
||
| type MediaSourceDescriptor = { | ||
| label: string; | ||
| type: string; | ||
|
|
@@ -206,7 +217,23 @@ export class MediaElementCenterPanel extends CenterPanel< | |
| } | ||
| } | ||
|
|
||
| // Captions may also be supplied as supplementing annotations on the | ||
| // canvas (IIIF cookbook recipe 0219). | ||
| const supplementing = await this.getSupplementingCaptions(canvas); | ||
| for (const caption of supplementing) { | ||
| if (!subtitles.some((subtitle) => subtitle.id === caption.id)) { | ||
| subtitles.push(caption); | ||
| } | ||
| } | ||
|
|
||
| if (subtitles.length > 0) { | ||
| // Resolve caption URLs to ones the player's XHR will be able to read. | ||
| for (const subtitle of subtitles) { | ||
| if (subtitle.id) { | ||
| subtitle.id = await this.resolveCaptionSource(subtitle.id); | ||
| } | ||
| } | ||
|
|
||
| // Show captions options popover for better interface feedback | ||
| subtitles.unshift({ id: "none" }); | ||
| } | ||
|
|
@@ -392,6 +419,96 @@ export class MediaElementCenterPanel extends CenterPanel< | |
| this.extensionHost.publish(Events.LOAD); | ||
| } | ||
|
|
||
| // Captions/transcriptions supplied as supplementing annotations in the | ||
| // canvas' annotations pages (IIIF cookbook recipe 0219). Inline | ||
| // annotation pages are read directly; pages referenced by id alone are | ||
| // fetched. | ||
| async getSupplementingCaptions( | ||
| canvas: Canvas | ||
| ): Promise<TextTrackDescriptor[]> { | ||
| const captions: TextTrackDescriptor[] = []; | ||
| const pages: any[] = canvas.getProperty("annotations") || []; | ||
|
|
||
| for (const page of pages) { | ||
| let items: any[] = page.items; | ||
|
|
||
| if (!items && page.id) { | ||
| try { | ||
| const response = await fetch(page.id); | ||
| if (response.ok) { | ||
| items = (await response.json()).items; | ||
| } | ||
| } catch { | ||
| console.warn( | ||
| `Annotation page ${page.id} could not be read (CORS headers are required); any captions it contains will be unavailable.` | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| if (!items) { | ||
| continue; | ||
| } | ||
|
|
||
| for (const annotation of items) { | ||
| const motivations = Array.isArray(annotation.motivation) | ||
| ? annotation.motivation | ||
| : [annotation.motivation]; | ||
|
|
||
| if (!motivations.includes("supplementing")) { | ||
| continue; | ||
| } | ||
|
|
||
| const bodies = Array.isArray(annotation.body) | ||
| ? annotation.body | ||
| : [annotation.body]; | ||
|
|
||
| for (const body of bodies) { | ||
| if (body && body.id && captionTypes.has(body.format)) { | ||
| captions.push({ | ||
| id: body.id, | ||
| label: captionLabel(body.label), | ||
| language: body.language, | ||
| }); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return captions; | ||
| } | ||
|
|
||
| // Captions are fetched with XHR by the player, so a cross-origin URL is | ||
| // only readable when every response hop carries CORS headers. Follow any | ||
| // CORS-friendly redirect to its final URL, and retry plain-http sources | ||
| // over https — an http -> https upgrade redirect whose 301 response lacks | ||
| // CORS headers is blocked by the browser even though its destination is | ||
| // readable. | ||
| async resolveCaptionSource(src: string): Promise<string> { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems promising as a possible resolution to the problems I encountered when testing #1298 on Villanova's https://digital.library.villanova.edu/Item/vudl:829943/Manifest manifest. I'll be interested to try that PR again after this one is merged! |
||
| const attempts: string[] = [src]; | ||
|
|
||
| if (src.startsWith("http://")) { | ||
| attempts.push(src.replace(/^http:\/\//, "https://")); | ||
| } | ||
|
|
||
| for (const attempt of attempts) { | ||
| try { | ||
| const response = await fetch(attempt); | ||
| if (response.ok) { | ||
| return response.url; | ||
| } | ||
| } catch { | ||
| // expected when the attempt is blocked by CORS or mixed content; | ||
| // fall through to the next candidate | ||
| } | ||
| } | ||
|
|
||
| console.warn( | ||
| `Captions at ${src} could not be read (CORS headers are required on every response, including redirects); the player will omit this track.` | ||
| ); | ||
|
|
||
| return src; | ||
| } | ||
|
|
||
| appendTextTracks(subtitles: Array<TextTrackDescriptor>) { | ||
| for (const subtitle of subtitles) { | ||
| this.$media.append( | ||
|
|
@@ -429,16 +546,14 @@ export class MediaElementCenterPanel extends CenterPanel< | |
| return typeGroup === "audio" || typeGroup === "video"; | ||
| } | ||
|
|
||
| // vtt, srt, csv | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Was unable to find support for csv.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have added tests that confirm functionality of both vtt and srt. |
||
| // vtt, srt | ||
| isTypeCaption(element: Rendering | AnnotationBody) { | ||
| const type: RenderingFormat | MediaType | null = element.getFormat(); | ||
|
|
||
| if (type === null) { | ||
| return false; | ||
| } | ||
|
|
||
| const captionTypes = new Set<String>(["text/vtt", "text/srt"]); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did not lose this. It's just higher up in the file now - https://github.com/UniversalViewer/universalviewer/pull/1796/changes#diff-cb64973a8a5a2e0eac4714d6ee79b3016b2f2e7b76e35a803c9f28b4bf197b16R30 |
||
|
|
||
| return captionTypes.has(type.toString()); | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we really want to pick the first key arbitrarily? Should we try to match the selected user language, or am I misunderstanding what's going on here?