func (t *TestSuite) TestResourceManager() { request := mandala.LoadResourceRequest{ Filename: "drawable/gopher.png", Response: make(chan mandala.LoadResourceResponse), } mandala.ResourceManager() <- request response := <-request.Response buffer := response.Buffer t.True(response.Error == nil, "An error occured during resource opening") _, err := png.Decode(bytes.NewBuffer(buffer)) t.True(err == nil, "An error occured during png decoding") // Load a non existent resource request = mandala.LoadResourceRequest{ Filename: "res/doesntexist", Response: make(chan mandala.LoadResourceResponse), } mandala.ResourceManager() <- request response = <-request.Response buffer = response.Buffer t.True(buffer == nil) t.True(response.Error != nil) // Use the helper API for loading resources responseCh := make(chan mandala.LoadResourceResponse) mandala.ReadResource("drawable/gopher.png", responseCh) response = <-responseCh buffer = response.Buffer t.True(buffer != nil) t.True(response.Error == nil, "An error occured during resource opening") _, err = png.Decode(bytes.NewBuffer(buffer)) t.True(err == nil, "An error occured during png decoding") }
// TestImage compares a saved expected image of a given filename with // an actual image.Image that typically contains the result of a // rendering. It returns the distance value and the two compared // images. func TestImage(filename string, act image.Image, adjust imagetest.Adjuster) (float64, image.Image, image.Image, error) { request := mandala.LoadResourceRequest{ Filename: filepath.Join(expectedImgPath, filename), Response: make(chan mandala.LoadResourceResponse), } mandala.ResourceManager() <- request response := <-request.Response buffer := response.Buffer if response.Error != nil { return 1, nil, nil, response.Error } exp, err := png.Decode(bytes.NewBuffer(buffer)) if err != nil { return 1, nil, nil, err } return imagetest.CompareDistance(exp, act, adjust), exp, act, nil }
func (t *TestSuite) TestDraw() { request := mandala.LoadResourceRequest{ Filename: "drawable/expected.png", Response: make(chan mandala.LoadResourceResponse), } mandala.ResourceManager() <- request response := <-request.Response buffer := response.Buffer t.True(response.Error == nil, "An error occured during resource opening") if buffer != nil { exp, err := png.Decode(bytes.NewBuffer(buffer)) t.True(err == nil, "An error occured during png decoding") distance := imagetest.CompareDistance(exp, <-t.testDraw, nil) t.True(distance < 0.1, fmt.Sprintf("Distance is %f", distance)) } }
// loadImageResource loads an image with the given filename from the // resource folder. func loadImageResource(filename string) (image.Image, error) { request := mandala.LoadResourceRequest{ Filename: filepath.Join(expectedImgPath, filename), Response: make(chan mandala.LoadResourceResponse), } mandala.ResourceManager() <- request response := <-request.Response buffer := response.Buffer if response.Error != nil { return nil, response.Error } img, err := png.Decode(bytes.NewReader(buffer)) if err != nil { return nil, err } return img, nil }
func loadImage(filename string) (image.Image, error) { // Request an asset to the resource manager. When the app runs // on an Android device, the apk will be unpacked and the file // will be read from it and copied to a byte buffer. request := mandala.LoadResourceRequest{ filename, make(chan mandala.LoadResourceResponse), } mandala.ResourceManager() <- request response := <-request.Response if response.Error != nil { return nil, response.Error } // Decode the image. img, err := png.Decode(bytes.NewBuffer(response.Buffer)) if err != nil { return nil, err } return img, nil }