1#!/usr/bin/env python
2#
3# Copyright 2014 The Chromium Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7import json
8import unittest
9from PIL import Image
10
11import ispy_api
12from common import cloud_bucket
13from common import mock_cloud_bucket
14
15
16class ISpyApiTest(unittest.TestCase):
17  """Unittest for the ISpy API."""
18
19  def setUp(self):
20    self.cloud_bucket = mock_cloud_bucket.MockCloudBucket()
21    self.ispy = ispy_api.ISpyApi(self.cloud_bucket)
22    self.white_img = Image.new('RGBA', (10, 10), (255, 255, 255, 255))
23    self.black_img = Image.new('RGBA', (10, 10), (0, 0, 0, 255))
24
25  def testGenerateExpectationsRunComparison(self):
26    self.ispy.GenerateExpectation(
27        'device', 'test', '1.1.1.1', 'versions.json',
28        [self.white_img, self.white_img])
29    self.ispy.UpdateExpectationVersion('1.1.1.1', 'versions.json')
30    self.ispy.PerformComparison(
31        'test1', 'device', 'test', '1.1.1.1', 'versions.json', self.white_img)
32    expect_name = self.ispy._CreateExpectationName(
33        'device', 'test', '1.1.1.1')
34    self.assertFalse(self.ispy._ispy.FailureExists('test1', expect_name))
35    self.ispy.PerformComparison(
36        'test2', 'device', 'test', '1.1.1.1','versions.json', self.black_img)
37    self.assertTrue(self.ispy._ispy.FailureExists('test2', expect_name))
38
39  def testUpdateExpectationVersion(self):
40    self.ispy.UpdateExpectationVersion('1.0.0.0', 'versions.json')
41    self.ispy.UpdateExpectationVersion('1.0.4.0', 'versions.json')
42    self.ispy.UpdateExpectationVersion('2.1.5.0', 'versions.json')
43    self.ispy.UpdateExpectationVersion('1.1.5.0', 'versions.json')
44    self.ispy.UpdateExpectationVersion('0.0.0.0', 'versions.json')
45    self.ispy.UpdateExpectationVersion('1.1.5.0', 'versions.json')
46    self.ispy.UpdateExpectationVersion('0.0.0.1', 'versions.json')
47    versions = json.loads(self.cloud_bucket.DownloadFile('versions.json'))
48    self.assertEqual(versions,
49        ['2.1.5.0', '1.1.5.0', '1.0.4.0', '1.0.0.0', '0.0.0.1', '0.0.0.0'])
50
51  def testPerformComparisonAndPrepareExpectation(self):
52    self.assertFalse(self.ispy.CanRebaselineToTestRun('test'))
53    self.assertRaises(
54        cloud_bucket.FileNotFoundError,
55        self.ispy.PerformComparisonAndPrepareExpectation,
56        'test', 'device', 'expect', '1.0', 'versions.json',
57        [self.white_img, self.white_img])
58    self.assertTrue(self.ispy.CanRebaselineToTestRun('test'))
59    self.ispy.RebaselineToTestRun('test')
60    versions = json.loads(self.cloud_bucket.DownloadFile('versions.json'))
61    self.assertEqual(versions, ['1.0'])
62    self.ispy.PerformComparisonAndPrepareExpectation(
63        'test1', 'device', 'expect', '1.1', 'versions.json',
64        [self.white_img, self.white_img])
65
66
67if __name__ == '__main__':
68  unittest.main()
69