From 459488a2e777380fcb65e3b4dd355fe525ff77ca Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Tue, 24 Dec 2024 22:28:38 +0530 Subject: Add search mode for text based searching with ocr --- specs/Specs/AppStateUpdateSpec.hs | 56 ++++++++++++++------------------------- specs/Specs/ViewSpec.hs | 11 +------- specs/TestUtils.hs | 7 ++++- 3 files changed, 27 insertions(+), 47 deletions(-) (limited to 'specs') diff --git a/specs/Specs/AppStateUpdateSpec.hs b/specs/Specs/AppStateUpdateSpec.hs index e54f50f..861264d 100644 --- a/specs/Specs/AppStateUpdateSpec.hs +++ b/specs/Specs/AppStateUpdateSpec.hs @@ -2,7 +2,7 @@ module Specs.AppStateUpdateSpec where import Chelleport (initialState, update) import Chelleport.Types -import Chelleport.Utils (intToCInt, uniq) +import Chelleport.Utils (uniq) import Control.Monad (join) import qualified SDL import Test.Hspec @@ -12,32 +12,23 @@ test :: SpecWith () test = do describe "#initialState" $ do it "returns the initial state of the app" $ do - (initState, _) <- runWithMocks initialState + ((initState, _), _) <- runWithMocks initialState stateKeySequence initState `shouldBe` [] stateIsMatched initState `shouldBe` False stateIsShiftPressed initState `shouldBe` False it "returns grid with 16x9 key sequences" $ do - (initState, _) <- runWithMocks initialState + ((initState, _), _) <- runWithMocks initialState length (stateGrid initState) `shouldBe` 9 stateGrid initState `shouldSatisfy` all ((== 16) . length) stateGrid initState `shouldSatisfy` all (all ((== 2) . length)) it "returns grid with all unique key sequences" $ do - (initState, _) <- runWithMocks initialState + ((initState, _), _) <- runWithMocks initialState join (stateGrid initState) `shouldBe` uniq (join $ stateGrid initState) describe "#update" $ do - let defaultState = - State - { stateKeySequence = [], - stateIsShiftPressed = False, - stateIsMatched = False, - stateGrid = [["ABC", "DEF"], ["DJK", "JKL"]], - stateRepetition = 1, - stateIsDragging = False, - stateMode = ModeHints - } + let defaultState = defaultAppState {stateGrid = [["ABC", "DEF"], ["DJK", "JKL"]]} context "with action HandleKeyInput" $ do context "when there are no matches" $ do @@ -63,9 +54,9 @@ test = do ((nextState, _), _) <- runWithMocks $ update currentState $ HandleKeyInput SDL.KeycodeF nextState `shouldBe` currentState {stateKeySequence = "DEF", stateIsMatched = True} - it "continues with MoveMousePosition action" $ do + it "continues with MoveMousePosition action at center of matched cell" $ do ((_, action), _) <- runWithMocks $ update currentState $ HandleKeyInput SDL.KeycodeF - action `shouldBe` Just (MoveMousePosition (0, 1)) + action `shouldBe` Just (MoveMousePosition (1640, 370)) context "with action TriggerMouseClick" $ do let currentState = defaultState @@ -185,16 +176,10 @@ test = do context "with action MoveMousePosition" $ do let currentState = defaultState - let rows = intToCInt $ length $ stateGrid currentState - let columns = intToCInt $ length $ head $ stateGrid currentState - -- TODO: Test with inline mocked values - it "moves mouse pointer to center of cell of given coordinates" $ do - (_, mock) <- runWithMocks $ update currentState $ MoveMousePosition (0, 0) - mock - `shouldHaveCalled` Mock_moveMousePointer - (mockWindowOffsetX + mockWindowWidth `div` columns `div` 2) - (mockWindowOffsetY + mockWindowHeight `div` rows `div` 2) + it "moves mouse pointer to the given coordinates" $ do + (_, mock) <- runWithMocks $ update currentState $ MoveMousePosition (23, 320) + mock `shouldHaveCalled` Mock_moveMousePointer 23 320 it "does not continue or update state" $ do (result, _) <- runWithMocks $ update currentState $ MoveMousePosition (0, 0) @@ -211,28 +196,27 @@ test = do context "with action IncrementMouseCursor" $ do let currentState = defaultState - -- TODO: Test with inline mocked values - it "increments mouse position relative to current position" $ do - (_, mock) <- runWithMocks $ update currentState $ IncrementMouseCursor (10, -5) - mock `shouldHaveCalled` Mock_moveMousePointer 52 37 + it "continues with MoveMousePosition" $ do + ((_, action), _) <- runWithMocks $ update currentState $ IncrementMouseCursor (10, -5) + action `shouldBe` Just (MoveMousePosition (52, 37)) - it "does not continue or update state" $ do - (result, _) <- runWithMocks $ update currentState $ IncrementMouseCursor (0, 0) - result `shouldBe` (currentState, Nothing) + it "does update state" $ do + ((state, _), _) <- runWithMocks $ update currentState $ IncrementMouseCursor (10, -5) + state `shouldBe` currentState context "when repetition is more than 1" $ do let currentState = defaultState {stateRepetition = 5} it "multiplies increment" $ do - (_, mock) <- runWithMocks $ update currentState $ IncrementMouseCursor (10, -5) - mock `shouldHaveCalled` Mock_moveMousePointer 92 17 + ((_, action), _) <- runWithMocks $ update currentState $ IncrementMouseCursor (10, -5) + action `shouldBe` Just (MoveMousePosition (92, 17)) context "when repetition is 0" $ do let currentState = defaultState {stateRepetition = 0} it "increments just once" $ do - (_, mock) <- runWithMocks $ update currentState $ IncrementMouseCursor (10, -5) - mock `shouldHaveCalled` Mock_moveMousePointer 52 37 + ((_, action), _) <- runWithMocks $ update currentState $ IncrementMouseCursor (10, -5) + action `shouldBe` Just (MoveMousePosition (52, 37)) context "with action ShutdownApp" $ do let currentState = defaultState diff --git a/specs/Specs/ViewSpec.hs b/specs/Specs/ViewSpec.hs index b8418d6..39e7fea 100644 --- a/specs/Specs/ViewSpec.hs +++ b/specs/Specs/ViewSpec.hs @@ -8,16 +8,7 @@ import TestUtils test :: SpecWith () test = do - let defaultState = - State - { stateKeySequence = [], - stateIsShiftPressed = False, - stateIsMatched = False, - stateGrid = [["ABC", "DEF"], ["DJK", "JKL"]], - stateRepetition = 1, - stateIsDragging = False, - stateMode = ModeHints - } + let defaultState = defaultAppState {stateGrid = [["ABC", "DEF"], ["DJK", "JKL"]]} let drawTextCalls = filter (\case Mock_drawText {} -> True; _ -> False) . calls describe "#render" $ do diff --git a/specs/TestUtils.hs b/specs/TestUtils.hs index c98899d..76a185d 100644 --- a/specs/TestUtils.hs +++ b/specs/TestUtils.hs @@ -3,6 +3,7 @@ module TestUtils where import Chelleport.AppShell (MonadAppShell (..)) import Chelleport.Control (MonadControl (..)) import Chelleport.Draw (MonadDraw (..)) +import Chelleport.OCR (MonadOCR (..)) import Control.Monad (void) import Control.Monad.IO.Class (MonadIO) import Control.Monad.State (MonadState (state), StateT (runStateT)) @@ -11,7 +12,7 @@ import Foreign.C (CInt) import Mock import Test.Hspec -$(generateMock [''MonadDraw, ''MonadControl, ''MonadAppShell]) +$(generateMock [''MonadDraw, ''MonadControl, ''MonadAppShell, ''MonadOCR]) newtype MockCalls = MockCalls {calls :: [Call]} deriving (Show) @@ -53,6 +54,7 @@ instance (MonadIO m) => MonadControl (TestM m) where instance (MonadIO m) => MonadDraw (TestM m) where drawLine p1 p2 = registerMockCall $ Mock_drawLine p1 p2 + fillRect p size = registerMockCall $ Mock_fillRect p size drawText p color text = (fromIntegral $ mockTextWidth * Text.length text, 0) <$ registerMockCall (Mock_drawText p color text) drawCircle radius p = registerMockCall $ Mock_drawCircle radius p setDrawColor color = registerMockCall $ Mock_setDrawColor color @@ -63,3 +65,6 @@ instance (MonadIO m) => MonadAppShell (TestM m) where hideWindow = registerMockCall Mock_hideWindow showWindow = registerMockCall Mock_showWindow shutdownApp = registerMockCall Mock_shutdownApp + +instance (MonadIO m) => MonadOCR (TestM m) where + getWordsOnScreen = [] <$ registerMockCall Mock_getWordsOnScreen -- cgit v1.3.1