ImageKnife/entry/src/test/MemoryLruCache.test.ets

98 lines
4.0 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium';
import image from '@ohos.multimedia.image';
import { ImageKnifeData } from '@ohos/imageword/src/main/ets/model/ImageKnifeData';
import { MemoryLruCache } from '@ohos/imageword/src/main/ets/utils/MemoryLruCache';
export default function MemoryLruCacheTest() {
describe('MemoryLruCacheTest',() => {
// Defines a test suite. Two parameters are supported: test suite name and test suite function.
beforeAll(() => {
// 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.
});
// 测试基础put,get以及size功能
it('assertBasicFunction', 0, async () => {
let memoryCache: MemoryLruCache = new MemoryLruCache(3, 3 * 1024 * 1024);
let data: ImageKnifeData = await getNewImageKnifeData()
memoryCache.put("aaa", data)
memoryCache.put("bbb", data)
memoryCache.put("ccc", data)
console.info(JSON.stringify(memoryCache.get("aaa")))
console.info(memoryCache.size() + "")
// expect(memoryCache.size()).assertEqual(3)
console.info("1111111")
// expect(memoryCache.get("aaa")).assertEqual(data)
// expect(memoryCache.get("bbb")).assertEqual(data)
// expect(memoryCache.get("ccc")).assertEqual(data)
// expect(memoryCache.size()).assertEqual(3)
//
// memoryCache.remove("ccc")
// memoryCache.remove("ddd")
// expect(memoryCache.size()).assertEqual(2)
//
// memoryCache.removeAll()
// expect(memoryCache.size()).assertEqual(0)
});
// it('assertSizeLruFuction', 0, async () => {
// let memoryCache: MemoryLruCache = new MemoryLruCache(3, 3 * 1024 * 1024);
//
// let data: ImageKnifeData = await getNewImageKnifeData()
// memoryCache.put("aaa", data)
// memoryCache.put("bbb", data)
// memoryCache.put("ccc", data)
// memoryCache.put("ddd", data)
//
// expect(memoryCache.get("aaa")).assertUndefined()
// expect(memoryCache.get("bbb")).assertEqual(data)
// memoryCache.put("eee", data)
// expect(memoryCache.get("ccc")).assertUndefined()
// expect(memoryCache.get("bbb")).assertEqual(data)
// expect(memoryCache.get("ddd")).assertEqual(data)
// expect(memoryCache.get("eee")).assertEqual(data)
// });
//
// it('assertMemorySizeLruFuction', 0, () => {
// // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function.
// let a = 'abc';
// let b = 'b';
// // Defines a variety of assertion methods, which are used to declare expected boolean conditions.
// expect(a).assertContain(b);
// expect(a).assertEqual(a);
// });
});
}
async function getNewImageKnifeData(): Promise<ImageKnifeData> {
const color: ArrayBuffer = new ArrayBuffer(96); //96为需要创建的像素buffer大小取值为height * width *4
let opts: image.InitializationOptions = {
editable: true, pixelFormat: 3, size: {
height: 4, width: 6
}
}
let pixelmap: PixelMap = await image.createPixelMap(color, opts)
let data: ImageKnifeData = {
source: pixelmap,
imageWidth: 5,
imageHeight: 5
}
return data
}