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
6import re
7
8from autotest_lib.client.bin import utils
9from autotest_lib.client.common_lib import error
10
11
12def  verify(cr, histogram_name, histogram_bucket_value):
13     """
14     Verifies histogram string and success rate in a parsed histogram bucket.
15
16     Full histogram URL is used to load histogram. Example Histogram URL is :
17     chrome://histograms/Media.GpuVideoDecoderInitializeStatus
18     @param cr: object, the Chrome instance
19     @param histogram_name: string, name of the histogram
20     @param histogram_bucket_value: int, required bucket number to look for
21     @raises error.TestError if histogram is not successful
22
23     """
24     bucket_pattern = '\n'+ str(histogram_bucket_value) +'.*100\.0%.*'
25     error_msg_format = ('{} not loaded or histogram bucket not found '
26                         'or histogram bucket found at < 100%')
27     tab = cr.browser.tabs.New()
28
29     def loaded():
30          """
31          Checks if the histogram page has been fully loaded.
32
33          """
34          docEle = 'document.documentElement'
35          tab.Navigate('chrome://histograms/%s' % histogram_name)
36          tab.WaitForDocumentReadyStateToBeComplete()
37          raw_text = tab.EvaluateJavaScript(
38                  "{0} && {0}.innerText".format(docEle))
39          return re.search(bucket_pattern, raw_text)
40
41     msg = error_msg_format.format(histogram_name)
42     utils.poll_for_condition(loaded,
43                              exception=error.TestError(msg),
44                              sleep_interval=1)
45
46
47def is_bucket_present(cr,histogram_name, histogram_bucket_value):
48     """
49     This returns histogram succes or fail to called function
50
51     @param cr: object, the Chrome instance
52     @param histogram_name: string, name of the histogram
53     @param histogram_bucket_value: int, required bucket number to look for
54     @returns True if histogram page was loaded and the bucket was found.
55              False otherwise
56
57     """
58     try:
59          verify(cr,histogram_name, histogram_bucket_value)
60     except error.TestError:
61          return False
62     else:
63          return True
64