import unittest
import io
import imghdr

from willow.backends import wand as wand_backend


class TestWandOperations(unittest.TestCase):
    def setUp(self):
        with open('tests/images/transparent.png', 'rb') as f:
            self.image = wand_backend.WandBackend.from_file(f)

    def test_get_size(self):
        width, height = self.image.get_size()
        self.assertEqual(width, 200)
        self.assertEqual(height, 150)

    def test_resize(self):
        self.image.resize((100, 75))
        self.assertEqual(self.image.image.size, (100, 75))

    def test_crop(self):
        self.image.crop((10, 10, 100, 100))
        self.assertEqual(self.image.image.size, (90, 90))

    def test_save_as_jpeg(self):
        output = io.BytesIO()
        self.image.save_as_jpeg(output)
        output.seek(0)

        self.assertEqual(imghdr.what(output), 'jpeg')

    def test_save_as_png(self):
        output = io.BytesIO()
        self.image.save_as_png(output)
        output.seek(0)

        self.assertEqual(imghdr.what(output), 'png')

    def test_save_as_gif(self):
        output = io.BytesIO()
        self.image.save_as_gif(output)
        output.seek(0)

        self.assertEqual(imghdr.what(output), 'gif')

    def test_has_alpha(self):
        has_alpha = self.image.has_alpha()
        self.assertTrue(has_alpha)

    def test_has_animation(self):
        has_animation = self.image.has_animation()
        self.assertFalse(has_animation)

    def test_transparent_gif(self):
        with open('tests/images/transparent.gif', 'rb') as f:
            image = wand_backend.WandBackend.from_file(f)

        self.assertTrue(image.has_alpha())
        self.assertFalse(image.has_animation())

        # Check that the alpha of pixel 1,1 is 0
        self.assertEqual(image.image[1][1].alpha, 0)

    def test_resize_transparent_gif(self):
        with open('tests/images/transparent.gif', 'rb') as f:
            image = wand_backend.WandBackend.from_file(f)

        image.resize((100, 75))

        self.assertTrue(image.has_alpha())
        self.assertFalse(image.has_animation())

        # Check that the alpha of pixel 1,1 is 0
        self.assertAlmostEqual(image.image[1][1].alpha, 0, places=6)

    def test_animated_gif(self):
        with open('tests/images/newtons_cradle.gif', 'rb') as f:
            image = wand_backend.WandBackend.from_file(f)

        self.assertTrue(image.has_animation())

    def test_resize_animated_gif(self):
        with open('tests/images/newtons_cradle.gif', 'rb') as f:
            image = wand_backend.WandBackend.from_file(f)

        image.resize((100, 75))

        self.assertTrue(image.has_animation())

@@ -50,44 +50,110 @@
 low.
-backends import wand as wand_backend
+states.files import PNGImageFileState, GIFImageFileState%0Afrom willow.states.wand import WandImageState
 %0A%0A%0Ac
@@ -308,45 +308,49 @@
 e = 
-wand_backend.WandBackend.from_fil
+WandImageState.open(PNGImageFileStat
 e(f)
+)
 %0A%0A  
@@ -524,32 +524,48 @@
 e(self):%0A       
+ resized_image =
  self.image.resi
@@ -603,37 +603,40 @@
 rtEqual(
-self.image.image.
+resized_image.get_
 size
+()
 , (100, 
@@ -665,32 +665,48 @@
 p(self):%0A       
+ cropped_image =
  self.image.crop
@@ -755,29 +755,32 @@
 ual(
-self.image.image.
+cropped_image.get_
 size
+()
 , (9
@@ -1685,45 +1685,49 @@
 e = 
-wand_backend.WandBackend.from_fil
+WandImageState.open(GIFImageFileStat
 e(f)
+)
 %0A%0A  
@@ -2048,45 +2048,49 @@
 e = 
-wand_backend.WandBackend.from_fil
+WandImageState.open(GIFImageFileStat
 e(f)
+)
 %0A%0A  
@@ -2086,32 +2086,48 @@
 ate(f))%0A%0A       
+ resized_image =
  image.resize((1
@@ -2152,32 +2152,40 @@
 self.assertTrue(
+resized_
 image.has_alpha(
@@ -2204,32 +2204,40 @@
 elf.assertFalse(
+resized_
 image.has_animat
@@ -2320,24 +2320,32 @@
 AlmostEqual(
+resized_
 image.image%5B
@@ -2493,45 +2493,49 @@
 e = 
-wand_backend.WandBackend.from_fil
+WandImageState.open(GIFImageFileStat
 e(f)
+)
 %0A%0A  
@@ -2709,45 +2709,49 @@
 e = 
-wand_backend.WandBackend.from_fil
+WandImageState.open(GIFImageFileStat
 e(f)
+)
 %0A%0A  
@@ -2751,24 +2751,40 @@
 f))%0A%0A       
+ resized_image =
  image.resiz
@@ -2813,32 +2813,40 @@
 self.assertTrue(
+resized_
 image.has_animat

Update Wand tests