Ejemplo n.º 1
0
        public async Task FindNext_finds_valid_positions_in_document()
        {
            var phrase   = "Lorem";
            var document = new TextDocument(
                @"Lorem ipsum abs xddd work or noe iavb Lorem back down weug
. This is Lorem or not Lor working and not.");

            var indexes      = new int[] { 0, 38, 70, 0 };
            var currentIndex = 0;

            var engine = new SearchEngine();
            var sut    = new FindViewModel(engine);

            sut.ChangeDocument(document);
            sut.SearchedText            = phrase;
            sut.OnCaretPositionChanged += (s, e) =>
            {
                Assert.Less(currentIndex, indexes.Length);
                Assert.AreEqual(indexes[currentIndex], e.NewPosition);
                currentIndex++;
            };

            for (int i = 0; i < indexes.Length; i++)
            {
                await sut.FindNext();
            }

            Assert.AreEqual(indexes.Length - 1, currentIndex);
        }
Ejemplo n.º 2
0
        public async Task Changing_parameters_triggers_search_cancelation()
        {
            var  searchCancelled = false;
            Task verifier        = null;

            var engine = new Mock <ISearchEngine>();

            engine.Setup(x =>
                         x.FindInDocument(
                             It.IsAny <TextReader>(),
                             It.IsAny <string>(),
                             It.IsAny <SearchModeEnum>(),
                             It.IsAny <CancellationToken>(),
                             It.IsAny <Action <int> >()))
            .Callback <TextReader, string, SearchModeEnum, CancellationToken, Action <int> >(async(a, b, v, ct, e) =>
            {
                verifier = new Task(async() =>
                {
                    while (true)
                    {
                        await Task.Delay(100);
                        if (ct.IsCancellationRequested)
                        {
                            searchCancelled = true;
                            ct.ThrowIfCancellationRequested();
                        }
                    }
                }, ct);

                await Task.Run(() => verifier);
            });

            var sut = new FindViewModel(engine.Object);

            sut.ChangeDocument(new TextDocument());

            await sut.FindNext();

            sut.IgnoreCase = !sut.IgnoreCase;
            await Task.Delay(150);

            Assert.NotNull(verifier);
            Assert.IsTrue(verifier.IsCanceled);
            Assert.IsTrue(searchCancelled);
        }