Beispiel #1
0
        public void LoadItems_IfCancelled_Throws()
        {
            var list = new InfiniteScrollList();

            Assert.Throws <OperationCanceledException>(
                () => list.LoadItems(
                    Mock.Of <IPackageItemLoader>(),
                    loadingMessage: "a",
                    logger: null,
                    searchResultTask: Task.FromResult <SearchResult <IPackageSearchMetadata> >(null),
                    token: new CancellationToken(canceled: true)));
        }
Beispiel #2
0
        public void LoadItems_LoadingMessageIsNullOrEmpty_Throws(string loadingMessage)
        {
            var list = new InfiniteScrollList();

            var exception = Assert.Throws <ArgumentException>(
                () => list.LoadItems(
                    Mock.Of <IPackageItemLoader>(),
                    loadingMessage,
                    logger: null,
                    searchResultTask: Task.FromResult <SearchResult <IPackageSearchMetadata> >(null),
                    token: CancellationToken.None));

            Assert.Equal("loadingMessage", exception.ParamName);
        }
Beispiel #3
0
        public void LoadItems_LoaderIsNull_Throws()
        {
            var list = new InfiniteScrollList();

            var exception = Assert.Throws <ArgumentNullException>(
                () => list.LoadItems(
                    loader: null,
                    loadingMessage: "a",
                    logger: null,
                    searchResultTask: Task.FromResult <SearchResult <IPackageSearchMetadata> >(null),
                    token: CancellationToken.None));

            Assert.Equal("loader", exception.ParamName);
        }
Beispiel #4
0
        public void LoadItems_SearchResultTaskIsNull_Throws()
        {
            var list = new InfiniteScrollList();

            var exception = Assert.Throws <ArgumentNullException>(
                () => list.LoadItems(
                    Mock.Of <IPackageItemLoader>(),
                    loadingMessage: "a",
                    logger: null,
                    searchResultTask: null,
                    token: CancellationToken.None));

            Assert.Equal("searchResultTask", exception.ParamName);
        }
Beispiel #5
0
        public async Task LoadItems_BeforeGettingCurrent_WaitsForInitialResults()
        {
            var loader    = new Mock <IPackageItemLoader>(MockBehavior.Strict);
            var state     = new Mock <IItemLoaderState>();
            var hasWaited = false;

            loader.SetupGet(x => x.IsMultiSource)
            .Returns(true);
            loader.SetupGet(x => x.State)
            .Returns(state.Object);
            loader.Setup(x => x.UpdateStateAndReportAsync(
                             It.IsNotNull <SearchResult <IPackageSearchMetadata> >(),
                             It.IsNotNull <IProgress <IItemLoaderState> >(),
                             It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(0));

            var loadingStatus          = LoadingStatus.Loading;
            var loadingStatusCallCount = 0;

            state.Setup(x => x.LoadingStatus)
            .Returns(() => loadingStatus)
            .Callback(() =>
            {
                ++loadingStatusCallCount;

                if (loadingStatusCallCount >= 2)
                {
                    loadingStatus = LoadingStatus.NoItemsFound;
                    hasWaited     = true;
                }
            });

            var itemsCount          = 0;
            var itemsCountCallCount = 0;

            state.Setup(x => x.ItemsCount)
            .Returns(() => itemsCount)
            .Callback(() =>
            {
                ++itemsCountCallCount;

                if (itemsCountCallCount >= 2)
                {
                    itemsCount = 1;
                    hasWaited  = true;
                }
            });

            loader.Setup(x => x.UpdateStateAsync(
                             It.IsNotNull <IProgress <IItemLoaderState> >(),
                             It.IsAny <CancellationToken>()))
            .Returns(() => Task.FromResult(0));

            var logger           = new Mock <INuGetUILogger>();
            var searchResultTask = Task.FromResult(new SearchResult <IPackageSearchMetadata>());

            using (var joinableTaskContext = new JoinableTaskContext(Thread.CurrentThread, SynchronizationContext.Current))
            {
                var list = new InfiniteScrollList(new Lazy <JoinableTaskFactory>(() => joinableTaskContext.Factory));
                var taskCompletionSource = new TaskCompletionSource <string>();

                // Despite LoadItems(...) being a synchronous method, the method internally fires an asynchronous task.
                // We'll know when that task completes successfully when the LoadItemsCompleted event fires,
                // and to avoid infinite waits in exceptional cases, we'll interpret a call to reset as a failure.
                list.LoadItemsCompleted += (sender, args) => taskCompletionSource.TrySetResult(null);

                loader.Setup(x => x.Reset());
                logger.Setup(x => x.Log(
                                 It.Is <MessageLevel>(m => m == MessageLevel.Error),
                                 It.IsNotNull <string>(),
                                 It.IsAny <object[]>()))
                .Callback <MessageLevel, string, object[]>(
                    (messageLevel, message, args) =>
                {
                    taskCompletionSource.TrySetResult(message);
                });
                loader.Setup(x => x.GetCurrent())
                .Returns(() =>
                {
                    if (!hasWaited)
                    {
                        taskCompletionSource.TrySetResult("GetCurrent() was called before waiting for initial results.");
                    }

                    return(Enumerable.Empty <PackageItemListViewModel>());
                });

                list.LoadItems(
                    loader.Object,
                    loadingMessage: "a",
                    logger: logger.Object,
                    searchResultTask: searchResultTask,
                    token: CancellationToken.None);

                var errorMessage = await taskCompletionSource.Task;

                Assert.Null(errorMessage);

                loader.Verify();
            }
        }