content-index.https.window.js (3227B)
1 // META: script=/resources/test-only-api.js 2 // META: script=/service-workers/service-worker/resources/test-helpers.sub.js 3 // META: script=resources.js 4 'use strict'; 5 6 contentIndexTest(async (t, index) => { 7 // Exposure of the interface and method. 8 assert_own_property(window, 'ContentIndex'); 9 assert_own_property(ContentIndex.prototype, 'add'); 10 11 assert_idl_attribute(index, 'add'); 12 assert_idl_attribute(index, 'delete'); 13 assert_idl_attribute(index, 'getAll'); 14 15 }, 'The Content Index API is exposed'); 16 17 contentIndexTest(async (t, index) => { 18 await expectTypeError( 19 index.add(createDescription({category: 'fake-category'}))); 20 21 await expectTypeError( 22 index.add(createDescription({iconUrl: 'file://some-local-file.png'}))); 23 24 const isFetchingIcons = await fetchesIcons(); 25 if (isFetchingIcons) { 26 // If the browser will try to fetch these icons we expect it to fail. 27 await expectTypeError( 28 index.add(createDescription({iconUrl: '/non-existent-icon.png'}))); 29 await expectTypeError( 30 index.add(createDescription({iconUrl: '/images/broken.png'}))); 31 } else { 32 // If the browser will not try to fetch these icons this should succeed. 33 await index.add(createDescription({iconUrl: '/non-existent-icon.png'})); 34 await index.add(createDescription({iconUrl: '/images/broken.png'})); 35 } 36 37 await expectTypeError(index.add(createDescription({url: 'https://other-domain.com/'}))); 38 await expectTypeError(index.add(createDescription({url: '/different-scope'}))); 39 40 await index.add(createDescription({})); 41 42 }, 'index.add parameters are validated.'); 43 44 contentIndexTest(async (t, index) => { 45 const description = createDescription({}); 46 47 // Initially there are no descriptions. 48 assert_array_equals(await index.getAll(), []); 49 50 await index.add(description); 51 52 const descriptions = await index.getAll(); 53 assert_equals(descriptions.length, 1); 54 55 assert_object_equals(descriptions[0], description); 56 57 }, 'index.getAll returns the same objects provided.'); 58 59 contentIndexTest(async (t, index) => { 60 const description1 = createDescription({title: 'title1'}); 61 const description2 = createDescription({title: 'title2'}); 62 63 await index.add(description1); 64 await index.add(description2); 65 66 // There should be one description. 67 const descriptions = await index.getAll(); 68 assert_equals(descriptions.length, 1); 69 70 assert_object_equals(descriptions[0], description2); 71 72 }, 'index.add with same ID overwrites existing entry.'); 73 74 contentIndexTest(async (t, index) => { 75 const description1 = createDescription({id: 'id1'}); 76 const description2 = createDescription({id: 'id2'}); 77 78 await index.add(description1); 79 await index.add(description2); 80 81 // There should be two descriptions. 82 assert_equals((await index.getAll()).length, 2); 83 84 await index.delete('id1'); 85 86 // There should be one description. 87 const descriptions = await index.getAll(); 88 assert_equals(descriptions.length, 1); 89 90 assert_object_equals(descriptions[0], description2); 91 92 }, 'index.delete removes entry.'); 93 94 contentIndexTest(async (t, index) => { 95 const descriptions = await index.getAll(); 96 assert_equals(descriptions.length, 0); 97 98 await index.delete('id'); 99 100 }, 'index.delete works on invalid ID.');