1# Copyright (c) 2014 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"""The generla class for extension pages."""
6
7import web_elements
8
9
10class ExtensionPages(object):
11    """Contains all the functions of the options page of the extension."""
12
13    def __init__(self, driver, url):
14        """Constructor."""
15        self._driver = driver
16        self._url = url
17
18
19    def go_to_page(self):
20        """Go to options page if current page is not options page."""
21        if self._driver.current_url != self._url:
22            self._driver.get(self._url)
23            self._driver.refresh()
24
25
26    def _get_button(self, element_id, name=None):
27        """Returns the button object on a page.
28
29        @param element_id: The ID of the element
30        @param name: The name of the button
31        @return The button object
32        """
33        self.go_to_page()
34        return web_elements.Button(self._driver, element_id, name)
35
36
37    def _get_check_box(self, element_id, name=None):
38        """Returns the check box object on a page.
39
40        @param element_id: The ID of the element
41        @param name: The name of the check box
42        @return The check box object
43        """
44        self.go_to_page()
45        return web_elements.CheckBox(self._driver, element_id, name)
46
47
48    def _get_text_box(self, element_id, name=None):
49        """Returns the text box object on a page.
50
51        @param element_id: The ID of the element
52        @param name: The name of the text box
53        @return The text box object
54        """
55        self.go_to_page()
56        return web_elements.TextBox(self._driver, element_id, name)
57
58
59    def _get_radio_button(self, element_id, name=None):
60        """Returns the radio button object on a page.
61
62        @param element_id: The ID of the element
63        @param name: The name of the radio button
64        @return The radio button object
65        """
66        self.go_to_page()
67        return web_elements.RadioButton(self._driver, element_id, name)
68
69
70    def _get_scroll_box(self, element_id, name=None):
71        """Returns the scroll box object on a page.
72
73        Args:
74            element_id: The ID of the element
75            name: The name of the scroll box
76        Returns:
77            The scroll box object
78        """
79        self.go_to_page()
80        return web_elements.ScrollBox(self._driver, element_id, name)
81
82
83    def _get_web_element_box(self, element_id, name=None):
84        """Returns the web element box object on a page.
85
86        @param element_id: The ID of the element
87        @param name: The name of the web element box
88        @return The web element box object
89        """
90        self.go_to_page()
91        return web_elements.WebElementBox(self._driver, element_id, name)
92
93
94    def get_extension_version(self):
95        """Returns the cast extension version based on its extension ID.
96
97        @return The version number, in string, of the Cast extension
98        """
99        self.go_to_page()
100        get_extension_version_js = ('return chrome.runtime.'
101                                    'getManifest().version;')
102        return self._driver.execute_script(get_extension_version_js)
103
104
105    def get_extension_name(self):
106        """Returns the cast extension name based on its extension ID.
107
108        @return The name of the Cast extension
109        """
110        self.go_to_page()
111        get_extension_name_js = 'return chrome.runtime.getManifest().name;'
112        return self._driver.execute_script(get_extension_name_js)
113
114
115    def execute_script(self, js_script):
116        """Executes the javascript code in current page context.
117
118        @param js_script: the javascript code to be executed.
119        @return The value returned by javascript code.
120        """
121        self.go_to_page()
122        return self._driver.execute_script(js_script)
123