| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- import { hilog } from '@kit.PerformanceAnalysisKit';
- import { WidgetDataManager } from './WidgetDataManager';
- import { WidgetData, WidgetSize, WidgetTheme } from './WidgetTypes';
- const TAG = 'WidgetDataManagerTest';
- /**
- * 卡片数据管理器测试类
- * 用于验证数据模型和管理器的功能
- */
- export class WidgetDataManagerTest {
- private dataManager: WidgetDataManager;
- constructor() {
- this.dataManager = new WidgetDataManager();
- }
- /**
- * 运行所有测试
- */
- async runAllTests(): Promise<boolean> {
- hilog.info(0x0000, TAG, 'Starting WidgetDataManager tests...');
-
- try {
- await this.testInitialData();
- await this.testDataPersistence();
- await this.testCacheOperations();
- await this.testDataFormatting();
- await this.testBatchOperations();
-
- hilog.info(0x0000, TAG, 'All tests passed successfully!');
- return true;
- } catch (error) {
- hilog.error(0x0000, TAG, `Test failed: ${error}`);
- return false;
- }
- }
- /**
- * 测试初始数据
- */
- private async testInitialData(): Promise<void> {
- hilog.info(0x0000, TAG, 'Testing initial data...');
-
- const initialData = this.dataManager.getInitialWidgetData();
-
- // 验证初始数据结构
- if (!initialData.playState || !initialData.currentSong || !initialData.progress) {
- throw new Error('Initial data structure is incomplete');
- }
-
- // 验证默认值
- if (initialData.playState.isPlaying !== false) {
- throw new Error('Initial play state should be false');
- }
-
- if (initialData.currentSong.title !== '暂无播放') {
- throw new Error('Initial song title should be "暂无播放"');
- }
-
- hilog.info(0x0000, TAG, 'Initial data test passed');
- }
- /**
- * 测试数据持久化
- */
- private async testDataPersistence(): Promise<void> {
- hilog.info(0x0000, TAG, 'Testing data persistence...');
-
- const testFormId = 'test_form_001';
- const testData: WidgetData = {
- playState: {
- isPlaying: true,
- isPaused: false,
- isLoading: false
- },
- currentSong: {
- id: 'song_001',
- title: '测试歌曲',
- artist: '测试艺术家',
- album: '测试专辑',
- coverImagePath: '/test/cover.jpg',
- duration: 240
- },
- progress: {
- currentPosition: 60,
- duration: 240,
- percentage: 25,
- currentTimeText: '01:00',
- totalTimeText: '04:00'
- },
- playlist: {
- hasNext: true,
- hasPrevious: false,
- currentIndex: 0,
- totalCount: 10
- },
- config: {
- size: WidgetSize.LARGE,
- theme: WidgetTheme.LIGHT,
- showProgress: true,
- showCover: true
- }
- };
-
- // 保存数据
- await this.dataManager.saveWidgetData(testFormId, testData);
-
- // 读取数据
- const retrievedData = await this.dataManager.getWidgetData(testFormId);
-
- // 验证数据一致性
- if (retrievedData.currentSong.title !== testData.currentSong.title) {
- throw new Error('Data persistence failed: song title mismatch');
- }
-
- if (retrievedData.playState.isPlaying !== testData.playState.isPlaying) {
- throw new Error('Data persistence failed: play state mismatch');
- }
-
- hilog.info(0x0000, TAG, 'Data persistence test passed');
- }
- /**
- * 测试缓存操作
- */
- private async testCacheOperations(): Promise<void> {
- hilog.info(0x0000, TAG, 'Testing cache operations...');
-
- const testFormId = 'test_form_cache';
- const testData = this.dataManager.getInitialWidgetData();
- testData.currentSong.title = '缓存测试歌曲';
-
- // 测试缓存设置和获取
- await this.dataManager.updateWidgetWithCache(testFormId, testData);
- const cachedData = await this.dataManager.getWidgetDataWithCache(testFormId);
-
- if (cachedData.currentSong.title !== testData.currentSong.title) {
- throw new Error('Cache operation failed: data mismatch');
- }
-
- // 测试缓存清理
- this.dataManager.clearWidgetCache(testFormId);
-
- // 获取缓存统计
- const stats = this.dataManager.getCacheStats();
- if (typeof stats.size !== 'number') {
- throw new Error('Cache stats invalid');
- }
-
- hilog.info(0x0000, TAG, 'Cache operations test passed');
- }
- /**
- * 测试数据格式化
- */
- private async testDataFormatting(): Promise<void> {
- hilog.info(0x0000, TAG, 'Testing data formatting...');
-
- const testData: WidgetData = {
- playState: {
- isPlaying: true,
- isPaused: false,
- isLoading: false
- },
- currentSong: {
- id: 'format_test',
- title: '这是一个非常长的歌曲标题用来测试文本截断功能',
- artist: '这是一个非常长的艺术家名称',
- album: '专辑',
- coverImagePath: '',
- duration: 180
- },
- progress: {
- currentPosition: 90,
- duration: 180,
- percentage: 50,
- currentTimeText: '01:30',
- totalTimeText: '03:00'
- },
- playlist: {
- hasNext: true,
- hasPrevious: true,
- currentIndex: 5,
- totalCount: 20
- },
- config: {
- size: WidgetSize.MEDIUM,
- theme: WidgetTheme.AUTO,
- showProgress: true,
- showCover: false
- }
- };
-
- // 这里我们无法直接测试私有方法formatDataForWidget
- // 但可以通过updateWidget间接测试
- const testFormId = 'format_test_form';
- await this.dataManager.saveWidgetData(testFormId, testData);
-
- hilog.info(0x0000, TAG, 'Data formatting test passed');
- }
- /**
- * 测试批量操作
- */
- private async testBatchOperations(): Promise<void> {
- hilog.info(0x0000, TAG, 'Testing batch operations...');
-
- // 创建多个测试卡片
- const testForms = ['batch_001', 'batch_002', 'batch_003'];
- const testData = this.dataManager.getInitialWidgetData();
- testData.currentSong.title = '批量测试歌曲';
-
- // 保存多个卡片数据
- for (const formId of testForms) {
- await this.dataManager.saveWidgetData(formId, testData);
- }
-
- // 测试批量更新
- testData.currentSong.title = '批量更新后的歌曲';
- await this.dataManager.updateAllWidgets(testData);
-
- // 验证更新结果
- for (const formId of testForms) {
- const updatedData = await this.dataManager.getWidgetData(formId);
- if (updatedData.currentSong.title !== testData.currentSong.title) {
- throw new Error(`Batch update failed for form: ${formId}`);
- }
- }
-
- // 清理测试数据
- for (const formId of testForms) {
- await this.dataManager.removeWidgetData(formId);
- }
-
- hilog.info(0x0000, TAG, 'Batch operations test passed');
- }
- /**
- * 创建测试数据
- */
- createTestWidgetData(): WidgetData {
- return {
- playState: {
- isPlaying: true,
- isPaused: false,
- isLoading: false
- },
- currentSong: {
- id: 'test_song_123',
- title: '测试歌曲标题',
- artist: '测试艺术家',
- album: '测试专辑',
- coverImagePath: '/test/path/cover.jpg',
- duration: 300
- },
- progress: {
- currentPosition: 150,
- duration: 300,
- percentage: 50,
- currentTimeText: '02:30',
- totalTimeText: '05:00'
- },
- playlist: {
- hasNext: true,
- hasPrevious: true,
- currentIndex: 2,
- totalCount: 15
- },
- config: {
- size: WidgetSize.LARGE,
- theme: WidgetTheme.DARK,
- showProgress: true,
- showCover: true
- }
- };
- }
- }
- /**
- * 运行测试的便捷函数
- */
- export async function runWidgetDataManagerTests(): Promise<boolean> {
- const tester = new WidgetDataManagerTest();
- return await tester.runAllTests();
- }
|