OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python | |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
3 # Use of this source code is governed by a BSD-style license that can be | |
4 # found in the LICENSE file. | |
5 import os | |
6 import sys | |
7 import time | |
8 import json | |
9 | |
10 sys.path.append(os.path.join(os.path.dirname(__file__), '..')) | |
11 import telemetry | |
12 | |
13 def ReadTestExpectations(json_test_expectations_path): | |
14 f = open(json_test_expectations_path, 'r') | |
15 json_contents = json.load(f) | |
16 return json_contents | |
17 | |
18 def CompareToExpectedResults(snapshot, expectations): | |
19 result = True | |
20 for expectation in expectations: | |
21 location = expectation["location"] | |
22 iter_result = snapshot.CheckPixelColor(location[0], location[1], | |
23 expectation["color"], expectation["tolerance"]) | |
24 if not iter_result: | |
25 print("FAILURE: Expected", location, "to be", expectation["color"], | |
26 "but got", snapshot.GetPixel(location[0], location[1])) | |
27 result &= iter_result | |
28 return result | |
29 | |
30 def RunSingleTest(browser, | |
31 url, | |
32 json_test_expectations_filename, | |
33 failure_filename_prefix): | |
34 expectations = ReadTestExpectations(json_test_expectations_filename) | |
35 | |
36 #TODO: BringBrowserWindowToFront? | |
Ken Russell (switch to Gerrit)
2012/11/09 20:26:28
TODOs require a contact person. http://google-styl
| |
37 #TODO: browser.window.SetBounds? | |
38 | |
39 with browser.ConnectToNthTab(0) as tab: | |
40 tab.page.Navigate(url) | |
41 time.sleep(2) # Why is this needed? | |
42 tab.WaitForDocumentReadyStateToBeComplete() | |
43 | |
44 if tab.runtime.Evaluate('window.chrome.gpuBenchmarking === undefined'): | |
45 print 'Browser does not support gpu benchmarks API.' | |
46 return 255 | |
47 | |
48 if tab.runtime.Evaluate( | |
49 'window.chrome.gpuBenchmarking.windowSnapshot === undefined'): | |
50 print 'Browser does not support window snapshot API.' | |
51 return 255 | |
52 | |
53 # Mock up the message queue functionality used by the mapping test | |
Ken Russell (switch to Gerrit)
2012/11/09 20:26:28
Please use periods after comments. It makes them e
| |
54 tab.runtime.Evaluate(""" | |
55 if(!window.domAutomationController) { | |
56 window.domAutomationController = {}; | |
57 } | |
58 window.domAutomationController.message_queue = []; | |
59 window.domAutomationController.setAutomationId = function(id) {} | |
60 window.domAutomationController.send = function(val) { | |
61 window.domAutomationController.message_queue.push(val); | |
62 } | |
63 """) | |
64 | |
65 def CheckMessages(): | |
66 message = tab.runtime.Evaluate( | |
67 "window.domAutomationController.message_queue.shift();") | |
68 return message == "FINISHED" | |
69 | |
70 telemetry.WaitFor(CheckMessages, 1000, 0.5) | |
71 | |
72 snapshot = tab.SnapshotContent() | |
73 if not snapshot: | |
74 print "Failed to capture snapshot" | |
75 return 255 | |
76 | |
77 all_pixels_match = CompareToExpectedResults(snapshot, expectations) | |
78 | |
79 if not all_pixels_match: | |
80 print "Color check failed at end of test" | |
81 snapshot.WriteFile(failure_filename_prefix + "-output.png") | |
82 return 1 | |
83 | |
84 return 0 | |
85 | |
86 def Main(args): | |
87 options = telemetry.BrowserOptions() | |
88 parser = options.CreateParser('snapshot_test.py') | |
89 options, args = parser.parse_args(args) | |
90 | |
91 options.extra_browser_args.append('--enable-gpu-benchmarking') | |
92 browser_to_create = telemetry.FindBrowser(options) | |
93 assert browser_to_create | |
94 with browser_to_create.Create() as browser: | |
95 return RunSingleTest(browser, | |
96 "http://localhost:8000/basic.html", | |
97 "mapsgl_single_run_basic_expectations.json", | |
98 "single-run-basic") | |
99 | |
100 if __name__ == '__main__': | |
101 sys.exit(Main(sys.argv[1:])) | |
OLD | NEW |