|
| 1 | +import 'dart:io'; |
| 2 | +import 'package:flutter_test/flutter_test.dart'; |
| 3 | +import 'package:janus_client/janus_client.dart'; |
| 4 | + |
| 5 | +class _MyHttpOverrides extends HttpOverrides {} |
| 6 | + |
| 7 | +void main() async { |
| 8 | + TestWidgetsFlutterBinding.ensureInitialized(); |
| 9 | + HttpOverrides.global = _MyHttpOverrides(); |
| 10 | + |
| 11 | + group('JanusPlugin getBitrate() Basic Tests', () { |
| 12 | + late JanusPlugin plugin; |
| 13 | + late JanusClient client; |
| 14 | + late JanusTransport transport; |
| 15 | + |
| 16 | + setUp(() { |
| 17 | + transport = RestJanusTransport(url: 'https://janus.conf.meetecho.com/janus'); |
| 18 | + client = JanusClient(transport: transport); |
| 19 | + |
| 20 | + // Create a basic streaming plugin for testing |
| 21 | + plugin = JanusStreamingPlugin( |
| 22 | + context: client, |
| 23 | + handleId: 123, |
| 24 | + transport: transport, |
| 25 | + session: JanusSession(transport: transport, context: client), |
| 26 | + ); |
| 27 | + }); |
| 28 | + |
| 29 | + test('getBitrate returns null when no WebRTC connection', () async { |
| 30 | + // Act |
| 31 | + final result = await plugin.getBitrate(); |
| 32 | + |
| 33 | + // Assert |
| 34 | + expect(result, isNull, |
| 35 | + reason: 'getBitrate should return null when no WebRTC connection is established'); |
| 36 | + }); |
| 37 | + |
| 38 | + test('getBitrate returns null when called with mid parameter and no connection', () async { |
| 39 | + // Act |
| 40 | + final result = await plugin.getBitrate('v1'); |
| 41 | + |
| 42 | + // Assert |
| 43 | + expect(result, isNull, |
| 44 | + reason: 'getBitrate should return null for specific mid when no WebRTC connection'); |
| 45 | + }); |
| 46 | + |
| 47 | + test('getBitrate handles empty mid parameter correctly', () async { |
| 48 | + // Act |
| 49 | + final result = await plugin.getBitrate(''); |
| 50 | + |
| 51 | + // Assert |
| 52 | + expect(result, isNull, |
| 53 | + reason: 'getBitrate should handle empty mid parameter gracefully'); |
| 54 | + }); |
| 55 | + |
| 56 | + test('getBitrate method exists and is callable', () { |
| 57 | + // Assert |
| 58 | + expect(plugin.getBitrate, isA<Function>(), |
| 59 | + reason: 'getBitrate method should exist on JanusPlugin'); |
| 60 | + }); |
| 61 | + |
| 62 | + test('getBitrate method signature accepts optional mid parameter', () { |
| 63 | + // This test verifies the method signature at compile time |
| 64 | + // No runtime assertions needed - compilation success is the test |
| 65 | + |
| 66 | + // Act & Assert - These should compile without errors |
| 67 | + plugin.getBitrate(); // No parameter |
| 68 | + plugin.getBitrate(null); // Explicit null |
| 69 | + plugin.getBitrate('v1'); // String parameter |
| 70 | + |
| 71 | + expect(true, isTrue, reason: 'Method signature test passed'); |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + group('JanusPlugin getBitrate() Implementation Tests', () { |
| 76 | + test('Bitrate calculation variables are properly initialized', () { |
| 77 | + final transport = RestJanusTransport(url: 'https://janus.conf.meetecho.com/janus'); |
| 78 | + final client = JanusClient(transport: transport); |
| 79 | + final plugin = JanusStreamingPlugin( |
| 80 | + context: client, |
| 81 | + handleId: 123, |
| 82 | + transport: transport, |
| 83 | + session: JanusSession(transport: transport, context: client), |
| 84 | + ); |
| 85 | + |
| 86 | + // The fact that we can create the plugin without errors |
| 87 | + // indicates that the bitrate tracking variables are properly initialized |
| 88 | + expect(plugin, isNotNull); |
| 89 | + expect(plugin.getBitrate, isA<Function>()); |
| 90 | + }); |
| 91 | + |
| 92 | + test('Multiple getBitrate calls with different mids should not interfere', () async { |
| 93 | + final transport = RestJanusTransport(url: 'https://janus.conf.meetecho.com/janus'); |
| 94 | + final client = JanusClient(transport: transport); |
| 95 | + final plugin = JanusStreamingPlugin( |
| 96 | + context: client, |
| 97 | + handleId: 123, |
| 98 | + transport: transport, |
| 99 | + session: JanusSession(transport: transport, context: client), |
| 100 | + ); |
| 101 | + |
| 102 | + // Act - Multiple calls with different parameters |
| 103 | + final result1 = await plugin.getBitrate(); |
| 104 | + final result2 = await plugin.getBitrate('v1'); |
| 105 | + final result3 = await plugin.getBitrate('v2'); |
| 106 | + final result4 = await plugin.getBitrate(); // Back to default |
| 107 | + |
| 108 | + // Assert - All should return null (no WebRTC connection) |
| 109 | + // but importantly, they should not throw exceptions |
| 110 | + expect(result1, isNull); |
| 111 | + expect(result2, isNull); |
| 112 | + expect(result3, isNull); |
| 113 | + expect(result4, isNull); |
| 114 | + }); |
| 115 | + |
| 116 | + test('getBitrate with null mid parameter should behave same as no parameter', () async { |
| 117 | + final transport = RestJanusTransport(url: 'https://janus.conf.meetecho.com/janus'); |
| 118 | + final client = JanusClient(transport: transport); |
| 119 | + final plugin = JanusStreamingPlugin( |
| 120 | + context: client, |
| 121 | + handleId: 123, |
| 122 | + transport: transport, |
| 123 | + session: JanusSession(transport: transport, context: client), |
| 124 | + ); |
| 125 | + |
| 126 | + // Act |
| 127 | + final result1 = await plugin.getBitrate(null); |
| 128 | + final result2 = await plugin.getBitrate(); |
| 129 | + |
| 130 | + // Assert - Both should return same result (null in this case) |
| 131 | + expect(result1, equals(result2), |
| 132 | + reason: 'getBitrate(null) should behave identically to getBitrate()'); |
| 133 | + }); |
| 134 | + |
| 135 | + test('getBitrate should handle very long mid strings gracefully', () async { |
| 136 | + final transport = RestJanusTransport(url: 'https://janus.conf.meetecho.com/janus'); |
| 137 | + final client = JanusClient(transport: transport); |
| 138 | + final plugin = JanusStreamingPlugin( |
| 139 | + context: client, |
| 140 | + handleId: 123, |
| 141 | + transport: transport, |
| 142 | + session: JanusSession(transport: transport, context: client), |
| 143 | + ); |
| 144 | + |
| 145 | + // Act |
| 146 | + final longMid = 'a' * 1000; // Very long string |
| 147 | + final result = await plugin.getBitrate(longMid); |
| 148 | + |
| 149 | + // Assert - Should not throw exception |
| 150 | + expect(result, isNull, |
| 151 | + reason: 'getBitrate should handle very long mid strings gracefully'); |
| 152 | + }); |
| 153 | + |
| 154 | + test('getBitrate should handle special characters in mid parameter', () async { |
| 155 | + final transport = RestJanusTransport(url: 'https://janus.conf.meetecho.com/janus'); |
| 156 | + final client = JanusClient(transport: transport); |
| 157 | + final plugin = JanusStreamingPlugin( |
| 158 | + context: client, |
| 159 | + handleId: 123, |
| 160 | + transport: transport, |
| 161 | + session: JanusSession(transport: transport, context: client), |
| 162 | + ); |
| 163 | + |
| 164 | + // Act - Test various special characters |
| 165 | + final specialMids = ['v1-test', 'v1_test', 'v1.test', 'v1@test', 'v1#test', '日本語']; |
| 166 | + |
| 167 | + for (final mid in specialMids) { |
| 168 | + final result = await plugin.getBitrate(mid); |
| 169 | + expect(result, isNull, |
| 170 | + reason: 'getBitrate should handle special characters in mid: $mid'); |
| 171 | + } |
| 172 | + }); |
| 173 | + }); |
| 174 | +} |
0 commit comments