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