| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- import { DataPersistenceService, PlaylistSyncService } from './DataPersistenceService';
- import { VideoItem } from '../../viewmodel/VideoItem';
- import { PlayMode } from './PlayerStateModel';
- import { LogUtils } from '@ohos/ijkplayer';
- /**
- * DataPersistenceService 测试类
- * 用于验证数据持久化功能的正确性
- */
- export class DataPersistenceServiceTest {
- private dataPersistence: DataPersistenceService;
- private playlistSync: PlaylistSyncService;
-
- constructor() {
- this.dataPersistence = DataPersistenceService.getInstance();
- this.playlistSync = PlaylistSyncService.getInstance();
- }
-
- /**
- * 测试播放进度记忆功能
- */
- async testPlaybackProgressMemory(): Promise<boolean> {
- try {
- const testSongId = 'test_song_001';
- const testFilePath = '/storage/test/song.mp3';
- const testPosition = 120000; // 2分钟
- const testDuration = 300000; // 5分钟
-
- // 测试保存播放进度
- await this.dataPersistence.savePlaybackProgress(testSongId, testFilePath, testPosition, testDuration);
- LogUtils.getInstance().LOGI('DataPersistenceServiceTest: Progress saved successfully');
-
- // 测试加载播放进度
- const loadedProgress = await this.dataPersistence.loadPlaybackProgress(testSongId);
- if (!loadedProgress) {
- LogUtils.getInstance().LOGE('DataPersistenceServiceTest: Failed to load progress');
- return false;
- }
-
- // 验证数据正确性
- if (loadedProgress.position !== testPosition ||
- loadedProgress.duration !== testDuration ||
- loadedProgress.filePath !== testFilePath) {
- LogUtils.getInstance().LOGE('DataPersistenceServiceTest: Progress data mismatch');
- return false;
- }
-
- // 测试清除播放进度
- await this.dataPersistence.clearPlaybackProgress(testSongId);
- const clearedProgress = await this.dataPersistence.loadPlaybackProgress(testSongId);
- if (clearedProgress !== null) {
- LogUtils.getInstance().LOGE('DataPersistenceServiceTest: Progress not cleared properly');
- return false;
- }
-
- LogUtils.getInstance().LOGI('DataPersistenceServiceTest: Playback progress memory test passed');
- return true;
- } catch (error) {
- LogUtils.getInstance().LOGE(`DataPersistenceServiceTest: Progress memory test failed: ${error}`);
- return false;
- }
- }
-
- /**
- * 测试播放列表同步功能
- */
- async testPlaylistSync(): Promise<boolean> {
- try {
- // 创建测试播放列表
- const testPlaylist: VideoItem[] = [
- {
- name: 'Test Song 1',
- filePath: '/storage/test/song1.mp3',
- artist: 'Test Artist 1',
- album: 'Test Album',
- pixelMapPath: ''
- } as VideoItem,
- {
- name: 'Test Song 2',
- filePath: '/storage/test/song2.mp3',
- artist: 'Test Artist 2',
- album: 'Test Album',
- pixelMapPath: ''
- } as VideoItem
- ];
-
- const testIndex = 1;
- const testPlayMode = PlayMode.RANDOM;
-
- // 测试保存播放列表
- await this.dataPersistence.savePlaylist(testPlaylist, testIndex, testPlayMode);
- LogUtils.getInstance().LOGI('DataPersistenceServiceTest: Playlist saved successfully');
-
- // 测试加载播放列表
- const loadedPlaylist = await this.dataPersistence.loadPlaylist();
- if (!loadedPlaylist) {
- LogUtils.getInstance().LOGE('DataPersistenceServiceTest: Failed to load playlist');
- return false;
- }
-
- // 验证数据正确性
- if (loadedPlaylist.songs.length !== testPlaylist.length ||
- loadedPlaylist.currentIndex !== testIndex ||
- loadedPlaylist.playMode !== testPlayMode) {
- LogUtils.getInstance().LOGE('DataPersistenceServiceTest: Playlist data mismatch');
- return false;
- }
-
- // 验证歌曲数据
- for (let i = 0; i < testPlaylist.length; i++) {
- if (loadedPlaylist.songs[i].name !== testPlaylist[i].name ||
- loadedPlaylist.songs[i].filePath !== testPlaylist[i].filePath) {
- LogUtils.getInstance().LOGE(`DataPersistenceServiceTest: Song ${i} data mismatch`);
- return false;
- }
- }
-
- LogUtils.getInstance().LOGI('DataPersistenceServiceTest: Playlist sync test passed');
- return true;
- } catch (error) {
- LogUtils.getInstance().LOGE(`DataPersistenceServiceTest: Playlist sync test failed: ${error}`);
- return false;
- }
- }
-
- /**
- * 测试双向同步功能
- */
- async testBidirectionalSync(): Promise<boolean> {
- try {
- const localPlaylist: VideoItem[] = [
- {
- name: 'Local Song',
- filePath: '/storage/local/song.mp3',
- artist: 'Local Artist',
- album: 'Local Album',
- pixelMapPath: ''
- } as VideoItem
- ];
-
- const localIndex = 0;
- const localPlayMode = PlayMode.SEQUENCE;
-
- // 测试双向同步
- const syncedData = await this.playlistSync.bidirectionalSync(localPlaylist, localIndex, localPlayMode);
-
- if (!syncedData) {
- LogUtils.getInstance().LOGE('DataPersistenceServiceTest: Bidirectional sync returned null');
- return false;
- }
-
- // 验证同步结果
- if (syncedData.songs.length === 0) {
- LogUtils.getInstance().LOGE('DataPersistenceServiceTest: Synced playlist is empty');
- return false;
- }
-
- LogUtils.getInstance().LOGI('DataPersistenceServiceTest: Bidirectional sync test passed');
- return true;
- } catch (error) {
- LogUtils.getInstance().LOGE(`DataPersistenceServiceTest: Bidirectional sync test failed: ${error}`);
- return false;
- }
- }
-
- /**
- * 运行所有测试
- */
- async runAllTests(): Promise<boolean> {
- LogUtils.getInstance().LOGI('DataPersistenceServiceTest: Starting all tests...');
-
- const progressTest = await this.testPlaybackProgressMemory();
- const playlistTest = await this.testPlaylistSync();
- const syncTest = await this.testBidirectionalSync();
-
- const allPassed = progressTest && playlistTest && syncTest;
-
- if (allPassed) {
- LogUtils.getInstance().LOGI('DataPersistenceServiceTest: All tests passed!');
- } else {
- LogUtils.getInstance().LOGE('DataPersistenceServiceTest: Some tests failed');
- }
-
- return allPassed;
- }
- }
|