<commit_before>﻿using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Jira.WallboardScreensaver.Screensaver;
using Jira.WallboardScreensaver.Services;
using NSubstitute;
using NUnit.Framework;

namespace Jira.WallboardScreensaver.Tests {
    [TestFixture]
    public class ScreensaverPresenterTests {
        [SetUp]
        public void SetUp() {
            _taskService = Substitute.For<ITaskService>();
            _browserService = Substitute.For<IBrowserService>();
            _view = Substitute.For<IScreensaverView>();
            _filter = Substitute.For<IUserActivityService>();
            _preferences = Substitute.For<Preferences>();

            _taskService.Delay(Arg.Any<TimeSpan>()).Returns(Task.CompletedTask);

            _presenter = new ScreensaverPresenter(_preferences, _browserService, _filter, _taskService);
        }

        private IScreensaverView _view;
        private IUserActivityService _filter;
        private ITaskService _taskService;
        private IBrowserService _browserService;
        private Preferences _preferences;
        private ScreensaverPresenter _presenter;

        private Task TaskThatNeverCompletes() {
            return new TaskCompletionSource<object>().Task;
        }

        [Test]
        public void ClosesWhenExitButtonClicked() {
            _presenter.Initialize(_view);
            _view.Load += Raise.Event();

            //

            _view.ExitButtonClicked += Raise.Event();

            //

            _view.Received(1).Close();
        }

        [Test]
        public void DisplaysConfiguredUrlOnLoad() {
            var uri = new Uri("http://google.com/");
            _preferences.DashboardUri.Returns(uri);

            //

            _presenter.Initialize(_view);
            _view.Load += Raise.Event();

            //

            _view.Received(1).Navigate(uri);
        }

        [Test]
        public void HidesControlsWhenUserIdleDetected() {
            _presenter.Initialize(_view);
            _view.Load += Raise.Event();

            //

            _filter.UserActive += Raise.Event();
            _filter.UserIdle += Raise.Event();

            //

            _view.Received(1).ControlsVisible = false;
        }

        [Test]
        public void IgnoresUserActivityForOneSecondAfterInitialization() {
            _taskService.Delay(Arg.Any<TimeSpan>()).Returns(TaskThatNeverCompletes());
            _presenter.Initialize(_view);
            _view.Load += Raise.Event();

            //

            _filter.UserActive += Raise.Event();

            //

            _view.Received(0).ControlsVisible = true;
            _taskService.Received(1).Delay(TimeSpan.FromSeconds(1));
        }

        [Test]
        public void IgnoresUserActivityForOneSecondAfterUserIdle() {
            _presenter.Initialize(_view);
            _view.Load += Raise.Event();

            _taskService.Delay(Arg.Any<TimeSpan>()).Returns(TaskThatNeverCompletes());

            //

            _filter.UserIdle += Raise.Event();
            Thread.Sleep(500);
            _filter.UserActive += Raise.Event();

            //

            _view.Received(0).ControlsVisible = true;
            _taskService.Received(2).Delay(TimeSpan.FromSeconds(1));
        }

        [Test]
        public void SetsCookiesAndConfiguresEmulationOnInitialize() {
            var uri = new Uri("http://www.google.com/some_uri");
            var baseUri = new Uri("http://www.google.com/");
            var cookies = new Dictionary<string, string> {
                {"cookie1", "value1"},
                {"cookie2", "value2"}
            };

            _preferences.DashboardUri.Returns(uri);
            _preferences.LoginCookies.Returns(cookies);

            //

            _presenter.Initialize(_view);

            //

            _browserService.Received(1).SetCookie(baseUri, "cookie1", "value1");
            _browserService.Received(1).SetCookie(baseUri, "cookie2", "value2");
            _browserService.Received(1).ConfigureEmulation();
        }

        [Test]
        public void ShowsControlsWhenUserActivityDetected() {
            _presenter.Initialize(_view);
            _view.Load += Raise.Event();

            //

            _filter.UserActive += Raise.Event();

            //

            _view.Received(1).ControlsVisible = true;
        }
    }
}<commit_msg>Race condition in test?<commit_after>﻿using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Jira.WallboardScreensaver.Screensaver;
using Jira.WallboardScreensaver.Services;
using NSubstitute;
using NUnit.Framework;

namespace Jira.WallboardScreensaver.Tests {
    [TestFixture]
    public class ScreensaverPresenterTests {
        [SetUp]
        public void SetUp() {
            _taskService = Substitute.For<ITaskService>();
            _browserService = Substitute.For<IBrowserService>();
            _view = Substitute.For<IScreensaverView>();
            _filter = Substitute.For<IUserActivityService>();
            _preferences = Substitute.For<Preferences>();

            _taskService.Delay(Arg.Any<TimeSpan>()).Returns(Task.CompletedTask);

            _presenter = new ScreensaverPresenter(_preferences, _browserService, _filter, _taskService);
        }

        private IScreensaverView _view;
        private IUserActivityService _filter;
        private ITaskService _taskService;
        private IBrowserService _browserService;
        private Preferences _preferences;
        private ScreensaverPresenter _presenter;

        private Task TaskThatNeverCompletes() {
            return new TaskCompletionSource<object>().Task;
        }

        [Test]
        public void ClosesWhenExitButtonClicked() {
            _presenter.Initialize(_view);
            _view.Load += Raise.Event();

            //

            _view.ExitButtonClicked += Raise.Event();

            //

            _view.Received(1).Close();
        }

        [Test]
        public void DisplaysConfiguredUrlOnLoad() {
            var uri = new Uri("http://google.com/");
            _preferences.DashboardUri.Returns(uri);

            //

            _presenter.Initialize(_view);
            _view.Load += Raise.Event();

            //

            _view.Received(1).Navigate(uri);
        }

        [Test]
        public void HidesControlsWhenUserIdleDetected() {
            _presenter.Initialize(_view);
            _view.Load += Raise.Event();

            //

            _filter.UserActive += Raise.Event();
            _filter.UserIdle += Raise.Event();

            //

            _view.Received(1).ControlsVisible = false;
        }

        [Test]
        public void IgnoresUserActivityForOneSecondAfterInitialization() {
            _taskService.Delay(Arg.Any<TimeSpan>()).Returns(TaskThatNeverCompletes());
            _presenter.Initialize(_view);
            _view.Load += Raise.Event();

            //

            _filter.UserActive += Raise.Event();

            //

            _view.Received(0).ControlsVisible = true;
            _taskService.Received(1).Delay(TimeSpan.FromSeconds(1));
        }

        [Test]
        public void IgnoresUserActivityForOneSecondAfterUserIdle() {
            _presenter.Initialize(_view);
            _view.Load += Raise.Event();

            _taskService.Delay(Arg.Any<TimeSpan>()).Returns(TaskThatNeverCompletes());

            //

            _filter.UserIdle += Raise.Event();
            Thread.Sleep(1000);
            _filter.UserActive += Raise.Event();

            //

            _view.Received(0).ControlsVisible = true;
            _taskService.Received(2).Delay(TimeSpan.FromSeconds(1));
        }

        [Test]
        public void SetsCookiesAndConfiguresEmulationOnInitialize() {
            var uri = new Uri("http://www.google.com/some_uri");
            var baseUri = new Uri("http://www.google.com/");
            var cookies = new Dictionary<string, string> {
                {"cookie1", "value1"},
                {"cookie2", "value2"}
            };

            _preferences.DashboardUri.Returns(uri);
            _preferences.LoginCookies.Returns(cookies);

            //

            _presenter.Initialize(_view);

            //

            _browserService.Received(1).SetCookie(baseUri, "cookie1", "value1");
            _browserService.Received(1).SetCookie(baseUri, "cookie2", "value2");
            _browserService.Received(1).ConfigureEmulation();
        }

        [Test]
        public void ShowsControlsWhenUserActivityDetected() {
            _presenter.Initialize(_view);
            _view.Load += Raise.Event();

            //

            _filter.UserActive += Raise.Event();

            //

            _view.Received(1).ControlsVisible = true;
        }
    }
}
{'fraction_non_alphanumeric': 0.11133763733152112, 'fraction_numerical': 0.005549892400045305, 'mean_word_length': 1.975067385444744, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2890101', 'n_tokens_mistral': 2447, 'n_tokens_neox': 2340, 'n_words': 351}