From 4c6dff35c3c29b1db70b8aeef33d2602bcfcb9df Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Sat, 25 Aug 2018 14:10:56 +0200 Subject: [PATCH] Add Canvas nullcheck to render function --- lib/components/simulation_component.dart | 5 +++-- lib/src/Engine.dart | 7 +++---- test/src/engine_test.dart | 16 +++++++++++++--- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/lib/components/simulation_component.dart b/lib/components/simulation_component.dart index c372026..b7e817c 100644 --- a/lib/components/simulation_component.dart +++ b/lib/components/simulation_component.dart @@ -25,8 +25,9 @@ class SimulationComponent implements OnInit { canvas.context2D.fillRect(0, 0, canvas.width, canvas.height); canvas.context2D.setFillColorRgb(0, 255, 0); canvas.context2D.fillText(''' - If you see this\n - the app is broken :( + If you see this + + the canvas did not load correctly :( ''', canvas.width / 2 - 50, canvas.height / 2); // engineService.create(canvas); } diff --git a/lib/src/Engine.dart b/lib/src/Engine.dart index 9b1814e..601e5ac 100644 --- a/lib/src/Engine.dart +++ b/lib/src/Engine.dart @@ -22,7 +22,7 @@ class Engine { num _updateLag = 0.0; num _drawLag = 0.0; - final html.CanvasElement canvas; + html.CanvasElement canvas; Grid _grid = new Grid(GRID_X, GRID_Y); bool running = false; @@ -43,7 +43,7 @@ class Engine { } void clear() { - _grid = new Grid(100, 100); + _grid = new Grid(GRID_X, GRID_Y); running = false; } @@ -78,8 +78,7 @@ class Engine { } void render([num interp]) { -// print("rendering"); - _grid.render(canvas, interp); + if(canvas != null) _grid.render(canvas, interp); } void addPattern( diff --git a/test/src/engine_test.dart b/test/src/engine_test.dart index 92794ff..72f8b0d 100644 --- a/test/src/engine_test.dart +++ b/test/src/engine_test.dart @@ -1,12 +1,22 @@ @TestOn('browser') -import 'dart:html'; - import 'package:rules_of_living/src/Engine.dart'; import 'package:test/test.dart'; void main() { test("Engine can be instantiated without canvas", () { - expect(Engine(), isNot(throwsA(anything))); + expect(Engine(), isNot(throwsNoSuchMethodError)); + }); + + test("Engine does not throw errors when calling render directly", () { + Engine sut = new Engine(); + // anonymous function necessary since throws can not use functions with args + expect(() => sut.render(), isNot(throwsNoSuchMethodError)); + }); + + test("Engine does not throw errors when processing without attached canvas", () { + Engine sut = new Engine(); + // anonymous function necessary since throws can not use functions with args + expect(() => sut.process(1000), isNot(throwsNoSuchMethodError)); }); } \ No newline at end of file