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 { 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 { 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 { 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 { 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 { 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 { 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 { const tester = new WidgetDataManagerTest(); return await tester.runAllTests(); }