sensor.lua 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. -- love.sensor
  2. -- @NOTE we can't test this module fully as it's hardware dependent
  3. -- however we can test methods do what is expected and can handle certain params
  4. --------------------------------------------------------------------------------
  5. --------------------------------------------------------------------------------
  6. ------------------------------------HELPERS-------------------------------------
  7. --------------------------------------------------------------------------------
  8. --------------------------------------------------------------------------------
  9. local function testIsEnabled(test, sensorType)
  10. love.sensor.setEnabled(sensorType, true)
  11. test:assertTrue(love.sensor.isEnabled(sensorType), 'check ' .. sensorType .. ' enabled')
  12. love.sensor.setEnabled(sensorType, false)
  13. test:assertFalse(love.sensor.isEnabled(sensorType), 'check ' .. sensorType .. ' disabled')
  14. end
  15. local function testGetName(test, sensorType)
  16. love.sensor.setEnabled(sensorType, true)
  17. local ok, name = pcall(love.sensor.getName, sensorType)
  18. test:assertTrue(ok, 'check sensor.getName("' .. sensorType .. '") success')
  19. test:assertEquals(type(name), 'string', 'check sensor.getName("' .. sensorType .. '") return value type')
  20. love.sensor.setEnabled(sensorType, false)
  21. ok, name = pcall(love.sensor.getName, sensorType)
  22. test:assertFalse(ok, 'check sensor.getName("' .. sensorType .. '") errors when disabled')
  23. love.sensor.setEnabled(sensorType, false)
  24. ok, x, y, z = pcall(love.sensor.getData, sensorType)
  25. test:assertFalse(ok, 'check sensor.getData("' .. sensorType .. '") errors when disabled')
  26. end
  27. local function testGetData(test, sensorType)
  28. love.sensor.setEnabled(sensorType, true)
  29. local ok, x, y, z = pcall(love.sensor.getData, sensorType)
  30. test:assertTrue(ok, 'check sensor.getData("' .. sensorType .. '") success')
  31. if ok then
  32. test:assertNotNil(x)
  33. test:assertNotNil(y)
  34. test:assertNotNil(z)
  35. end
  36. love.sensor.setEnabled(sensorType, false)
  37. ok, x, y, z = pcall(love.sensor.getData, sensorType)
  38. test:assertFalse(ok, 'check sensor.getData("' .. sensorType .. '") errors when disabled')
  39. end
  40. --------------------------------------------------------------------------------
  41. --------------------------------------------------------------------------------
  42. ------------------------------------METHODS-------------------------------------
  43. --------------------------------------------------------------------------------
  44. --------------------------------------------------------------------------------
  45. -- love.sensor.hasSensor
  46. love.test.sensor.hasSensor = function(test)
  47. -- but we can make sure that the SensorTypes can be passed
  48. local accelerometer = love.sensor.hasSensor('accelerometer')
  49. local gyroscope = love.sensor.hasSensor('gyroscope')
  50. test:assertNotNil(accelerometer)
  51. test:assertNotNil(gyroscope)
  52. end
  53. -- love.sensor.isEnabled and love.sensor.setEnabled
  54. love.test.sensor.isEnabled = function(test)
  55. local accelerometer = love.sensor.hasSensor('accelerometer')
  56. local gyroscope = love.sensor.hasSensor('gyroscope')
  57. if accelerometer or gyroscope then
  58. if accelerometer then testIsEnabled(test, 'accelerometer') end
  59. if gyroscope then testIsEnabled(test, 'gyroscope') end
  60. else
  61. test:skipTest('neither accelerometer nor gyroscope are supported in this system')
  62. end
  63. end
  64. -- love.sensor.getName
  65. love.test.sensor.getName = function(test)
  66. local accelerometer = love.sensor.hasSensor('accelerometer')
  67. local gyroscope = love.sensor.hasSensor('gyroscope')
  68. if accelerometer or gyroscope then
  69. if accelerometer then testGetName(test, 'accelerometer') end
  70. if gyroscope then testGetName(test, 'gyroscope') end
  71. else
  72. test:skipTest('neither accelerometer nor gyroscope are supported in this system')
  73. end
  74. end
  75. -- love.sensor.getData
  76. love.test.sensor.getData = function(test)
  77. local accelerometer = love.sensor.hasSensor('accelerometer')
  78. local gyroscope = love.sensor.hasSensor('gyroscope')
  79. if accelerometer or gyroscope then
  80. if accelerometer then testGetData(test, 'accelerometer') end
  81. if gyroscope then testGetData(test, 'gyroscope') end
  82. else
  83. test:skipTest('neither accelerometer nor gyroscope are supported in this system')
  84. end
  85. end