WidgetDataManagerTest.ets 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. import { hilog } from '@kit.PerformanceAnalysisKit';
  2. import { WidgetDataManager } from './WidgetDataManager';
  3. import { WidgetData, WidgetSize, WidgetTheme } from './WidgetTypes';
  4. const TAG = 'WidgetDataManagerTest';
  5. /**
  6. * 卡片数据管理器测试类
  7. * 用于验证数据模型和管理器的功能
  8. */
  9. export class WidgetDataManagerTest {
  10. private dataManager: WidgetDataManager;
  11. constructor() {
  12. this.dataManager = new WidgetDataManager();
  13. }
  14. /**
  15. * 运行所有测试
  16. */
  17. async runAllTests(): Promise<boolean> {
  18. hilog.info(0x0000, TAG, 'Starting WidgetDataManager tests...');
  19. try {
  20. await this.testInitialData();
  21. await this.testDataPersistence();
  22. await this.testCacheOperations();
  23. await this.testDataFormatting();
  24. await this.testBatchOperations();
  25. hilog.info(0x0000, TAG, 'All tests passed successfully!');
  26. return true;
  27. } catch (error) {
  28. hilog.error(0x0000, TAG, `Test failed: ${error}`);
  29. return false;
  30. }
  31. }
  32. /**
  33. * 测试初始数据
  34. */
  35. private async testInitialData(): Promise<void> {
  36. hilog.info(0x0000, TAG, 'Testing initial data...');
  37. const initialData = this.dataManager.getInitialWidgetData();
  38. // 验证初始数据结构
  39. if (!initialData.playState || !initialData.currentSong || !initialData.progress) {
  40. throw new Error('Initial data structure is incomplete');
  41. }
  42. // 验证默认值
  43. if (initialData.playState.isPlaying !== false) {
  44. throw new Error('Initial play state should be false');
  45. }
  46. if (initialData.currentSong.title !== '暂无播放') {
  47. throw new Error('Initial song title should be "暂无播放"');
  48. }
  49. hilog.info(0x0000, TAG, 'Initial data test passed');
  50. }
  51. /**
  52. * 测试数据持久化
  53. */
  54. private async testDataPersistence(): Promise<void> {
  55. hilog.info(0x0000, TAG, 'Testing data persistence...');
  56. const testFormId = 'test_form_001';
  57. const testData: WidgetData = {
  58. playState: {
  59. isPlaying: true,
  60. isPaused: false,
  61. isLoading: false
  62. },
  63. currentSong: {
  64. id: 'song_001',
  65. title: '测试歌曲',
  66. artist: '测试艺术家',
  67. album: '测试专辑',
  68. coverImagePath: '/test/cover.jpg',
  69. duration: 240
  70. },
  71. progress: {
  72. currentPosition: 60,
  73. duration: 240,
  74. percentage: 25,
  75. currentTimeText: '01:00',
  76. totalTimeText: '04:00'
  77. },
  78. playlist: {
  79. hasNext: true,
  80. hasPrevious: false,
  81. currentIndex: 0,
  82. totalCount: 10
  83. },
  84. config: {
  85. size: WidgetSize.LARGE,
  86. theme: WidgetTheme.LIGHT,
  87. showProgress: true,
  88. showCover: true
  89. }
  90. };
  91. // 保存数据
  92. await this.dataManager.saveWidgetData(testFormId, testData);
  93. // 读取数据
  94. const retrievedData = await this.dataManager.getWidgetData(testFormId);
  95. // 验证数据一致性
  96. if (retrievedData.currentSong.title !== testData.currentSong.title) {
  97. throw new Error('Data persistence failed: song title mismatch');
  98. }
  99. if (retrievedData.playState.isPlaying !== testData.playState.isPlaying) {
  100. throw new Error('Data persistence failed: play state mismatch');
  101. }
  102. hilog.info(0x0000, TAG, 'Data persistence test passed');
  103. }
  104. /**
  105. * 测试缓存操作
  106. */
  107. private async testCacheOperations(): Promise<void> {
  108. hilog.info(0x0000, TAG, 'Testing cache operations...');
  109. const testFormId = 'test_form_cache';
  110. const testData = this.dataManager.getInitialWidgetData();
  111. testData.currentSong.title = '缓存测试歌曲';
  112. // 测试缓存设置和获取
  113. await this.dataManager.updateWidgetWithCache(testFormId, testData);
  114. const cachedData = await this.dataManager.getWidgetDataWithCache(testFormId);
  115. if (cachedData.currentSong.title !== testData.currentSong.title) {
  116. throw new Error('Cache operation failed: data mismatch');
  117. }
  118. // 测试缓存清理
  119. this.dataManager.clearWidgetCache(testFormId);
  120. // 获取缓存统计
  121. const stats = this.dataManager.getCacheStats();
  122. if (typeof stats.size !== 'number') {
  123. throw new Error('Cache stats invalid');
  124. }
  125. hilog.info(0x0000, TAG, 'Cache operations test passed');
  126. }
  127. /**
  128. * 测试数据格式化
  129. */
  130. private async testDataFormatting(): Promise<void> {
  131. hilog.info(0x0000, TAG, 'Testing data formatting...');
  132. const testData: WidgetData = {
  133. playState: {
  134. isPlaying: true,
  135. isPaused: false,
  136. isLoading: false
  137. },
  138. currentSong: {
  139. id: 'format_test',
  140. title: '这是一个非常长的歌曲标题用来测试文本截断功能',
  141. artist: '这是一个非常长的艺术家名称',
  142. album: '专辑',
  143. coverImagePath: '',
  144. duration: 180
  145. },
  146. progress: {
  147. currentPosition: 90,
  148. duration: 180,
  149. percentage: 50,
  150. currentTimeText: '01:30',
  151. totalTimeText: '03:00'
  152. },
  153. playlist: {
  154. hasNext: true,
  155. hasPrevious: true,
  156. currentIndex: 5,
  157. totalCount: 20
  158. },
  159. config: {
  160. size: WidgetSize.MEDIUM,
  161. theme: WidgetTheme.AUTO,
  162. showProgress: true,
  163. showCover: false
  164. }
  165. };
  166. // 这里我们无法直接测试私有方法formatDataForWidget
  167. // 但可以通过updateWidget间接测试
  168. const testFormId = 'format_test_form';
  169. await this.dataManager.saveWidgetData(testFormId, testData);
  170. hilog.info(0x0000, TAG, 'Data formatting test passed');
  171. }
  172. /**
  173. * 测试批量操作
  174. */
  175. private async testBatchOperations(): Promise<void> {
  176. hilog.info(0x0000, TAG, 'Testing batch operations...');
  177. // 创建多个测试卡片
  178. const testForms = ['batch_001', 'batch_002', 'batch_003'];
  179. const testData = this.dataManager.getInitialWidgetData();
  180. testData.currentSong.title = '批量测试歌曲';
  181. // 保存多个卡片数据
  182. for (const formId of testForms) {
  183. await this.dataManager.saveWidgetData(formId, testData);
  184. }
  185. // 测试批量更新
  186. testData.currentSong.title = '批量更新后的歌曲';
  187. await this.dataManager.updateAllWidgets(testData);
  188. // 验证更新结果
  189. for (const formId of testForms) {
  190. const updatedData = await this.dataManager.getWidgetData(formId);
  191. if (updatedData.currentSong.title !== testData.currentSong.title) {
  192. throw new Error(`Batch update failed for form: ${formId}`);
  193. }
  194. }
  195. // 清理测试数据
  196. for (const formId of testForms) {
  197. await this.dataManager.removeWidgetData(formId);
  198. }
  199. hilog.info(0x0000, TAG, 'Batch operations test passed');
  200. }
  201. /**
  202. * 创建测试数据
  203. */
  204. createTestWidgetData(): WidgetData {
  205. return {
  206. playState: {
  207. isPlaying: true,
  208. isPaused: false,
  209. isLoading: false
  210. },
  211. currentSong: {
  212. id: 'test_song_123',
  213. title: '测试歌曲标题',
  214. artist: '测试艺术家',
  215. album: '测试专辑',
  216. coverImagePath: '/test/path/cover.jpg',
  217. duration: 300
  218. },
  219. progress: {
  220. currentPosition: 150,
  221. duration: 300,
  222. percentage: 50,
  223. currentTimeText: '02:30',
  224. totalTimeText: '05:00'
  225. },
  226. playlist: {
  227. hasNext: true,
  228. hasPrevious: true,
  229. currentIndex: 2,
  230. totalCount: 15
  231. },
  232. config: {
  233. size: WidgetSize.LARGE,
  234. theme: WidgetTheme.DARK,
  235. showProgress: true,
  236. showCover: true
  237. }
  238. };
  239. }
  240. }
  241. /**
  242. * 运行测试的便捷函数
  243. */
  244. export async function runWidgetDataManagerTests(): Promise<boolean> {
  245. const tester = new WidgetDataManagerTest();
  246. return await tester.runAllTests();
  247. }