public void OnNavigatedTo_Fill_Items_And_SelectedProduct()
        {
            var repository = new MockProductCatalogRepository();
            var navigationService = new MockNavigationService();

            repository.GetProductAsyncDelegate = (productNumber) =>
                {
                    Product product = null;

                    if (productNumber == "1")
                    {
                        product = new Product { ProductNumber = productNumber, SubcategoryId = 1 };
                    }

                    return Task.FromResult(product);
                };

            repository.GetProductsAsyncDelegate = (subCategoryId) =>
                {
                    ReadOnlyCollection<Product> products = null;

                    if (subCategoryId == 1)
                    {
                        products = new ReadOnlyCollection<Product>(new List<Product>
                        {
                            new Product(){ ProductNumber = "1", ImageUri = new Uri("http://image") },
                            new Product(){ ProductNumber = "2", ImageUri = new Uri("http://image") },
                            new Product(){ ProductNumber = "3", ImageUri = new Uri("http://image") }
                        });
                    }

                    return Task.FromResult(products);
                };

            var target = new ItemDetailPageViewModel(repository, navigationService, new MockShoppingCartRepository(), null, null, null, new MockSearchPaneService());
            target.OnNavigatedTo("1", NavigationMode.New, null);

            Assert.IsNotNull(target.Items);
            Assert.AreEqual(3, ((IReadOnlyCollection<ProductViewModel>)target.Items).Count);
            Assert.AreEqual(target.Items.First(), target.SelectedProduct);
        }
        public void OnNavigatedTo_When_Service_Not_Available_Then_Pops_Alert()
        {
            var repository = new MockProductCatalogRepository();
            var navigationService = new MockNavigationService();
            var alertService = new MockAlertMessageService();
            var resourceLoader = new MockResourceLoader();

            bool alertCalled = false;
            repository.GetProductAsyncDelegate = (productNumber) =>
                {
                    throw new HttpRequestException();
                };

            repository.GetProductsAsyncDelegate = (subCategoryId) =>
                {
                    throw new HttpRequestException();
                };

            alertService.ShowAsyncDelegate = (msg, title) =>
                {
                    alertCalled = true;
                    return Task.FromResult(string.Empty);
                };

            var target = new ItemDetailPageViewModel(repository, navigationService, new MockShoppingCartRepository(), alertService, resourceLoader, null, new MockSearchPaneService());
            target.OnNavigatedTo("1", NavigationMode.New, null);

            Assert.IsTrue(alertCalled);
        }
        public async Task UnpinFromStart_FiresOnly_IfProductIsSelected_And_SecondaryTileDoesNotExist()
        {
            bool fired = false;
            var tileService = new MockTileService();
            var target = new ItemDetailPageViewModel(null, new MockNavigationService(), null, null, null, tileService, null);

            // Case 1: Item not selected --> should not be fired
            tileService.SecondaryTileExistsDelegate = (a) => true;
            tileService.UnpinTileDelegate = (a) =>
            {
                fired = true;
                return Task.FromResult(true);
            };

            await target.UnpinProductCommand.Execute();
            Assert.IsFalse(fired);

            // Case 2: Item selected but SecondaryTile does not exist --> should not be fired
            tileService.SecondaryTileExistsDelegate = (a) => false;
            target.SelectedProduct = new ProductViewModel(new Product() { ImageUri = new Uri("http://dummy-image-uri.com") });

            await target.UnpinProductCommand.Execute();
            Assert.IsFalse(fired);

            // Case 3: Item selected and SecondaryTile exists --> should be fired
            tileService.SecondaryTileExistsDelegate = (a) => true;

            await target.UnpinProductCommand.Execute();
            Assert.IsTrue(fired);
        }
        public async Task UnpinFromStart_Changes_IsAppBarSticky()
        {
            var tileService = new MockTileService() { SecondaryTileExistsDelegate = (a) => false };
            var target = new ItemDetailPageViewModel(null, new MockNavigationService(), null, null, null, tileService, null);
            target.SelectedProduct = new ProductViewModel(new Product() { ImageUri = new Uri("http://dummy-image-uri.com") });

            // The AppBar should be sticky when the item is being unpinned
            tileService.UnpinTileDelegate = (a) =>
                {
                    Assert.IsTrue(target.IsAppBarSticky);
                    return Task.FromResult(true);
                };

            // Check if the AppBar is Sticky before unpinning
            Assert.IsFalse(target.IsAppBarSticky);

            await target.UnpinProductCommand.Execute();

            // Check if the AppBar is Sticky after unpinning
            Assert.IsFalse(target.IsAppBarSticky);
        }
        public async Task GoBack_When_CanGoBack_Is_True()
        {
            var repository = new MockProductCatalogRepository();
            var navigationService = new MockNavigationService
                {
                    CanGoBackDelegate = () => true,
                    GoBackDelegate = () => Assert.IsTrue(true, "I can go back")
                };

            var target = new ItemDetailPageViewModel(repository, navigationService, new MockShoppingCartRepository(), null, null, null, null);
            bool canExecute = target.GoBackCommand.CanExecute();

            if (canExecute)
            {
                await target.GoBackCommand.Execute();
            }
            else
            {
                Assert.Fail();
            }
        }