forked from floraachy/ImageKnife
162 lines
7.9 KiB
Plaintext
162 lines
7.9 KiB
Plaintext
/*
|
|
* Copyright (C) 2024 Huawei Device Co., Ltd.
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium'
|
|
import { DiskLruCache, DiskCacheEntry } from '@ohos/imageknife'
|
|
import { common } from '@kit.AbilityKit';
|
|
import fs from '@ohos.file.fs';
|
|
import { GlobalContext } from '../testability/GlobalContext'
|
|
|
|
const BASE_COUNT: number = 2000;
|
|
|
|
export default function DiskLruCacheTest() {
|
|
|
|
describe('DiskLruCacheTest', () => {
|
|
// Defines a test suite. Two parameters are supported: test suite name and test suite function.
|
|
beforeAll(() => {
|
|
//ImageKnife.with(this.context.createModuleContext("entry_test"));
|
|
// Presets an action, which is performed only once before all test cases of the test suite start.
|
|
// This API supports only one parameter: preset action function.
|
|
})
|
|
beforeEach(() => {
|
|
|
|
// Presets an action, which is performed before each unit test case starts.
|
|
// The number of execution times is the same as the number of test cases defined by **it**.
|
|
// This API supports only one parameter: preset action function.
|
|
})
|
|
afterEach(() => {
|
|
// Presets a clear action, which is performed after each unit test case ends.
|
|
// The number of execution times is the same as the number of test cases defined by **it**.
|
|
// This API supports only one parameter: clear action function.
|
|
})
|
|
afterAll(() => {
|
|
// Presets a clear action, which is performed after all test cases of the test suite end.
|
|
// This API supports only one parameter: clear action function.
|
|
})
|
|
it('testSetCahe', 0, () => {
|
|
let context: object | undefined = GlobalContext.getInstance().getObject("hapContext");
|
|
let disLruCache: DiskLruCache = DiskLruCache.create(context as common.UIAbilityContext, 1024);
|
|
let startTime = new Date().getTime();
|
|
disLruCache.set('test', "Hello World Simple Example.");
|
|
disLruCache.set('testABC', "Hello World ABC");
|
|
disLruCache.set('testDE', "Hello World Simple DE");
|
|
expect(String.fromCharCode(...new Uint8Array(disLruCache.get('test') as ArrayBufferLike)) == "Hello World Simple Example.")
|
|
.assertTrue()
|
|
expect(String.fromCharCode(...new Uint8Array(disLruCache.get('testABC') as ArrayBufferLike)) == "Hello World ABC")
|
|
.assertTrue()
|
|
expect(String.fromCharCode(...new Uint8Array(disLruCache.get('testDE') as ArrayBufferLike)) == "Hello World Simple DE")
|
|
.assertTrue()
|
|
for (let index = 0; index < 100; index++) {
|
|
let str: string = 'abc';
|
|
str += index;
|
|
disLruCache.set('test' + index, str);
|
|
}
|
|
expect(disLruCache.getSize() <= 1024).assertTrue()
|
|
endTime(startTime, 'testSetCahe');
|
|
disLruCache.cleanCacheData();
|
|
|
|
})
|
|
it('testGetCacheAsync', 1, () => {
|
|
let context: object | undefined = GlobalContext.getInstance().getObject("hapContext");
|
|
let filesDir: object | undefined = GlobalContext.getInstance().getObject("filesDir");
|
|
let cache: DiskLruCache = DiskLruCache.create(context as common.UIAbilityContext, 1024);
|
|
let startTime = new Date().getTime();
|
|
let path = filesDir + '/testFile.txt';
|
|
let file = fs.openSync(path, 0o102);
|
|
fs.writeSync(file.fd, "hello, world!");
|
|
let length = fs.statSync(path).size;
|
|
let dataArr = new ArrayBuffer(length);
|
|
fs.readSync(file.fd, dataArr);
|
|
cache.set('testFile', dataArr);
|
|
expect(cache.get('testFile')?.byteLength == 13).assertTrue()
|
|
endTime(startTime, 'testGetCacheAsync');
|
|
cache.cleanCacheData();
|
|
})
|
|
it('testDeleteCacheDataByKey', 2, () => {
|
|
let context: object | undefined = GlobalContext.getInstance().getObject("hapContext");
|
|
let cache: DiskLruCache = DiskLruCache.create(context as common.UIAbilityContext, 1024);
|
|
let startTime = new Date().getTime();
|
|
cache.set('test', "Hello World Simple Example.");
|
|
cache.set('testABC', "Hello World ABC");
|
|
expect(String.fromCharCode(...new Uint8Array(cache.get('test') as ArrayBufferLike)))
|
|
.assertEqual("Hello World Simple Example.");
|
|
expect(String.fromCharCode(...new Uint8Array(cache.get('testABC') as ArrayBufferLike)))
|
|
.assertEqual("Hello World ABC");
|
|
cache.deleteCacheDataByKey('test');
|
|
cache.deleteCacheDataByKey('testABC');
|
|
expect(String.fromCharCode(...new Uint8Array(cache.get('test') as ArrayBufferLike))).assertEqual('');
|
|
expect(String.fromCharCode(...new Uint8Array(cache.get('testABC') as ArrayBufferLike))).assertEqual('');
|
|
endTime(startTime, 'testDeleteCacheDataByKey');
|
|
cache.cleanCacheData();
|
|
})
|
|
it('testGetPath', 3, () => {
|
|
let context: object | undefined = GlobalContext.getInstance().getObject("hapContext");
|
|
let cache: DiskLruCache = DiskLruCache.create(context as common.UIAbilityContext, 1024);
|
|
let startTime = new Date().getTime();
|
|
cache.set('test', "Hello World Simple Example.");
|
|
let path: string = cache.getFileToPath('test');
|
|
expect(String.fromCharCode(...new Uint8Array(cache.get('test') as ArrayBufferLike)) == "Hello World Simple Example.")
|
|
.assertTrue()
|
|
expect(cache.getFileToPath('test') == path).assertTrue()
|
|
// expect(cache.getPath() + cache.getCacheMap().getFirstKey() == path).assertTrue()
|
|
endTime(startTime, 'testGetPath');
|
|
cache.cleanCacheData();
|
|
})
|
|
// it('testGetCacheMap', 6, () => {
|
|
// let context: object | undefined = GlobalContext.getInstance().getObject("hapContext");
|
|
// let cache: DiskLruCache = DiskLruCache.create(context as common.UIAbilityContext, 1024);
|
|
// let startTime = new Date().getTime();
|
|
// cache.set('test', "Hello World Simple Example.");
|
|
// expect(cache.getCacheMap().getFirstKey() == '098f6bcd4621d373cade4e832627b4f6').assertTrue()
|
|
// expect(cache.getCacheMap().hasKey('098f6bcd4621d373cade4e832627b4f6') == true).assertTrue()
|
|
// endTime(startTime, 'testGetCacheMap');
|
|
// cache.cleanCacheData();
|
|
//
|
|
// })
|
|
|
|
it('testGetSize', 7, () => {
|
|
let context: object | undefined = GlobalContext.getInstance().getObject("hapContext");
|
|
let cache: DiskLruCache = DiskLruCache.create(context as common.UIAbilityContext, 1024);
|
|
let startTime = new Date().getTime();
|
|
cache.set('test', "Hello World Simple Example.");
|
|
expect(String.fromCharCode(...new Uint8Array(cache.get('test') as ArrayBufferLike)) == "Hello World Simple Example.")
|
|
.assertTrue()
|
|
expect(cache.getSize() == 27).assertTrue()
|
|
endTime(startTime, 'testGetSize');
|
|
cache.cleanCacheData();
|
|
|
|
})
|
|
|
|
it('testDiskCacheEntry', 8, () => {
|
|
let startTime = new Date().getTime();
|
|
let dentry = new DiskCacheEntry('test', 30 * 1024 * 1024)
|
|
expect(dentry.getKey() == 'test').assertTrue()
|
|
dentry.setKey('newtest')
|
|
expect(dentry.getKey() == 'newtest').assertTrue()
|
|
expect(dentry.getLength() == 30 * 1024 * 1024).assertTrue()
|
|
dentry.setLength(12 * 1024 * 1024)
|
|
expect(dentry.getLength() == 12 * 1024 * 1024).assertTrue()
|
|
expect(dentry.toString() == dentry.getKey() + ' - ' + dentry.getLength()).assertTrue()
|
|
endTime(startTime, 'testDiskCacheEntry');
|
|
})
|
|
})
|
|
}
|
|
|
|
function endTime(startTime: number, tag: string) {
|
|
let endTime: number = new Date().getTime();
|
|
let averageTime = ((endTime - startTime) * 1000 / BASE_COUNT)
|
|
console.info(tag + " startTime: " + endTime)
|
|
console.info(tag + " endTime: " + endTime)
|
|
console.log(tag + " averageTime: " + averageTime + "μs");
|
|
} |