1// Copyright (C) 2019 The Android Open Source Project 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15import {arrayEquals} from './array_utils'; 16import { 17 base64Decode, 18 base64Encode, 19 binaryDecode, 20 binaryEncode, 21 cropText, 22 sqliteString, 23 utf8Decode, 24 utf8Encode, 25} from './string_utils'; 26 27test('string_utils.stringToBase64', () => { 28 const bytes = [...'Hello, world'].map((c) => c.charCodeAt(0)); 29 const buffer = new Uint8Array(bytes); 30 const b64Encoded = base64Encode(buffer); 31 expect(b64Encoded).toEqual('SGVsbG8sIHdvcmxk'); 32 expect(base64Decode(b64Encoded)).toEqual(buffer); 33}); 34 35test('string_utils.bufferToBase64', () => { 36 const buffer = new Uint8Array([0xff, 0, 0, 0x81, 0x2a, 0xfe]); 37 const b64Encoded = base64Encode(buffer); 38 expect(b64Encoded).toEqual('/wAAgSr+'); 39 expect(base64Decode(b64Encoded)).toEqual(buffer); 40}); 41 42test('string_utils.utf8EncodeAndDecode', () => { 43 const testString = '¡HéllØ wörld!'; 44 const buffer = utf8Encode(testString); 45 const expectedUtf8 = [ 46 194, 161, 72, 195, 169, 108, 108, 195, 152, 32, 119, 195, 182, 114, 108, 47 100, 33, 48 ]; 49 expect(arrayEquals(buffer, expectedUtf8)).toBe(true); 50 expect(utf8Decode(buffer)).toEqual(testString); 51}); 52 53test('string_utils.binaryEncodeAndDecode', () => { 54 const buf = new Uint8Array(256 + 4); 55 for (let i = 0; i < 256; i++) { 56 buf[i] = i; 57 } 58 buf.set([0xf0, 0x28, 0x8c, 0xbc], 256); 59 const encodedStr = binaryEncode(buf); 60 expect(encodedStr.length).toEqual(buf.length); 61 const encodedThroughJson = JSON.parse(JSON.stringify(encodedStr)); 62 expect(binaryDecode(encodedStr)).toEqual(buf); 63 expect(binaryDecode(encodedThroughJson)).toEqual(buf); 64}); 65 66test('string_utils.sqliteString', () => { 67 expect(sqliteString("that's it")).toEqual("'that''s it'"); 68 expect(sqliteString('no quotes')).toEqual("'no quotes'"); 69 expect(sqliteString(`foo ' bar '`)).toEqual(`'foo '' bar '''`); 70}); 71 72test('cropHelper regular text', () => { 73 const tripleDot = '\u2026'; 74 const emoji = '\uD83D\uDE00'; 75 expect( 76 cropText( 77 'com.android.camera [4096]', 78 /* charWidth=*/ 5, 79 /* rectWidth=*/ 2 * 5, 80 ), 81 ).toBe('c'); 82 expect(cropText('com.android.camera [4096]', 5, 4 * 5 + 2)).toBe( 83 'co' + tripleDot, 84 ); 85 expect(cropText('com.android.camera [4096]', 5, 5 * 5 + 2)).toBe( 86 'com' + tripleDot, 87 ); 88 expect(cropText('com.android.camera [4096]', 5, 13 * 5 + 2)).toBe( 89 'com.android' + tripleDot, 90 ); 91 expect(cropText('com.android.camera [4096]', 5, 26 * 5 + 2)).toBe( 92 'com.android.camera [4096]', 93 ); 94 expect(cropText(emoji + 'abc', 5, 2 * 5)).toBe(emoji); 95 expect(cropText(emoji + 'abc', 5, 5 * 5)).toBe(emoji + 'a' + tripleDot); 96}); 97