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('unzips archive', async () => { 52 const validZipFile = await UnitTestUtils.getFixtureFile( 53 'traces/winscope.zip', 54 ); 55 const unzippedFiles = await FileUtils.unzipFile(validZipFile); 56 expect(unzippedFiles.length).toBe(2); 57 }); 58 59 it('decompresses gzipped file', async () => { 60 const gzippedFile = await UnitTestUtils.getFixtureFile( 61 'traces/WindowManager.pb.gz', 62 ); 63 const unzippedFile = await FileUtils.decompressGZipFile(gzippedFile); 64 expect(unzippedFile.name).toEqual('traces/WindowManager.pb'); 65 expect(unzippedFile.size).toEqual(377137); 66 }); 67 68 it('has download filename regex that accepts all expected inputs', () => { 69 expect(FileUtils.DOWNLOAD_FILENAME_REGEX.test('Winscope2')).toBeTrue(); 70 expect(FileUtils.DOWNLOAD_FILENAME_REGEX.test('win_scope')).toBeTrue(); 71 expect(FileUtils.DOWNLOAD_FILENAME_REGEX.test('win-scope')).toBeTrue(); 72 expect(FileUtils.DOWNLOAD_FILENAME_REGEX.test('win.scope')).toBeTrue(); 73 expect(FileUtils.DOWNLOAD_FILENAME_REGEX.test('win.sc.ope')).toBeTrue(); 74 }); 75 76 it('has download filename regex that rejects all expected inputs', () => { 77 expect(FileUtils.DOWNLOAD_FILENAME_REGEX.test('w?n$cope')).toBeFalse(); 78 expect(FileUtils.DOWNLOAD_FILENAME_REGEX.test('winscope.')).toBeFalse(); 79 expect(FileUtils.DOWNLOAD_FILENAME_REGEX.test('w..scope')).toBeFalse(); 80 expect(FileUtils.DOWNLOAD_FILENAME_REGEX.test('wins--pe')).toBeFalse(); 81 expect(FileUtils.DOWNLOAD_FILENAME_REGEX.test('wi##cope')).toBeFalse(); 82 }); 83}); 84