 Chromium Code Reviews
 Chromium Code Reviews| Index: chrome/test/data/extensions/storage_monitor/hosted_apps/common.js | 
| diff --git a/chrome/test/data/extensions/storage_monitor/hosted_apps/common.js b/chrome/test/data/extensions/storage_monitor/hosted_apps/common.js | 
| new file mode 100644 | 
| index 0000000000000000000000000000000000000000..3d451b6c92f9177e222f97dce8388ae124452de4 | 
| --- /dev/null | 
| +++ b/chrome/test/data/extensions/storage_monitor/hosted_apps/common.js | 
| @@ -0,0 +1,66 @@ | 
| +// 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. | 
| + | 
| +var kHundredChars = ''; | 
| 
Devlin
2017/06/12 20:15:05
do we care what these chars are?  If not, maybe
va
 
ncarter (slow)
2017/06/12 22:52:08
Removed -- this was all copypasta from ../write_da
 | 
| +for (var i = 0; i < 10; i++) { | 
| + kHundredChars += '0123456789'; | 
| +} | 
| + | 
| +// Opens the filesystem and returns a Promise that resolves to the opened | 
| +// filesystem. | 
| +function GetFileSystem(type, size) { | 
| + return new Promise( | 
| + (resolve, reject) => | 
| + webkitRequestFileSystem(type, size, resolve, reject)); | 
| +} | 
| + | 
| +// Returns a .then()-chainable handler that accepts a fileystem, creates a file, | 
| +// and returns a Promise that resolves to the successfully created file. | 
| +function CreateFile(filename) { | 
| + return (filesystem) => new Promise( | 
| + (resolve, reject) => filesystem.root.getFile( | 
| + filename, {create: true}, resolve, reject)); | 
| +} | 
| + | 
| +// Returns a .then()-chainable handler that accepts a filesystem file, appends | 
| +// |numChars| to it, and returns a Promise that resolves to the file once the | 
| +// append operation is successful. | 
| +function AppendDataToFile(numChars) { | 
| + return ((fileEntry) => { | 
| 
Devlin
2017/06/12 20:15:05
Hmm... where's the first paren closed?  Am I missi
 
ncarter (slow)
2017/06/12 22:52:07
Whoops! Serves me right for trying to rejigger thi
 | 
| + return new Promise((resolve, reject) => { | 
| + fileEntry.createWriter((fileWriter) => { | 
| + // FileWriter's onwriteend resolves the promise; onerror rejects it. | 
| + fileWriter.onwriteend = (e) => resolve(fileEntry); | 
| + fileWriter.onerror = reject; | 
| + | 
| + fileWriter.seek(fileWriter.length); | 
| + var str = ''; | 
| + | 
| + // Avoid many loops for large data packets. | 
| 
Devlin
2017/06/12 20:15:05
or, here just:
str = 'a'.repeat(numChars);
 
ncarter (slow)
2017/06/12 22:52:07
Much simpler! Done.
 | 
| + var iterations = Math.floor(numChars / 100); | 
| + for (var i = 0; i < iterations; ++i) { | 
| + str += kHundredChars; | 
| + } | 
| + | 
| + iterations = numChars % 100; | 
| + for (var i = 0; i < iterations; ++i) { | 
| + str += 'a'; | 
| + } | 
| + | 
| + var blob = new Blob([str], {type: 'text/plain'}); | 
| + fileWriter.write(blob); | 
| + }, reject); | 
| + }); | 
| + } | 
| +} | 
| + | 
| +// Entry point to be called via ExecuteScript in the browser test C++ code. | 
| +// | 
| +// Asynchronously opens writes |numChars| to the filesystem. Returns a Promise | 
| +// that resolves upon completion. | 
| +function HostedAppWriteData(type, numChars) { | 
| + return GetFileSystem(type, 16384) | 
| + .then(CreateFile('test.txt')) | 
| + .then(AppendDataToFile(numChars)); | 
| +} |