public void ApplicationStoppingEventIsRaisedWhenPhoneAppIsDeactivated()
        {
            dynamic phoneApplicationService = CreateFakePhoneApplicationService();

            var applicationLifecycle = new PlatformApplicationLifecycle
            {
                Dispatcher = CreateFakeDispatcher(),
                GetPhoneApplicationService = () => phoneApplicationService
            };

            applicationLifecycle.Initialize();

            object applicationStoppingSender    = null;
            object applicationStoppingEventArgs = null;

            applicationLifecycle.Stopping += (sender, args) =>
            {
                applicationStoppingSender    = sender;
                applicationStoppingEventArgs = args;
            };

            var deactivatedEventArgs = new DeactivatedEventArgs();

            phoneApplicationService.Deactivated(phoneApplicationService, deactivatedEventArgs);

            Assert.NotNull(applicationStoppingSender);
            Assert.NotNull(applicationStoppingEventArgs);
        }
        public void ApplicationStoppingEventIsRaisedWhenPhoneAppIsDeactivated()
        {
            dynamic phoneApplicationService = CreateFakePhoneApplicationService();

            var applicationLifecycle = new PlatformApplicationLifecycle
                                            {
                                                Dispatcher = CreateFakeDispatcher(),
                                                GetPhoneApplicationService = () => phoneApplicationService
                                            };
            applicationLifecycle.Initialize();

            object applicationStoppingSender = null;
            object applicationStoppingEventArgs = null;
            applicationLifecycle.Stopping += (sender, args) =>
            {
                applicationStoppingSender = sender;
                applicationStoppingEventArgs = args;
            };

            var deactivatedEventArgs = new DeactivatedEventArgs();
            phoneApplicationService.Deactivated(phoneApplicationService, deactivatedEventArgs);

            Assert.NotNull(applicationStoppingSender);
            Assert.NotNull(applicationStoppingEventArgs);            
        }
        public void ApplicationStartedEventIsRaisedWhenExistingInstanceOfPhoneAppIsActivated()
        {
            dynamic phoneApplicationService = CreateFakePhoneApplicationService();

            var applicationLifecycle = new PlatformApplicationLifecycle
            {
                Dispatcher = CreateFakeDispatcher(),
                GetPhoneApplicationService = () => phoneApplicationService
            };

            applicationLifecycle.Initialize();

            object applicationStartedEventArgs = null;

            applicationLifecycle.Started += (sender, args) =>
            {
                applicationStartedEventArgs = args;
            };

            var activatedEventArgs = new ActivatedEventArgs();

            phoneApplicationService.Activated(phoneApplicationService, activatedEventArgs);

            Assert.Same(activatedEventArgs, applicationStartedEventArgs);
        }
        public void InitializeThrowsInvalidOperationExceptionWhenPhoneApplicationServiceIsNotAvailable()
        {
            var applicationLifecycle = new PlatformApplicationLifecycle
            {
                Dispatcher = CreateFakeDispatcher(),
                GetPhoneApplicationService = () => null
            };

            var exception = Assert.Throws <InvalidOperationException>(() => applicationLifecycle.Initialize());

            Assert.Contains(typeof(PhoneApplicationService).Name, exception.Message, StringComparison.OrdinalIgnoreCase);
        }
        public void OnApplicationStoppingDoesNotThrowWhithoutEventHandlers()
        {
            dynamic phoneApplicationService = CreateFakePhoneApplicationService();
            var     applicationLifecycle    = new PlatformApplicationLifecycle
            {
                Dispatcher = CreateFakeDispatcher(),
                GetPhoneApplicationService = () => phoneApplicationService
            };

            applicationLifecycle.Initialize();

            phoneApplicationService.Deactivated(phoneApplicationService, new DeactivatedEventArgs());
        }
        public void ApplicationStartedEventIsRaisedWhenExistingInstanceOfPhoneAppIsActivated()
        {
            dynamic phoneApplicationService = CreateFakePhoneApplicationService();

            var applicationLifecycle = new PlatformApplicationLifecycle
                                           {
                                               Dispatcher = CreateFakeDispatcher(),
                                               GetPhoneApplicationService = () => phoneApplicationService
                                           };
            applicationLifecycle.Initialize();

            object applicationStartedEventArgs = null;
            applicationLifecycle.Started += (sender, args) =>
            {
                applicationStartedEventArgs = args;
            };

            var activatedEventArgs = new ActivatedEventArgs();
            phoneApplicationService.Activated(phoneApplicationService, activatedEventArgs);

            Assert.Same(activatedEventArgs, applicationStartedEventArgs);            
        }
        public void ConstructorInvokesInitializeOnDispatcherThreadToLetApplicationConstructPhoneApplicationServiceFromXaml()
        {
            Action actionInvokedOnDispatcherThread = null;
            var    dispatcher = new StubPlatformDispatcher
            {
                OnRunAsync = action =>
                {
                    actionInvokedOnDispatcherThread = action;
                    return(Task.FromResult <object>(null));
                },
            };

            var applicationLifecycle = new PlatformApplicationLifecycle
            {
                Dispatcher = dispatcher,
                GetPhoneApplicationService = CreateFakePhoneApplicationService
            };

            applicationLifecycle.Initialize();

            Assert.NotNull(actionInvokedOnDispatcherThread);
        }
        public void ApplicationStoppingEventArgsRunsAsyncTasksWithCurrentThreadTaskSchedulerToPreventSuspendingOfAsyncOperations()
        {
            dynamic phoneApplicationService = CreateFakePhoneApplicationService();

            var applicationLifecycle = new PlatformApplicationLifecycle
            {
                Dispatcher = CreateFakeDispatcher(),
                GetPhoneApplicationService = () => phoneApplicationService
            };

            applicationLifecycle.Initialize();

            TaskScheduler actualTaskScheduler = null;

            applicationLifecycle.Stopping += (sender, args) => args.Run(() => Task.Factory.StartNew(() => actualTaskScheduler = TaskScheduler.Current));

            var deactivatedEventArgs = new DeactivatedEventArgs();

            phoneApplicationService.Deactivated(phoneApplicationService, deactivatedEventArgs);

            Assert.IsType(typeof(CurrentThreadTaskScheduler), actualTaskScheduler);
        }
        public void ApplicationStoppingEventArgsRunsAsyncTasksWithCurrentThreadTaskSchedulerToPreventSuspendingOfAsyncOperations()
        {
            dynamic phoneApplicationService = CreateFakePhoneApplicationService();

            var applicationLifecycle = new PlatformApplicationLifecycle
                                            {
                                                Dispatcher = CreateFakeDispatcher(),
                                                GetPhoneApplicationService = () => phoneApplicationService
                                            };

            applicationLifecycle.Initialize();

            TaskScheduler actualTaskScheduler = null;
            applicationLifecycle.Stopping += (sender, args) => args.Run(() => Task.Factory.StartNew(() => actualTaskScheduler = TaskScheduler.Current));

            var deactivatedEventArgs = new DeactivatedEventArgs();
            phoneApplicationService.Deactivated(phoneApplicationService, deactivatedEventArgs);

            Assert.IsType(typeof(CurrentThreadTaskScheduler), actualTaskScheduler);
        }
        public void OnApplicationStoppingDoesNotThrowWhithoutEventHandlers()
        {
            dynamic phoneApplicationService = CreateFakePhoneApplicationService();
            var applicationLifecycle = new PlatformApplicationLifecycle
                                            {
                                                Dispatcher = CreateFakeDispatcher(),
                                                GetPhoneApplicationService = () => phoneApplicationService
                                            };
            applicationLifecycle.Initialize();

            phoneApplicationService.Deactivated(phoneApplicationService, new DeactivatedEventArgs());
        }
        public void InitializeThrowsInvalidOperationExceptionWhenPhoneApplicationServiceIsNotAvailable()
        {
            var applicationLifecycle = new PlatformApplicationLifecycle
            {
                Dispatcher = CreateFakeDispatcher(),
                GetPhoneApplicationService = () => null
            };

            var exception = Assert.Throws<InvalidOperationException>(() => applicationLifecycle.Initialize());
            Assert.Contains(typeof(PhoneApplicationService).Name, exception.Message, StringComparison.OrdinalIgnoreCase);
        }
        public void ConstructorInvokesInitializeOnDispatcherThreadToLetApplicationConstructPhoneApplicationServiceFromXaml()
        {
            Action actionInvokedOnDispatcherThread = null;
            var dispatcher = new StubPlatformDispatcher
            {
                OnRunAsync = action =>
                {
                    actionInvokedOnDispatcherThread = action;
                    return Task.FromResult<object>(null);
                },
            };

            var applicationLifecycle = new PlatformApplicationLifecycle
            {
                Dispatcher = dispatcher,
                GetPhoneApplicationService = CreateFakePhoneApplicationService
            };
            applicationLifecycle.Initialize();
            
            Assert.NotNull(actionInvokedOnDispatcherThread);
        }