github.com/qxnw/lib4go@v0.0.0-20180426074627-c80c7e84b925/images/image_test.go (about)

     1  package images
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func TestNewDraw(t *testing.T) {
     8  	img := NewImage(100, 100)
     9  	if img == nil {
    10  		t.Error("test fail")
    11  	}
    12  
    13  	img = NewImage(0, 0)
    14  	if img == nil {
    15  		t.Error("test fail")
    16  	}
    17  
    18  	img = NewImage(-100, 100)
    19  	if img == nil {
    20  		t.Error("test fail")
    21  	}
    22  
    23  	img = NewImage(-100, -100)
    24  	if img == nil {
    25  		t.Error("test fail")
    26  	}
    27  
    28  	img = NewImage(100, -100)
    29  	if img == nil {
    30  		t.Error("test fail")
    31  	}
    32  }
    33  
    34  func TestNewImageFromFile(t *testing.T) {
    35  	path := "/home/champly/picture.png"
    36  	img, err := NewImageFromFile(100, 100, path)
    37  	if err != nil {
    38  		t.Errorf("test fail %v", err)
    39  	}
    40  	if img == nil {
    41  		t.Error("test fail")
    42  	}
    43  
    44  	path = "/home/champly/picture.jpg"
    45  	img, err = NewImageFromFile(-100, 100, path)
    46  	if err != nil {
    47  		t.Errorf("test fail %v", err)
    48  	}
    49  	if img == nil {
    50  		t.Error("test fail")
    51  	}
    52  
    53  	path = "/home/champly/picture.jpg"
    54  	img, err = NewImageFromFile(-100, -100, path)
    55  	if err != nil {
    56  		t.Errorf("test fail %v", err)
    57  	}
    58  	if img == nil {
    59  		t.Error("test fail")
    60  	}
    61  
    62  	path = "/home/champly/picture.jpg"
    63  	img, err = NewImageFromFile(100, -100, path)
    64  	if err != nil {
    65  		t.Errorf("test fail %v", err)
    66  	}
    67  	if img == nil {
    68  		t.Error("test fail")
    69  	}
    70  
    71  	path = "/home/champly/err_picture.jpg"
    72  	img, err = NewImageFromFile(100, 100, path)
    73  	if err == nil {
    74  		t.Error("test fail")
    75  	}
    76  
    77  	path = "/home/champly/picture.gif"
    78  	img, err = NewImageFromFile(100, 100, path)
    79  	if err == nil {
    80  		t.Error("test fail")
    81  	}
    82  }
    83  
    84  func TestDrawFont(t *testing.T) {
    85  	path := "/home/champly/picture.jpg"
    86  	img, err := NewImageFromFile(1920, 1080, path)
    87  	if err != nil {
    88  		t.Errorf("test fail %v", err)
    89  	}
    90  	if img == nil {
    91  		t.Error("test fail")
    92  	}
    93  
    94  	fontPath := "/usr/share/fonts/msyhbd.ttf"
    95  	text := "Hello World"
    96  	col := "155"
    97  	fontSize := 16.0
    98  	img.DrawFont(fontPath, text, col, fontSize, 100, 300)
    99  	err = img.Save("/home/champly/picture_test.png")
   100  	if err != nil {
   101  		t.Errorf("test fail %v", err)
   102  	}
   103  
   104  	fontPath = "/usr/share/fonts/msyhbdsdfadf.ttf"
   105  	text = "Hello World"
   106  	col = "155"
   107  	fontSize = 16.0
   108  	img.DrawFont(fontPath, text, col, fontSize, 100, 300)
   109  	err = img.Save("/home/champly/picture_test.png")
   110  	if err != nil {
   111  		t.Errorf("test fail %v", err)
   112  	}
   113  }
   114  
   115  func TestDrawImage(t *testing.T) {
   116  	path := "/home/champly/picture_test.png"
   117  	img, err := NewImageFromFile(1920, 1080, path)
   118  	if err != nil {
   119  		t.Errorf("test fail %v", err)
   120  	}
   121  	if img == nil {
   122  		t.Error("test fail")
   123  	}
   124  
   125  	err = img.Save("/home/champly/picture_test.png")
   126  	if err != nil {
   127  		t.Errorf("test fail %v", err)
   128  	}
   129  
   130  	// 路径不正确保存文件
   131  	err = img.Save("/home/champly/picture/picture_test.png")
   132  	if err == nil {
   133  		t.Error("test fail")
   134  	}
   135  }