Chromium Code Reviews| Index: chrome/test/data/extensions/api_test/feedback_private/read_log_source/test.js |
| diff --git a/chrome/test/data/extensions/api_test/feedback_private/read_log_source/test.js b/chrome/test/data/extensions/api_test/feedback_private/read_log_source/test.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..472fa395b1af9f31e1f46dc39fce56a81989fddd |
| --- /dev/null |
| +++ b/chrome/test/data/extensions/api_test/feedback_private/read_log_source/test.js |
| @@ -0,0 +1,199 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +// Keeps count of how many alarms have fired. |
| +var alarmCount = 0; |
| +var maxAlarmCount = 5; |
| + |
| +// Gets the expected result from calling the API repeatedly. Although the API |
| +// is set up in the apitest to always return a non-empty result, this test only |
| +// cares about the first |maxAlarmCount| results. |
| +function getExpectedIncrementalResponseForAlarmCount(count) { |
| + switch (count) { |
| + case 0: return ["a"]; |
| + case 1: return ["bb"]; |
| + case 2: return ["ccc"]; |
| + case 3: return ["dddd"]; |
| + case 4: return ["eeeee"]; |
| + default: return []; |
| + } |
| +} |
| + |
| +// Repeating non-incremental read, which closes the log source and re-creates it |
| +// anew each time. |
| +function singleNonIncremental() { |
| + var nonIncrementalParams = |
| + {source: "messages", readerId: 0, incremental: false}; |
| + var waitForAlarms = chrome.test.listenForever( |
| + chrome.alarms.onAlarm, |
| + function(alarm) { |
| + if (alarmCount < maxAlarmCount) { |
| + console.log("Alarm count: " + alarmCount); |
| + chrome.feedbackPrivate.readLogSource( |
| + nonIncrementalParams, |
| + function(count, readerId, response) { |
| + console.log("Callback with count " + count); |
| + chrome.test.assertEq(readerId, 0); |
| + chrome.test.assertEq(["a"], response); |
| + }.bind(null, alarmCount)); |
| + // Attempting to start a second read while the first is ongoing will |
| + // result in an empty result. |
| + chrome.feedbackPrivate.readLogSource( |
| + nonIncrementalParams, |
| + function(count, readerId, response) { |
| + console.log("Callback with count " + count); |
| + chrome.test.assertEq(readerId, 0); |
| + chrome.test.assertEq([], response); |
| + }.bind(null, alarmCount)); |
| + } else { |
| + chrome.alarms.clearAll(); |
| + waitForAlarms(); |
| + } |
| + ++alarmCount; |
| + }); |
| +} |
| + |
| +// Repeating incremental read. |
| +function singleIncremental() { |
| + var incrementalParams1 = |
| + {source: "messages", readerId: 0, incremental: true}; |
| + var incrementalParams2 = |
| + {source: "messages", readerId: 0, incremental: true}; |
| + |
| + var waitForAlarms = chrome.test.listenForever( |
| + chrome.alarms.onAlarm, |
| + function(alarm) { |
| + if (alarmCount < maxAlarmCount) { |
| + console.log("Alarm count: " + alarmCount); |
| + chrome.feedbackPrivate.readLogSource( |
| + incrementalParams1, |
| + function(count, readerId, response) { |
| + var expectedResponse = |
| + getExpectedIncrementalResponseForAlarmCount(count); |
| + console.log("Callback with count " + count); |
| + chrome.test.assertEq(expectedResponse, response); |
| + chrome.test.assertTrue(readerId != 0); |
| + |
| + if (!incrementalParams1.readerId) { |
| + incrementalParams1.readerId = readerId; |
| + } |
| + }.bind(null, alarmCount)); |
| + // Attempting to start a second read while the first is ongoing will |
| + // result in an empty result. |
| + chrome.feedbackPrivate.readLogSource( |
|
tbarzic
2017/05/22 23:02:55
hm, this seems like it could be flaky - is it guar
|
| + incrementalParams2, |
| + function(count, readerId, response) { |
| + console.log("Callback with count " + count); |
| + chrome.test.assertEq(readerId, 0); |
| + chrome.test.assertEq([], response); |
| + }.bind(null, alarmCount)); |
| + } else { |
| + console.log("Single incremental done"); |
| + chrome.alarms.clearAll(); |
| + |
| + incrementalParams1.incremental = false; |
| + incrementalParams2.incremental = false; |
| + var cleanupFunc = function(readerId, response) { |
| + chrome.test.assertEq(readerId, 0); |
| + } |
| + chrome.feedbackPrivate.readLogSource(incrementalParams1, cleanupFunc); |
| + chrome.feedbackPrivate.readLogSource(incrementalParams2, cleanupFunc); |
| + |
| + waitForAlarms(); |
| + } |
| + ++alarmCount; |
| + }); |
| +} |
| + |
| +// Read incrementally from multiple sources simultaneously. |
| +function multipleIncremental() { |
| + var incrementalParams1a = |
| + {source: "messages", readerId: 0, incremental: true}; |
| + var incrementalParams1b = |
| + {source: "ui_latest", readerId: 0, incremental: true}; |
| + var incrementalParams2a = |
| + {source: "messages", readerId: 0, incremental: true}; |
| + var incrementalParams2b = |
| + {source: "ui_latest", readerId: 0, incremental: true}; |
| + |
| + var waitForAlarms = chrome.test.listenForever( |
| + chrome.alarms.onAlarm, |
| + function(alarm) { |
| + if (alarmCount < maxAlarmCount) { |
| + console.log("Alarm count: " + alarmCount); |
| + chrome.feedbackPrivate.readLogSource( |
| + incrementalParams1a, |
| + function(count, readerId, response) { |
| + var expectedResponse = |
| + getExpectedIncrementalResponseForAlarmCount(count); |
| + console.log("Callback with count " + count); |
| + chrome.test.assertEq(expectedResponse, response); |
| + chrome.test.assertTrue(readerId != 0); |
| + |
| + if (!incrementalParams1a.readerId) { |
| + incrementalParams1a.readerId = readerId; |
| + } |
| + }.bind(null, alarmCount)); |
| + chrome.feedbackPrivate.readLogSource( |
| + incrementalParams1b, |
| + function(count, readerId, response) { |
| + var expectedResponse = |
| + getExpectedIncrementalResponseForAlarmCount(count); |
| + console.log("Callback with count " + count); |
| + chrome.test.assertEq(expectedResponse, response); |
| + chrome.test.assertTrue(readerId != 0); |
| + |
| + if (!incrementalParams1b.readerId) { |
| + incrementalParams1b.readerId = readerId; |
| + } |
| + }.bind(null, alarmCount)); |
| + // Attempting to start a second read while the first is ongoing will |
| + // result in an empty result. |
| + chrome.feedbackPrivate.readLogSource( |
| + incrementalParams2a, |
| + function(count, readerId, response) { |
| + console.log("Callback with count " + count); |
|
tbarzic
2017/05/22 23:02:55
generally, try to avoid unnecessary log statements
|
| + chrome.test.assertEq(readerId, 0); |
| + chrome.test.assertEq([], response); |
| + }.bind(null, alarmCount)); |
| + chrome.feedbackPrivate.readLogSource( |
| + incrementalParams2b, |
| + function(count, readerId, response) { |
| + console.log("Callback with count " + count); |
| + chrome.test.assertEq(readerId, 0); |
| + chrome.test.assertEq([], response); |
| + }.bind(null, alarmCount)); |
| + } else { |
| + chrome.alarms.clearAll(); |
| + |
| + incrementalParams1a.incremental = false; |
| + incrementalParams1b.incremental = false; |
| + incrementalParams2a.incremental = false; |
| + incrementalParams2b.incremental = false; |
| + var cleanup = function(readerId, response) { |
| + chrome.test.assertEq(readerId, 0); |
| + } |
| + chrome.feedbackPrivate.readLogSource(incrementalParams1a, cleanup); |
| + chrome.feedbackPrivate.readLogSource(incrementalParams1b, cleanup); |
| + chrome.feedbackPrivate.readLogSource(incrementalParams2a, cleanup); |
| + chrome.feedbackPrivate.readLogSource(incrementalParams2b, cleanup); |
| + |
| + waitForAlarms(); |
| + } |
| + ++alarmCount; |
| + }); |
| +} |
| + |
| +function runTestWithAlarm(testFunc, period) { |
| + alarmCount = 0; |
| + chrome.alarms.create("test_alarm", |
| + {delayInMinutes: 0.0, periodInMinutes: period}); |
|
tbarzic
2017/05/22 23:02:55
I'm a bit skeptical about using alarms here, espec
|
| + testFunc(); |
| +} |
| + |
| +chrome.test.runTests([ |
| + runTestWithAlarm.bind(null, singleNonIncremental, 0.01), |
| + runTestWithAlarm.bind(null, singleIncremental, 0.01), |
| + runTestWithAlarm.bind(null, multipleIncremental, 0.01) |
| +]); |