예제 #1
0
        public void AddAndRemoveEventHandlerThroughIntroduction()
        {
            TestObject target = new TestObject();
            DoubleClickableIntroduction dci     = new DoubleClickableIntroduction();
            DefaultIntroductionAdvisor  advisor = new DefaultIntroductionAdvisor(dci);
            CountingBeforeAdvice        countingBeforeAdvice = new CountingBeforeAdvice();

            target.Name = "SOME-NAME";
            ProxyFactory pf = new ProxyFactory(target);

            pf.AddIntroduction(advisor);
            pf.AddAdvisor(new DefaultPointcutAdvisor(countingBeforeAdvice));
            object      proxy = pf.GetProxy();
            ITestObject to    = proxy as ITestObject;

            Assert.IsNotNull(to);
            Assert.AreEqual("SOME-NAME", to.Name);
            IDoubleClickable doubleClickable = proxy as IDoubleClickable;

            // add event handler through introduction
            doubleClickable.DoubleClick += new EventHandler(OnClick);
            OnClickWasCalled             = false;
            doubleClickable.FireDoubleClickEvent();
            Assert.IsTrue(OnClickWasCalled);
            Assert.AreEqual(3, countingBeforeAdvice.GetCalls());
            // remove event handler through introduction
            doubleClickable.DoubleClick -= new EventHandler(OnClick);
            OnClickWasCalled             = false;
            doubleClickable.FireDoubleClickEvent();
            Assert.IsFalse(OnClickWasCalled);
            Assert.AreEqual(5, countingBeforeAdvice.GetCalls());
        }
 public void AddAndRemoveEventHandlerThroughIntroduction()
 {
     TestObject target = new TestObject();
     DoubleClickableIntroduction dci = new DoubleClickableIntroduction();
     DefaultIntroductionAdvisor advisor = new DefaultIntroductionAdvisor(dci);
     CountingBeforeAdvice countingBeforeAdvice = new CountingBeforeAdvice();
     target.Name = "SOME-NAME";
     ProxyFactory pf = new ProxyFactory(target);
     pf.AddIntroduction(advisor);
     pf.AddAdvisor(new DefaultPointcutAdvisor(countingBeforeAdvice));
     object proxy = pf.GetProxy();
     ITestObject to = proxy as ITestObject;
     Assert.IsNotNull(to);
     Assert.AreEqual("SOME-NAME", to.Name);
     IDoubleClickable doubleClickable = proxy as IDoubleClickable;
     // add event handler through introduction
     doubleClickable.DoubleClick += new EventHandler(OnClick);
     OnClickWasCalled = false;
     doubleClickable.FireDoubleClickEvent();
     Assert.IsTrue(OnClickWasCalled);
     Assert.AreEqual(3, countingBeforeAdvice.GetCalls());
     // remove event handler through introduction
     doubleClickable.DoubleClick -= new EventHandler(OnClick);
     OnClickWasCalled = false;
     doubleClickable.FireDoubleClickEvent();
     Assert.IsFalse(OnClickWasCalled);
     Assert.AreEqual(5, countingBeforeAdvice.GetCalls());
 }