OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/python |
| 2 # Copyright (c) 2010 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 |
| 6 # this functional test spawns a web server, and runs chrome to point |
| 7 # at that web server. The content served contains prefetch requests, |
| 8 # and the tests assert that the webserver logs reflect that. |
| 9 |
| 10 # run like any functional test: |
| 11 # $ python chrome/test/functional/prefetch.py |
| 12 # in a repo with a built pyautolib |
| 13 |
| 14 # the import of multiprocessing implies python 2.6 is required |
| 15 |
| 16 import os |
| 17 import time |
| 18 import multiprocessing |
| 19 import Queue |
| 20 import string |
| 21 from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer |
| 22 |
| 23 import pyauto_functional # Must be imported before pyauto |
| 24 import pyauto |
| 25 |
| 26 # this class handles IPC retrieving server "logs" from our integral |
| 27 # server. Each test should clear() the log, and then run asserts on |
| 28 # the retrieval list. |
| 29 |
| 30 # at startup, the server puts an int in the queue which is its port, |
| 31 # we store that for subsequent tests |
| 32 |
| 33 class ServerLog: |
| 34 def clear(self): |
| 35 self.log = {} |
| 36 |
| 37 def __init__(self,queue): |
| 38 self.clear() |
| 39 self.port = None |
| 40 self.queue = queue |
| 41 |
| 42 def _readQueue(self): |
| 43 try: |
| 44 while True: |
| 45 queueval = self.queue.get(False) |
| 46 if isinstance(queueval,int): |
| 47 self.port = queueval |
| 48 else: |
| 49 self.log[queueval] = True |
| 50 except Queue.Empty: |
| 51 return |
| 52 |
| 53 def getPort(self): |
| 54 if not self.port: |
| 55 self._readQueue() |
| 56 return self.port |
| 57 |
| 58 def isRetrieved(self,path): |
| 59 self._readQueue() |
| 60 try: |
| 61 return self.log[path] |
| 62 except KeyError: |
| 63 return None |
| 64 |
| 65 # |
| 66 # The next few classes run a simple web server that returns log information |
| 67 # via a multiprocessing.Queue. |
| 68 # |
| 69 class AbstractPrefetchServerHandler(BaseHTTPRequestHandler): |
| 70 content = { |
| 71 "prefetch-origin.html": |
| 72 (200, """<html><head> |
| 73 <link rel="prefetch" href="static-prefetch-target.html"> |
| 74 <script type="text/javascript"> |
| 75 function changeParagraph() |
| 76 { |
| 77 var newPara = document.createElement("p"); |
| 78 newPara.innerHTML = |
| 79 "<link rel=\\"prefetch\\" href=\\"dynamic-prefetch-target.html\\">" + |
| 80 "<p>This paragraph contains a dynamic link prefetch. " + |
| 81 "The target of this prefetch is " + |
| 82 "<a href=\\"dynamic-prefetch-target.html\\">this document.</a>"; |
| 83 var para = document.getElementById("p1"); |
| 84 document.body.insertBefore(newPara,para); |
| 85 } |
| 86 </script> |
| 87 </head> |
| 88 <body onload="changeParagraph()"> |
| 89 <p id="p1">This is a document that contains a link prefetch. The target of |
| 90 that prefetch is <a href="static-prefetch-target.html">this document.</a> |
| 91 </body>"""), |
| 92 "static-prefetch-target.html": |
| 93 (200, "<html><head></head><body>empty</body>"), |
| 94 "dynamic-prefetch-target.html": |
| 95 (200, "<html><head></head><body>empty</body>")} |
| 96 |
| 97 def do_GET(self): |
| 98 self.queue.put(self.path[1:]) |
| 99 try: |
| 100 response_code, response = self.content[self.path[1:]] |
| 101 self.send_response(response_code) |
| 102 self.end_headers() |
| 103 self.wfile.write(response) |
| 104 except KeyError: |
| 105 self.send_response(404) |
| 106 self.end_headers() |
| 107 |
| 108 def run_web_server(queue_arg): |
| 109 class PrefetchServerHandler(AbstractPrefetchServerHandler): |
| 110 queue = queue_arg |
| 111 server = HTTPServer(('',0), PrefetchServerHandler) |
| 112 queue.put(server.server_port) |
| 113 server.serve_forever() |
| 114 |
| 115 # |
| 116 # Here's the test itself |
| 117 # |
| 118 queue = multiprocessing.Queue() |
| 119 server_log = ServerLog(queue) |
| 120 |
| 121 class PrefetchTest(pyauto.PyUITest): |
| 122 """Testcase for Prefetching""" |
| 123 def testBasic(self): |
| 124 server_log.clear() |
| 125 url = "http://localhost:%d/prefetch-origin.html" % server_log.getPort() |
| 126 self.NavigateToURL(url) |
| 127 self.assertEqual(True, server_log.isRetrieved("prefetch-origin.html")) |
| 128 time.sleep(0.1) # required since prefetches occur after onload |
| 129 self.assertEqual(True, server_log.isRetrieved( |
| 130 "static-prefetch-target.html")) |
| 131 self.assertEqual(True, server_log.isRetrieved( |
| 132 "dynamic-prefetch-target.html")) |
| 133 |
| 134 if __name__ == '__main__': |
| 135 web_server = multiprocessing.Process(target=run_web_server,args=(queue,)) |
| 136 web_server.daemon = True |
| 137 web_server.start() |
| 138 pyauto_functional.Main() |
OLD | NEW |