code: def test_subscribe(event_channels):
      """Test subscribing to UI events."""
      callback_calls = list()
  
      def callback(event):
          """Respond to time change event."""
          callback_calls.append(event)
          assert isinstance(event, ui_events.TimeChange)
          assert event.time == 10.2
  
      fig = plt.figure()
      ui_events.subscribe(fig, "time_change", callback)
  
      # Subscribing to the event should have created the needed channel.
      assert "time_change" in ui_events._get_event_channel(fig)
  
      # Publishing the time change event should call the callback function.
      ui_events.publish(fig, ui_events.TimeChange(time=10.2))
      assert callback_calls
  
      # Publishing a different event should not call the callback function.
      callback_calls.clear()  # Reset
      ui_events.publish(fig, ui_events.FigureClosing())
      assert not callback_calls
  
      # Test disposing of the event channel, even with subscribers.
      # During tests, matplotlib does not open an actual window so we need to force the
      # close event.
      fig.canvas.callbacks.process("close_event", None)
      assert len(event_channels) == 0
  
file_path: mne-python/mne/viz/tests/test_ui_events.py
imports: import matplotlib.pyplot
  import pytest
  from mne.datasets import testing
  from mne.viz import ui_events
label_desc: Use image node_id 6 for calling a global function with example usage: test_subscribe(event_channels) without return types