Do not transition ICE gathering state to 'complete' when closing

Bug: webrtc:4728
Change-Id: I6bcb3dd0eb47dc945d96555f9481146f22ceb4fa
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/167440
Reviewed-by: Qingsi Wang <qingsi@webrtc.org>
Commit-Queue: Steve Anton <steveanton@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#30433}
diff --git a/pc/peer_connection.cc b/pc/peer_connection.cc
index cf4189b..5ace3e3 100644
--- a/pc/peer_connection.cc
+++ b/pc/peer_connection.cc
@@ -4780,10 +4780,6 @@
         PeerConnectionInterface::IceConnectionState::kIceConnectionClosed;
     connection_state_ = PeerConnectionInterface::PeerConnectionState::kClosed;
     Observer()->OnConnectionChange(connection_state_);
-    if (ice_gathering_state_ != kIceGatheringComplete) {
-      ice_gathering_state_ = kIceGatheringComplete;
-      Observer()->OnIceGatheringChange(ice_gathering_state_);
-    }
   }
   Observer()->OnSignalingChange(signaling_state_);
 }
diff --git a/pc/peer_connection_ice_unittest.cc b/pc/peer_connection_ice_unittest.cc
index ab5a8f4..8502dd4 100644
--- a/pc/peer_connection_ice_unittest.cc
+++ b/pc/peer_connection_ice_unittest.cc
@@ -1404,4 +1404,13 @@
   }
 }
 
+// Regression test for https://bugs.chromium.org/p/webrtc/issues/detail?id=4728
+TEST_P(PeerConnectionIceTest, CloseDoesNotTransitionGatheringStateToComplete) {
+  auto pc = CreatePeerConnectionWithAudioVideo();
+  pc->pc()->Close();
+  EXPECT_FALSE(pc->IsIceGatheringDone());
+  EXPECT_EQ(PeerConnectionInterface::kIceGatheringNew,
+            pc->pc()->ice_gathering_state());
+}
+
 }  // namespace webrtc