/// <summary>
 /// Needs to be called everytime the control is made visible
 /// </summary>
 private void Setup(IServiceProvider serviceProvider, ExpressionViewerControl view)
 {
     var sourceMonitor = new SourceMonitor(serviceProvider);
     var viewController = new ExpressionViewController(view);
     var viewGenerator = new ViewGenerator();
     Runner = new Runner(sourceMonitor, viewController, viewGenerator);
 }
        public void SourceMonitor_UsingBadDTEToGetSolution_DoesntFail()
        {
            var dte = new Mock<DTE>();
            dte.SetupGet(mock => mock.Solution).Returns(() => { throw new InvalidOperationException(); } );

            var serviceProvider = new Mock<IServiceProvider>();
            serviceProvider.Setup(mock => mock.GetService(It.IsAny<Type>())).Returns(dte.Object);

            using (var sourceMonitor = new SourceMonitor(serviceProvider.Object))
            {
                System.Threading.Thread.Sleep(1500);
                Assert.IsNotNull(sourceMonitor.LastThreadException);
            }
        }
        public void SourceMonitor_WhilePreviouseNotificationInProgress_ItWaits()
        {
            using (var sourceMonitor = new SourceMonitor(null))
            {
                var longHandlerEvent = new AutoResetEvent(false);
                var sourceChangedEvent = new AutoResetEvent(false);

                var changes = 0;
                sourceMonitor.SourceChanged += (s, e) =>
                {
                    // this is happening in a different thread
                    changes += 1;
                    sourceChangedEvent.Set();
                    longHandlerEvent.WaitOne();
                };

                sourceChangedEvent.WaitOne();
                var longTimeRequiredToProcessTheEvent = 3000;
                System.Threading.Thread.Sleep(longTimeRequiredToProcessTheEvent);
                longHandlerEvent.Set();

                Assert.AreEqual(1, changes);
            }
        }
 public void SourceMonitor_TimerThrowsMultipleExceptions_SourceMonitorKeepsTheLastOne()
 {
     var exception1 = new Exception();
     var exception2 = new Exception();
     var sourceChanges = 0;
     using (var sourceMonitor = new SourceMonitor(null))
     {
         sourceMonitor.SourceChanged += (s, e) =>
         {
             sourceChanges += 1;
             throw (sourceChanges == 1) ? exception1 : exception2;
         };
         System.Threading.Thread.Sleep(3000);
         Assert.AreEqual(exception2, sourceMonitor.LastThreadException);
     }
 }
 public void SourceMonitor_TimerThrowsException_SourceMonitorKeepsIt()
 {
     var exception = new Exception();
     using (var sourceMonitor = new SourceMonitor(null))
     {
         sourceMonitor.SourceChanged += (s, e) =>
         {
             throw exception;
         };
         System.Threading.Thread.Sleep(1500);
         Assert.AreEqual(exception, sourceMonitor.LastThreadException);
     }
 }
 public void SourceMonitor_Object_IsDisposable()
 {
     using (var sourceMonitor = new SourceMonitor(null))
     {
         Assert.IsTrue(sourceMonitor is IDisposable);
     }
 }
        public void SourceMonitor_FromTimeToTime_TriggerSourceChanged()
        {
            using (var sourceMonitor = new SourceMonitor(null))
            {
                var changes = 0;
                sourceMonitor.SourceChanged += (s, e) => changes += 1;

                var arbitraryExpectedChanges = 3;
                while (changes < arbitraryExpectedChanges)
                {
                    System.Threading.Thread.Sleep(10);
                }
            }
        }
        public void SourceMonitor_AfterDisposing_DoesntTriggerSourceChanged()
        {
            using (var sourceMonitor = new SourceMonitor(null))
            {
                sourceMonitor.Dispose();

                var changes = 0;
                sourceMonitor.SourceChanged += (s, e) => changes += 1;
                var arbitraryWaitTime = 1500;
                System.Threading.Thread.Sleep(arbitraryWaitTime);
                Assert.AreEqual(0, changes);
            }
        }