public void PagingPastEndSkippingLastPageKnownTotalItemCount()
        {
            Catalog context = new Catalog();

            EnqueueCallback(() =>
            {
                this._dds.PageSize = 5;
                this._dds.QueryName = "GetPurchaseOrdersQuery";
                this._dds.DomainContext = context;
                this._dds.Load();
            });

            this.AssertLoadingData();
            this.AssertPageChanged();

            int totalItemCount = 0;

            EnqueueCallback(() =>
            {
                this.ResetLoadState();
                this.ResetPageChanged();

                Assert.AreEqual(5, this._view.Count, "Count after the initial load");
                Assert.AreEqual(0, this._view.PageIndex, "PageIndex after the initial load");
                Assert.IsTrue(this._view.TotalItemCount > this._view.Count, "After the initial load, TotalItemCount is expected to be greater than the Count");
                Assert.IsTrue(this._view.PageCount > 0, "After the initial load, PageCount is expected to be greater than 0");

                totalItemCount = this._view.TotalItemCount;

                // Move well beyond the end
                this._view.MoveToPage(this._view.PageCount + 100);
            });

            this.AssertLoadingData(2);
            this.AssertPageChanged();

            EnqueueCallback(() =>
            {
                this.ResetLoadState();
                this.ResetPageChanged();

                int countOnLastPage = this._view.TotalItemCount % this._view.PageSize;
                if (countOnLastPage == 0)
                {
                    countOnLastPage = this._view.PageSize;
                }

                Assert.AreEqual(countOnLastPage, this._view.Count, "Count after changing page");
                Assert.AreEqual(PagingHelper.CalculatePageCount(this._view.TotalItemCount, this._view.PageSize) - 1, this._view.PageIndex, "PageIndex after changing page");
                Assert.AreEqual(totalItemCount, this._view.TotalItemCount, "TotalItemCount after changing page");
            });

            EnqueueTestComplete();
        }
        public void CountPropertiesAvailableWithinLoadedDataWithPaging()
        {
            // Catalog supports TotalItemCount without loading all data (CityDomainContext does not)
            Catalog context = new Catalog();

            this._dds.LoadedData += (s, e) =>
            {
                Assert.AreEqual<int>(5, this._view.Count, "Count");
                Assert.AreNotEqual<int>(0, this._view.TotalItemCount, "TotalItemCount");
                Assert.AreNotEqual<int>(0, this._pagedCollectionView.ItemCount, "ItemCount (IPagedCollectionView)");
            };

            EnqueueCallback(() =>
            {
                this._dds.QueryName = "GetProducts";
                this._dds.DomainContext = context;
                this._dds.PageSize = 5;
                this._dds.Load();
            });

            this.AssertLoadingData();

            EnqueueTestComplete();
        }
        public void IncludeTotalCountOverrideWhenPaging()
        {
            // Catalog supports total count
            Catalog catalog = new Catalog();

            bool defaultIncludeTotalCount = false;
            int totalEntityCount = 0;

            // When the LoadingData event is raised, we will capture the default
            // value for IncludeTotalCount and then set it to true
            this._dds.LoadingData += (s, e) =>
            {
                defaultIncludeTotalCount = e.Query.IncludeTotalCount;
                e.Query.IncludeTotalCount = false;
            };

            // When the LoadedData event is raised, we will capture the
            // TotalEntityCount to ensure it was set
            this._dds.LoadedData += (s, e) =>
            {
                totalEntityCount = e.TotalEntityCount;
            };

            EnqueueCallback(() =>
            {
                this._dds.AutoLoad = false;
                this._dds.DomainContext = catalog;
                this._dds.QueryName = "GetProducts";
                this._dds.PageSize = 5;
                this._dds.Load();
            });

            this.AssertLoadingData();

            EnqueueCallback(() =>
            {
                Assert.IsTrue(defaultIncludeTotalCount, "IncludeTotalCount should have been true by default");
                Assert.AreEqual(-1, totalEntityCount, "The TotalEntityCount should not have been included");
            });

            EnqueueTestComplete();
        }
        public void CountPropertiesRefreshedWithLoadAfterEmptyLoad()
        {
            // Catalog supports TotalItemCount without loading all data (CityDomainContext does not)
            Catalog context = new Catalog();
            int totalItemCountExpected = -1;
            Dictionary<string, int> propertiesChanged = new Dictionary<string, int>();

            EnqueueCallback(() =>
            {
                this._dds.QueryName = "GetProducts";
                this._dds.DomainContext = context;
                this._dds.PageSize = 2;

                // Apply a filter that will guarantee an empty load
                this._dds.FilterDescriptors.Add(new FilterDescriptor("Name", FilterOperator.IsEqualTo, Guid.NewGuid().ToString()));
                this._dds.Load();
            });

            this.AssertLoadingData();
            this.AssertPageChanged();

            EnqueueCallback(() =>
            {
                this.ResetLoadState();
                this.ResetPageChanged();

                Assert.AreEqual<int>(1, this._view.PageCount, "PageCount after the empty load");
                Assert.AreEqual<int>(0, this._view.TotalItemCount, "TotalItemCount after the empty load");

                // Overwrite the filter with one that will guarantee all data is loaded
                this._dds.FilterDescriptors[0] = new FilterDescriptor("Name", FilterOperator.IsNotEqualTo, Guid.NewGuid().ToString());

                // When this data is loaded, set the expectation for TotalItemCount
                this._dds.LoadedData += (s, e) => totalItemCountExpected = e.TotalEntityCount;

                ((INotifyPropertyChanged)this._view).PropertyChanged += (s, e) =>
                {
                    if (!propertiesChanged.ContainsKey(e.PropertyName))
                    {
                        propertiesChanged.Add(e.PropertyName, 1);
                    }
                    else
                    {
                        propertiesChanged[e.PropertyName] = propertiesChanged[e.PropertyName] + 1;
                    }
                };

                this._viewPageChangedExpected = 0;
                this._asyncEventFailureMessage = "Full load";
                this._dds.Load();
            });

            this.AssertLoadingData();

            EnqueueCallback(() =>
            {
                this.ResetLoadState();

                // This test isn't valid unless more than 1 page of data was loaded
                if (this._view.TotalItemCount == context.Products.Count)
                {
                    Assert.Inconclusive("TotalItemCount must be greater than the number of products loaded in order for this to be a valid test.");
                }

                Assert.AreEqual<int>(totalItemCountExpected, this._view.TotalItemCount, "TotalItemCount after full load");
                Assert.IsTrue(propertiesChanged.ContainsKey("TotalItemCount"), "TotalItemCount should have had a property changed notification");
                Assert.AreEqual<int>(1, propertiesChanged["TotalItemCount"], "TotalItemCount property changed notifications");

                Assert.AreEqual<int>(totalItemCountExpected, this._pagedCollectionView.ItemCount, "ItemCount after full load");
                Assert.IsTrue(propertiesChanged.ContainsKey("ItemCount"), "ItemCount should have had a property changed notification");
                Assert.AreEqual<int>(1, propertiesChanged["ItemCount"], "ItemCount property changed notifications");

                int pageCount = PagingHelper.CalculatePageCount(this._view.TotalItemCount, this._dds.PageSize);
                Assert.AreEqual<int>(pageCount, this._view.PageCount, "PageCount after full load");
                Assert.IsTrue(propertiesChanged.ContainsKey("PageCount"), "PageCount should have had a property changed notification");
                Assert.AreEqual<int>(1, propertiesChanged["PageCount"], "PageCount property changed notifications");
            });

            EnqueueTestComplete();
        }