2016-08-03 18:00:07 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
2016-04-20 20:01:51 -04:00
|
|
|
|
2016-08-15 22:37:42 -04:00
|
|
|
import {Provider, ReflectiveInjector} from '@angular/core';
|
2017-03-02 15:12:46 -05:00
|
|
|
import {AsyncTestCompleter, describe, expect, inject, it} from '@angular/core/testing/src/testing_internal';
|
2016-08-26 19:34:08 -04:00
|
|
|
|
2016-09-27 20:12:25 -04:00
|
|
|
import {Options, PerfLogEvent, PerfLogFeatures, UserMetric, WebDriverAdapter} from '../../index';
|
2016-04-20 20:01:51 -04:00
|
|
|
|
|
|
|
export function main() {
|
2016-11-12 08:08:58 -05:00
|
|
|
let wdAdapter: MockDriverAdapter;
|
2016-04-20 20:01:51 -04:00
|
|
|
|
2016-08-03 18:00:07 -04:00
|
|
|
function createMetric(
|
2016-08-26 19:34:08 -04:00
|
|
|
perfLogs: PerfLogEvent[], perfLogFeatures: PerfLogFeatures,
|
2016-08-03 18:00:07 -04:00
|
|
|
{userMetrics}: {userMetrics?: {[key: string]: string}} = {}): UserMetric {
|
2016-09-30 12:26:53 -04:00
|
|
|
if (!perfLogFeatures) {
|
2016-04-20 20:01:51 -04:00
|
|
|
perfLogFeatures =
|
|
|
|
new PerfLogFeatures({render: true, gc: true, frameCapture: true, userTiming: true});
|
|
|
|
}
|
2016-09-30 12:26:53 -04:00
|
|
|
if (!userMetrics) {
|
2016-09-19 20:15:57 -04:00
|
|
|
userMetrics = {};
|
2016-04-20 20:01:51 -04:00
|
|
|
}
|
|
|
|
wdAdapter = new MockDriverAdapter();
|
2016-11-12 08:08:58 -05:00
|
|
|
const providers: Provider[] = [
|
2016-08-03 18:00:07 -04:00
|
|
|
Options.DEFAULT_PROVIDERS, UserMetric.PROVIDERS,
|
2016-08-15 22:37:42 -04:00
|
|
|
{provide: Options.USER_METRICS, useValue: userMetrics},
|
|
|
|
{provide: WebDriverAdapter, useValue: wdAdapter}
|
2016-04-20 20:01:51 -04:00
|
|
|
];
|
2016-08-26 19:34:08 -04:00
|
|
|
return ReflectiveInjector.resolveAndCreate(providers).get(UserMetric);
|
2016-04-20 20:01:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
describe('user metric', () => {
|
|
|
|
|
|
|
|
it('should describe itself based on userMetrics', () => {
|
2016-08-03 18:00:07 -04:00
|
|
|
expect(createMetric([[]], new PerfLogFeatures(), {
|
|
|
|
userMetrics: {'loadTime': 'time to load'}
|
|
|
|
}).describe())
|
2016-04-20 20:01:51 -04:00
|
|
|
.toEqual({'loadTime': 'time to load'});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('endMeasure', () => {
|
|
|
|
it('should stop measuring when all properties have numeric values',
|
2016-08-26 19:34:08 -04:00
|
|
|
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const metric = createMetric(
|
2016-04-20 20:01:51 -04:00
|
|
|
[[]], new PerfLogFeatures(),
|
|
|
|
{userMetrics: {'loadTime': 'time to load', 'content': 'time to see content'}});
|
|
|
|
metric.beginMeasure()
|
|
|
|
.then((_) => metric.endMeasure(true))
|
|
|
|
.then((values: {[key: string]: string}) => {
|
|
|
|
expect(values['loadTime']).toBe(25);
|
|
|
|
expect(values['content']).toBe(250);
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
|
|
|
|
wdAdapter.data['loadTime'] = 25;
|
|
|
|
// Wait before setting 2nd property.
|
2016-08-02 18:53:34 -04:00
|
|
|
setTimeout(() => { wdAdapter.data['content'] = 250; }, 50);
|
2016-04-20 20:01:51 -04:00
|
|
|
|
|
|
|
}), 600);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
class MockDriverAdapter extends WebDriverAdapter {
|
|
|
|
data: any = {};
|
|
|
|
|
|
|
|
executeScript(script: string): any {
|
|
|
|
// Just handles `return window.propName` ignores `delete window.propName`.
|
|
|
|
if (script.indexOf('return window.') == 0) {
|
2016-11-12 08:08:58 -05:00
|
|
|
const metricName = script.substring('return window.'.length);
|
2016-08-02 18:53:34 -04:00
|
|
|
return Promise.resolve(this.data[metricName]);
|
2016-04-20 20:01:51 -04:00
|
|
|
} else if (script.indexOf('delete window.') == 0) {
|
2016-08-02 18:53:34 -04:00
|
|
|
return Promise.resolve(null);
|
2016-04-20 20:01:51 -04:00
|
|
|
} else {
|
2016-08-02 18:53:34 -04:00
|
|
|
return Promise.reject(`Unexpected syntax: ${script}`);
|
2016-04-20 20:01:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|