Home | History | Annotate | Download | only in actions
      1 # Copyright 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 
      5 from telemetry.core import util
      6 from telemetry.page.actions import play
      7 from telemetry.unittest import tab_test_case
      8 
      9 AUDIO_1_PLAYING_CHECK = 'window.__hasEventCompleted("#audio_1", "playing");'
     10 VIDEO_1_PLAYING_CHECK = 'window.__hasEventCompleted("#video_1", "playing");'
     11 VIDEO_1_ENDED_CHECK = 'window.__hasEventCompleted("#video_1", "ended");'
     12 
     13 
     14 class PlayActionTest(tab_test_case.TabTestCase):
     15 
     16   def setUp(self):
     17     tab_test_case.TabTestCase.setUp(self)
     18     self._browser.SetHTTPServerDirectories(util.GetUnittestDataDir())
     19     self._tab.Navigate(self._browser.http_server.UrlOf('video_test.html'))
     20     self._tab.WaitForDocumentReadyStateToBeComplete()
     21 
     22   def testPlayWithNoSelector(self):
     23     """Tests that with no selector Play action plays first video element."""
     24     data = {'wait_for_playing': True}
     25     action = play.PlayAction(data)
     26     action.WillRunAction(None, self._tab)
     27     # Both videos not playing before running action.
     28     self.assertFalse(self._tab.EvaluateJavaScript(VIDEO_1_PLAYING_CHECK))
     29     self.assertFalse(self._tab.EvaluateJavaScript(AUDIO_1_PLAYING_CHECK))
     30     action.RunAction(None, self._tab, None)
     31     # Assert only first video has played.
     32     self.assertTrue(self._tab.EvaluateJavaScript(VIDEO_1_PLAYING_CHECK))
     33     self.assertFalse(self._tab.EvaluateJavaScript(AUDIO_1_PLAYING_CHECK))
     34 
     35   def testPlayWithVideoSelector(self):
     36     """Tests that Play action plays video element matching selector."""
     37     data = {'selector': '#video_1', 'wait_for_playing': True}
     38     action = play.PlayAction(data)
     39     action.WillRunAction(None, self._tab)
     40     # Both videos not playing before running action.
     41     self.assertFalse(self._tab.EvaluateJavaScript(VIDEO_1_PLAYING_CHECK))
     42     self.assertFalse(self._tab.EvaluateJavaScript(AUDIO_1_PLAYING_CHECK))
     43     action.RunAction(None, self._tab, None)
     44     # Assert only video matching selector has played.
     45     self.assertTrue(self._tab.EvaluateJavaScript(VIDEO_1_PLAYING_CHECK))
     46     self.assertFalse(self._tab.EvaluateJavaScript(AUDIO_1_PLAYING_CHECK))
     47 
     48   def testPlayWithAllSelector(self):
     49     """Tests that Play action plays all video elements with selector='all'."""
     50     data = {'selector': 'all', 'wait_for_playing': True}
     51     action = play.PlayAction(data)
     52     action.WillRunAction(None, self._tab)
     53     # Both videos not playing before running action.
     54     self.assertFalse(self._tab.EvaluateJavaScript(VIDEO_1_PLAYING_CHECK))
     55     self.assertFalse(self._tab.EvaluateJavaScript(AUDIO_1_PLAYING_CHECK))
     56     action.RunAction(None, self._tab, None)
     57     # Assert all media elements played.
     58     self.assertTrue(self._tab.EvaluateJavaScript(VIDEO_1_PLAYING_CHECK))
     59     self.assertTrue(self._tab.EvaluateJavaScript(AUDIO_1_PLAYING_CHECK))
     60 
     61   def testPlayWaitForPlayTimeout(self):
     62     """Tests that wait_for_playing timeouts if video does not play."""
     63     data = {'selector': '#video_1',
     64             'wait_for_playing': True,
     65             'wait_timeout': 1}
     66     action = play.PlayAction(data)
     67     action.WillRunAction(None, self._tab)
     68     self._tab.EvaluateJavaScript('document.getElementById("video_1").src = ""')
     69     self.assertFalse(self._tab.EvaluateJavaScript(VIDEO_1_PLAYING_CHECK))
     70     self.assertRaises(util.TimeoutException, action.RunAction, None, self._tab,
     71                       None)
     72 
     73   def testPlayWaitForEnded(self):
     74     """Tests that wait_for_ended waits for video to end."""
     75     data = {'selector': '#video_1', 'wait_for_ended': True}
     76     action = play.PlayAction(data)
     77     action.WillRunAction(None, self._tab)
     78     # Assert video not playing before running action.
     79     self.assertFalse(self._tab.EvaluateJavaScript(VIDEO_1_PLAYING_CHECK))
     80     self.assertFalse(self._tab.EvaluateJavaScript(VIDEO_1_ENDED_CHECK))
     81     action.RunAction(None, self._tab, None)
     82     # Assert video ended.
     83     self.assertTrue(self._tab.EvaluateJavaScript(VIDEO_1_ENDED_CHECK))
     84 
     85   def testPlayWithoutWaitForEnded(self):
     86     """Tests that wait_for_ended waits for video to end."""
     87     data = {'selector': '#video_1', 'wait_for_ended': False}
     88     action = play.PlayAction(data)
     89     action.WillRunAction(None, self._tab)
     90     # Assert video not playing before running action.
     91     self.assertFalse(self._tab.EvaluateJavaScript(VIDEO_1_PLAYING_CHECK))
     92     self.assertFalse(self._tab.EvaluateJavaScript(VIDEO_1_ENDED_CHECK))
     93     action.RunAction(None, self._tab, None)
     94     # Assert video did not end.
     95     self.assertFalse(self._tab.EvaluateJavaScript(VIDEO_1_ENDED_CHECK))
     96 
     97   def testPlayWaitForEndedTimeout(self):
     98     """Tests that action raises exception if timeout is reached."""
     99     data = {'selector': '#video_1', 'wait_for_ended': True, 'wait_timeout': 1}
    100     action = play.PlayAction(data)
    101     action.WillRunAction(None, self._tab)
    102     # Assert video not playing before running action.
    103     self.assertFalse(self._tab.EvaluateJavaScript(VIDEO_1_PLAYING_CHECK))
    104     self.assertFalse(self._tab.EvaluateJavaScript(VIDEO_1_ENDED_CHECK))
    105     self.assertRaises(util.TimeoutException, action.RunAction, None, self._tab,
    106                       None)
    107     # Assert video did not end.
    108     self.assertFalse(self._tab.EvaluateJavaScript(VIDEO_1_ENDED_CHECK))
    109