1#!/usr/bin/env python
2# Copyright (c) 2012 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6import os
7import re
8
9import pyauto_functional  # Must be imported before pyauto
10import pyauto
11import pyauto_utils
12
13
14class AboutPluginsUITest(pyauto.PyUITest):
15  """Testcase for chrome://plugins UI."""
16
17  def testAboutPluginDetailInfo(self):
18    """Verify chrome://plugins page shows plugin details."""
19    self.NavigateToURL('chrome://plugins/')
20    driver = self.NewWebDriver()
21    detail_link = driver.find_element_by_id('details-link')
22    self.assertTrue(self.WaitUntil(lambda: detail_link.is_displayed()),
23        msg='Details link could not be found.')
24    detail_link.click()
25    # Verify that detail info for Remote Viewer plugin shows up.
26    # Remote Viewer plugin is expected to be present on all platforms.
27    self.assertTrue(self.WaitUntil(lambda: len(driver.find_elements_by_xpath(
28        '//*[@jscontent="path"][text()="internal-remoting-viewer"]'))))
29
30
31class ChromeAboutPluginsUITest(pyauto.PyUITest):
32  """Testcase for official build only plugins in chrome://plugins UI."""
33
34  def Debug(self):
35    """chrome://plugins test debug method.
36
37    This method will not run automatically.
38    """
39    self.NavigateToURL('chrome://plugins/')
40    driver = self.NewWebDriver()
41    import pdb
42    pdb.set_trace()
43
44  def _IsEnabled(self, driver, plugin_name):
45    """Checks if plugin is enabled.
46
47    Args:
48      driver: A Chrome driver object.
49      plugin_name: Plugin name to verify.
50
51    Returns:
52      True, if plugin is enabled, or False otherwise.
53    """
54    check_plugin_enabled_js = 'return navigator.plugins["%s"] != undefined' % \
55        plugin_name
56    return driver.execute_script(check_plugin_enabled_js)
57
58  def _ExpandDetailInfoLink(self, driver):
59    """Expand detail info link.
60
61    Args:
62      driver: A Chrome driver object.
63    """
64    detail_link = driver.find_element_by_id('details-link')
65    self.assertTrue(self.WaitUntil(lambda: detail_link.is_displayed()),
66        msg='Details link could not be found.')
67    detail_link.click()
68
69  def _OverridePluginPageAnimation(self, driver):
70    """Override the animation for expanding detail info to make sure element
71    remain at the same location where web driver found it.
72
73    Args:
74      driver: A Chrome driver object.
75    """
76    override_animation_style_js = """
77        style = document.createElement('style');
78        style.innerHTML = "* { -webkit-transition: all 0s ease-in !important}";
79        document.head.appendChild(style);
80        """
81    driver.execute_script(override_animation_style_js)
82
83  def testAboutPluginEnableAndDisablePDFPlugin(self):
84    """Verify enable and disable pdf plugins from about:plugins page."""
85    self.NavigateToURL('chrome://plugins/')
86    driver = self.NewWebDriver()
87
88    self._OverridePluginPageAnimation(driver)
89    self._ExpandDetailInfoLink(driver)
90
91    pdf_disable_path = '//*[@class="plugin-name"][text()="Chrome PDF Viewer"' \
92        ']//ancestor::*[@class="plugin-text"]//a[text()="Disable"]'
93    pdf_enable_path = '//*[@class="plugin-name"][text()="Chrome PDF Viewer"' \
94        ']//ancestor::*[@class="plugin-text"]//a[text()="Enable"]'
95
96    # Confirm Chrome PDF Viewer plugin is found and find disable PDF link.
97    pdf_disable_link = pyauto_utils.WaitForDomElement(self, driver,
98                                                      pdf_disable_path)
99
100    # Disable PDF viewer plugin in about:plugins.
101    pdf_disable_link.click()
102    self.assertTrue(self.WaitUntil(lambda: not
103        self._IsEnabled(driver, 'Chrome PDF Viewer')))
104
105    # Re-enable PDF viewer plugin.
106    pdf_enable_link = driver.find_element_by_xpath(pdf_enable_path)
107    pdf_enable_link.click()
108    self.assertTrue(self.WaitUntil(lambda:
109        self._IsEnabled(driver, 'Chrome PDF Viewer')))
110
111  def testEnableAndDisableFlashPlugin(self):
112    """Verify enable and disable flash plugins from about:plugins page."""
113    self.NavigateToURL('chrome://plugins/')
114    driver = self.NewWebDriver()
115
116    self._OverridePluginPageAnimation(driver)
117    self._ExpandDetailInfoLink(driver)
118    flash_plugins_elem = driver.find_element_by_xpath(
119        '//*[@jscontent="name"][text()="Flash"]//ancestor' \
120        '::*[@class="plugin-text"]')
121
122    # Disable flash plugin from flash detail info.
123    flash_disable_link = flash_plugins_elem.find_element_by_xpath(
124        './/a[text()="Disable"]')
125    flash_disable_link.click()
126    self.assertTrue(self.WaitUntil(lambda: not
127        self._IsEnabled(driver, 'Shockwave Flash')))
128
129    # Re-enable Flash plugin from flash detail info.
130    flash_enable_link = flash_plugins_elem.find_element_by_xpath(
131        './/a[text()="Enable"]')
132    flash_enable_link.click()
133    self.assertTrue(self.WaitUntil(lambda:
134        self._IsEnabled(driver, 'Shockwave Flash')))
135
136
137if __name__ == '__main__':
138  pyauto_functional.Main()
139