2 # Copyright (c) 2011 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.
6 """This functional test spawns a web server, and runs chrome to point
9 The content served contains prefetch requests, and the tests assert that the
10 webserver logs reflect that.
12 Run like any functional test:
13 $ python chrome/test/functional/prefetch.py
14 in a repo with a built pyautolib
16 The import of multiprocessing implies python 2.6 is required
21 import multiprocessing
24 from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
26 import pyauto_functional # Must be imported before pyauto
29 # this class handles IPC retrieving server "logs" from our integral
30 # server. Each test should clear() the log, and then run asserts on
33 # at startup, the server puts an int in the queue which is its port,
34 # we store that for subsequent tests
40 def __init__(self,queue):
48 queueval = self.queue.get(False)
49 if isinstance(queueval,int):
52 self.log[queueval] = True
61 def isRetrieved(self,path):
69 # The next few classes run a simple web server that returns log information
70 # via a multiprocessing.Queue.
72 class AbstractPrefetchServerHandler(BaseHTTPRequestHandler):
74 "prefetch-origin.html":
76 <link rel="prefetch" href="static-prefetch-target.html">
77 <script type="text/javascript">
78 function changeParagraph()
80 var newPara = document.createElement("p");
82 "<link rel=\\"prefetch\\" href=\\"dynamic-prefetch-target.html\\">" +
83 "<p>This paragraph contains a dynamic link prefetch. " +
84 "The target of this prefetch is " +
85 "<a href=\\"dynamic-prefetch-target.html\\">this document.</a>";
86 var para = document.getElementById("p1");
87 document.body.insertBefore(newPara,para);
91 <body onload="changeParagraph()">
92 <p id="p1">This is a document that contains a link prefetch. The target of
93 that prefetch is <a href="static-prefetch-target.html">this document.</a>
95 "static-prefetch-target.html":
96 (200, "<html><head></head><body>empty</body>"),
97 "dynamic-prefetch-target.html":
98 (200, "<html><head></head><body>empty</body>")}
101 self.queue.put(self.path[1:])
103 response_code, response = self.content[self.path[1:]]
104 self.send_response(response_code)
106 self.wfile.write(response)
108 self.send_response(404)
111 def run_web_server(queue_arg):
112 class PrefetchServerHandler(AbstractPrefetchServerHandler):
114 server = HTTPServer(('',0), PrefetchServerHandler)
115 queue.put(server.server_port)
116 server.serve_forever()
119 # Here's the test itself
121 queue = multiprocessing.Queue()
122 server_log = ServerLog(queue)
124 class PrefetchTest(pyauto.PyUITest):
125 """Testcase for Prefetching"""
128 url = "http://localhost:%d/prefetch-origin.html" % server_log.getPort()
129 self.NavigateToURL(url)
130 self.assertEqual(True, server_log.isRetrieved("prefetch-origin.html"))
131 time.sleep(0.1) # required since prefetches occur after onload
132 self.assertEqual(True, server_log.isRetrieved(
133 "static-prefetch-target.html"))
134 self.assertEqual(True, server_log.isRetrieved(
135 "dynamic-prefetch-target.html"))
137 if __name__ == '__main__':
138 web_server = multiprocessing.Process(target=run_web_server,args=(queue,))
139 web_server.daemon = True
141 pyauto_functional.Main()