1/* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16import {UnitTestUtils} from 'test/unit/utils'; 17import {FileUtils} from './file_utils'; 18 19describe('FileUtils', () => { 20 it('extracts file extensions', () => { 21 expect(FileUtils.getFileExtension('winscope.zip')).toEqual('zip'); 22 expect(FileUtils.getFileExtension('win.scope.zip')).toEqual('zip'); 23 expect(FileUtils.getFileExtension('winscopezip')).toEqual(undefined); 24 }); 25 26 it('removes directory from filename', () => { 27 expect(FileUtils.removeDirFromFileName('test/winscope.zip')).toEqual( 28 'winscope.zip', 29 ); 30 expect(FileUtils.removeDirFromFileName('test/test/winscope.zip')).toEqual( 31 'winscope.zip', 32 ); 33 }); 34 35 it('removes extension from filename', () => { 36 expect(FileUtils.removeExtensionFromFilename('winscope.zip')).toEqual( 37 'winscope', 38 ); 39 expect(FileUtils.removeExtensionFromFilename('win.scope.zip')).toEqual( 40 'win.scope', 41 ); 42 }); 43 44 it('creates zip archive', async () => { 45 const zip = await FileUtils.createZipArchive([ 46 new File([], 'test_file.txt'), 47 ]); 48 expect(zip).toBeInstanceOf(Blob); 49 }); 50 51 it('creates zip archive with progress listener', async () => { 52 const progressSpy = jasmine.createSpy(); 53 const zip = await FileUtils.createZipArchive( 54 [ 55 new File([], 'test_file.txt'), 56 new File([], 'test_file_2.txt'), 57 new File([], 'test_file_3.txt'), 58 new File([], 'test_file_4.txt'), 59 ], 60 progressSpy, 61 ); 62 expect(zip).toBeInstanceOf(Blob); 63 expect(progressSpy).toHaveBeenCalledTimes(4); 64 expect(progressSpy).toHaveBeenCalledWith(0.25); 65 expect(progressSpy).toHaveBeenCalledWith(0.5); 66 expect(progressSpy).toHaveBeenCalledWith(0.75); 67 expect(progressSpy).toHaveBeenCalledWith(1); 68 }); 69 70 it('unzips archive', async () => { 71 const validZipFile = await UnitTestUtils.getFixtureFile( 72 'traces/winscope.zip', 73 ); 74 const unzippedFiles = await FileUtils.unzipFile(validZipFile); 75 expect(unzippedFiles.map((f) => f.name)).toEqual([ 76 'Surface Flinger/SurfaceFlinger.pb', 77 'Window Manager/WindowManager.pb', 78 ]); 79 }); 80 81 it('recursively unzips archive', async () => { 82 const validZipFile = await UnitTestUtils.getFixtureFile( 83 'traces/recursive_winscope.zip', 84 ); 85 const unzippedFiles = await FileUtils.unzipFile(validZipFile); 86 expect(unzippedFiles.map((f) => f.name)).toEqual([ 87 'Surface Flinger/SurfaceFlinger.pb', 88 'Window Manager/WindowManager.pb', 89 ]); 90 }); 91 92 it('decompresses gzipped file', async () => { 93 const gzippedFile = await UnitTestUtils.getFixtureFile( 94 'traces/WindowManager.pb.gz', 95 ); 96 const unzippedFile = await FileUtils.decompressGZipFile(gzippedFile); 97 expect(unzippedFile.name).toEqual('traces/WindowManager.pb'); 98 expect(unzippedFile.size).toEqual(377137); 99 }); 100 101 it('decompresses gzipped archive', async () => { 102 const gzippedFile = await UnitTestUtils.getFixtureFile( 103 'traces/WindowManager.zip.gz', 104 ); 105 const unzippedFile = await FileUtils.decompressGZipFile(gzippedFile); 106 expect(unzippedFile.name).toEqual('traces/WindowManager.zip'); 107 expect(unzippedFile.size).toEqual(10158); 108 }); 109 110 it('has download filename regex that accepts all expected inputs', () => { 111 expect(FileUtils.DOWNLOAD_FILENAME_REGEX.test('Winscope2')).toBeTrue(); 112 expect(FileUtils.DOWNLOAD_FILENAME_REGEX.test('win_scope')).toBeTrue(); 113 expect(FileUtils.DOWNLOAD_FILENAME_REGEX.test('win-scope')).toBeTrue(); 114 expect(FileUtils.DOWNLOAD_FILENAME_REGEX.test('win.scope')).toBeTrue(); 115 expect(FileUtils.DOWNLOAD_FILENAME_REGEX.test('win.sc.ope')).toBeTrue(); 116 }); 117 118 it('has download filename regex that rejects all expected inputs', () => { 119 expect(FileUtils.DOWNLOAD_FILENAME_REGEX.test('w?n$cope')).toBeFalse(); 120 expect(FileUtils.DOWNLOAD_FILENAME_REGEX.test('winscope.')).toBeFalse(); 121 expect(FileUtils.DOWNLOAD_FILENAME_REGEX.test('w..scope')).toBeFalse(); 122 expect(FileUtils.DOWNLOAD_FILENAME_REGEX.test('wins--pe')).toBeFalse(); 123 expect(FileUtils.DOWNLOAD_FILENAME_REGEX.test('wi##cope')).toBeFalse(); 124 }); 125}); 126