Chromium Code Reviews| Index: functional/nacl_sdk.py |
| =================================================================== |
| --- functional/nacl_sdk.py (revision 0) |
| +++ functional/nacl_sdk.py (revision 0) |
| @@ -0,0 +1,1117 @@ |
| +#!/usr/bin/python |
| +# Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
| +import copy |
| +import ctypes |
| +from distutils import version |
| +import fnmatch |
| +import glob |
| +import hashlib |
| +import os |
| +import platform |
| +import random |
| +import shutil |
| +import subprocess |
| +import sys |
| +import tarfile |
| +import time |
| +import urllib2 |
| + |
| +import pyauto_functional # Must be imported before pyauto. |
| +import pyauto |
| + |
| +class NaClSDKTest(pyauto.PyUITest): |
| + """Tests for the NaCl SDK.""" |
| + _EXTRACTED_NACL_SDL_PATH = 'extracted_nacl_sdk' |
| + _SEVENZIP_PATH = 'C:\\Program Files\\7-Zip\\7z.exe' |
|
anantha
2011/08/05 20:15:36
its not guaranteed that C drive will be available
chrisphan
2011/08/05 21:32:14
Yes, it is handled. I was told the bot does have
|
| + |
| + def testNaClSDK(self): |
| + """Verify that NaCl SDK is working properly.""" |
| + if self._HasAllSystemRequirements() == False: |
| + return |
| + |
| + # Not working ATM because SDK and Chrome version mismatch issue. |
| + #self._VerifyNaClPlugin() |
|
anantha
2011/08/05 20:15:36
Do we have bug for this?
chrisphan
2011/08/05 21:32:14
Some of these tests verify the live examples. And
|
| + |
| + self._VerifyDownloadLinks() |
| + |
| + self._VerifyNaClSDKInstaller() |
| + |
| + self._VerifyBuildStubProject() |
| + |
| + self._LaunchServerAndVerifyExamples() |
| + |
| + self._VerifyRebuildExamples() |
| + |
| + self._VerifySelldrAndNcval() |
| + |
| + def testVerifyNaClSDKChecksum(self): |
| + """Verify NaCl SDK Checksum.""" |
| + if self._HasAllSystemRequirements() == False: |
| + return |
| + |
| + settings = self._GetTestSetting() |
| + |
| + self._DownloadNaClSDK() |
| + |
| + download_dir = os.path.join(self.DataDir(), 'downloads') |
| + |
| + if pyauto.PyUITest.IsWin(): |
| + expected_shasum = settings['release_win_expected_shasum'] |
| + file_path = os.path.join(download_dir, 'naclsdk_win.exe') |
| + elif pyauto.PyUITest.IsMac(): |
| + expected_shasum = settings['release_mac_expected_shasum'] |
| + file_path = os.path.join(download_dir, 'naclsdk_mac.tgz') |
| + elif pyauto.PyUITest.IsLinux(): |
| + expected_shasum = settings['release_lin_expected_shasum'] |
| + file_path = os.path.join(download_dir, 'naclsdk_linux.tgz') |
| + else: |
| + self.fail(msg='NaCl SDK does not support this OS.') |
| + |
| + sha = hashlib.sha1() |
| + try: |
| + f = open(file_path, 'rb') |
| + sha.update(f.read()) |
| + shasum = sha.hexdigest() |
| + self.assertEqual(expected_shasum, shasum, |
| + msg='Unexpected checksum. Expected: %s, get: %s' |
| + % (expected_shasum, shasum)) |
| + except IOError: |
| + self.fail(msg='Cannot open %s.' % file_path) |
| + finally: |
| + f.close() |
| + |
| + def _testVerifyPrereleaseGallery(self): |
| + """Verify Pre-release gallery examples.""" |
| + # Not working ATM because SDK and Chrome version mismatch issue. |
| + if self._HasAllSystemRequirements() == False: |
| + return |
| + settings = self._GetTestSetting() |
| + examples = settings['prerelease_gallery'] |
| + self._OpenExamplesAndStartTest(examples) |
| + |
| + def _VerifyNaClPlugin(self): |
| + """Verify NaCl Plugin.""" |
| + settings = self._GetTestSetting() |
| + examples = settings['gallery_examples'] |
| + self._OpenExamplesAndStartTest(examples) |
| + |
| + def _VerifyDownloadLinks(self): |
|
anantha
2011/08/05 20:15:36
Can you first detect current platform and then ver
chrisphan
2011/08/05 21:32:14
This is for step 2. I believe we just need to mak
|
| + """Verify the download links.""" |
| + settings = self._GetTestSetting() |
| + win_sdk_url = settings['post_win_sdk_url'] |
| + mac_sdk_url = settings['post_mac_sdk_url'] |
| + lin_sdk_url = settings['post_lin_sdk_url'] |
| + sdk_download_url = settings['post_sdk_download_url'] |
| + |
| + self.NavigateToURL(sdk_download_url) |
| + html = self.GetTabContents() |
| + |
| + # Make sure the correct URL is under the correct label. |
| + win_url_index = html.find(win_sdk_url) |
| + self.assertTrue(win_url_index > -1, |
| + msg='Missing SDK download URL: %s' % win_sdk_url) |
| + mac_url_index = html.find(mac_sdk_url) |
| + self.assertTrue(mac_url_index > -1, |
| + msg='Missing SDK download URL: %s' % mac_sdk_url) |
| + lin_url_index = html.find(lin_sdk_url) |
| + self.assertTrue(lin_url_index > -1, |
| + msg='Missing SDK download URL: %s' % lin_sdk_url) |
| + |
| + win_keyword_index = html.rfind('Windows', 0, win_url_index) |
| + self.assertTrue(win_keyword_index > -1, |
| + msg='Misplace download link: %s' % win_sdk_url) |
| + mac_keyword_index = html.rfind('Macintosh', 0, mac_url_index) |
| + self.assertTrue(mac_keyword_index > -1, |
| + msg='Misplace download link: %s' % mac_sdk_url) |
| + lin_keyword_index = html.rfind('Linux', 0, lin_url_index) |
| + self.assertTrue(lin_keyword_index > -1, |
| + msg='Misplace download link: %s' % lin_sdk_url) |
| + |
| + def _VerifyNaClSDKInstaller(self): |
| + """Verify NaCl SDK installer.""" |
| + search_list = [ |
| + 'build.scons', |
| + 'favicon.ico', |
| + 'geturl/', |
| + 'hello_world/', |
| + 'hello_world_c/', |
| + 'httpd.py', |
| + 'index.html', |
| + 'nacl_sdk_scons/', |
| + 'pi_generator/', |
| + 'scons', |
| + 'sine_synth/' |
| + ] |
| + |
| + mac_lin_additional_search_items = [ |
| + 'sel_ldr_x86_32', |
| + 'sel_ldr_x86_64', |
| + 'ncval_x86_32', |
| + 'ncval_x86_64' |
| + ] |
| + |
| + win_additional_search_items = [ |
| + 'httpd.cmd', |
| + 'sel_ldr_x86_32.exe', |
| + 'sel_ldr_x86_64.exe', |
| + 'ncval_x86_32.exe', |
| + 'ncval_x86_64.exe' |
| + ] |
| + |
| + download_dir = os.path.join(self.DataDir(), 'downloads') |
| + |
| + self._DownloadNaClSDK() |
| + |
| + if pyauto.PyUITest.IsWin(): |
| + source_file = os.path.join(download_dir, 'naclsdk_win.exe') |
| + destination = os.path.join(download_dir, 'naclsdk_win') |
| + self._SearchNaClSDKFileWindows( |
| + search_list + win_additional_search_items, |
| + source_file, destination) |
| + elif pyauto.PyUITest.IsMac(): |
| + source_file = os.path.join(download_dir, 'naclsdk_mac.tgz') |
| + self._SearchNaClSDKTarFile(search_list + mac_lin_additional_search_items, |
| + source_file) |
| + elif pyauto.PyUITest.IsLinux(): |
| + source_file = os.path.join(download_dir, 'naclsdk_linux.tgz') |
| + self._SearchNaClSDKTarFile(search_list + mac_lin_additional_search_items, |
| + source_file) |
| + else: |
| + self.fail(msg='NaCl SDK does not support this OS.') |
| + |
| + self._ExtractNaClSDK() |
| + |
| + def _VerifyBuildStubProject(self): |
| + """Build stub project.""" |
| + stub_project_files = [ |
| + 'build.scons', |
| + 'scons' |
| + ] |
| + |
| + download_dir = os.path.join(self.DataDir(), 'downloads') |
| + sdk_path = os.path.join(download_dir, self._EXTRACTED_NACL_SDL_PATH) |
| + project_template_path = self._GetDirectoryPath('project_templates', |
| + sdk_path) |
| + examples_path = self._GetDirectoryPath('examples', sdk_path) |
| + init_project_path = os.path.join(project_template_path, 'init_project.py') |
| + |
| + # Build a c project. |
| + proc = subprocess.Popen( |
| + ['python', init_project_path, '-n', 'hello_c', '-c', '-d', |
| + examples_path], stdout=subprocess.PIPE) |
| + proc.communicate() |
| + |
| + hello_c_path = os.path.join(examples_path, 'hello_c') |
| + for file in stub_project_files: |
| + self.assertTrue(self._HasFile(file, hello_c_path), |
| + msg='Cannot build hello_c stub project.') |
| + |
| + # Build a cc project. |
| + proc = subprocess.Popen( |
| + ['python', init_project_path, '-n', 'hello_cc', '-c', '-d', |
| + examples_path], stdout=subprocess.PIPE) |
| + proc.communicate() |
| + |
| + hello_cc_path = os.path.join(examples_path, 'hello_cc') |
| + for file in stub_project_files: |
| + self.assertTrue(self._HasFile(file, hello_cc_path), |
| + msg='Cannot build hello_cc stub project.') |
| + |
| + def _LaunchServerAndVerifyExamples(self): |
| + """Start local HTTP server and verify examples.""" |
| + download_dir = os.path.join(self.DataDir(), 'downloads') |
| + sdk_path = os.path.join(download_dir, self._EXTRACTED_NACL_SDL_PATH) |
| + examples_path = self._GetDirectoryPath('examples', sdk_path) |
| + |
| + # Make sure server is not open. |
| + if not self._IsURLAlive('http://localhost:5103'): |
| + self._CloseHTTPServer() |
| + |
| + # Start HTTP server. |
| + if pyauto.PyUITest.IsWin(): |
| + http_path = os.path.join(examples_path, 'httpd.cmd') |
| + proc = subprocess.Popen([http_path], cwd=examples_path) |
| + else: |
| + http_path = os.path.join(examples_path, 'httpd.py') |
| + proc = subprocess.Popen(['python', http_path], cwd=examples_path) |
| + |
| + self.NavigateToURL('http://localhost:5103') |
| + self.assertTrue(self._IsURLAlive('http://localhost:5103'), |
| + msg='Cannot open HTTP server. %s' % |
| + self.GetActiveTabTitle()) |
| + |
| + examples = { |
| + 'hello_world_c': 'http://localhost:5103/hello_world_c/' |
| + 'hello_world.html', |
| + 'hello_world': 'http://localhost:5103/hello_world/hello_world.html', |
| + 'geturl': 'http://localhost:5103/geturl/geturl.html', |
| + 'pi_generator': 'http://localhost:5103/pi_generator/pi_generator.html', |
| + 'sine_synth': 'http://localhost:5103/sine_synth/sine_synth.html', |
| + } |
| + try: |
| + self._OpenExamplesAndStartTest(examples) |
| + finally: |
| + self._CloseHTTPServer(proc) |
| + |
| + def _VerifyRebuildExamples(self): |
| + """Re-build the examples.""" |
| + download_dir = os.path.join(self.DataDir(), 'downloads') |
| + sdk_path = os.path.join(download_dir, self._EXTRACTED_NACL_SDL_PATH) |
| + examples_path = self._GetDirectoryPath('examples', sdk_path) |
| + scons_path = os.path.join(examples_path, 'scons -c') |
| + |
| + example_dirs = [ |
| + 'geturl', |
| + 'hello_world', |
| + 'hello_world_c', |
| + 'pi_generator', |
| + 'sine_synth' |
| + ] |
| + |
| + proc = subprocess.Popen([scons_path], cwd=examples_path, shell=True) |
| + proc.communicate() |
| + for x in example_dirs: |
| + ex_path = os.path.join(examples_path, x) |
| + if self._HasFile('*.nmf', ex_path): |
| + self.fail(msg='Failed running scons -c.') |
| + |
| + scons_path = os.path.join(examples_path, 'scons') |
| + proc = subprocess.Popen([scons_path], cwd=examples_path, |
| + stdout=subprocess.PIPE, shell=True) |
| + proc.communicate() |
| + |
| + # Verify each example directory contains .nmf file. |
| + for dir in example_dirs: |
| + dir = os.path.join(examples_path, dir) |
| + if not self._HasFile('*.nmf', dir): |
| + self.fail(msg='Failed running scons.') |
| + |
| + self._LaunchServerAndVerifyExamples() |
| + |
| + def _VerifySelldrAndNcval(self): |
| + """Verify sel_ldr and ncval.""" |
| + download_dir = os.path.join(self.DataDir(), 'downloads') |
| + sdk_path = os.path.join(download_dir, self._EXTRACTED_NACL_SDL_PATH) |
| + examples_path = self._GetDirectoryPath('examples', sdk_path) |
| + |
| + architecture = self._GetPlatformArchitecture() |
| + scons_arg = None |
| + if pyauto.PyUITest.IsWin(): |
| + if architecture == '64bit': |
| + scons_arg = 'test64' |
| + else: |
| + scons_arg = 'test32' |
| + elif pyauto.PyUITest.IsMac(): |
| + scons_arg = 'test64' |
| + elif pyauto.PyUITest.IsLinux(): |
| + scons_arg = 'test64' |
| + scons_path = os.path.join(examples_path, 'scons ' + scons_arg) |
| + |
| + # Build and run the unit test. |
| + proc = subprocess.Popen([scons_path], stdout=subprocess.PIPE, |
| + shell=True, cwd=examples_path) |
| + stdout = proc.communicate()[0] |
| + lines = stdout.splitlines() |
| + test_ran = False |
| + for line in lines: |
| + if 'Check:' in line: |
| + self.assertTrue('passed' in line, |
| + msg='Nacl-sel_ldr unit test failed.') |
| + test_ran = True |
| + self.assertTrue(test_ran, |
| + msg='Failed to build and run nacl-sel_ldr unit test.') |
| + |
| + if architecture == '64bit': |
| + if not self._HasFileInTree('hello_world_x86_64.nexe', examples_path): |
| + self.fail(msg='Missing file: hello_world_x86_64.nexe.') |
| + else: |
| + if not self._HasFileInTree('hello_world_x86_32.nexe', examples_path): |
| + self.fail(msg='Missing file: hello_world_x86_32.nexe.') |
| + |
| + # Verify that a mismatch of sel_ldr and .nexe produces an error. |
| + toolchain_path = self._GetDirectoryPath('toolchain', sdk_path) |
| + bin_path = self._GetDirectoryPath('bin', toolchain_path) |
| + hello_world_path = self._GetDirectoryPath('hello_world', examples_path) |
| + sel_32_path = os.path.join(bin_path, 'sel_ldr_x86_32') |
| + sel_64_path = os.path.join(bin_path, 'sel_ldr_x86_64') |
| + nexe_32_path = os.path.join(hello_world_path, 'hello_world_x86_32.nexe') |
| + nexe_64_path = os.path.join(hello_world_path, 'hello_world_x86_64.nexe') |
| + |
| + success = False |
| + if architecture == '64bit': |
| + success = self._RunProcessAndCheckOutput( |
| + [sel_64_path, nexe_32_path], 'Error while loading') |
| + else: |
| + success = self._RunProcessAndCheckOutput( |
| + [sel_32_path, nexe_64_path], 'Error while loading') |
| + self.assertTrue(success, |
| + msg='Failed to verify sel_ldr and .nexe mismatch.') |
| + |
| + # Run the appropriate ncval for your platform on the matching .nexe. |
| + ncval_32_path = os.path.join(bin_path, 'ncval_x86_32') |
| + ncval_64_path = os.path.join(bin_path, 'ncval_x86_64') |
| + success = False |
| + if architecture == '64bit': |
| + success = self._RunProcessAndCheckOutput( |
| + [ncval_64_path, nexe_64_path], 'is safe') |
| + else: |
| + success = self._RunProcessAndCheckOutput( |
| + [ncval_32_path, nexe_32_path], 'is safe') |
| + self.assertTrue(success, msg='Failed to verify ncval.') |
| + |
| + # Verify that a mismatch of ncval and .nexe produces an error. |
| + success = False |
| + if architecture == '64bit': |
| + success = self._RunProcessAndCheckOutput( |
| + [ncval_64_path, nexe_32_path], 'is safe', is_in=False) |
| + else: |
| + success = self._RunProcessAndCheckOutput( |
| + [ncval_32_path, nexe_64_path], 'is safe', is_in=False) |
| + self.assertTrue(success, msg='Failed to verify ncval and .nexe mismatch.') |
| + |
| + def _RunProcessAndCheckOutput(self, args, look_for, is_in=True): |
| + """Run process and look for string in output. |
| + |
| + Args: |
| + args: Argument strings to pass to subprocess. |
| + look_for: The string to search in output. |
| + is_in: True if checking if param look_for is in output. |
| + False if checking if param look_out is not in output. |
| + |
| + Returns: |
| + True, if output contains parameter look_for, or |
| + False otherwise. |
| + """ |
| + proc = subprocess.Popen(args, stdout=subprocess.PIPE, |
| + stderr=subprocess.PIPE) |
| + (stdout, stderr) = proc.communicate() |
| + lines = stdout.splitlines() |
| + for line in lines: |
| + if look_for in line: |
| + if is_in: |
| + return True |
| + else: |
| + return False |
| + |
| + lines = stderr.splitlines() |
| + for line in lines: |
| + if look_for in line: |
| + if is_in: |
| + return True |
| + else: |
| + return False |
| + if is_in: |
| + return False |
| + else: |
| + return True |
| + |
| + def _OpenExamplesAndStartTest(self, examples): |
| + """Open each example and verify that it's working. |
| + |
| + Args: |
| + examples: A dict of name to url of examples. |
| + """ |
| + self._EnableNaClPlugin() |
| + |
| + # Open all examples. |
| + for name, url in examples.items(): |
| + self.AppendTab(pyauto.GURL(url)) |
| + self._CheckForCrashes() |
| + |
| + # Verify all examples are working. |
| + for name, url in examples.items(): |
| + self._VerifyAnExample(name, url) |
| + self._CheckForCrashes() |
| + |
| + # Reload all examples. |
| + for _ in range(1): |
| + for tab_index in range(self.GetTabCount()): |
| + self.GetBrowserWindow(0).GetTab(tab_index).Reload() |
| + self._CheckForCrashes() |
| + |
| + # Verify all examples are working. |
| + for name, url in examples.items(): |
| + self._VerifyAnExample(name, url) |
| + self._CheckForCrashes() |
| + |
| + # Randomly close a tab, check for crashes and verify all open |
| + # examples operate correctly. |
| + tab_count = self.GetTabCount() |
| + for index in xrange(tab_count - 1, 0, -1): |
| + tab_index = None |
| + if index > 1: |
| + tab_index = random.randint(1, index) |
| + else: |
| + tab_index = 1 |
| + |
| + self.GetBrowserWindow(0).GetTab(tab_index).Close(True) |
| + self._CheckForCrashes() |
| + |
| + tabs = self.GetBrowserInfo()['windows'][0]['tabs'] |
| + for tab in tabs: |
| + if tab['index'] > 0: |
| + for name, url in examples.items(): |
| + if url == tab['url']: |
| + self._VerifyAnExample(name, url) |
| + break |
| + |
| + def _VerifyAnExample(self, name, url): |
| + """Verify NaCl Example is working. |
| + |
| + Args: |
| + name: A string name of the example. |
| + url: A string url of the example. |
| + """ |
| + available_example_tests = { |
| + 'hello_world_c': self._VerifyHelloWorldExample, |
| + 'hello_world': self._VerifyHelloWorldExample, |
| + 'pi_generator': self._VerifyPiGeneratorExample, |
| + 'sine_synth': self._VerifySineSynthExample, |
| + 'geturl': self._VerifyGetURLExample, |
| + 'life': self._VerifyConwaysLifeExample |
| + } |
| + |
| + if not name in available_example_tests: |
| + self.fail(msg='No test available for %s.' % name) |
| + |
| + info = self.GetBrowserInfo() |
| + tabs = info['windows'][0]['tabs'] |
| + tab_index = None |
| + for tab in tabs: |
| + if url == tab['url']: |
| + self.ActivateTab(tab['index']) |
| + tab_index = tab['index'] |
| + break |
| + |
| + if tab_index != None: |
| + available_example_tests[name](tab_index, name, url) |
| + |
| + def _VerifyHelloWorldExample(self, tab_index, name, url): |
| + """Verify Hello World Example. |
| + |
| + Args: |
| + tab_index: Tab index integer that the example is on. |
| + name: A string name of the example. |
| + url: A string url of the example. |
| + """ |
| + wait_for = 60 |
| + success = self.WaitUntil( |
| + lambda: self.GetDOMValue( |
| + 'document.getElementById("statusField").innerHTML', |
| + 0, tab_index), |
| + wait_for, expect_retval='SUCCESS') |
| + self.assertTrue(success, msg='Example %s failed. URL: %s' % (name, url)) |
| + |
| + js_code = """ |
| + window.alert = function(e) { |
| + window.domAutomationController.send(String(e)); |
| + } |
| + window.domAutomationController.send("done"); |
| + """ |
| + self.ExecuteJavascript(js_code, 0, tab_index) |
| + |
| + result = self.ExecuteJavascript('document.helloForm.elements[1].click();', |
| + 0, tab_index) |
| + self.assertEqual(result, '42', |
| + msg='Example %s failed. URL: %s' % (name, url)) |
| + |
| + result = self.ExecuteJavascript('document.helloForm.elements[2].click();', |
| + 0, tab_index) |
| + self.assertEqual(result, 'dlrow olleH', |
| + msg='Example %s failed. URL: %s' % (name, url)) |
| + |
| + def _VerifyPiGeneratorExample(self, tab_index, name, url): |
| + """Verify Pi Generator Example. |
| + |
| + Args: |
| + tab_index: Tab index integer that the example is on. |
| + name: A string name of the example. |
| + url: A string url of the example. |
| + """ |
| + wait_for = 120 |
| + success = self.WaitUntil( |
| + lambda: self.GetDOMValue('document.form.pi.value', 0, tab_index)[0:3], |
| + wait_for, expect_retval='3.1') |
| + self.assertTrue(success, msg='Example %s failed. URL: %s' % (name, url)) |
| + |
| + # Get top corner of pi image. |
| + js_code = """ |
| + var obj = document.getElementById('piGenerator'); |
| + var curleft = curtop = 0; |
| + do { |
| + curleft += obj.offsetLeft; |
| + curtop += obj.offsetTop; |
| + } while (obj = obj.offsetParent); |
| + window.domAutomationController.send(curleft + "," + curtop); |
| + """ |
| + result = self.ExecuteJavascript(js_code, 0, tab_index) |
| + result_split = result.split(",") |
| + x = int(result_split[0]) |
| + y = int(result_split[1]) |
| + window = self.GetBrowserInfo()['windows'][0] |
| + window_to_content_x = 2 |
| + window_to_content_y = 80 |
| + pi_image_x = x + window['x'] + window_to_content_x |
| + pi_image_y = y + window['y'] + window_to_content_y |
| + |
| + if self._IsGetPixelSupported(): |
| + is_animating = self._IsColorChanging(pi_image_x, pi_image_y, 50, 50) |
| + self.assertTrue(is_animating, |
| + msg='Example %s failed. URL: %s' % (name, url)) |
| + |
| + def _VerifySineSynthExample(self, tab_index, name, url): |
| + """Verify Sine Wave Synthesizer Example. |
| + |
| + Args: |
| + tab_index: Tab index integer that the example is on. |
| + name: A string name of the example. |
| + url: A string url of the example. |
| + """ |
| + success = self.WaitUntil( |
| + lambda: self.GetDOMValue( |
| + 'document.getElementById("frequency_field").value', |
| + 0, tab_index), |
| + timeout=30, expect_retval='440') |
| + self.assertTrue(success, msg='Example %s failed. URL: %s' % (name, url)) |
| + |
| + self.ExecuteJavascript( |
| + 'document.body.getElementsByTagName("button")[0].click();' |
| + 'window.domAutomationController.send("done")', |
| + 0, tab_index) |
| + |
| + # TODO(chrisphan): Verify sound. |
| + |
| + def _VerifyGetURLExample(self, tab_index, name, url): |
| + """Verify GetURL Example. |
| + |
| + Args: |
| + tab_index: Tab index integer that the example is on. |
| + name: A string name of the example. |
| + url: A string url of the example. |
| + """ |
| + success = self.WaitUntil( |
| + lambda: self.GetDOMValue( |
| + 'document.getElementById("status_field").innerHTML', |
| + 0, tab_index), |
| + timeout=60, expect_retval='SUCCESS') |
| + self.assertTrue(success, msg='Example %s failed. URL: %s' % (name, url)) |
| + |
| + self.ExecuteJavascript( |
| + 'document.geturl_form.elements[0].click();' |
| + 'window.domAutomationController.send("done")', |
| + 0, tab_index) |
| + |
| + js_code = """ |
| + var output = document.getElementById("general_output").innerHTML; |
| + var result; |
| + if (output.indexOf("test passed") != -1) |
| + result = "pass"; |
| + else |
| + result = "fail"; |
| + window.domAutomationController.send(result); |
| + """ |
| + success = self.WaitUntil( |
| + lambda: self.ExecuteJavascript(js_code, 0, tab_index), |
| + timeout=30, expect_retval='pass') |
| + self.assertTrue(success, msg='Example %s failed. URL: %s' % (name, url)) |
| + |
| + def _VerifyConwaysLifeExample(self, tab_index, name, url): |
| + """Verify Conway's Life Example. |
| + |
| + Args: |
| + tab_index: Tab index integer that the example is on. |
| + name: A string name of the example. |
| + url: A string url of the example. |
| + """ |
| + window = self.GetBrowserInfo()['windows'][0] |
| + window_to_content_x = 2 |
| + window_to_content_y = 80 |
| + x = window['x'] + window_to_content_x |
| + y = window['y'] + window_to_content_y |
| + offset_pixel = 100 |
| + if self._IsGetPixelSupported(): |
| + wait_for = 30 |
| + success = self.WaitUntil( |
| + lambda: self._GetPixel(x + offset_pixel, y + offset_pixel), |
| + wait_for, expect_retval=16777215) |
| + self.assertTrue(success, msg='Example %s failed. URL: %s' % (name, url)) |
| + |
| + def _IsColorChanging(self, x, y, width, height, tries=3, retry_sleep=2): |
| + """Check screen for anything that is moving. |
| + |
| + Args: |
| + x: X coordinate on the screen. |
| + y: Y coordinate on the screen. |
| + width: Width of the area to look scan. |
| + height: Height of the area to look scan. |
| + tries: Number of tries. |
| + retry_sleep: Sleep time in between each tries. |
| + |
| + Returns: |
| + True, if pixel color in area is changing, or |
| + False otherwise. |
| + """ |
| + color_a = self._GetAreaPixelColor(x, y, width, height) |
| + for _ in xrange(tries): |
| + time.sleep(retry_sleep) |
| + color_b = self._GetAreaPixelColor(x, y, width, height) |
| + if color_a != color_b: |
| + return True |
| + return False |
| + |
| + def _IsGetPixelSupported(self): |
| + """Check if get pixel is supported. |
| + |
| + Returns: |
| + True, if get pixel is supported, or |
| + False otherwise. |
| + """ |
| + if pyauto.PyUITest.IsWin(): |
| + return True |
| + else: |
| + return False |
| + |
| + def _GetAreaPixelColor(self, x, y, width, height): |
| + """Get an area of pixel color and return a list. |
| + |
| + Args: |
| + x: X coordinate on the screen. |
| + y: Y coordinate on the screen. |
| + width: Width of the area to look scan. |
| + height: Height of the area to look scan. |
| + |
| + Returns: |
| + A list containing color codes. |
| + """ |
| + if pyauto.PyUITest.IsMac(): |
| + pass # TODO(chrisphan): Do Mac. |
| + elif pyauto.PyUITest.IsWin(): |
| + return self._GetAreaPixelColorWin(x, y, width, height) |
| + elif pyauto.PyUITest.IsLinux(): |
| + pass # TODO(chrisphan): Do Linux. |
| + return None |
| + |
| + def _GetAreaPixelColorWin(self, x, y, width, height): |
| + """Get an area of pixel color for Windows and return a list. |
| + |
| + Args: |
| + x: X coordinate on the screen. |
| + y: Y coordinate on the screen. |
| + width: Width of the area to look scan. |
| + height: Height of the area to look scan. |
| + |
| + Returns: |
| + A list containing color codes. |
| + """ |
| + colors = [] |
| + hdc = ctypes.windll.user32.GetDC(0) |
| + for x_pos in xrange(x, x + width, 1): |
| + for y_pos in xrange(y, y + height, 1): |
| + color = ctypes.windll.gdi32.GetPixel(hdc, x_pos, y_pos) |
| + colors.append(color) |
| + return colors |
| + |
| + def _GetPixel(self, x, y): |
| + """Get pixel color at coordinate x and y. |
| + |
| + Args: |
| + x: X coordinate on the screen. |
| + y: Y coordinate on the screen. |
| + |
| + Returns: |
| + An integer color code. |
| + """ |
| + if pyauto.PyUITest.IsMac(): |
| + pass # TODO(chrisphan): Do Mac. |
| + elif pyauto.PyUITest.IsWin(): |
| + return self._GetPixelWin(x, y) |
| + elif pyauto.PyUITest.IsLinux(): |
| + pass # TODO(chrisphan): Do Linux. |
| + return None |
| + |
| + def _GetPixelWin(self, x, y): |
| + """Get pixel color at coordinate x and y for Windows |
| + |
| + Args: |
| + x: X coordinate on the screen. |
| + y: Y coordinate on the screen. |
| + |
| + Returns: |
| + An integer color code. |
| + """ |
| + hdc = ctypes.windll.user32.GetDC(0) |
| + color = ctypes.windll.gdi32.GetPixel(hdc, x, y) |
| + return color |
| + |
| + def _CheckForCrashes(self, last_action=None, last_action_param=None): |
| + """Check for any browser/tab crashes and hangs. |
| + |
| + Args: |
| + last_action: Specify action taken before checking for crashes. |
| + last_action_param: Parameter for last action. |
| + """ |
| + self.assertTrue(self.GetBrowserWindowCount(), |
| + msg='Browser crashed, no window is open.') |
| + |
| + info = self.GetBrowserInfo() |
| + breakpad_folder = info['properties']['DIR_CRASH_DUMPS'] |
| + old_dmp_files = glob.glob(os.path.join(breakpad_folder, '*.dmp')) |
| + |
| + # Verify there're no crash dump files |
| + for dmp_file in glob.glob(os.path.join(breakpad_folder, '*.dmp')): |
| + self.assertTrue(dmp_file in old_dmp_files, |
| + msg='Crash dump %s found' % dmp_file) |
| + |
| + # Check for any crashed tabs. |
| + tabs = info['windows'][0]['tabs'] |
| + for tab in tabs: |
| + if tab['url'] != 'about:blank': |
| + if self.GetDOMValue('document.body.innerHTML', 0, tab['index']) == '': |
| + self.fail(msg='Tab crashed on %s' % tab['url']) |
| + |
| + # TODO(chrisphan): check for tab hangs and browser hangs |
| + # TODO(chrisphan): handle specific action: close browser, close tab |
| + if last_action == 'close tab': |
| + pass |
| + elif last_action == 'close browser': |
| + pass |
| + |
| + def _GetPlatformArchitecture(self): |
| + """Get platform architecture. |
| + |
| + Args: |
| + last_action: Specify action taken before checking for crashes. |
| + last_action_param: Parameter for last action. |
| + |
| + Returns: |
| + A string representing the platform architecture. |
| + """ |
| + architecture = platform.architecture()[0] |
| + if pyauto.PyUITest.IsWin(): |
| + if os.environ['PROGRAMFILES'] == 'C:\\Program Files (x86)': |
| + return '64bit' |
| + else: |
| + return '32bit' |
| + elif pyauto.PyUITest.IsMac() or pyauto.PyUITest.IsLinux(): |
| + return architecture |
| + return None |
| + |
| + def _HasFile(self, pattern, root=os.curdir): |
| + """Check if file exist in directory. |
| + |
| + Args: |
| + pattern: Pattern of file name. |
| + root: Directory to start looking. |
| + |
| + Returns: |
| + True, if root contains the file name pattern, or |
| + False otherwise. |
| + """ |
| + if len(glob.glob(os.path.join(root, pattern))) > 0: |
| + return True |
| + return False |
| + |
| + def _HasFileInTree(self, pattern, root=os.curdir): |
| + """Check if file exist in directory. |
| + |
| + Args: |
| + pattern: Pattern of file name. |
| + root: Directory to start looking. |
| + |
| + Returns: |
| + True, if root contains the file name pattern, or |
| + False otherwise. |
| + """ |
| + for path, dirs, files in os.walk(os.path.abspath(root)): |
| + if len(fnmatch.filter(files, pattern)) > 0: |
| + return True |
| + return False |
| + |
| + def _HasDir(self, pattern, root=os.curdir): |
| + """Check if directory exist in another directory. |
| + |
| + Args: |
| + pattern: Pattern of directory name. |
| + root: Directory to start looking. |
| + |
| + Returns: |
| + True, if root contains the directory name pattern, or |
| + False otherwise. |
| + """ |
| + for path, dirs, files in os.walk(os.path.abspath(root)): |
| + if len(fnmatch.filter(dirs, pattern)) > 0: |
| + return True |
| + return False |
| + |
| + def _GetDirectoryPath(self, pattern, root=os.curdir): |
| + """Get the path of a directory in another directory. |
| + |
| + Args: |
| + pattern: Pattern of directory name. |
| + root: Directory to start looking. |
| + |
| + Returns: |
| + A string of the path. |
| + """ |
| + for path, dirs, files in os.walk(os.path.abspath(root)): |
| + result = fnmatch.filter(dirs, pattern) |
| + if len(result) > 0: |
| + return os.path.join(path, result[0]) |
| + return None |
| + |
| + def _HasAllSystemRequirements(self): |
| + """Verify NaCl SDK Installation system requirements. |
| + |
| + Returns: |
| + True, if system passed requirements, or |
| + False otherwise. |
| + """ |
| + # Check python version. |
| + if sys.version_info[0:2] < (2, 5): |
| + return False |
| + |
| + # Check OS requirements. |
| + if pyauto.PyUITest.IsMac(): |
| + mac_min_version = version.StrictVersion('10.6') |
| + mac_version = version.StrictVersion(platform.mac_ver()[0]) |
| + if mac_version < mac_min_version: |
| + return False |
| + elif pyauto.PyUITest.IsWin(): |
| + pass # TODO(chrisphan): Check Win requirements. |
| + elif pyauto.PyUITest.IsLinux(): |
| + pass # TODO(chrisphan): Check Lin requirements. |
| + else: |
| + return False |
| + |
| + # Check for Chrome version compatibility. |
| + # Nacl supports Chrome 10 and higher builds. |
| + settings = self._GetTestSetting() |
| + min_required_chrome_build = settings['min_required_chrome_build'] |
| + browser_info = self.GetBrowserInfo() |
| + chrome_version = browser_info['properties']['ChromeVersion'] |
| + chrome_build = int(chrome_version.split('.')[0]) |
| + if chrome_build < min_required_chrome_build: |
| + return False |
| + |
| + return True |
| + |
| + def _DownloadNaClSDK(self): |
| + """Download NaCl SDK.""" |
| + settings = self._GetTestSetting() |
| + win_sdk_url = settings['release_win_sdk_url'] |
| + mac_sdk_url = settings['release_mac_sdk_url'] |
| + lin_sdk_url = settings['release_lin_sdk_url'] |
| + |
| + download_dir = os.path.join(self.DataDir(), 'downloads') |
| + |
| + if pyauto.PyUITest.IsWin(): |
| + dl_file = urllib2.urlopen(win_sdk_url) |
| + file_path = os.path.join(download_dir, 'naclsdk_win.exe') |
| + elif pyauto.PyUITest.IsMac(): |
| + dl_file = urllib2.urlopen(mac_sdk_url) |
| + file_path = os.path.join(download_dir, 'naclsdk_mac.tgz') |
| + elif pyauto.PyUITest.IsLinux(): |
| + dl_file = urllib2.urlopen(lin_sdk_url) |
| + file_path = os.path.join(download_dir, 'naclsdk_linux.tgz') |
| + else: |
| + self.fail(msg='NaCl SDK does not support this OS.') |
| + |
| + try: |
| + f = open(file_path, 'wb') |
| + f.write(dl_file.read()) |
| + except IOError: |
| + self.fail(msg='Cannot open %s.' % file_path) |
| + finally: |
| + f.close() |
| + |
| + |
| + def _ExtractNaClSDK(self): |
| + """Extract NaCl SDK.""" |
| + download_dir = os.path.join(self.DataDir(), 'downloads') |
| + destination = os.path.join(download_dir, self._EXTRACTED_NACL_SDL_PATH) |
| + if os.path.exists(destination): |
| + shutil.rmtree(destination) |
| + os.makedirs(destination) |
| + |
| + if pyauto.PyUITest.IsWin(): |
| + # Requires 7-Zip to extract self-install archive. |
| + seven_z_file_path = self._Get7ZipPath() |
| + self.assertNotEqual(seven_z_file_path, None, |
| + msg='7-Zip is required but could not be found.') |
| + |
| + source_file = os.path.join(download_dir, 'naclsdk_win.exe') |
| + proc = subprocess.Popen( |
| + [seven_z_file_path, 'x', source_file, '-o' + destination], |
| + stdout=subprocess.PIPE) |
| + proc.communicate() |
| + elif pyauto.PyUITest.IsMac(): |
| + source_file = os.path.join(download_dir, 'naclsdk_mac.tgz') |
| + tar = tarfile.open(source_file, 'r') |
| + tar.extractall(destination) |
| + elif pyauto.PyUITest.IsLinux(): |
| + source_file = os.path.join(download_dir, 'naclsdk_linux.tgz') |
| + tar = tarfile.open(source_file, 'r') |
| + tar.extractall(destination) |
| + else: |
| + self.fail(msg='NaCl SDK does not support this OS.') |
| + |
| + def _Get7ZipPath(self): |
| + """Check if 7-Zip is installed. |
| + |
| + Returns: |
| + String path of 7-Zip. |
| + None if not exist. |
| + """ |
| + if os.path.isfile(self._SEVENZIP_PATH): |
| + return self._SEVENZIP_PATH |
| + |
| + current_dir = os.path.dirname(__file__) |
| + seven_z_file_path = os.path.join( |
| + current_dir, os.pardir, os.pardir, |
| + os.pardir, 'third_party', '7-Zip', '7z.exe') |
| + |
| + if os.path.isfile(seven_z_file_path): |
| + return seven_z_file_path |
| + return None |
| + |
| + def _IsURLAlive(self, url): |
| + """Test if URL is alive.""" |
| + try: |
| + urllib2.urlopen(url) |
| + except: |
| + return False |
| + return True |
| + |
| + def _CloseHTTPServer(self, proc=None): |
| + """Close HTTP server. |
| + |
| + Args: |
| + proc: Process that opened the HTTP server. |
| + """ |
| + if not self._IsURLAlive('http://localhost:5103'): |
| + return |
| + urllib2.urlopen('http://localhost:5103?quit=1') |
| + wait_for = 30 |
| + success = self.WaitUntil( |
| + lambda: self._IsURLAlive('http://localhost:5103'), |
| + timeout=wait_for, expect_retval=False) |
| + if not success: |
| + if proc == None: |
| + self.fail(msg='Fail to close HTTP server') |
| + else: |
| + if proc.poll() == None: |
| + try: |
| + proc.kill() |
| + except: |
| + pass |
| + |
| + def _SearchNaClSDKTarFile(self, search_list, source_file): |
| + """Search NaCl SDK tar file for example files and directories. |
| + |
| + Args: |
| + search_list: A list of strings, representing file and |
| + directory names for which to search. |
| + source_file: The string name of an NaCl SDK tar file. |
| + """ |
| + tar = tarfile.open(source_file, 'r') |
| + |
| + # Look for files and directories in examples. |
| + files = copy.deepcopy(search_list) |
| + for tar_info in tar: |
| + file_name = tar_info.name |
| + if tar_info.isdir() and not file_name.endswith('/'): |
| + file_name = file_name + '/' |
| + |
| + for name in files: |
| + if file_name.find('examples/' + name): |
| + files.remove(name) |
| + if len(files) == 0: |
| + break |
| + |
| + tar.close() |
| + |
| + self.assertEqual(len(files), 0, |
| + msg='Missing files or directories: %s' % |
| + ', '.join(map(str, files))) |
| + |
| + def _SearchNaClSDKFileWindows(self, search_list, source_file, destination): |
| + """Search NaCl SDK file for example files and directories. |
| + |
| + Args: |
| + search_list: A list of strings, representing file and |
| + directory names for which to search. |
| + source_file: The string name of an NaCl SDK Windows |
| + self-install archive file. |
| + """ |
| + files = [] |
| + for i in xrange(len(search_list)): |
| + files.append(search_list[i].replace(r'/', '\\')) |
| + |
| + # Requires 7-Zip to look at self-install archive. |
| + seven_z_file_path = self._Get7ZipPath() |
| + self.assertNotEqual(seven_z_file_path, None, |
| + msg='7-Zip is required but could not be found.') |
| + |
| + proc = subprocess.Popen([seven_z_file_path, 'l', source_file], |
| + stdout=subprocess.PIPE) |
| + stdout = proc.communicate()[0] |
| + lines = stdout.splitlines() |
| + for x in lines: |
| + item_name = x.split(' ')[-1] |
| + for name in files: |
| + if item_name.find(name) > 0: |
| + files.remove(name) |
| + if len(files) == 0: |
| + break |
| + |
| + self.assertEqual(len(files), 0, |
| + msg='Missing files or directories: %s' % |
| + ', '.join(map(str, files))) |
| + |
| + def _EnableNaClPlugin(self): |
| + """"Enable NaCl Plugin.""" |
| + nacl_plugin = self.GetPluginsInfo().PluginForName('Chrome NaCl') |
| + if len(nacl_plugin) == 0: |
| + nacl_plugin = self.GetPluginsInfo().PluginForName('Native Client') |
| + if len(nacl_plugin) == 0: |
| + self.fail(msg='No NaCl Plugin found.') |
| + self.EnablePlugin(nacl_plugin[0]['path']) |
| + |
| + self.NavigateToURL('about:flags') |
| + |
| + js_code = """ |
| + chrome.send('enableFlagsExperiment', ['enable-nacl', 'true']); |
| + requestFlagsExperimentsData(); |
| + window.domAutomationController.send('done'); |
| + """ |
| + self.ExecuteJavascript(js_code) |
| + self.RestartBrowser(False) |
| + |
| + def _GetTestSetting(self): |
| + """Read the given data file and return a dictionary of items. |
| + |
| + Returns: |
| + A dict mapping of keys/values in the NaCl SDK setting file. |
| + """ |
| + data_file = os.path.join(self.DataDir(), 'nacl_sdk', 'nacl_sdk_setting') |
| + |
| + try: |
| + f = open(data_file, 'r') |
| + except IOError: |
| + self.fail(msg='Cannot open %s.' % data_file) |
| + |
| + try: |
| + dictionary = eval(f.read(), {'__builtins__': None}, None) |
| + except SyntaxError: |
| + self.fail(msg='%s is an invalid setting file.' % data_file) |
| + finally: |
| + f.close() |
| + |
| + return dictionary |
| + |
| + |
| +if __name__ == '__main__': |
| + pyauto_functional.Main() |