1 # Copyright (c) 2013 The Chromium Authors. All rights reserved. 2 # Use of this source code is governed by a BSD-style license that can be 3 # found in the LICENSE file. 4 import os 5 import unittest 6 7 from telemetry.page import page as page_module 8 from telemetry.page import page_test 9 from telemetry.page.actions import all_page_actions 10 from telemetry.page.actions import page_action 11 12 def _CreatePage(test_filename): 13 url = 'file:///' + os.path.join('..', '..', 'unittest_data', test_filename) 14 base_dir = os.path.dirname(__file__) 15 page = page_module.Page(url, None, base_dir=base_dir) 16 return page 17 18 class DoNothingPageTest(page_test.PageTest): 19 def __init__(self, action_name_to_run=''): 20 super(DoNothingPageTest, self).__init__('DoNothing', action_name_to_run) 21 22 def DoNothing(self, page, tab, results): 23 pass 24 25 class AppendAction(page_action.PageAction): 26 def RunAction(self, page, tab, previous_action): 27 self.var.append(True) 28 29 class WrapAppendAction(page_action.PageAction): 30 def RunsPreviousAction(self): 31 return True 32 33 def RunAction(self, page, tab, previous_action): 34 self.var.append('before') 35 previous_action.WillRunAction(page, tab) 36 previous_action.RunAction(page, tab, None) 37 self.var.append('after') 38 39 class PageTestUnitTest(unittest.TestCase): 40 def setUp(self): 41 super(PageTestUnitTest, self).setUp() 42 all_page_actions.RegisterClassForTest('append', AppendAction) 43 all_page_actions.RegisterClassForTest('wrap_append', WrapAppendAction) 44 45 self._page_test = DoNothingPageTest('action_to_run') 46 self._page = _CreatePage('blank.html') 47 48 def testRunActions(self): 49 action_called = [] 50 action_to_run = [ 51 { 'action': 'append', 'var': action_called } 52 ] 53 setattr(self._page, 'action_to_run', action_to_run) 54 55 self._page_test.Run(None, self._page, None, None) 56 57 self.assertTrue(action_called) 58 59 def testPreviousAction(self): 60 action_list = [] 61 action_to_run = [ 62 { 'action': 'append', 'var': action_list }, 63 { 'action': 'wrap_append', 'var': action_list } 64 ] 65 setattr(self._page, 'action_to_run', action_to_run) 66 67 self._page_test.Run(None, self._page, None, None) 68 69 self.assertEqual(action_list, ['before', True, 'after']) 70 71 def testReferenceAction(self): 72 action_list = [] 73 action_to_run = [ 74 { 'action': 'referenced_action_1' }, 75 { 'action': 'referenced_action_2' } 76 ] 77 referenced_action_1 = { 'action': 'append', 'var': action_list } 78 referenced_action_2 = { 'action': 'wrap_append', 'var': action_list } 79 setattr(self._page, 'action_to_run', action_to_run) 80 setattr(self._page, 'referenced_action_1', referenced_action_1) 81 setattr(self._page, 'referenced_action_2', referenced_action_2) 82 83 self._page_test.Run(None, self._page, None, None) 84 85 self.assertEqual(action_list, ['before', True, 'after']) 86 87 def testRepeatAction(self): 88 action_list = [] 89 action_to_run = { 'action': 'append', 'var': action_list, 'repeat': 10 } 90 setattr(self._page, 'action_to_run', action_to_run) 91 92 self._page_test.Run(None, self._page, None, None) 93 94 self.assertEqual(len(action_list), 10) 95 96 def testRepeatReferenceAction(self): 97 action_list = [] 98 action_to_run = { 'action': 'referenced_action', 'repeat': 2 } 99 referenced_action = [ 100 { 'action': 'append', 'var': action_list }, 101 { 'action': 'wrap_append', 'var': action_list } 102 ] 103 setattr(self._page, 'action_to_run', action_to_run) 104 setattr(self._page, 'referenced_action', referenced_action) 105 106 self._page_test.Run(None, self._page, None, None) 107 108 self.assertEqual(action_list, 109 ['before', True, 'after', 'before', True, 'after']) 110 111 def testRepeatPreviousActionFails(self): 112 action_list = [] 113 action_to_run = { 'action': 'wrap_append', 'var': action_list, 'repeat': 2 } 114 setattr(self._page, 'action_to_run', action_to_run) 115 116 self.assertRaises(page_action.PageActionFailed, 117 lambda: self._page_test.Run(None, self._page, None, None)) 118