unicornpaint

A web-based painting app for raspberry PI and pimoroni Unicorn Hat HD
Log | Files | Refs | README

Unicorn_test.go (1997B)


      1 package unicorn
      2 
      3 import (
      4 	"reflect"
      5 	"testing"
      6 	"time"
      7 )
      8 
      9 func TestFakeUnicorn(t *testing.T) {
     10 	unicorn, err := NewUnicorn()
     11 	if err != nil {
     12 		t.Errorf("Got an error making a fake unicorn, shouldn't happen")
     13 	}
     14 	defer unicorn.Close()
     15 
     16 	// Check simple functions
     17 	if unicorn.GetHeight() != 16 {
     18 		t.Errorf("Height was wrong, expecting 16")
     19 	}
     20 	if unicorn.GetWidth() != 16 {
     21 		t.Errorf("Width was wrong, expecting 16")
     22 	}
     23 	// Pixels should be black to start with
     24 	pixels := unicorn.GetPixels()
     25 	for x := uint8(0); x < 16; x++ {
     26 		for y := uint8(0); y < 16; y++ {
     27 			if !reflect.DeepEqual(pixels[x][y], []uint8{0, 0, 0}) {
     28 				t.Errorf("Expecting black pixels to start with")
     29 			}
     30 		}
     31 	}
     32 
     33 	// Should be able to set a pixel, no others should change
     34 	unicorn.SetPixel(0, 0, uint8(255), uint8(255), uint8(255))
     35 	pixels = unicorn.GetPixels()
     36 	if !reflect.DeepEqual(pixels[0][0], []uint8{255, 255, 255}) {
     37 		t.Errorf("Pixel wasn't set when it should be")
     38 	}
     39 	for x := uint8(0); x < 16; x++ {
     40 		for y := uint8(0); y < 16; y++ {
     41 			if x == 0 && y == 0 {
     42 				continue
     43 			}
     44 			if !reflect.DeepEqual(pixels[x][y], []uint8{0, 0, 0}) {
     45 				t.Errorf("Expecting black pixels to start with")
     46 			}
     47 		}
     48 	}
     49 
     50 	// Should be able to set a second pixel
     51 	unicorn.SetPixel(3, 4, uint8(4), uint8(5), uint8(6))
     52 	pixels = unicorn.GetPixels()
     53 	for x := uint8(0); x < 16; x++ {
     54 		for y := uint8(0); y < 16; y++ {
     55 			checkcolor := []uint8{0, 0, 0}
     56 			if x == 0 && y == 0 {
     57 				checkcolor = []uint8{255, 255, 255}
     58 			} else if x == 3 && y == 4 {
     59 				checkcolor = []uint8{4, 5, 6}
     60 			}
     61 			if !reflect.DeepEqual(pixels[x][y], checkcolor) {
     62 				t.Errorf("Got incorrect pixel color at %d %d", x, y)
     63 			}
     64 		}
     65 	}
     66 
     67 	unicorn.Show()
     68 	time.Sleep(time.Duration(500) * time.Millisecond)
     69 	unicorn.SetPixel(10, 10, uint8(255), uint8(255), uint8(0))
     70 	unicorn.Show()
     71 	time.Sleep(time.Duration(500) * time.Millisecond)
     72 
     73 	unicorn.SetPixel(0, 15, uint8(255), uint8(0), uint8(0))
     74 	unicorn.Show()
     75 	time.Sleep(time.Duration(500) * time.Millisecond)
     76 }