github.com/pion/webrtc/v4@v4.0.1/signalingstate_test.go (about) 1 // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly> 2 // SPDX-License-Identifier: MIT 3 4 package webrtc 5 6 import ( 7 "testing" 8 9 "github.com/pion/webrtc/v4/pkg/rtcerr" 10 "github.com/stretchr/testify/assert" 11 ) 12 13 func TestNewSignalingState(t *testing.T) { 14 testCases := []struct { 15 stateString string 16 expectedState SignalingState 17 }{ 18 {ErrUnknownType.Error(), SignalingStateUnknown}, 19 {"stable", SignalingStateStable}, 20 {"have-local-offer", SignalingStateHaveLocalOffer}, 21 {"have-remote-offer", SignalingStateHaveRemoteOffer}, 22 {"have-local-pranswer", SignalingStateHaveLocalPranswer}, 23 {"have-remote-pranswer", SignalingStateHaveRemotePranswer}, 24 {"closed", SignalingStateClosed}, 25 } 26 27 for i, testCase := range testCases { 28 assert.Equal(t, 29 testCase.expectedState, 30 newSignalingState(testCase.stateString), 31 "testCase: %d %v", i, testCase, 32 ) 33 } 34 } 35 36 func TestSignalingState_String(t *testing.T) { 37 testCases := []struct { 38 state SignalingState 39 expectedString string 40 }{ 41 {SignalingStateUnknown, ErrUnknownType.Error()}, 42 {SignalingStateStable, "stable"}, 43 {SignalingStateHaveLocalOffer, "have-local-offer"}, 44 {SignalingStateHaveRemoteOffer, "have-remote-offer"}, 45 {SignalingStateHaveLocalPranswer, "have-local-pranswer"}, 46 {SignalingStateHaveRemotePranswer, "have-remote-pranswer"}, 47 {SignalingStateClosed, "closed"}, 48 } 49 50 for i, testCase := range testCases { 51 assert.Equal(t, 52 testCase.expectedString, 53 testCase.state.String(), 54 "testCase: %d %v", i, testCase, 55 ) 56 } 57 } 58 59 func TestSignalingState_Transitions(t *testing.T) { 60 testCases := []struct { 61 desc string 62 current SignalingState 63 next SignalingState 64 op stateChangeOp 65 sdpType SDPType 66 expectedErr error 67 }{ 68 { 69 "stable->SetLocal(offer)->have-local-offer", 70 SignalingStateStable, 71 SignalingStateHaveLocalOffer, 72 stateChangeOpSetLocal, 73 SDPTypeOffer, 74 nil, 75 }, 76 { 77 "stable->SetRemote(offer)->have-remote-offer", 78 SignalingStateStable, 79 SignalingStateHaveRemoteOffer, 80 stateChangeOpSetRemote, 81 SDPTypeOffer, 82 nil, 83 }, 84 { 85 "have-local-offer->SetRemote(answer)->stable", 86 SignalingStateHaveLocalOffer, 87 SignalingStateStable, 88 stateChangeOpSetRemote, 89 SDPTypeAnswer, 90 nil, 91 }, 92 { 93 "have-local-offer->SetRemote(pranswer)->have-remote-pranswer", 94 SignalingStateHaveLocalOffer, 95 SignalingStateHaveRemotePranswer, 96 stateChangeOpSetRemote, 97 SDPTypePranswer, 98 nil, 99 }, 100 { 101 "have-remote-pranswer->SetRemote(answer)->stable", 102 SignalingStateHaveRemotePranswer, 103 SignalingStateStable, 104 stateChangeOpSetRemote, 105 SDPTypeAnswer, 106 nil, 107 }, 108 { 109 "have-remote-offer->SetLocal(answer)->stable", 110 SignalingStateHaveRemoteOffer, 111 SignalingStateStable, 112 stateChangeOpSetLocal, 113 SDPTypeAnswer, 114 nil, 115 }, 116 { 117 "have-remote-offer->SetLocal(pranswer)->have-local-pranswer", 118 SignalingStateHaveRemoteOffer, 119 SignalingStateHaveLocalPranswer, 120 stateChangeOpSetLocal, 121 SDPTypePranswer, 122 nil, 123 }, 124 { 125 "have-local-pranswer->SetLocal(answer)->stable", 126 SignalingStateHaveLocalPranswer, 127 SignalingStateStable, 128 stateChangeOpSetLocal, 129 SDPTypeAnswer, 130 nil, 131 }, 132 { 133 "(invalid) stable->SetRemote(pranswer)->have-remote-pranswer", 134 SignalingStateStable, 135 SignalingStateHaveRemotePranswer, 136 stateChangeOpSetRemote, 137 SDPTypePranswer, 138 &rtcerr.InvalidModificationError{}, 139 }, 140 { 141 "(invalid) stable->SetRemote(rollback)->have-local-offer", 142 SignalingStateStable, 143 SignalingStateHaveLocalOffer, 144 stateChangeOpSetRemote, 145 SDPTypeRollback, 146 &rtcerr.InvalidModificationError{}, 147 }, 148 } 149 150 for i, tc := range testCases { 151 next, err := checkNextSignalingState(tc.current, tc.next, tc.op, tc.sdpType) 152 if tc.expectedErr != nil { 153 assert.Error(t, err, "testCase: %d %s", i, tc.desc) 154 } else { 155 assert.NoError(t, err, "testCase: %d %s", i, tc.desc) 156 assert.Equal(t, 157 tc.next, 158 next, 159 "testCase: %d %s", i, tc.desc, 160 ) 161 } 162 } 163 }