Home | History | Annotate | Download | only in video
      1 # Copyright 2015 The Chromium OS 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 
      6 import re
      7 from autotest_lib.client.bin import utils
      8 from autotest_lib.client.common_lib import error
      9 
     10 
     11 def loaded(tab, histogram_name, pattern):
     12      """
     13      Checks if the histogram page has been fully loaded.
     14 
     15      @param tab: object, Chrome tab instance
     16      @param histogram_name: string, name of the histogram
     17      @param pattern: string, required text to look for
     18      @returns re.MatchObject if the given pattern is found in the text
     19               None otherwise
     20 
     21      """
     22      docEle = 'document.documentElement'
     23      tab.Navigate('chrome://histograms/%s' % histogram_name)
     24      tab.WaitForDocumentReadyStateToBeComplete()
     25      raw_text = tab.EvaluateJavaScript(
     26           '{0} && {0}.innerText'.format(docEle))
     27      return re.search(pattern, raw_text)
     28 
     29 
     30 def  verify(cr, histogram_name, histogram_bucket_value):
     31      """
     32      Verifies histogram string and success rate in a parsed histogram bucket.
     33 
     34      Full histogram URL is used to load histogram. Example Histogram URL is :
     35      chrome://histograms/Media.GpuVideoDecoderInitializeStatus
     36      @param cr: object, the Chrome instance
     37      @param histogram_name: string, name of the histogram
     38      @param histogram_bucket_value: int, required bucket number to look for
     39      @raises error.TestError if histogram is not successful
     40 
     41      """
     42      bucket_pattern = '\n'+ str(histogram_bucket_value) +'.*100\.0%.*'
     43      error_msg_format = ('{} not loaded or histogram bucket not found '
     44                          'or histogram bucket found at < 100%')
     45      tab = cr.browser.tabs.New()
     46      msg = error_msg_format.format(histogram_name)
     47      utils.poll_for_condition(lambda : loaded(tab, histogram_name,
     48                                               bucket_pattern),
     49                               exception=error.TestError(msg),
     50                               sleep_interval=1)
     51 
     52 
     53 def is_bucket_present(cr,histogram_name, histogram_bucket_value):
     54      """
     55      This returns histogram succes or fail to called function
     56 
     57      @param cr: object, the Chrome instance
     58      @param histogram_name: string, name of the histogram
     59      @param histogram_bucket_value: int, required bucket number to look for
     60      @returns True if histogram page was loaded and the bucket was found.
     61               False otherwise
     62 
     63      """
     64      try:
     65           verify(cr,histogram_name, histogram_bucket_value)
     66      except error.TestError:
     67           return False
     68      else:
     69           return True
     70 
     71 
     72 def is_histogram_present(cr, histogram_name):
     73      """
     74      This checks if the given histogram is present and non-zero.
     75 
     76      @param cr: object, the Chrome instance
     77      @param histogram_name: string, name of the histogram
     78      @returns True if histogram page was loaded and the histogram is present
     79               False otherwise
     80 
     81      """
     82      histogram_pattern = 'Histogram: '+ histogram_name + ' recorded ' + \
     83                          r'[1-9][0-9]*' + ' samples'
     84      tab = cr.browser.tabs.New()
     85      try:
     86           utils.poll_for_condition(lambda : loaded(tab, histogram_name,
     87                                                    histogram_pattern),
     88                                    timeout=2,
     89                                    sleep_interval=0.1)
     90           return True
     91      except utils.TimeoutError:
     92           # the histogram is not present, and then returns false
     93           return False
     94 
     95 
     96 def get_histogram(cr, histogram_name):
     97      """"
     98      This returns contents of the given histogram.
     99      @param cr: object, the Chrome instance
    100      @param histogram_name: string, name of the histogram
    101      @returns string: contents of the histogram
    102 
    103      """
    104      tab = cr.browser.tabs.New()
    105      docEle = 'document.documentElement'
    106      tab.Navigate('chrome://histograms/%s' % histogram_name)
    107      tab.WaitForDocumentReadyStateToBeComplete()
    108      raw_text = tab.EvaluateJavaScript(
    109           '{0} && {0}.innerText'.format(docEle))
    110      # extract the contents of the histogram
    111      histogram = raw_text[raw_text.find('Histogram:'):]
    112      return histogram
    113