public void CodePackageActivationContext_PackageChangeNotificationsWorkForDataPackages()
        {
            {
                var testData = new ChangeNotificationTestData <DataPackageInfo, DataPackage>
                {
                    EventHookupFunction = (context, handler) => context.DataPackageAddedEvent += new EventHandler <PackageAddedEventArgs <DataPackage> >(handler),
                    EventInvoker        = (stub, oldInfo, newInfo) => stub.DataPackageRegistrations[0].OnPackageAdded(null, newInfo),
                    Helper = DataPackageTest.HelperInstance
                };

                this.AddOfNewPackageChangeNotificationTestHelper(testData);
            }

            {
                var testData = new ChangeNotificationTestData <DataPackageInfo, DataPackage>
                {
                    EventHookupFunction = (context, handler) => context.DataPackageRemovedEvent += new EventHandler <PackageRemovedEventArgs <DataPackage> >(handler),
                    EventInvoker        = (stub, oldInfo, newInfo) => stub.DataPackageRegistrations[0].OnPackageRemoved(null, oldInfo),
                    Helper = DataPackageTest.HelperInstance
                };

                this.DeleteOfExistingPackageChangeNotificationTestHelper(testData);
            }

            {
                var testData = new ChangeNotificationTestData <DataPackageInfo, DataPackage>
                {
                    EventHookupFunction = (context, handler) => context.DataPackageModifiedEvent += new EventHandler <PackageModifiedEventArgs <DataPackage> >(handler),
                    EventInvoker        = (stub, oldInfo, newInfo) => stub.DataPackageRegistrations[0].OnPackageModified(null, oldInfo, newInfo),
                    Helper = DataPackageTest.HelperInstance
                };

                this.UpgradeOfExistingPackageChangeNotificationTestHelper(testData);
            }
        }
        private void ChangeNotificationTestHelper <TPackageInfo, TPackage>(
            ChangeNotificationTestData <TPackageInfo, TPackage> data,
            TPackageInfo oldPackage,
            TPackageInfo newPackage,
            Action <EventArgs> validator)
            where TPackageInfo : IPackageInfo
            where TPackage : class
        {
            var stub = new CodePackageActivationContextStub();

            // create the context
            var context = new CodePackageActivationContext(stub);

            EventArgs packageChangedArgs = null;

            // create a delegate for handling the event
            Action <object, EventArgs> eventHandler = (sender, args) =>
            {
                Assert.AreSame(context, sender);

                packageChangedArgs = args;
            };

            // hookup the delegate as the event handler
            data.EventHookupFunction(context, eventHandler);

            // invoke the event
            data.EventInvoker(stub, oldPackage, newPackage);

            // validate that the event was invoked
            Assert.IsNotNull(packageChangedArgs);

            validator(packageChangedArgs);
        }
        private void DeleteOfExistingPackageChangeNotificationTestHelper <TPackageInfo, TPackage>(ChangeNotificationTestData <TPackageInfo, TPackage> data)
            where TPackageInfo : IPackageInfo
            where TPackage : class
        {
            var initialPackage  = data.Helper.CreateDefaultInfo();
            var upgradedPackage = default(TPackageInfo);

            this.ChangeNotificationTestHelper(
                data,
                initialPackage,
                upgradedPackage,
                (packageChangedArgs) =>
            {
                var casted = packageChangedArgs as PackageRemovedEventArgs <TPackage>;
                Assert.IsNotNull(casted);

                data.Helper.Compare(data.Helper.Factory(initialPackage), casted.Package);
            });
        }
        private void UpgradeOfExistingPackageChangeNotificationTestHelper <TPackageInfo, TPackage>(ChangeNotificationTestData <TPackageInfo, TPackage> data)
            where TPackageInfo : IPackageInfo
            where TPackage : class
        {
            var initialPackage  = data.Helper.CreateDefaultInfo();
            var upgradedPackage = data.Helper.CreateDefaultInfo();

            upgradedPackage.Version = upgradedPackage.Version + "a";

            this.ChangeNotificationTestHelper(
                data,
                initialPackage,
                upgradedPackage,
                (packageChangedArgs) =>
            {
                var casted = packageChangedArgs as PackageModifiedEventArgs <TPackage>;
                Assert.IsNotNull(casted);

                data.Helper.Compare(data.Helper.Factory(initialPackage), casted.OldPackage);
                data.Helper.Compare(data.Helper.Factory(upgradedPackage), casted.NewPackage);
            });
        }