示例#1
0
        public void Find_all_finds_all_full_aggregates()
        {
            var user      = UserTestUtils.CreateTestUser();
            var workTime1 = WorkTimeTestUtils.CreateManual(user);

            workTime1.StartManually();
            _repository.Save(workTime1);

            InternalTimeService.GetCurrentDateTime = () => DateTime.UtcNow.AddHours(1);

            var workTime2 = WorkTimeTestUtils.CreateManual(user);

            workTime2.StartManually();
            _repository.Save(workTime2);

            InternalTimeService.GetCurrentDateTime = () => DateTime.UtcNow.AddHours(2);

            var workTime3 = WorkTimeTestUtils.CreateManual(user);

            workTime3.StartManually();
            _repository.Save(workTime3);


            var found = _repository.FindAll(user, null, null);

            found.Count.Should().Be(3);
        }
示例#2
0
        public async Task InitFace_when_faces_captured_returns_valid_reports()
        {
            var mProgress            = new Mock <IProgress <ProfileInitProgressArgs> >();
            var mHcFaceDetection     = SetupHcFaceDetection();
            var mTestImageRepository = SetupTestImageRepository();
            var mCaptureService      = SetupCaptureService();
            var mDnFaceRecognition   = SetupDnFaceRecognition();
            var mHeadPositionService = SetupHeadPositionService();

            mDnFaceRecognition
            .Setup(f => f.CompareFaces(It.IsAny <Mat>(), It.IsAny <FaceEncodingData?>(), It.IsAny <Mat>(), It.IsAny <FaceEncodingData?>()))
            .Returns(true);

            mDnFaceRecognition.Setup(f => f.GetFaceEncodings(It.IsAny <Mat>())).Returns(FaceEncodingData.ValidFaceEncodingData);

            mCaptureService.Setup(f => f.CaptureFrames(It.IsAny <CancellationToken>())).Returns(SuccessfulFaceCapturingScenario(mHcFaceDetection, mProgress, mHeadPositionService));
            mTestImageRepository.Setup(f => f.Count(It.IsAny <User>())).Returns(0);

            var initTestService = _mocker.CreateInstance <ProfileInitService>();

            initTestService.InitFaceProgress = mProgress.Object;
            await await initTestService.InitFace(UserTestUtils.CreateTestUser(1), mCaptureService.Object.CaptureFrames(CancellationToken.None).GetAsyncEnumerator(),
                                                 CancellationToken.None);


            mProgress.Verify(f => f.Report(It.Is(InitFaceProgress())), Times.AtLeast(3));
            mProgress.Verify(f => f.Report(It.Is(PhotosTaken())), Times.Exactly(1));

            mProgress.Verify(f => f.Report(It.Is(InitFaceProgress(null, 100))), Times.Once());
            mTestImageRepository.Verify(f => f.Clear(It.IsAny <User>()), Times.Once());
        }
示例#3
0
        public void FindLatestFromSnapshot_returns_latest_aggregate_from_snapshot()
        {
            var user     = UserTestUtils.CreateTestUser();
            var workTime = WorkTimeTestUtils.CreateManual(user);

            workTime.StartManually();
            var snap = workTime.TakeSnapshot();

            workTime.AddMouseAction(new MouseKeyboardEvent());
            _repository.Save(workTime);

            InternalTimeService.GetCurrentDateTime = () => DateTime.UtcNow.AddDays(1);

            var workTime2 = WorkTimeTestUtils.CreateManual(user);

            workTime2.StartManually();
            var snap2 = workTime2.TakeSnapshot();

            workTime2.AddMouseAction(new MouseKeyboardEvent());
            _repository.Save(workTime2);

            var found = _repository.FindLatestFromSnapshot(user);

            CompareWorkTimes(found, workTime2);
        }
        protected override TestImage CreateTestImage(bool isReferenceImg = true)
        {
            var utils = new ImageTestUtils();

            var(rect, face) = utils.GetFaceImg("front");
            var user = UserTestUtils.CreateTestUser(1);

            return(new TestImage(FaceEncodings, rect, face, HeadRotation.Front, DateTime.UtcNow, isReferenceImg, user.UserId));
        }
示例#5
0
        public void g()
        {
            var workTime =
                _buildService.CreateStartedManually(UserTestUtils.CreateTestUser(), DateTime.UtcNow.AddMinutes(60));

            _service.SetWorkTime(workTime);
            _service.KeyboardEventBufferSz = 1;
            _service.MouseEventBufferSz    = 1;
            workTime.StartManually();

            _service.StartTempChanges();

            _service.AddKeyboardEvent(new MonitorEvent()
            {
                EventStart  = DateTime.UtcNow,
                TotalTimeMs = 200,
            });

            _service.AddMouseEvent(new MonitorEvent()
            {
                EventStart  = DateTime.UtcNow,
                TotalTimeMs = 500
            });

            _service.AddKeyboardEvent(new MonitorEvent()
            {
                EventStart  = DateTime.UtcNow.AddSeconds(65),
                TotalTimeMs = 200,
            });

            _service.AddMouseEvent(new MonitorEvent()
            {
                EventStart  = DateTime.UtcNow.AddSeconds(65),
                TotalTimeMs = 500
            });

            var found = _repository.Find(workTime.User, DateTime.UtcNow);

            found.KeyboardActionEvents.Count.Should().Be(0);
            found.MouseActionEvents.Count.Should().Be(0);


            _service.CommitTempChanges();
            workTime.PendingEvents.Count.Should().Be(0);


            found = _repository.Find(workTime.User, DateTime.UtcNow);
            found.KeyboardActionEvents.Count.Should().Be(1);
            found.MouseActionEvents.Count.Should().Be(1);
        }
示例#6
0
        public async Task InitFace_when_gen_encoding_throws_returns_error_progress()
        {
            var mProgress            = new Mock <IProgress <ProfileInitProgressArgs> >();
            var mHcFaceDetection     = SetupHcFaceDetection();
            var mTestImageRepository = SetupTestImageRepository();
            var mCaptureService      = SetupCaptureService();
            var mDnFaceRecognition   = SetupDnFaceRecognition();
            var mHeadPositionService = SetupHeadPositionService();

            mCaptureService.Setup(f => f.CaptureFrames(It.IsAny <CancellationToken>())).Returns(SuccessfulFaceCapturingScenario(mHcFaceDetection, mProgress, mHeadPositionService));
            var initTestService = _mocker.CreateInstance <ProfileInitService>();

            initTestService.InitFaceProgress = mProgress.Object;
            await await initTestService.InitFace(UserTestUtils.CreateTestUser(1),
                                                 mCaptureService.Object.CaptureFrames(CancellationToken.None).GetAsyncEnumerator(),
                                                 CancellationToken.None);

            mProgress.Verify(f => f.Report(It.Is(InitFaceProgress(_nonEmptyFrame, 100))), Times.Never());
            mProgress.Verify(f => f.Report(It.Is(InitFaceProgress(null, 100))), Times.Never());
            mProgress.Verify(f => f.Report(It.Is(FaceRecognitionError())), Times.Once());

            mTestImageRepository.Verify(f => f.Clear(It.IsAny <User>()), Times.Exactly(2));
        }
示例#7
0
        public async Task InitFace_when_faces_not_captured_and_interrupted_returns_cancelled_progress()
        {
            var mProgress            = new Mock <IProgress <ProfileInitProgressArgs> >();
            var mHcFaceDetection     = SetupHcFaceDetection();
            var mTestImageRepository = SetupTestImageRepository();
            var mCaptureService      = SetupCaptureService();
            var mDnFaceRecognition   = SetupDnFaceRecognition();

            SetupHeadPositionService();

            async IAsyncEnumerable <Mat> InterruptedCapture()
            {
                mHcFaceDetection.Setup(f => f.DetectFrontalFaces(It.IsAny <Mat>())).Returns(new Rect[0]);
                yield return(_nonEmptyFrame);

                yield return(_nonEmptyFrame);
            }

            var cts = new CancellationTokenSource();

            mCaptureService.Setup(f => f.CaptureFrames(cts.Token)).Returns(InterruptedCapture);
            mTestImageRepository.Setup(f => f.Count(It.IsAny <User>())).Returns(0);

            var initTestService = _mocker.CreateInstance <ProfileInitService>();

            initTestService.InitFaceProgress = mProgress.Object;

            cts.Cancel();
            await initTestService.InitFace(
                UserTestUtils.CreateTestUser(1),
                mCaptureService.Object.CaptureFrames(cts.Token).GetAsyncEnumerator(cts.Token),
                CancellationToken.None);

            mProgress.Verify(f => f.Report(It.Is(CancelledByUser())), Times.Once());

            mTestImageRepository.Verify(f => f.Clear(It.IsAny <User>()), Times.Exactly(2));
        }
        public void TestImage_to_DbTestImage()
        {
            var utils = new ImageTestUtils();

            var(rect, face) = utils.GetFaceImg("front");

            var encodings = new DnFaceRecognition().GetFaceEncodings(face);

            var now       = DateTime.UtcNow;
            var testImg   = new TestImage(encodings, new Rect(0, 1, 20, 21), Mat.Zeros(4, 4, MatType.CV_8UC1), HeadRotation.Left, now, true, UserTestUtils.CreateTestUser(1).UserId);
            var dbTestImg = _mapper.Map <DbTestImage>(testImg);

            dbTestImg.Img.Should().NotBeEmpty();
            dbTestImg.FaceEncoding.Should().NotBeEmpty();
            dbTestImg.FaceLocation_x.Should().Be(0);
            dbTestImg.FaceLocation_y.Should().Be(1);
            dbTestImg.FaceLocation_width.Should().Be(20);
            dbTestImg.FaceLocation_height.Should().Be(21);
            dbTestImg.DateCreated.Should().Be(now);
            dbTestImg.IsReferenceImg.Should().BeTrue();
            dbTestImg.HorizontalHeadRotation.Should().Be((int)HeadRotation.Left);
            dbTestImg.Id.Should().BeNull();
            dbTestImg.UserId.Should().Be(1);
        }
示例#9
0
        public async Task InitFace_when_invalid_frames_returns_valid_progress_states()
        {
            var mProgress            = new Mock <IProgress <ProfileInitProgressArgs> >();
            var mHcFaceDetection     = SetupHcFaceDetection();
            var mTestImageRepository = SetupTestImageRepository();
            var mCaptureService      = SetupCaptureService();
            var mDnFaceRecognition   = SetupDnFaceRecognition();
            var mHeadPositionService = SetupHeadPositionService();

            async IAsyncEnumerable <Mat> InvalidCapture()
            {
                mHcFaceDetection.Setup(f => f.DetectFrontalFaces(It.IsAny <Mat>())).Returns(new Rect[0]);
                yield return(_nonEmptyFrame);

                mProgress.Verify(f => f.Report(It.Is(FaceNotDetected())), Times.Once());

                mHeadPositionService.Setup(f => f.GetHeadPosition(It.IsAny <Mat>(), It.IsAny <Rect>()))
                .Returns(() => (HeadRotation.Front, HeadRotation.Left));
                mHcFaceDetection.ReturnFace(_testUtils, "frontrotleft");
                yield return(_nonEmptyFrame);

                mProgress.Verify(f => f.Report(It.Is(FaceNotStraight())), Times.Once());


                mHeadPositionService.Setup(f => f.GetHeadPosition(It.IsAny <Mat>(), It.IsAny <Rect>()))
                .Returns(() => (HeadRotation.Front, HeadRotation.Right));
                yield return(_nonEmptyFrame);

                mProgress.Verify(f => f.Report(It.Is(FaceNotStraight())), Times.Exactly(2));

                mHeadPositionService.Setup(f => f.GetHeadPosition(It.IsAny <Mat>(), It.IsAny <Rect>()))
                .Returns(() => (HeadRotation.Front, HeadRotation.Front));
                mHcFaceDetection.ReturnFace(_testUtils, "front");
                yield return(_nonEmptyFrame);

                mProgress.Verify(f => f.Report(It.Is(InitFaceProgress())), Times.Exactly(1));


                mHcFaceDetection.Setup(f => f.DetectFrontalFaces(It.IsAny <Mat>())).Returns(new Rect[0]);
                mHcFaceDetection.Setup(f => f.DetectFrontalThenProfileFaces(It.IsAny <Mat>()))
                .Returns(new Rect[2]);
                yield return(_nonEmptyFrame);

                mProgress.Verify(f => f.Report(It.Is(ProfileFaceNotDetected())), Times.Once());


                mHcFaceDetection.ReturnFrontProfileFace(_testUtils, "left");
                mHeadPositionService.Setup(f => f.GetHeadPosition(It.IsAny <Mat>(), It.IsAny <Rect>()))
                .Returns(() => (HeadRotation.Front, HeadRotation.Right));
                yield return(_nonEmptyFrame);

                mProgress.Verify(f => f.Report(It.Is(FaceNotLeft())), Times.Exactly(1));


                mHeadPositionService.Setup(f => f.GetHeadPosition(It.IsAny <Mat>(), It.IsAny <Rect>()))
                .Returns(() => (HeadRotation.Right, HeadRotation.Right));
                yield return(_nonEmptyFrame);

                mProgress.Verify(f => f.Report(It.Is(FaceNotLeft())), Times.Exactly(2));

                mHeadPositionService.Setup(f => f.GetHeadPosition(It.IsAny <Mat>(), It.IsAny <Rect>()))
                .Returns(() => (HeadRotation.Left, HeadRotation.Left));
                yield return(_nonEmptyFrame);

                mProgress.Verify(f => f.Report(It.Is(InitFaceProgress())), Times.Exactly(2));


                mHcFaceDetection.ReturnFrontProfileFace(_testUtils, "right");
                mHeadPositionService.Setup(f => f.GetHeadPosition(It.IsAny <Mat>(), It.IsAny <Rect>()))
                .Returns(() => (HeadRotation.Right, HeadRotation.Left));
                yield return(_nonEmptyFrame);

                mProgress.Verify(f => f.Report(It.Is(FaceNotRight())), Times.Exactly(1));


                mHeadPositionService.Setup(f => f.GetHeadPosition(It.IsAny <Mat>(), It.IsAny <Rect>()))
                .Returns(() => (HeadRotation.Left, HeadRotation.Right));
                yield return(_nonEmptyFrame);

                mProgress.Verify(f => f.Report(It.Is(FaceNotRight())), Times.Exactly(2));
            }

            mDnFaceRecognition.Setup(f => f.GetFaceEncodings(It.IsAny <Mat>())).Returns(FaceEncodingData.ValidFaceEncodingData);
            mDnFaceRecognition
            .Setup(f => f.CompareFaces(It.IsAny <Mat>(), It.IsAny <FaceEncodingData?>(), It.IsAny <Mat>(), It.IsAny <FaceEncodingData?>()))
            .Returns(true);

            mCaptureService.Setup(f => f.CaptureFrames(It.IsAny <CancellationToken>())).Returns(InvalidCapture);
            var initTestService = _mocker.CreateInstance <ProfileInitService>();

            initTestService.InitFaceProgress = mProgress.Object;
            await await initTestService.InitFace(
                UserTestUtils.CreateTestUser(1), mCaptureService.Object.CaptureFrames(CancellationToken.None).GetAsyncEnumerator(),
                CancellationToken.None);

            mTestImageRepository.Verify(f => f.Clear(It.IsAny <User>()), Times.Exactly(2));
        }
示例#10
0
        public void FindLatestFromSnapshot_returns_null_if_does_not_exist()
        {
            var user = UserTestUtils.CreateTestUser();

            _repository.FindLatestFromSnapshot(user).Should().BeNull();
        }
 protected override User CreateUser()
 {
     return(UserTestUtils.CreateTestUser(1));
 }