| <!DOCTYPE html> |
| <!-- |
| Copyright 2023 The Cobalt Authors. All Rights Reserved. |
| |
| 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. |
| --> |
| |
| <html> |
| <head> |
| <title>Cobalt Telemetry Test</title> |
| <script src='black_box_js_test_utils.js'></script> |
| </head> |
| <body> |
| <script> |
| setTimeout(notReached, 20000); |
| const {metrics} = window.h5vcc; |
| const now = () => new Date().getTime(); |
| const start = now(); |
| const elapsed = () => now() - start; |
| const expectedEventCount = 2; |
| |
| let resolverIndex = 0; |
| const resolvers = []; |
| const resolveNext = () => { |
| if (resolverIndex >= resolvers.length) { |
| notReached('Unexpected event.') |
| console.error(`Resolver ${resolverIndex} does not exist.`); |
| return; |
| } |
| resolvers[resolverIndex](); |
| resolverIndex++ |
| }; |
| const promises = Array.from({length: expectedEventCount}, () => |
| new Promise(resolve => { |
| resolvers.push(resolve); |
| })); |
| |
| window.addEventListener('load', () => { |
| metrics.setMetricEventInterval(/*intervalSeconds=*/5); |
| metrics.onMetricEvent((metricType, payload) => { |
| if (resolverIndex === 0) { |
| assertTrue(elapsed() < 4000); |
| } else if (resolverIndex === 1) { |
| assertTrue(elapsed() > 4000); |
| assertTrue(elapsed() < 10000); |
| } |
| assertTrue(metrics.isEnabled()); |
| assertEqual('COBALT_UMA', metricType); |
| assertTrue(payload.length > 0); |
| resolveNext(); |
| }); |
| |
| metrics.disable() |
| .then(() => { |
| assertFalse(metrics.isEnabled()); |
| }).then(() => metrics.enable()) |
| .then(() => { |
| assertTrue(metrics.isEnabled()); |
| }).then(() => Promise.all(promises)) |
| .then(() => metrics.disable()) |
| .then(() => { |
| assertFalse(metrics.isEnabled()); |
| onEndTest(); |
| }); |
| |
| setupFinished(); |
| }); |
| </script> |
| </body> |
| </html> |