- add sources.
[platform/framework/web/crosswalk.git] / src / tools / telemetry / telemetry / core / backends / png_bitmap_unittest.py
1 # Copyright 2013 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
4
5 import unittest
6 import os
7
8 from telemetry.core import util
9 from telemetry.core.backends import png_bitmap
10
11
12 # This is a simple base64 encoded 2x2 PNG which contains, in order, a single
13 # Red, Yellow, Blue, and Green pixel.
14 test_png = """
15 iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAIAAAD91
16 JpzAAAAAXNSR0IArs4c6QAAAAlwSFlzAAALEwAACx
17 MBAJqcGAAAABZJREFUCNdj/M/AwPCfgYGB4T/DfwY
18 AHAAD/iOWZXsAAAAASUVORK5CYII=
19 """
20
21
22 test_png_path = os.path.join(util.GetUnittestDataDir(), 'test_png.png')
23 test_png_2_path = os.path.join(util.GetUnittestDataDir(), 'test_png_2.png')
24
25
26 class PngBitmapTest(unittest.TestCase):
27   def testReadFromBase64(self):
28     png = png_bitmap.PngBitmap.FromBase64(test_png)
29
30     self.assertEquals(2, png.width)
31     self.assertEquals(2, png.height)
32
33     png.GetPixelColor(0, 0).AssertIsRGB(255, 0, 0)
34     png.GetPixelColor(1, 1).AssertIsRGB(0, 255, 0)
35     png.GetPixelColor(0, 1).AssertIsRGB(0, 0, 255)
36     png.GetPixelColor(1, 0).AssertIsRGB(255, 255, 0)
37
38   def testReadFromFile(self):
39     file_png = png_bitmap.PngBitmap.FromFile(test_png_path)
40
41     self.assertEquals(2, file_png.width)
42     self.assertEquals(2, file_png.height)
43
44     file_png.GetPixelColor(0, 0).AssertIsRGB(255, 0, 0)
45     file_png.GetPixelColor(1, 1).AssertIsRGB(0, 255, 0)
46     file_png.GetPixelColor(0, 1).AssertIsRGB(0, 0, 255)
47     file_png.GetPixelColor(1, 0).AssertIsRGB(255, 255, 0)
48
49   def testIsEqual(self):
50     png = png_bitmap.PngBitmap.FromBase64(test_png)
51     file_png = png_bitmap.PngBitmap.FromFile(test_png_path)
52     self.assertTrue(png.IsEqual(file_png))
53
54   def testDiff(self):
55     file_png = png_bitmap.PngBitmap.FromFile(test_png_path)
56     file_png_2 = png_bitmap.PngBitmap.FromFile(test_png_2_path)
57
58     diff_png = file_png.Diff(file_png)
59
60     self.assertEquals(2, diff_png.width)
61     self.assertEquals(2, diff_png.height)
62
63     diff_png.GetPixelColor(0, 0).AssertIsRGB(0, 0, 0)
64     diff_png.GetPixelColor(1, 1).AssertIsRGB(0, 0, 0)
65     diff_png.GetPixelColor(0, 1).AssertIsRGB(0, 0, 0)
66     diff_png.GetPixelColor(1, 0).AssertIsRGB(0, 0, 0)
67
68     diff_png = file_png.Diff(file_png_2)
69
70     self.assertEquals(3, diff_png.width)
71     self.assertEquals(3, diff_png.height)
72
73     diff_png.GetPixelColor(0, 0).AssertIsRGB(0, 255, 255)
74     diff_png.GetPixelColor(1, 1).AssertIsRGB(255, 0, 255)
75     diff_png.GetPixelColor(0, 1).AssertIsRGB(255, 255, 0)
76     diff_png.GetPixelColor(1, 0).AssertIsRGB(0, 0, 255)
77
78     diff_png.GetPixelColor(0, 2).AssertIsRGB(255, 255, 255)
79     diff_png.GetPixelColor(1, 2).AssertIsRGB(255, 255, 255)
80     diff_png.GetPixelColor(2, 0).AssertIsRGB(255, 255, 255)
81     diff_png.GetPixelColor(2, 1).AssertIsRGB(255, 255, 255)
82     diff_png.GetPixelColor(2, 2).AssertIsRGB(255, 255, 255)