github.com/bazelbuild/rules_webtesting@v0.2.0/go/cropper/cropper_test.go (about)

     1  // Copyright 2016 Google Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package cropper
    16  
    17  import (
    18  	"image"
    19  	"image/color"
    20  	"image/png"
    21  	"log"
    22  	"os"
    23  	"path/filepath"
    24  	"testing"
    25  
    26  	"github.com/bazelbuild/rules_webtesting/go/bazel"
    27  )
    28  
    29  func TestCrop(t *testing.T) {
    30  	p, err := bazel.Runfile("testdata/example.png")
    31  	if err != nil {
    32  		t.Fatal(err)
    33  	}
    34  
    35  	in, err := os.Open(p)
    36  	if err != nil {
    37  		t.Fatal(err)
    38  	}
    39  
    40  	src, err := png.Decode(in)
    41  	in.Close()
    42  	if err != nil {
    43  		t.Fatal(err)
    44  	}
    45  
    46  	dst, err := Crop(src, image.Rect(10, 20, 100, 90))
    47  	if err != nil {
    48  		t.Fatal(err)
    49  	}
    50  
    51  	tmpDir, err := bazel.NewTmpDir("crop_test")
    52  	if err != nil {
    53  		t.Fatal(err)
    54  	}
    55  
    56  	outPath := filepath.Join(tmpDir, "cropped.png")
    57  
    58  	out, err := os.Create(outPath)
    59  	if err != nil {
    60  		t.Fatal(err)
    61  	}
    62  
    63  	err = png.Encode(out, dst)
    64  	out.Close()
    65  	if err != nil {
    66  		t.Fatal(err)
    67  	}
    68  
    69  	check, err := os.Open(outPath)
    70  	if err != nil {
    71  		t.Fatal(err)
    72  	}
    73  
    74  	config, err := png.DecodeConfig(check)
    75  	check.Close()
    76  	if err != nil {
    77  		t.Fatal(err)
    78  	}
    79  
    80  	if config.Width != 90 || config.Height != 70 {
    81  		t.Errorf("got size == %d, %d, expected 90, 70", config.Width, config.Height)
    82  	}
    83  }
    84  
    85  func TestBlackout(t *testing.T) {
    86  	p, err := bazel.Runfile("testdata/example.png")
    87  	if err != nil {
    88  		t.Fatal(err)
    89  	}
    90  
    91  	in, err := os.Open(p)
    92  	if err != nil {
    93  		t.Fatal(err)
    94  	}
    95  
    96  	src, err := png.Decode(in)
    97  	in.Close()
    98  	if err != nil {
    99  		t.Fatal(err)
   100  	}
   101  
   102  	dst, err := Blackout(src, image.Rect(10, 20, 200, 90))
   103  	if err != nil {
   104  		t.Fatal(err)
   105  	}
   106  
   107  	tmpDir, err := bazel.NewTmpDir("crop_test")
   108  	if err != nil {
   109  		t.Fatal(err)
   110  	}
   111  
   112  	outPath := filepath.Join(tmpDir, "blackedout.png")
   113  
   114  	log.Printf("Output file: %s", outPath)
   115  
   116  	out, err := os.Create(outPath)
   117  	if err != nil {
   118  		t.Fatal(err)
   119  	}
   120  
   121  	err = png.Encode(out, dst)
   122  	out.Close()
   123  	if err != nil {
   124  		t.Fatal(err)
   125  	}
   126  
   127  	check, err := os.Open(outPath)
   128  	if err != nil {
   129  		t.Fatal(err)
   130  	}
   131  
   132  	i, err := png.Decode(check)
   133  	check.Close()
   134  	if err != nil {
   135  		t.Fatal(err)
   136  	}
   137  
   138  	img, ok := i.(*image.NRGBA)
   139  	if !ok {
   140  		t.Fatalf("got %T, expected image to be RGBA", i)
   141  	}
   142  
   143  	px := color.RGBAModel.Convert(img.At(20, 30))
   144  
   145  	if px != color.RGBAModel.Convert(color.Black) {
   146  		t.Fatalf("got %v, expected RGBA(R: 0, G: 0, B: 0, A: 255)", px)
   147  	}
   148  }