/chrome/test/data/extensions/api_test/file_browser/holding_space/test.js

https://github.com/chromium/chromium · JavaScript · 140 lines · 101 code · 14 blank · 25 comment · 0 complexity · 83d5ac3ec80cc15a30fb1d4f5cbe8e2c MD5 · raw file

  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. const callbackPass = chrome.test.callbackPass;
  5. /**
  6. * Gets an external file entry from specified path.
  7. * @param {string} volumeType volume type for entry.
  8. * @param {string} path path of entry.
  9. * @return {!Promise<Entry>} specified entry.
  10. */
  11. function getFileEntry(volumeType, path) {
  12. return new Promise(resolve => {
  13. chrome.fileManagerPrivate.getVolumeMetadataList(list => {
  14. const volume = list.find(v => v.volumeType === volumeType);
  15. chrome.fileSystem.requestFileSystem({volumeId: volume.volumeId}, fs => {
  16. fs.root.getFile(path, {}, entry => {
  17. chrome.fileManagerPrivate.resolveIsolatedEntries(
  18. [entry], externalEntries => {
  19. resolve(externalEntries[0]);
  20. });
  21. });
  22. });
  23. });
  24. });
  25. }
  26. /**
  27. * Wrapper around <code>getFileEntry()</code> that resolves multiple paths.
  28. * @param {string} volumeType
  29. * @param {Array<string>} paths
  30. * @return {!Promise<Array<Entry>>}
  31. */
  32. function getFileEntries(volumeType, paths) {
  33. return Promise.all(paths.map(path => getFileEntry(volumeType, path)));
  34. }
  35. /**
  36. * Converts test file entry indices in |testEntries| into their associated URL.
  37. * @param {Array<number>} indices
  38. * @return {Array<string>}
  39. */
  40. function testItemIndicesToUrls(indices) {
  41. return indices.map(index => testEntries[index]).map(entry => entry.toURL());
  42. }
  43. /**
  44. * List of entries used in tests.
  45. * @type {Array<Entry>}
  46. */
  47. let testEntries = [];
  48. // Run the tests.
  49. chrome.test.runTests([
  50. function testGetTestEntries() {
  51. getFileEntries('testing', [
  52. 'test_dir/test_file.txt', 'test_audio.mp3', 'test_image.jpg'
  53. ]).then(callbackPass(entries => {
  54. testEntries = entries;
  55. }));
  56. },
  57. function testEmptyHoldingSpace() {
  58. chrome.fileManagerPrivate.getHoldingSpaceState(callbackPass(state => {
  59. chrome.test.assertEq({itemUrls: []}, state);
  60. }));
  61. },
  62. function testAddSingleEntry() {
  63. chrome.fileManagerPrivate.toggleAddedToHoldingSpace(
  64. [testEntries[0]], true, callbackPass(() => {
  65. chrome.fileManagerPrivate.getHoldingSpaceState(callbackPass(state => {
  66. const expectedUrls = testItemIndicesToUrls([0]);
  67. chrome.test.assertEq({itemUrls: expectedUrls}, state);
  68. }));
  69. }));
  70. },
  71. function testAddTwoEntries() {
  72. chrome.fileManagerPrivate.toggleAddedToHoldingSpace(
  73. [testEntries[1], testEntries[2]], true, callbackPass(() => {
  74. chrome.fileManagerPrivate.getHoldingSpaceState(callbackPass(state => {
  75. const expectedUrls = testItemIndicesToUrls([0, 1, 2]);
  76. chrome.test.assertEq({itemUrls: expectedUrls}, state);
  77. }));
  78. }));
  79. },
  80. function testRemoveTwoEntries() {
  81. chrome.fileManagerPrivate.toggleAddedToHoldingSpace(
  82. [testEntries[0], testEntries[2]], false, callbackPass(() => {
  83. chrome.fileManagerPrivate.getHoldingSpaceState(callbackPass(state => {
  84. const expectedUrls = testItemIndicesToUrls([1]);
  85. chrome.test.assertEq({itemUrls: expectedUrls}, state);
  86. }));
  87. }));
  88. },
  89. function testAddPreviouslyAddedItem() {
  90. chrome.fileManagerPrivate.toggleAddedToHoldingSpace(
  91. [testEntries[0], testEntries[1]], true, callbackPass(() => {
  92. chrome.fileManagerPrivate.getHoldingSpaceState(callbackPass(state => {
  93. const expectedUrls = testItemIndicesToUrls([1, 0]);
  94. chrome.test.assertEq({itemUrls: expectedUrls}, state);
  95. }));
  96. }));
  97. },
  98. function testRemoveSingleItem() {
  99. chrome.fileManagerPrivate.toggleAddedToHoldingSpace(
  100. [testEntries[1]], false, callbackPass(() => {
  101. chrome.fileManagerPrivate.getHoldingSpaceState(callbackPass(state => {
  102. const expectedUrls = testItemIndicesToUrls([0]);
  103. chrome.test.assertEq({itemUrls: expectedUrls}, state);
  104. }));
  105. }));
  106. },
  107. function testRemoveAllItemsItem() {
  108. chrome.fileManagerPrivate.toggleAddedToHoldingSpace(
  109. [testEntries[0], testEntries[1], testEntries[2]], false,
  110. callbackPass(() => {
  111. chrome.fileManagerPrivate.getHoldingSpaceState(callbackPass(state => {
  112. chrome.test.assertEq({itemUrls: []}, state);
  113. }));
  114. }));
  115. },
  116. function testAddAllItemsItem() {
  117. chrome.fileManagerPrivate.toggleAddedToHoldingSpace(
  118. [testEntries[0], testEntries[1], testEntries[2]], true,
  119. callbackPass(() => {
  120. chrome.fileManagerPrivate.getHoldingSpaceState(callbackPass(state => {
  121. const expectedUrls = testItemIndicesToUrls([0, 1, 2]);
  122. chrome.test.assertEq({itemUrls: expectedUrls}, state);
  123. }));
  124. }));
  125. },
  126. ]);