public void ShouldLoadDownloadedAssemblies()
        {
            // NOTE: This test method uses a resource that is built in a pre-build event when building the project. The resource is a
            // dynamically generated XAP file that is built with the Mocks/Modules/createXap.bat batch file.
            // If this test is failing unexpectedly, it may be that the batch file is not working correctly in the current environment.

            var          mockFileDownloader = new MockFileDownloader();
            const string moduleTypeName     = "Microsoft.Practices.Composite.Tests.Mocks.Modules.RemoteModule, RemoteModuleA, Version=0.0.0.0";
            var          remoteModuleUri    = "http://MyModule.xap";
            var          moduleInfo         = new ModuleInfo()
            {
                Ref = remoteModuleUri
            };
            XapModuleTypeLoader typeLoader    = new TestableXapModuleTypeLoader(mockFileDownloader);
            ManualResetEvent    callbackEvent = new ManualResetEvent(false);

            typeLoader.BeginLoadModuleType(
                moduleInfo,
                delegate
            {
                callbackEvent.Set();
            });

            Assert.IsNull(Type.GetType(moduleTypeName));

            Stream xapStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Microsoft.Practices.Composite.Tests.Mocks.Modules.RemoteModules.xap");

            mockFileDownloader.InvokeOpenReadCompleted(new DownloadCompletedEventArgs(xapStream, null, false, mockFileDownloader.DownloadAsyncArgumentUserToken));
            Assert.IsTrue(callbackEvent.WaitOne(500));

            Assert.IsNotNull(Type.GetType(moduleTypeName));
        }
        public void ShouldLoadDownloadedAssemblies()
        {
            // NOTE: This test method uses a resource that is built in a pre-build event when building the project. The resource is a
            // dynamically generated XAP file that is built with the Mocks/Modules/createXap.bat batch file.
            // If this test is failing unexpectedly, it may be that the batch file is not working correctly in the current environment.

            var mockFileDownloader = new MockFileDownloader();
            const string moduleTypeName = "Microsoft.Practices.Composite.Tests.Mocks.Modules.RemoteModule, RemoteModuleA, Version=0.0.0.0";
            var remoteModuleUri = "http://MyModule.xap";
            var moduleInfo = new ModuleInfo() { Ref = remoteModuleUri };
            XapModuleTypeLoader typeLoader = new TestableXapModuleTypeLoader(mockFileDownloader);
            ManualResetEvent callbackEvent = new ManualResetEvent(false);
            typeLoader.BeginLoadModuleType(
                moduleInfo,
                delegate
                {
                    callbackEvent.Set();
                });

            Assert.IsNull(Type.GetType(moduleTypeName));

            Stream xapStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Microsoft.Practices.Composite.Tests.Mocks.Modules.RemoteModules.xap");
            mockFileDownloader.InvokeOpenReadCompleted(new DownloadCompletedEventArgs(xapStream, null, false, mockFileDownloader.DownloadAsyncArgumentUserToken));
            Assert.IsTrue(callbackEvent.WaitOne(500));

            Assert.IsNotNull(Type.GetType(moduleTypeName));
        }
        public void ShouldReturnErrorIfDownloadFails()
        {
            var mockFileDownloader = new MockFileDownloader();
            var remoteModuleUri    = "http://MyModule.xap";
            var moduleInfo         = new ModuleInfo()
            {
                Ref = remoteModuleUri
            };
            XapModuleTypeLoader typeLoader    = new TestableXapModuleTypeLoader(mockFileDownloader);
            ManualResetEvent    callbackEvent = new ManualResetEvent(false);
            Exception           error         = null;

            typeLoader.BeginLoadModuleType(
                moduleInfo,
                delegate(ModuleInfo callbackModuleInfo, Exception moduleError)
            {
                error = moduleError;
                callbackEvent.Set();
            });
            mockFileDownloader.InvokeOpenReadCompleted(new DownloadCompletedEventArgs(null, new InvalidOperationException("Mock Ex"), false, mockFileDownloader.DownloadAsyncArgumentUserToken));
            Assert.IsTrue(callbackEvent.WaitOne(500));

            Assert.IsNotNull(error);
            Assert.IsInstanceOfType(error, typeof(InvalidOperationException));
        }
        public void ShouldCallDownloadAsync()
        {
            var mockFileDownloader = new MockFileDownloader();
            var remoteModuleUri = "http://MyModule.xap";
            var moduleInfo = new ModuleInfo() { Ref = remoteModuleUri };
            XapModuleTypeLoader typeLoader = new TestableXapModuleTypeLoader(mockFileDownloader);

            typeLoader.BeginLoadModuleType(moduleInfo, delegate { });

            Assert.IsTrue(mockFileDownloader.DownloadAsyncCalled);
            Assert.AreEqual(remoteModuleUri, mockFileDownloader.downloadAsyncArgumentUri.OriginalString);
        }
        public void ShouldCallDownloadAsync()
        {
            var mockFileDownloader = new MockFileDownloader();
            var remoteModuleUri    = "http://MyModule.xap";
            var moduleInfo         = new ModuleInfo()
            {
                Ref = remoteModuleUri
            };
            XapModuleTypeLoader typeLoader = new TestableXapModuleTypeLoader(mockFileDownloader);

            typeLoader.BeginLoadModuleType(moduleInfo, delegate { });

            Assert.IsTrue(mockFileDownloader.DownloadAsyncCalled);
            Assert.AreEqual(remoteModuleUri, mockFileDownloader.downloadAsyncArgumentUri.OriginalString);
        }
        public void ShouldDownloadOnlyOnceIfRetrievingTwoGroupsFromSameUri()
        {
            var mockFileDownloader = new MockFileDownloader();
            var remoteModuleUri    = "http://MyPackage.xap";
            var moduleInfo1        = new ModuleInfo()
            {
                Ref = remoteModuleUri
            };
            var moduleInfo2 = new ModuleInfo()
            {
                Ref = remoteModuleUri
            };
            XapModuleTypeLoader typeLoader = new TestableXapModuleTypeLoader(mockFileDownloader);

            typeLoader.BeginLoadModuleType(moduleInfo1, delegate { });
            mockFileDownloader.DownloadAsyncCalled = false;
            typeLoader.BeginLoadModuleType(moduleInfo2, delegate { });

            Assert.IsFalse(mockFileDownloader.DownloadAsyncCalled);
        }
        public void ShouldReturnErrorIfDownloadFails()
        {
            var mockFileDownloader = new MockFileDownloader();
            var remoteModuleUri = "http://MyModule.xap";
            var moduleInfo = new ModuleInfo() { Ref = remoteModuleUri };
            XapModuleTypeLoader typeLoader = new TestableXapModuleTypeLoader(mockFileDownloader);
            ManualResetEvent callbackEvent = new ManualResetEvent(false);
            Exception error = null;
            typeLoader.BeginLoadModuleType(
                moduleInfo,
                delegate(ModuleInfo callbackModuleInfo, Exception moduleError)
                {
                    error = moduleError;
                    callbackEvent.Set();
                });
            mockFileDownloader.InvokeOpenReadCompleted(new DownloadCompletedEventArgs(null, new InvalidOperationException("Mock Ex"), false, mockFileDownloader.DownloadAsyncArgumentUserToken));
            Assert.IsTrue(callbackEvent.WaitOne(500));

            Assert.IsNotNull(error);
            Assert.IsInstanceOfType(error, typeof(InvalidOperationException));
        }
        public void ShouldFireAllCallbacksCorrespondingToSameUri()
        {
            var mockFileDownloader = new MockFileDownloader();
            var remoteModuleUri    = "http://MyPackage.xap";
            var moduleInfo1        = new ModuleInfo()
            {
                Ref = remoteModuleUri
            };
            var moduleInfo2 = new ModuleInfo()
            {
                Ref = remoteModuleUri
            };

            XapModuleTypeLoader typeLoader = new TestableXapModuleTypeLoader(mockFileDownloader);
            bool       callback1Called     = false;
            bool       callback2Called     = false;
            ModuleInfo callbackModuleInfo1 = null;

            typeLoader.BeginLoadModuleType(moduleInfo1, (m, e) =>
            {
                callbackModuleInfo1 = m;
                callback1Called     = true;
            });
            ModuleInfo callbackModuleInfo2 = null;

            typeLoader.BeginLoadModuleType(moduleInfo2, (m, e) =>
            {
                callbackModuleInfo2 = m;
                callback2Called     = true;
            });
            mockFileDownloader.InvokeOpenReadCompleted(new DownloadCompletedEventArgs(null, new Exception("Mock Ex"), false, mockFileDownloader.DownloadAsyncArgumentUserToken));

            Assert.IsTrue(callback1Called);
            Assert.IsTrue(callback2Called);
            Assert.AreSame(moduleInfo1, callbackModuleInfo1);
            Assert.AreSame(moduleInfo2, callbackModuleInfo2);
        }
        public void ShouldDownloadOnlyOnceIfRetrievingTwoGroupsFromSameUri()
        {
            var mockFileDownloader = new MockFileDownloader();
            var remoteModuleUri = "http://MyPackage.xap";
            var moduleInfo1 = new ModuleInfo() { Ref = remoteModuleUri };
            var moduleInfo2 = new ModuleInfo() { Ref = remoteModuleUri };
            XapModuleTypeLoader typeLoader = new TestableXapModuleTypeLoader(mockFileDownloader);

            typeLoader.BeginLoadModuleType(moduleInfo1, delegate { });
            mockFileDownloader.DownloadAsyncCalled = false;
            typeLoader.BeginLoadModuleType(moduleInfo2, delegate { });

            Assert.IsFalse(mockFileDownloader.DownloadAsyncCalled);
        }
        public void ShouldFireAllCallbacksCorrespondingToSameUri()
        {
            var mockFileDownloader = new MockFileDownloader();
            var remoteModuleUri = "http://MyPackage.xap";
            var moduleInfo1 = new ModuleInfo() { Ref = remoteModuleUri };
            var moduleInfo2 = new ModuleInfo() { Ref = remoteModuleUri };

            XapModuleTypeLoader typeLoader = new TestableXapModuleTypeLoader(mockFileDownloader);
            bool callback1Called = false;
            bool callback2Called = false;
            ModuleInfo callbackModuleInfo1 = null;
            typeLoader.BeginLoadModuleType(moduleInfo1, (m, e) =>
                                                           {
                                                               callbackModuleInfo1 = m;
                                                               callback1Called = true;
                                                           });
            ModuleInfo callbackModuleInfo2 = null;
            typeLoader.BeginLoadModuleType(moduleInfo2, (m, e) =>
                                                           {
                                                               callbackModuleInfo2 = m;
                                                               callback2Called = true;
                                                           });
            mockFileDownloader.InvokeOpenReadCompleted(new DownloadCompletedEventArgs(null, new Exception("Mock Ex"), false, mockFileDownloader.DownloadAsyncArgumentUserToken));

            Assert.IsTrue(callback1Called);
            Assert.IsTrue(callback2Called);
            Assert.AreSame(moduleInfo1, callbackModuleInfo1);
            Assert.AreSame(moduleInfo2, callbackModuleInfo2);
        }