DataPersistenceServiceTest.ets 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import { DataPersistenceService, PlaylistSyncService } from './DataPersistenceService';
  2. import { VideoItem } from '../../viewmodel/VideoItem';
  3. import { PlayMode } from './PlayerStateModel';
  4. import { LogUtils } from '@ohos/ijkplayer';
  5. /**
  6. * DataPersistenceService 测试类
  7. * 用于验证数据持久化功能的正确性
  8. */
  9. export class DataPersistenceServiceTest {
  10. private dataPersistence: DataPersistenceService;
  11. private playlistSync: PlaylistSyncService;
  12. constructor() {
  13. this.dataPersistence = DataPersistenceService.getInstance();
  14. this.playlistSync = PlaylistSyncService.getInstance();
  15. }
  16. /**
  17. * 测试播放进度记忆功能
  18. */
  19. async testPlaybackProgressMemory(): Promise<boolean> {
  20. try {
  21. const testSongId = 'test_song_001';
  22. const testFilePath = '/storage/test/song.mp3';
  23. const testPosition = 120000; // 2分钟
  24. const testDuration = 300000; // 5分钟
  25. // 测试保存播放进度
  26. await this.dataPersistence.savePlaybackProgress(testSongId, testFilePath, testPosition, testDuration);
  27. LogUtils.getInstance().LOGI('DataPersistenceServiceTest: Progress saved successfully');
  28. // 测试加载播放进度
  29. const loadedProgress = await this.dataPersistence.loadPlaybackProgress(testSongId);
  30. if (!loadedProgress) {
  31. LogUtils.getInstance().LOGE('DataPersistenceServiceTest: Failed to load progress');
  32. return false;
  33. }
  34. // 验证数据正确性
  35. if (loadedProgress.position !== testPosition ||
  36. loadedProgress.duration !== testDuration ||
  37. loadedProgress.filePath !== testFilePath) {
  38. LogUtils.getInstance().LOGE('DataPersistenceServiceTest: Progress data mismatch');
  39. return false;
  40. }
  41. // 测试清除播放进度
  42. await this.dataPersistence.clearPlaybackProgress(testSongId);
  43. const clearedProgress = await this.dataPersistence.loadPlaybackProgress(testSongId);
  44. if (clearedProgress !== null) {
  45. LogUtils.getInstance().LOGE('DataPersistenceServiceTest: Progress not cleared properly');
  46. return false;
  47. }
  48. LogUtils.getInstance().LOGI('DataPersistenceServiceTest: Playback progress memory test passed');
  49. return true;
  50. } catch (error) {
  51. LogUtils.getInstance().LOGE(`DataPersistenceServiceTest: Progress memory test failed: ${error}`);
  52. return false;
  53. }
  54. }
  55. /**
  56. * 测试播放列表同步功能
  57. */
  58. async testPlaylistSync(): Promise<boolean> {
  59. try {
  60. // 创建测试播放列表
  61. const testPlaylist: VideoItem[] = [
  62. {
  63. name: 'Test Song 1',
  64. filePath: '/storage/test/song1.mp3',
  65. artist: 'Test Artist 1',
  66. album: 'Test Album',
  67. pixelMapPath: ''
  68. } as VideoItem,
  69. {
  70. name: 'Test Song 2',
  71. filePath: '/storage/test/song2.mp3',
  72. artist: 'Test Artist 2',
  73. album: 'Test Album',
  74. pixelMapPath: ''
  75. } as VideoItem
  76. ];
  77. const testIndex = 1;
  78. const testPlayMode = PlayMode.RANDOM;
  79. // 测试保存播放列表
  80. await this.dataPersistence.savePlaylist(testPlaylist, testIndex, testPlayMode);
  81. LogUtils.getInstance().LOGI('DataPersistenceServiceTest: Playlist saved successfully');
  82. // 测试加载播放列表
  83. const loadedPlaylist = await this.dataPersistence.loadPlaylist();
  84. if (!loadedPlaylist) {
  85. LogUtils.getInstance().LOGE('DataPersistenceServiceTest: Failed to load playlist');
  86. return false;
  87. }
  88. // 验证数据正确性
  89. if (loadedPlaylist.songs.length !== testPlaylist.length ||
  90. loadedPlaylist.currentIndex !== testIndex ||
  91. loadedPlaylist.playMode !== testPlayMode) {
  92. LogUtils.getInstance().LOGE('DataPersistenceServiceTest: Playlist data mismatch');
  93. return false;
  94. }
  95. // 验证歌曲数据
  96. for (let i = 0; i < testPlaylist.length; i++) {
  97. if (loadedPlaylist.songs[i].name !== testPlaylist[i].name ||
  98. loadedPlaylist.songs[i].filePath !== testPlaylist[i].filePath) {
  99. LogUtils.getInstance().LOGE(`DataPersistenceServiceTest: Song ${i} data mismatch`);
  100. return false;
  101. }
  102. }
  103. LogUtils.getInstance().LOGI('DataPersistenceServiceTest: Playlist sync test passed');
  104. return true;
  105. } catch (error) {
  106. LogUtils.getInstance().LOGE(`DataPersistenceServiceTest: Playlist sync test failed: ${error}`);
  107. return false;
  108. }
  109. }
  110. /**
  111. * 测试双向同步功能
  112. */
  113. async testBidirectionalSync(): Promise<boolean> {
  114. try {
  115. const localPlaylist: VideoItem[] = [
  116. {
  117. name: 'Local Song',
  118. filePath: '/storage/local/song.mp3',
  119. artist: 'Local Artist',
  120. album: 'Local Album',
  121. pixelMapPath: ''
  122. } as VideoItem
  123. ];
  124. const localIndex = 0;
  125. const localPlayMode = PlayMode.SEQUENCE;
  126. // 测试双向同步
  127. const syncedData = await this.playlistSync.bidirectionalSync(localPlaylist, localIndex, localPlayMode);
  128. if (!syncedData) {
  129. LogUtils.getInstance().LOGE('DataPersistenceServiceTest: Bidirectional sync returned null');
  130. return false;
  131. }
  132. // 验证同步结果
  133. if (syncedData.songs.length === 0) {
  134. LogUtils.getInstance().LOGE('DataPersistenceServiceTest: Synced playlist is empty');
  135. return false;
  136. }
  137. LogUtils.getInstance().LOGI('DataPersistenceServiceTest: Bidirectional sync test passed');
  138. return true;
  139. } catch (error) {
  140. LogUtils.getInstance().LOGE(`DataPersistenceServiceTest: Bidirectional sync test failed: ${error}`);
  141. return false;
  142. }
  143. }
  144. /**
  145. * 运行所有测试
  146. */
  147. async runAllTests(): Promise<boolean> {
  148. LogUtils.getInstance().LOGI('DataPersistenceServiceTest: Starting all tests...');
  149. const progressTest = await this.testPlaybackProgressMemory();
  150. const playlistTest = await this.testPlaylistSync();
  151. const syncTest = await this.testBidirectionalSync();
  152. const allPassed = progressTest && playlistTest && syncTest;
  153. if (allPassed) {
  154. LogUtils.getInstance().LOGI('DataPersistenceServiceTest: All tests passed!');
  155. } else {
  156. LogUtils.getInstance().LOGE('DataPersistenceServiceTest: Some tests failed');
  157. }
  158. return allPassed;
  159. }
  160. }