|
| 1 | +package frostutil |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "runtime" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/hajimehoshi/ebiten/v2" |
| 10 | +) |
| 11 | + |
| 12 | +func init() { |
| 13 | + runtime.LockOSThread() |
| 14 | +} |
| 15 | + |
| 16 | +// Any test function meant to run in Update must have this signature |
| 17 | +type UpdateTestFunc func(t *testing.T) |
| 18 | + |
| 19 | +// Any test function meant to run in Draw must have this signature |
| 20 | +type DrawTestFunc func(t *testing.T, screen *ebiten.Image) |
| 21 | + |
| 22 | +// Any test function meant to run in Layout must have this signature |
| 23 | +type LayoutTestFunc func(t *testing.T, outsideWidth, outsideHeight int) (screenWidth, screenHeight int) |
| 24 | + |
| 25 | +var updateTests chan *UpdateTest = make(chan *UpdateTest, 1) |
| 26 | +var drawTests chan *DrawTest = make(chan *DrawTest, 1) |
| 27 | +var layoutTests chan *LayoutTest = make(chan *LayoutTest, 1) |
| 28 | +var awaitUpdateTestCompletion chan bool = make(chan bool) |
| 29 | +var awaitDrawTestCompletion chan bool = make(chan bool) |
| 30 | +var awaitLayoutTestCompletion chan bool = make(chan bool) |
| 31 | +var hasTestMain bool // set to true by OnTestMain prior to calling m.Run(), if it is false in Queue*Test, then OnTestMain was never called. |
| 32 | +var testsQueued int |
| 33 | + |
| 34 | +// TestGame contains the Update, Layout, and Draw methods that Ebitengine calls. |
| 35 | +type TestGame struct { |
| 36 | + screenWidth, screenHeight int |
| 37 | +} |
| 38 | + |
| 39 | +// This has to be called from a TestMain(m *testing.M) function in any package that uses QueueUpdateTest, QueueDrawTest, or QueueLayoutTest. |
| 40 | +// It sets up and runs Ebitengine, runs your test functions (via m.Run) which should call Queue*Test, waits for it to finish, |
| 41 | +// and then closes the channels and sets their variables to nil, which prompts Update to tell Ebitengine to shut down. |
| 42 | +func OnTestMain(m *testing.M) { |
| 43 | + runtime.LockOSThread() |
| 44 | + f := func() { |
| 45 | + hasTestMain = true |
| 46 | + m.Run() |
| 47 | + close(updateTests) |
| 48 | + close(drawTests) |
| 49 | + close(layoutTests) |
| 50 | + drawTests = nil |
| 51 | + layoutTests = nil |
| 52 | + updateTests = nil |
| 53 | + for testsQueued > 0 { |
| 54 | + time.Sleep(100 * time.Millisecond) |
| 55 | + } |
| 56 | + } |
| 57 | + go f() |
| 58 | + ebiten.SetWindowSize(1280, 720) |
| 59 | + ebiten.SetWindowTitle("Test") |
| 60 | + //time.Sleep(time.Second) |
| 61 | + testGame := &TestGame{screenWidth: 1920, screenHeight: 1080} |
| 62 | + ebiten.RunGame(testGame) |
| 63 | + runtime.UnlockOSThread() |
| 64 | +} |
| 65 | + |
| 66 | +// UpdateTest pointers are sent through a channel from QueueUpdateTest to *TestGame.Update. |
| 67 | +type UpdateTest struct { |
| 68 | + t *testing.T |
| 69 | + f UpdateTestFunc |
| 70 | +} |
| 71 | + |
| 72 | +// DrawTest pointers are sent through a channel from QueueDrawTest to *TestGame.Draw. |
| 73 | +type DrawTest struct { |
| 74 | + t *testing.T |
| 75 | + f DrawTestFunc |
| 76 | +} |
| 77 | + |
| 78 | +// LayoutTest pointers are sent through a channel from QueueLayoutTest to *TestGame.Layout. |
| 79 | +type LayoutTest struct { |
| 80 | + t *testing.T |
| 81 | + f LayoutTestFunc |
| 82 | +} |
| 83 | + |
| 84 | +// Each time Update is called by Ebitengine, it retrieves an update test, if any are queued, from the updateTests channel, runs it, |
| 85 | +// and then lets QueueUpdateTest know that it has finished running it (so that it will return). |
| 86 | +// If updateTests is nil, then it returns an error to tell Ebitengine to shut down. |
| 87 | +func (game *TestGame) Update() (err error) { |
| 88 | + if updateTests != nil { |
| 89 | + if len(updateTests) > 0 { |
| 90 | + test := <-updateTests |
| 91 | + test.f(test.t) |
| 92 | + awaitUpdateTestCompletion <- true |
| 93 | + } |
| 94 | + } else { |
| 95 | + err = errors.New("Done") |
| 96 | + } |
| 97 | + return |
| 98 | +} |
| 99 | + |
| 100 | +// Each time Draw is called by Ebitengine, it retrieves a draw test, if any are queued, from the drawTests channel, runs it, |
| 101 | +// and then lets QueueDrawTest know that it has finished running it (so that it will return). |
| 102 | +// If drawTests is nil, it does nothing. |
| 103 | +func (game *TestGame) Draw(screen *ebiten.Image) { |
| 104 | + if drawTests != nil { |
| 105 | + if len(drawTests) > 0 { |
| 106 | + test := <-drawTests |
| 107 | + test.f(test.t, screen) |
| 108 | + awaitDrawTestCompletion <- true |
| 109 | + } |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +// Each time Layout is called by Ebitengine, it retrieves a layout test, if any are queued, from the layoutTests channel, runs it, |
| 114 | +// records the screenWidth and screenHeight that it returns, and then lets QueueLayoutTest know that it has finished running it (so that it will return). |
| 115 | +// If layoutTests is nil, it does nothing. |
| 116 | +// It returns the screenWidth and screenHeight returned by the last layout test, or 1920 and 1080 if no layout tests were ever queued. |
| 117 | +func (game *TestGame) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) { |
| 118 | + if layoutTests != nil { |
| 119 | + if len(layoutTests) > 0 { |
| 120 | + test := <-layoutTests |
| 121 | + game.screenWidth, game.screenHeight = test.f(test.t, outsideWidth, outsideHeight) |
| 122 | + awaitLayoutTestCompletion <- true |
| 123 | + } |
| 124 | + } |
| 125 | + return game.screenWidth, game.screenHeight |
| 126 | +} |
| 127 | + |
| 128 | +// QueueUpdateTest checks to make sure OnTestMain was called, and if it was, it packages up the parameters t and f, |
| 129 | +// and sends them through the updateTests channel for Update. It waits for Update to let it know that it has finished running f(t), and then returns. |
| 130 | +// If OnTestMain was never called, it triggers a test failure and warns that you need to call OnTestMain from TestMain in every package which contains calls to QueueUpdateTest. |
| 131 | +func QueueUpdateTest(t *testing.T, f func(t *testing.T)) { |
| 132 | + if hasTestMain { |
| 133 | + testsQueued++ |
| 134 | + updateTests <- &UpdateTest{t, f} |
| 135 | + <-awaitUpdateTestCompletion |
| 136 | + testsQueued-- |
| 137 | + } else { |
| 138 | + t.Fatal("Missing call to frostutil.OnTestMain. OnTestMain must be called from a TestMain(m *testing.M) function in every package in which you want to use QueueLayoutTest, QueueUpdateTest, and/or QueueDrawTest.") |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +// QueueDrawTest checks to make sure OnTestMain was called, and if it was, it packages up the parameters t and f, |
| 143 | +// and sends them through the drawTests channel for Draw. It waits for Draw to let it know that it has finished running f(t, screen), and then returns. |
| 144 | +// If OnTestMain was never called, it triggers a test failure and warns that you need to call OnTestMain from TestMain in every package which contains calls to QueueDrawTest. |
| 145 | +func QueueDrawTest(t *testing.T, f func(t *testing.T, screen *ebiten.Image)) { |
| 146 | + if hasTestMain { |
| 147 | + testsQueued++ |
| 148 | + drawTests <- &DrawTest{t, f} |
| 149 | + <-awaitDrawTestCompletion |
| 150 | + testsQueued-- |
| 151 | + } else { |
| 152 | + t.Fatal("Missing call to frostutil.OnTestMain. OnTestMain must be called from a TestMain(m *testing.M) function in every package in which you want to use QueueLayoutTest, QueueUpdateTest, and/or QueueDrawTest.") |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +// QueueLayoutTest checks to make sure OnTestMain was called, and if it was, it packages up the parameters t and f, |
| 157 | +// and sends them through the layoutTests channel for Layout. It waits for Layout to let it know that it has finished running f(t, outsideWidth, outsideHeight), |
| 158 | +// and then returns. |
| 159 | +// If OnTestMain was never called, it triggers a test failure and warns that you need to call OnTestMain from TestMain in every package which contains calls to QueueLayoutTest. |
| 160 | +func QueueLayoutTest(t *testing.T, f func(t *testing.T, outsideWidth, outsideHeight int) (screenWidth, screenHeight int)) { |
| 161 | + if hasTestMain { |
| 162 | + testsQueued++ |
| 163 | + layoutTests <- &LayoutTest{t, f} |
| 164 | + <-awaitLayoutTestCompletion |
| 165 | + testsQueued-- |
| 166 | + } else { |
| 167 | + t.Fatal("Missing call to frostutil.OnTestMain. OnTestMain must be called from a TestMain(m *testing.M) function in every package in which you want to use QueueLayoutTest, QueueUpdateTest, and/or QueueDrawTest.") |
| 168 | + } |
| 169 | +} |
0 commit comments