Ejemplo n.º 1
0
        public void when_subscribing_to_initialized_shell_then_receives_event_and_completes()
        {
            object zombie     = false;
            var    observable = new ShellInitializedObservable(Mock.Of <IVsShell>(shell => shell.GetProperty(ZombieProperty, out zombie) == VSConstants.S_OK));

            var completed         = false;
            ShellInitialized data = null;

            using (observable.Subscribe(e => data = e, () => completed = true)) { }

            Assert.True(completed);
            Assert.NotNull(data);
        }
Ejemplo n.º 2
0
 internal static void NotifyShellInitialized(SchubertOptions options, ShellContext context)
 {
     if (ShellInitialized != null)
     {
         ShellInitialized(options, context);
         //清除委托列表,防止资源无法被 GC 收集。
         if (ShellInitialized != null) //再次判断,因为可能在事件订阅的方法中使用 “-=” 操作手动移除事件订阅使得委托链再次为空。
         {
             foreach (var delegateItem in ShellInitialized.GetInvocationList())
             {
                 ShellInitialized -= (ShellInitializedHandler)delegateItem;
             }
         }
     }
 }
Ejemplo n.º 3
0
        public void when_subscribing_to_initialized_shell_then_receives_event_and_completes()
        {
            object zombie = false;

#pragma warning disable VSSDK005 // Avoid instantiating JoinableTaskContext
            var observable = new ShellInitializedObservable(new JoinableLazy <IVsShell>(() => Mock.Of <IVsShell>(shell => shell.GetProperty(ZombieProperty, out zombie) == VSConstants.S_OK), taskFactory: new JoinableTaskContext().Factory));
#pragma warning restore VSSDK005 // Avoid instantiating JoinableTaskContext

            var completed         = false;
            ShellInitialized data = null;

            using (observable.Subscribe(e => data = e, () => completed = true)) { }

            Assert.True(completed);
            Assert.NotNull(data);
        }
Ejemplo n.º 4
0
        public async Task when_subscribing_to_noninitialized_shell_then_can_wait_event_and_completion()
        {
            object zombie = true;
            uint   cookie = 1;
            IVsShellPropertyEvents callback = null;

            var shell = new Mock <IVsShell>();

            shell.Setup(x => x.GetProperty(ZombieProperty, out zombie)).Returns(VSConstants.S_OK);

            var capture = new CaptureMatch <IVsShellPropertyEvents>(s => callback = s);

            shell.Setup(x => x.AdviseShellPropertyChanges(Capture.With(capture), out cookie))
            .Returns(VSConstants.S_OK);

#pragma warning disable VSSDK005 // Avoid instantiating JoinableTaskContext
            var observable = new ShellInitializedObservable(new JoinableLazy <IVsShell>(() => shell.Object, taskFactory: new JoinableTaskContext().Factory));
#pragma warning restore VSSDK005 // Avoid instantiating JoinableTaskContext

            // Callback should have been provided at this point.
            Assert.NotNull(callback);

            var completed         = false;
            ShellInitialized data = null;

            using (observable.Subscribe(e => data = e, () => completed = true))
            {
                Assert.False(completed, "Observable shouldn't have completed yet.");
                Assert.Null(data);

                callback.OnShellPropertyChange(ZombieProperty, false);

                SpinWait.SpinUntil(() => completed, 5000);

                Assert.True(completed, "Observable should have completed already.");
                Assert.NotNull(data);

                shell.Verify(x => x.UnadviseShellPropertyChanges(cookie));

                // Subsequent subscription should get one and complete right away.
                ShellInitialized ev = default;
                observable.Subscribe(e => ev = e);

                Assert.Same(data, ev);
            }
        }