|
@@ -0,0 +1,203 @@
|
|
|
|
|
+import { hilog } from '@kit.PerformanceAnalysisKit';
|
|
|
|
|
+import { PlayerControlService } from './PlayerControlService';
|
|
|
|
|
+import { AvSessionWidgetListener } from './AvSessionWidgetListener';
|
|
|
|
|
+import { WidgetDataManager } from './WidgetDataManager';
|
|
|
|
|
+import { WidgetData, WidgetSize, WidgetTheme } from './WidgetTypes';
|
|
|
|
|
+
|
|
|
|
|
+const TAG = 'WidgetDataFlowTest';
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 卡片数据流测试工具
|
|
|
|
|
+ * 用于调试和验证数据流是否正常工作
|
|
|
|
|
+ */
|
|
|
|
|
+export class WidgetDataFlowTest {
|
|
|
|
|
+ private playerControlService: PlayerControlService;
|
|
|
|
|
+ private avSessionListener: AvSessionWidgetListener;
|
|
|
|
|
+ private widgetDataManager: WidgetDataManager;
|
|
|
|
|
+
|
|
|
|
|
+ constructor() {
|
|
|
|
|
+ this.playerControlService = new PlayerControlService();
|
|
|
|
|
+ this.avSessionListener = AvSessionWidgetListener.getInstance();
|
|
|
|
|
+ this.widgetDataManager = new WidgetDataManager();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 测试数据流
|
|
|
|
|
+ */
|
|
|
|
|
+ public async testDataFlow(): Promise<void> {
|
|
|
|
|
+ hilog.info(0x0000, TAG, '🔍 Starting widget data flow test...');
|
|
|
|
|
+
|
|
|
|
|
+ // 1. 测试 AvSession 监听器
|
|
|
|
|
+ await this.testAvSessionListener();
|
|
|
|
|
+
|
|
|
|
|
+ // 2. 测试 PlayerControlService
|
|
|
|
|
+ await this.testPlayerControlService();
|
|
|
|
|
+
|
|
|
|
|
+ // 3. 测试 WidgetDataManager
|
|
|
|
|
+ await this.testWidgetDataManager();
|
|
|
|
|
+
|
|
|
|
|
+ // 4. 测试端到端数据流
|
|
|
|
|
+ await this.testEndToEndFlow();
|
|
|
|
|
+
|
|
|
|
|
+ hilog.info(0x0000, TAG, '✅ Widget data flow test completed');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 测试 AvSession 监听器
|
|
|
|
|
+ */
|
|
|
|
|
+ private async testAvSessionListener(): Promise<void> {
|
|
|
|
|
+ hilog.info(0x0000, TAG, '📡 Testing AvSession listener...');
|
|
|
|
|
+
|
|
|
|
|
+ const testData = this.createTestWidgetData();
|
|
|
|
|
+
|
|
|
|
|
+ // 注册监听器
|
|
|
|
|
+ this.avSessionListener.addStateListener((data: WidgetData) => {
|
|
|
|
|
+ hilog.info(0x0000, TAG, `✅ AvSession listener received data: isPlaying=${data.playState.isPlaying}, title=${data.currentSong.title}`);
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ // 更新数据
|
|
|
|
|
+ this.avSessionListener.updateWidgetData(testData);
|
|
|
|
|
+
|
|
|
|
|
+ // 获取当前数据
|
|
|
|
|
+ const currentData = this.avSessionListener.getCurrentWidgetData();
|
|
|
|
|
+ hilog.info(0x0000, TAG, `📊 AvSession current data: hasNext=${currentData.playlist.hasNext}, hasPrevious=${currentData.playlist.hasPrevious}`);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 测试 PlayerControlService
|
|
|
|
|
+ */
|
|
|
|
|
+ private async testPlayerControlService(): Promise<void> {
|
|
|
|
|
+ hilog.info(0x0000, TAG, '🎮 Testing PlayerControlService...');
|
|
|
|
|
+
|
|
|
|
|
+ // 注册监听器
|
|
|
|
|
+ this.playerControlService.registerStateListener((data: WidgetData) => {
|
|
|
|
|
+ hilog.info(0x0000, TAG, `✅ PlayerControlService listener received data: isPlaying=${data.playState.isPlaying}, title=${data.currentSong.title}`);
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ // 获取当前状态
|
|
|
|
|
+ const currentState = await this.playerControlService.getCurrentPlayState();
|
|
|
|
|
+ hilog.info(0x0000, TAG, `📊 PlayerControlService current state: hasNext=${currentState.playlist.hasNext}, hasPrevious=${currentState.playlist.hasPrevious}`);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 测试 WidgetDataManager
|
|
|
|
|
+ */
|
|
|
|
|
+ private async testWidgetDataManager(): Promise<void> {
|
|
|
|
|
+ hilog.info(0x0000, TAG, '📋 Testing WidgetDataManager...');
|
|
|
|
|
+
|
|
|
|
|
+ const testFormId = 'test_form_123';
|
|
|
|
|
+ const testData = this.createTestWidgetData();
|
|
|
|
|
+
|
|
|
|
|
+ // 保存数据
|
|
|
|
|
+ await this.widgetDataManager.saveWidgetData(testFormId, testData);
|
|
|
|
|
+ hilog.info(0x0000, TAG, `💾 Test data saved for form: ${testFormId}`);
|
|
|
|
|
+
|
|
|
|
|
+ // 读取数据
|
|
|
|
|
+ const retrievedData = await this.widgetDataManager.getWidgetData(testFormId);
|
|
|
|
|
+ hilog.info(0x0000, TAG, `📖 Test data retrieved: title=${retrievedData.currentSong.title}, hasNext=${retrievedData.playlist.hasNext}`);
|
|
|
|
|
+
|
|
|
|
|
+ // 模拟更新卡片(不会真正更新,因为formId是测试用的)
|
|
|
|
|
+ try {
|
|
|
|
|
+ await this.widgetDataManager.updateWidget(testFormId, testData);
|
|
|
|
|
+ hilog.info(0x0000, TAG, `🔄 Mock widget update completed for: ${testFormId}`);
|
|
|
|
|
+ } catch (error) {
|
|
|
|
|
+ hilog.error(0x0000, TAG, `❌ Widget update failed (expected for test): ${error}`);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 清理测试数据
|
|
|
|
|
+ await this.widgetDataManager.removeWidgetData(testFormId);
|
|
|
|
|
+ hilog.info(0x0000, TAG, `🗑️ Test data cleaned up for form: ${testFormId}`);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 测试端到端数据流
|
|
|
|
|
+ */
|
|
|
|
|
+ private async testEndToEndFlow(): Promise<void> {
|
|
|
|
|
+ hilog.info(0x0000, TAG, '🔄 Testing end-to-end data flow...');
|
|
|
|
|
+
|
|
|
|
|
+ let receivedByPlayerControl = false;
|
|
|
|
|
+ let receivedByAvSession = false;
|
|
|
|
|
+
|
|
|
|
|
+ // 注册 PlayerControlService 监听器
|
|
|
|
|
+ this.playerControlService.registerStateListener((data: WidgetData) => {
|
|
|
|
|
+ receivedByPlayerControl = true;
|
|
|
|
|
+ hilog.info(0x0000, TAG, `✅ End-to-end: PlayerControlService received data`);
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ // 注册 AvSession 监听器
|
|
|
|
|
+ this.avSessionListener.addStateListener((data: WidgetData) => {
|
|
|
|
|
+ receivedByAvSession = true;
|
|
|
|
|
+ hilog.info(0x0000, TAG, `✅ End-to-end: AvSession received data`);
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ // 模拟数据更新
|
|
|
|
|
+ const testData = this.createTestWidgetData();
|
|
|
|
|
+ this.avSessionListener.updateWidgetData(testData);
|
|
|
|
|
+
|
|
|
|
|
+ // 检查结果
|
|
|
|
|
+ setTimeout(() => {
|
|
|
|
|
+ hilog.info(0x0000, TAG, `📊 End-to-end test results:`);
|
|
|
|
|
+ hilog.info(0x0000, TAG, ` PlayerControlService received: ${receivedByPlayerControl}`);
|
|
|
|
|
+ hilog.info(0x0000, TAG, ` AvSession received: ${receivedByAvSession}`);
|
|
|
|
|
+
|
|
|
|
|
+ if (receivedByPlayerControl && receivedByAvSession) {
|
|
|
|
|
+ hilog.info(0x0000, TAG, `✅ End-to-end data flow is working correctly!`);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ hilog.warn(0x0000, TAG, `⚠️ End-to-end data flow has issues!`);
|
|
|
|
|
+ }
|
|
|
|
|
+ }, 1000);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 创建测试用的 WidgetData
|
|
|
|
|
+ */
|
|
|
|
|
+ private createTestWidgetData(): WidgetData {
|
|
|
|
|
+ return {
|
|
|
|
|
+ playState: {
|
|
|
|
|
+ isPlaying: true,
|
|
|
|
|
+ isPaused: false,
|
|
|
|
|
+ isLoading: false
|
|
|
|
|
+ },
|
|
|
|
|
+ currentSong: {
|
|
|
|
|
+ id: 'test_song_123',
|
|
|
|
|
+ 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: true,
|
|
|
|
|
+ currentIndex: 1,
|
|
|
|
|
+ totalCount: 5
|
|
|
|
|
+ },
|
|
|
|
|
+ config: {
|
|
|
|
|
+ size: WidgetSize.MEDIUM,
|
|
|
|
|
+ theme: WidgetTheme.AUTO,
|
|
|
|
|
+ showProgress: true,
|
|
|
|
|
+ showCover: true
|
|
|
|
|
+ }
|
|
|
|
|
+ };
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 打印当前所有监听器状态
|
|
|
|
|
+ */
|
|
|
|
|
+ public printListenerStatus(): void {
|
|
|
|
|
+ hilog.info(0x0000, TAG, '📊 Current listener status:');
|
|
|
|
|
+ hilog.info(0x0000, TAG, ` PlayerControlService listeners: Available`);
|
|
|
|
|
+ hilog.info(0x0000, TAG, ` AvSession listeners: Available`);
|
|
|
|
|
+
|
|
|
|
|
+ // 获取当前数据
|
|
|
|
|
+ const avSessionData = this.avSessionListener.getCurrentWidgetData();
|
|
|
|
|
+ hilog.info(0x0000, TAG, ` Current AvSession data: isPlaying=${avSessionData.playState.isPlaying}, title=${avSessionData.currentSong.title}`);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|