| 
									
										
										
										
											2015-03-13 11:10:11 +01:00
										 |  |  | import { | 
					
						
							|  |  |  |   afterEach, | 
					
						
							|  |  |  |   AsyncTestCompleter, | 
					
						
							|  |  |  |   beforeEach, | 
					
						
							|  |  |  |   ddescribe, | 
					
						
							|  |  |  |   describe, | 
					
						
							|  |  |  |   expect, | 
					
						
							|  |  |  |   iit, | 
					
						
							|  |  |  |   inject, | 
					
						
							|  |  |  |   it, | 
					
						
							|  |  |  |   xit, | 
					
						
							|  |  |  | } from 'angular2/test_lib'; | 
					
						
							| 
									
										
										
										
											2015-02-18 14:39:52 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | import { ListWrapper } from 'angular2/src/facade/collection'; | 
					
						
							|  |  |  | import { PromiseWrapper } from 'angular2/src/facade/async'; | 
					
						
							|  |  |  | import { Json, isBlank, isPresent } from 'angular2/src/facade/lang'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import { | 
					
						
							|  |  |  |   WebDriverExtension, IOsDriverExtension, | 
					
						
							|  |  |  |   WebDriverAdapter, Injector, bind | 
					
						
							| 
									
										
										
										
											2015-03-02 09:23:09 -08:00
										 |  |  | } from 'benchpress/common'; | 
					
						
							| 
									
										
										
										
											2015-02-18 14:39:52 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | import { TraceEventFactory } from '../trace_event_factory'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export function main() { | 
					
						
							|  |  |  |   describe('ios driver extension', () => { | 
					
						
							|  |  |  |     var log; | 
					
						
							|  |  |  |     var extension; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     var normEvents = new TraceEventFactory('timeline', 'pid0'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function createExtension(perfRecords = null) { | 
					
						
							|  |  |  |       if (isBlank(perfRecords)) { | 
					
						
							|  |  |  |         perfRecords = []; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       log = []; | 
					
						
							| 
									
										
										
										
											2015-04-10 17:05:31 -07:00
										 |  |  |       extension = Injector.resolveAndCreate([ | 
					
						
							| 
									
										
										
										
											2015-02-18 14:39:52 -08:00
										 |  |  |         IOsDriverExtension.BINDINGS, | 
					
						
							|  |  |  |         bind(WebDriverAdapter).toValue(new MockDriverAdapter(log, perfRecords)) | 
					
						
							| 
									
										
										
										
											2015-02-20 13:32:54 -08:00
										 |  |  |       ]).get(IOsDriverExtension); | 
					
						
							| 
									
										
										
										
											2015-02-18 14:39:52 -08:00
										 |  |  |       return extension; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-06 17:34:27 -08:00
										 |  |  |     it('should throw on forcing gc', () => { | 
					
						
							|  |  |  |       expect( () => createExtension().gc() ).toThrowError('Force GC is not supported on iOS'); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2015-02-18 14:39:52 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-13 11:10:11 +01:00
										 |  |  |     it('should mark the timeline via console.time()', inject([AsyncTestCompleter], (async) => { | 
					
						
							| 
									
										
										
										
											2015-02-18 14:39:52 -08:00
										 |  |  |       createExtension().timeBegin('someName').then( (_) => { | 
					
						
							|  |  |  |         expect(log).toEqual([['executeScript', `console.time('someName');`]]); | 
					
						
							| 
									
										
										
										
											2015-03-13 11:10:11 +01:00
										 |  |  |         async.done(); | 
					
						
							| 
									
										
										
										
											2015-02-18 14:39:52 -08:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2015-03-13 11:10:11 +01:00
										 |  |  |     })); | 
					
						
							| 
									
										
										
										
											2015-02-18 14:39:52 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-13 11:10:11 +01:00
										 |  |  |     it('should mark the timeline via console.timeEnd()', inject([AsyncTestCompleter], (async) => { | 
					
						
							| 
									
										
										
										
											2015-02-18 14:39:52 -08:00
										 |  |  |       createExtension().timeEnd('someName').then( (_) => { | 
					
						
							|  |  |  |         expect(log).toEqual([['executeScript', `console.timeEnd('someName');`]]); | 
					
						
							| 
									
										
										
										
											2015-03-13 11:10:11 +01:00
										 |  |  |         async.done(); | 
					
						
							| 
									
										
										
										
											2015-02-18 14:39:52 -08:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2015-03-13 11:10:11 +01:00
										 |  |  |     })); | 
					
						
							| 
									
										
										
										
											2015-02-18 14:39:52 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-13 11:10:11 +01:00
										 |  |  |     it('should mark the timeline via console.time() and console.timeEnd()', inject([AsyncTestCompleter], (async) => { | 
					
						
							| 
									
										
										
										
											2015-02-18 14:39:52 -08:00
										 |  |  |       createExtension().timeEnd('name1', 'name2').then( (_) => { | 
					
						
							|  |  |  |         expect(log).toEqual([['executeScript', `console.timeEnd('name1');console.time('name2');`]]); | 
					
						
							| 
									
										
										
										
											2015-03-13 11:10:11 +01:00
										 |  |  |         async.done(); | 
					
						
							| 
									
										
										
										
											2015-02-18 14:39:52 -08:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2015-03-13 11:10:11 +01:00
										 |  |  |     })); | 
					
						
							| 
									
										
										
										
											2015-02-18 14:39:52 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     describe('readPerfLog', () => { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-13 11:10:11 +01:00
										 |  |  |       it('should execute a dummy script before reading them', inject([AsyncTestCompleter], (async) => { | 
					
						
							| 
									
										
										
										
											2015-02-18 14:39:52 -08:00
										 |  |  |         // TODO(tbosch): This seems to be a bug in ChromeDriver:
 | 
					
						
							|  |  |  |         // Sometimes it does not report the newest events of the performance log
 | 
					
						
							|  |  |  |         // to the WebDriver client unless a script is executed...
 | 
					
						
							|  |  |  |         createExtension([]).readPerfLog().then( (_) => { | 
					
						
							|  |  |  |           expect(log).toEqual([ [ 'executeScript', '1+1' ], [ 'logs', 'performance' ] ]); | 
					
						
							| 
									
										
										
										
											2015-03-13 11:10:11 +01:00
										 |  |  |           async.done(); | 
					
						
							| 
									
										
										
										
											2015-02-18 14:39:52 -08:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2015-03-13 11:10:11 +01:00
										 |  |  |       })); | 
					
						
							| 
									
										
										
										
											2015-02-18 14:39:52 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-13 11:10:11 +01:00
										 |  |  |       it('should report FunctionCall records as "script"', inject([AsyncTestCompleter], (async) => { | 
					
						
							| 
									
										
										
										
											2015-02-18 14:39:52 -08:00
										 |  |  |         createExtension([ | 
					
						
							|  |  |  |           durationRecord('FunctionCall', 1, 5) | 
					
						
							|  |  |  |         ]).readPerfLog().then( (events) => { | 
					
						
							|  |  |  |           expect(events).toEqual([ | 
					
						
							|  |  |  |             normEvents.start('script', 1), | 
					
						
							|  |  |  |             normEvents.end('script', 5) | 
					
						
							|  |  |  |           ]); | 
					
						
							| 
									
										
										
										
											2015-03-13 11:10:11 +01:00
										 |  |  |           async.done(); | 
					
						
							| 
									
										
										
										
											2015-02-18 14:39:52 -08:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2015-03-13 11:10:11 +01:00
										 |  |  |       })); | 
					
						
							| 
									
										
										
										
											2015-02-18 14:39:52 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-13 11:10:11 +01:00
										 |  |  |       it('should ignore FunctionCalls from webdriver', inject([AsyncTestCompleter], (async) => { | 
					
						
							| 
									
										
										
										
											2015-02-18 14:39:52 -08:00
										 |  |  |         createExtension([ | 
					
						
							|  |  |  |           internalScriptRecord(1, 5) | 
					
						
							|  |  |  |         ]).readPerfLog().then( (events) => { | 
					
						
							|  |  |  |           expect(events).toEqual([]); | 
					
						
							| 
									
										
										
										
											2015-03-13 11:10:11 +01:00
										 |  |  |           async.done(); | 
					
						
							| 
									
										
										
										
											2015-02-18 14:39:52 -08:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2015-03-13 11:10:11 +01:00
										 |  |  |       })); | 
					
						
							| 
									
										
										
										
											2015-02-18 14:39:52 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-13 11:10:11 +01:00
										 |  |  |       it('should report begin time', inject([AsyncTestCompleter], (async) => { | 
					
						
							| 
									
										
										
										
											2015-02-18 14:39:52 -08:00
										 |  |  |         createExtension([ | 
					
						
							|  |  |  |           timeBeginRecord('someName', 12) | 
					
						
							|  |  |  |         ]).readPerfLog().then( (events) => { | 
					
						
							|  |  |  |           expect(events).toEqual([ | 
					
						
							|  |  |  |             normEvents.markStart('someName', 12) | 
					
						
							|  |  |  |           ]); | 
					
						
							| 
									
										
										
										
											2015-03-13 11:10:11 +01:00
										 |  |  |           async.done(); | 
					
						
							| 
									
										
										
										
											2015-02-18 14:39:52 -08:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2015-03-13 11:10:11 +01:00
										 |  |  |       })); | 
					
						
							| 
									
										
										
										
											2015-02-18 14:39:52 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-13 11:10:11 +01:00
										 |  |  |       it('should report end timestamps', inject([AsyncTestCompleter], (async) => { | 
					
						
							| 
									
										
										
										
											2015-02-18 14:39:52 -08:00
										 |  |  |         createExtension([ | 
					
						
							|  |  |  |           timeEndRecord('someName', 12) | 
					
						
							|  |  |  |         ]).readPerfLog().then( (events) => { | 
					
						
							|  |  |  |           expect(events).toEqual([ | 
					
						
							|  |  |  |             normEvents.markEnd('someName', 12) | 
					
						
							|  |  |  |           ]); | 
					
						
							| 
									
										
										
										
											2015-03-13 11:10:11 +01:00
										 |  |  |           async.done(); | 
					
						
							| 
									
										
										
										
											2015-02-18 14:39:52 -08:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2015-03-13 11:10:11 +01:00
										 |  |  |       })); | 
					
						
							| 
									
										
										
										
											2015-02-18 14:39:52 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |       ['RecalculateStyles', 'Layout', 'UpdateLayerTree', 'Paint', 'Rasterize', 'CompositeLayers'].forEach( (recordType) => { | 
					
						
							| 
									
										
										
										
											2015-03-13 11:10:11 +01:00
										 |  |  |         it(`should report ${recordType}`, inject([AsyncTestCompleter], (async) => { | 
					
						
							| 
									
										
										
										
											2015-02-18 14:39:52 -08:00
										 |  |  |           createExtension([ | 
					
						
							|  |  |  |             durationRecord(recordType, 0, 1) | 
					
						
							|  |  |  |           ]).readPerfLog().then( (events) => { | 
					
						
							|  |  |  |             expect(events).toEqual([ | 
					
						
							|  |  |  |               normEvents.start('render', 0), | 
					
						
							|  |  |  |               normEvents.end('render', 1), | 
					
						
							|  |  |  |             ]); | 
					
						
							| 
									
										
										
										
											2015-03-13 11:10:11 +01:00
										 |  |  |             async.done(); | 
					
						
							| 
									
										
										
										
											2015-02-18 14:39:52 -08:00
										 |  |  |           }); | 
					
						
							| 
									
										
										
										
											2015-03-13 11:10:11 +01:00
										 |  |  |         })); | 
					
						
							| 
									
										
										
										
											2015-02-18 14:39:52 -08:00
										 |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-13 11:10:11 +01:00
										 |  |  |       it('should walk children', inject([AsyncTestCompleter], (async) => { | 
					
						
							| 
									
										
										
										
											2015-02-18 14:39:52 -08:00
										 |  |  |         createExtension([ | 
					
						
							|  |  |  |           durationRecord('FunctionCall', 1, 5, [ | 
					
						
							|  |  |  |             timeBeginRecord('someName', 2) | 
					
						
							|  |  |  |           ]) | 
					
						
							|  |  |  |         ]).readPerfLog().then( (events) => { | 
					
						
							|  |  |  |           expect(events).toEqual([ | 
					
						
							|  |  |  |             normEvents.start('script', 1), | 
					
						
							|  |  |  |             normEvents.markStart('someName', 2), | 
					
						
							|  |  |  |             normEvents.end('script', 5) | 
					
						
							|  |  |  |           ]); | 
					
						
							| 
									
										
										
										
											2015-03-13 11:10:11 +01:00
										 |  |  |           async.done(); | 
					
						
							| 
									
										
										
										
											2015-02-18 14:39:52 -08:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2015-03-13 11:10:11 +01:00
										 |  |  |       })); | 
					
						
							| 
									
										
										
										
											2015-02-18 14:39:52 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-20 13:32:54 -08:00
										 |  |  |       it('should match safari browsers', () => { | 
					
						
							|  |  |  |         expect(createExtension().supports({ | 
					
						
							|  |  |  |           'browserName': 'safari' | 
					
						
							|  |  |  |         })).toBe(true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         expect(createExtension().supports({ | 
					
						
							|  |  |  |           'browserName': 'Safari' | 
					
						
							|  |  |  |         })).toBe(true); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-18 14:39:52 -08:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function timeBeginRecord(name, time) { | 
					
						
							|  |  |  |   return { | 
					
						
							|  |  |  |     'type': 'Time', | 
					
						
							|  |  |  |     'startTime': time, | 
					
						
							|  |  |  |     'data': { | 
					
						
							|  |  |  |       'message': name | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function timeEndRecord(name, time) { | 
					
						
							|  |  |  |   return { | 
					
						
							|  |  |  |     'type': 'TimeEnd', | 
					
						
							|  |  |  |     'startTime': time, | 
					
						
							|  |  |  |     'data': { | 
					
						
							|  |  |  |       'message': name | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function durationRecord(type, startTime, endTime, children = null) { | 
					
						
							|  |  |  |   if (isBlank(children)) { | 
					
						
							|  |  |  |     children = []; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return { | 
					
						
							|  |  |  |     'type': type, | 
					
						
							|  |  |  |     'startTime': startTime, | 
					
						
							|  |  |  |     'endTime': endTime, | 
					
						
							|  |  |  |     'children': children | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function internalScriptRecord(startTime, endTime) { | 
					
						
							|  |  |  |   return { | 
					
						
							|  |  |  |     'type': 'FunctionCall', | 
					
						
							|  |  |  |     'startTime': startTime, | 
					
						
							|  |  |  |     'endTime': endTime, | 
					
						
							|  |  |  |     'data': { | 
					
						
							|  |  |  |       'scriptName': 'InjectedScript' | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class MockDriverAdapter extends WebDriverAdapter { | 
					
						
							|  |  |  |   _log:List; | 
					
						
							|  |  |  |   _perfRecords:List; | 
					
						
							|  |  |  |   constructor(log, perfRecords) { | 
					
						
							|  |  |  |     super(); | 
					
						
							|  |  |  |     this._log = log; | 
					
						
							|  |  |  |     this._perfRecords = perfRecords; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   executeScript(script) { | 
					
						
							|  |  |  |     ListWrapper.push(this._log, ['executeScript', script]); | 
					
						
							|  |  |  |     return PromiseWrapper.resolve(null); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   logs(type) { | 
					
						
							|  |  |  |     ListWrapper.push(this._log, ['logs', type]); | 
					
						
							|  |  |  |     if (type === 'performance') { | 
					
						
							|  |  |  |       return PromiseWrapper.resolve(this._perfRecords.map(function(record) { | 
					
						
							|  |  |  |         return { | 
					
						
							|  |  |  |           'message': Json.stringify({ | 
					
						
							|  |  |  |             'message': { | 
					
						
							|  |  |  |               'method': 'Timeline.eventRecorded', | 
					
						
							|  |  |  |               'params': { | 
					
						
							|  |  |  |                 'record': record | 
					
						
							|  |  |  |               } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |           }) | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  |       })); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       return null; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |