Advertisement
Cililing

OpenGL TestExample (Kotlin)

Feb 22nd, 2021 (edited)
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.20 KB | None | 0 0
  1. interface Context {
  2.     fun init()
  3.     fun destroy()
  4. } // subset of opengl.Context methods
  5.  
  6. class OpenGLContext : Context {
  7.     // wrapper for OpenGLContext
  8.     // holds logic and possible interactions with opengl.Context
  9.     // but hides so much as possible
  10.  
  11.     override fun init() {
  12.         if (opengl.getContext() != null) {
  13.             throw IllegalStateException("context is already initalized")
  14.         }
  15.  
  16.         opengl.init()
  17.     }
  18.  
  19.     override fun destroy() {
  20.         if (opengl.getContext() == null) {
  21.             throw IllegalStateException("context is alredy destroyed")
  22.         }
  23.  
  24.         opengl.destroy()
  25.     }
  26. }
  27.  
  28. // GameEngine doesn't have to know it uses OpenGLContext,
  29. // knows only about Context from the app
  30. // and it doesn't care about the static shit in the 3rd party library
  31. class GameEngine(private val context: Context) {
  32.     fun stop() {
  33.         // ...
  34.         this.context.destroy()
  35.     }
  36. }
  37.  
  38. fun GameEngineRunning_Stop_DestroyContext() {
  39.      // given
  40.      val contextMock = mockk.Create<Context>()
  41.      val engine = GameEngine(contextMock)
  42.  
  43.      // when
  44.      engine.stop()
  45.  
  46.      // then
  47.      verify(exactly = 1) {
  48.          contextMock.destroy()
  49.      }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement