Set a print listener as soon as possible in the autoprint integration test
This test intermittently fails, likely because the auto-print is triggered fast enough that we don't manage to get it. So this patch aims to try to set a listener very early in order to be sure that we'll be aware that a print has been triggered.
This commit is contained in:
		
							parent
							
								
									553729869b
								
							
						
					
					
						commit
						82be74d036
					
				| @ -14,6 +14,7 @@ | ||||
|  */ | ||||
| 
 | ||||
| import { | ||||
|   awaitPromise, | ||||
|   clearInput, | ||||
|   closePages, | ||||
|   getAnnotationStorage, | ||||
| @ -1771,28 +1772,58 @@ describe("Interaction", () => { | ||||
| 
 | ||||
|   describe("in autoprint.pdf", () => { | ||||
|     let pages; | ||||
|     const printHandles = new Map(); | ||||
| 
 | ||||
|     beforeAll(async () => { | ||||
|       // Autoprinting is triggered by the `Open` event, which is one of the
 | ||||
|       // first events to be dispatched to the sandbox, even before scripting
 | ||||
|       // is reported to be ready. It's therefore important that `loadAndWait`
 | ||||
|       // returns control as soon as possible after opening the PDF document,
 | ||||
|       // and the first element we can check for is the `<html>` tag of the
 | ||||
|       // viewer. Note that the `autoprint.pdf` file is very small, so printing
 | ||||
|       // returns control as soon as possible after opening the PDF document.
 | ||||
|       // Note that the `autoprint.pdf` file is very small, so printing
 | ||||
|       // it is usually very fast and therefore activating the selector check
 | ||||
|       // too late will cause it to never resolve because printing is already
 | ||||
|       // done (and the printed page div removed) before we even get to it.
 | ||||
|       pages = await loadAndWait("autoprint.pdf", "html"); | ||||
|       pages = await loadAndWait( | ||||
|         "autoprint.pdf", | ||||
|         "", | ||||
|         null /* pageSetup = */, | ||||
|         async page => { | ||||
|           printHandles.set( | ||||
|             page, | ||||
|             await page.evaluateHandle(() => [ | ||||
|               new Promise(resolve => { | ||||
|                 globalThis.printResolve = resolve; | ||||
|               }), | ||||
|             ]) | ||||
|           ); | ||||
|           await page.waitForFunction(() => { | ||||
|             if (!window.PDFViewerApplication?.eventBus) { | ||||
|               return false; | ||||
|             } | ||||
|             window.PDFViewerApplication.eventBus.on( | ||||
|               "print", | ||||
|               () => { | ||||
|                 const resolve = globalThis.printResolve; | ||||
|                 delete globalThis.printResolve; | ||||
|                 resolve(); | ||||
|               }, | ||||
|               { once: true } | ||||
|             ); | ||||
|             return true; | ||||
|           }); | ||||
|         } | ||||
|       ); | ||||
|     }); | ||||
| 
 | ||||
|     afterAll(async () => { | ||||
|       await closePages(pages); | ||||
|       printHandles.clear(); | ||||
|     }); | ||||
| 
 | ||||
|     it("must check if printing is triggered when the document is open", async () => { | ||||
|       await Promise.all( | ||||
|         pages.map(async ([browserName, page]) => { | ||||
|           await page.waitForSelector(".printedPage"); | ||||
|           await awaitPromise(printHandles.get(page)); | ||||
|         }) | ||||
|       ); | ||||
|     }); | ||||
|  | ||||
| @ -46,9 +46,11 @@ function loadAndWait(filename, selector, zoom, pageSetup) { | ||||
|       } | ||||
| 
 | ||||
|       await page.bringToFront(); | ||||
|       await page.waitForSelector(selector, { | ||||
|         timeout: 0, | ||||
|       }); | ||||
|       if (selector) { | ||||
|         await page.waitForSelector(selector, { | ||||
|           timeout: 0, | ||||
|         }); | ||||
|       } | ||||
|       return [session.name, page]; | ||||
|     }) | ||||
|   ); | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user