| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Copyright 2018 Google Inc. All rights reserved. | 
					
						
							|  |  |  |  * Modifications copyright (c) Microsoft Corporation. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Licensed under the Apache License, Version 2.0 (the "License"); | 
					
						
							|  |  |  |  * you may not use this file except in compliance with the License. | 
					
						
							|  |  |  |  * You may obtain a copy of the License at | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *     http://www.apache.org/licenses/LICENSE-2.0
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Unless required by applicable law or agreed to in writing, software | 
					
						
							|  |  |  |  * distributed under the License is distributed on an "AS IS" BASIS, | 
					
						
							|  |  |  |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
					
						
							|  |  |  |  * See the License for the specific language governing permissions and | 
					
						
							|  |  |  |  * limitations under the License. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-09-26 10:59:27 -07:00
										 |  |  | import { it, expect } from './fixtures'; | 
					
						
							| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | it('should work', async ({ browser }) => { | 
					
						
							|  |  |  |   const func = () => new Date(1479579154987).toString(); | 
					
						
							|  |  |  |   { | 
					
						
							| 
									
										
										
										
											2020-09-14 16:53:25 +02:00
										 |  |  |     const context = await browser.newContext({ locale: 'en-US', timezoneId: 'America/Jamaica' }); | 
					
						
							| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  |     const page = await context.newPage(); | 
					
						
							|  |  |  |     expect(await page.evaluate(func)).toBe('Sat Nov 19 2016 13:12:34 GMT-0500 (Eastern Standard Time)'); | 
					
						
							|  |  |  |     await context.close(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   { | 
					
						
							| 
									
										
										
										
											2020-09-14 16:53:25 +02:00
										 |  |  |     const context = await browser.newContext({ locale: 'en-US', timezoneId: 'Pacific/Honolulu' }); | 
					
						
							| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  |     const page = await context.newPage(); | 
					
						
							|  |  |  |     expect(await page.evaluate(func)).toBe('Sat Nov 19 2016 08:12:34 GMT-1000 (Hawaii-Aleutian Standard Time)'); | 
					
						
							|  |  |  |     await context.close(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   { | 
					
						
							| 
									
										
										
										
											2020-09-14 16:53:25 +02:00
										 |  |  |     const context = await browser.newContext({ locale: 'en-US', timezoneId: 'America/Buenos_Aires' }); | 
					
						
							| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  |     const page = await context.newPage(); | 
					
						
							|  |  |  |     expect(await page.evaluate(func)).toBe('Sat Nov 19 2016 15:12:34 GMT-0300 (Argentina Standard Time)'); | 
					
						
							|  |  |  |     await context.close(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   { | 
					
						
							| 
									
										
										
										
											2020-09-14 16:53:25 +02:00
										 |  |  |     const context = await browser.newContext({ locale: 'en-US', timezoneId: 'Europe/Berlin' }); | 
					
						
							| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  |     const page = await context.newPage(); | 
					
						
							|  |  |  |     expect(await page.evaluate(func)).toBe('Sat Nov 19 2016 19:12:34 GMT+0100 (Central European Standard Time)'); | 
					
						
							|  |  |  |     await context.close(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-28 04:20:29 -07:00
										 |  |  | it('should throw for invalid timezone IDs when creating pages', async ({browser}) => { | 
					
						
							| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  |   for (const timezoneId of ['Foo/Bar', 'Baz/Qux']) { | 
					
						
							|  |  |  |     let error = null; | 
					
						
							|  |  |  |     const context = await browser.newContext({ timezoneId }); | 
					
						
							| 
									
										
										
										
											2020-08-28 04:20:29 -07:00
										 |  |  |     await context.newPage().catch(e => error = e); | 
					
						
							| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  |     expect(error.message).toContain(`Invalid timezone ID: ${timezoneId}`); | 
					
						
							|  |  |  |     await context.close(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-28 04:20:29 -07:00
										 |  |  | it('should work for multiple pages sharing same process', async ({browser, server}) => { | 
					
						
							| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  |   const context = await browser.newContext({ timezoneId: 'Europe/Moscow' }); | 
					
						
							|  |  |  |   const page = await context.newPage(); | 
					
						
							|  |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   let [popup] = await Promise.all([ | 
					
						
							|  |  |  |     page.waitForEvent('popup'), | 
					
						
							|  |  |  |     page.evaluate(url => { window.open(url); }, server.EMPTY_PAGE), | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  |   [popup] = await Promise.all([ | 
					
						
							|  |  |  |     popup.waitForEvent('popup'), | 
					
						
							|  |  |  |     popup.evaluate(url => { window.open(url); }, server.EMPTY_PAGE), | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  |   await context.close(); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-08-05 16:31:43 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-28 04:20:29 -07:00
										 |  |  | it('should not change default timezone in another context', async ({browser, server}) => { | 
					
						
							| 
									
										
										
										
											2020-08-05 16:31:43 -07:00
										 |  |  |   async function getContextTimezone(context) { | 
					
						
							|  |  |  |     const page = await context.newPage(); | 
					
						
							|  |  |  |     return await page.evaluate(() => Intl.DateTimeFormat().resolvedOptions().timeZone); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   let defaultTimezone; | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     const context = await browser.newContext(); | 
					
						
							|  |  |  |     defaultTimezone = await getContextTimezone(context); | 
					
						
							|  |  |  |     await context.close(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   const timezoneOverride = defaultTimezone === 'Europe/Moscow' ? 'America/Los_Angeles' : 'Europe/Moscow'; | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     const context = await browser.newContext({ timezoneId: timezoneOverride}); | 
					
						
							|  |  |  |     expect(await getContextTimezone(context)).toBe(timezoneOverride); | 
					
						
							|  |  |  |     await context.close(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     const context = await browser.newContext(); | 
					
						
							|  |  |  |     expect(await getContextTimezone(context)).toBe(defaultTimezone); | 
					
						
							|  |  |  |     await context.close(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | }); |