Skip to content

Commit

Permalink
100% test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Achille Roussel committed May 25, 2016
1 parent 312b51b commit b98c04d
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 21 deletions.
4 changes: 2 additions & 2 deletions CoreGraphics.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func extractImageData(img image.Image) imageData {
width: bounds.Dx(),
height: bounds.Dy(),
colors: C.CGColorSpaceCreateDeviceGray(),
info: C.CGBitmapInfo(C.kCGImageAlphaOnly),
info: C.CGBitmapInfo(C.kCGImageAlphaNone),
}

case *image.Alpha16:
Expand All @@ -183,7 +183,7 @@ func extractImageData(img image.Image) imageData {
width: bounds.Dx(),
height: bounds.Dy(),
colors: C.CGColorSpaceCreateDeviceGray(),
info: C.CGBitmapInfo(C.kCGImageAlphaOnly),
info: C.CGBitmapInfo(C.kCGImageAlphaNone),
}

case *image.Gray:
Expand Down
28 changes: 20 additions & 8 deletions CoreGraphics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,33 @@
package cocoa

import (
"image"
"testing"

"image/color"
_ "image/png"
)

func TestCGImageCreate(t *testing.T) {
img := CGImageCreate(gopher)
CFRetain(CFTypeRef(img))
CFRelease(CFTypeRef(img))
CFRelease(CFTypeRef(img))
for _, test := range gopherImages {
img := CGImageCreate(test)
CFRetain(CFTypeRef(img))
CFRelease(CFTypeRef(img))
CFRelease(CFTypeRef(img))
}
}

func TestCGImageCreateNoCopy(t *testing.T) {
img := CGImageCreateNoCopy(gopher)
CFRetain(CFTypeRef(img))
CFRelease(CFTypeRef(img))
CFRelease(CFTypeRef(img))
for _, test := range gopherImages {
img := CGImageCreateNoCopy(test)
CFRetain(CFTypeRef(img))
CFRelease(CFTypeRef(img))
CFRelease(CFTypeRef(img))
}
}

func TestCGImageCreatePanic(t *testing.T) {
defer func() { recover() }()
CGImageCreate(image.NewUniform(color.Black))
t.Error("calling CGImageCreate with an unsupported image type did not panic!")
}
83 changes: 72 additions & 11 deletions image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,21 @@ package cocoa
import (
"bufio"
"image"
"image/draw"
"os"
"testing"
)

var gopher image.Image

func init() {
f, _ := os.Open("fixtures/gopher.png")
gopher, _, _ = image.Decode(bufio.NewReader(f))
f.Close()
}

func TestNewImage(t *testing.T) {
img := NewImage(gopher)
img := NewImage(gopherNRGBA)

if img.Ref() == nil {
t.Error("invalid nil image reference")
}
}

func TestNewImageWrap(t *testing.T) {
img1 := CGImageCreate(gopher)
img1 := CGImageCreate(gopherNRGBA)
img2 := NewImageWrap(img1)

if ref := img2.Ref(); ref != img1 {
Expand All @@ -35,10 +28,78 @@ func TestNewImageWrap(t *testing.T) {
}

func TestImageRelease(t *testing.T) {
img := NewImage(gopher)
img := NewImage(gopherNRGBA)
img.release()

if img.Ref() != nil {
t.Error("invalid non-nil image reference found after releasing")
}
}

func loadImage() image.Image {
f, _ := os.Open("fixtures/gopher.png")
i, _, _ := image.Decode(bufio.NewReader(f))
f.Close()
return i
}

func copyImage(dst draw.Image, src image.Image) image.Image {
draw.Draw(dst, dst.Bounds(), src, image.Point{}, draw.Over)
return dst
}

func imageToAlpha(img image.Image) image.Image {
return copyImage(image.NewAlpha(img.Bounds()), img)
}

func imageToAlpha16(img image.Image) image.Image {
return copyImage(image.NewAlpha16(img.Bounds()), img)
}

func imageToGray(img image.Image) image.Image {
return copyImage(image.NewGray(img.Bounds()), img)
}

func imageToGray16(img image.Image) image.Image {
return copyImage(image.NewGray16(img.Bounds()), img)
}

func imageToRGBA(img image.Image) image.Image {
return copyImage(image.NewRGBA(img.Bounds()), img)
}

func imageToRGBA64(img image.Image) image.Image {
return copyImage(image.NewRGBA64(img.Bounds()), img)
}

func imageToNRGBA64(img image.Image) image.Image {
return copyImage(image.NewNRGBA64(img.Bounds()), img)
}

func imageToCMYK(img image.Image) image.Image {
return copyImage(image.NewCMYK(img.Bounds()), img)
}

var (
gopherNRGBA = loadImage()
gopherAlpha = imageToAlpha(gopherNRGBA)
gopherAlpha16 = imageToAlpha16(gopherNRGBA)
gopherGray = imageToGray(gopherNRGBA)
gopherGray16 = imageToGray16(gopherNRGBA)
gopherRGBA = imageToRGBA(gopherNRGBA)
gopherRGBA64 = imageToRGBA64(gopherNRGBA)
gopherNRGBA64 = imageToNRGBA64(gopherNRGBA)
gopherCMYK = imageToCMYK(gopherNRGBA)

gopherImages = [...]image.Image{
gopherNRGBA,
gopherAlpha,
gopherAlpha16,
gopherGray,
gopherGray16,
gopherRGBA,
gopherRGBA64,
gopherNRGBA64,
gopherCMYK,
}
)

0 comments on commit b98c04d

Please sign in to comment.