1# Copyright (c) 2013 The Chromium 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
5import os
6import urlparse
7
8import ap_spec
9import netgear_single_band_configurator
10from netgear_single_band_configurator import *
11
12
13class Netgear2000APConfigurator(netgear_single_band_configurator.
14                                NetgearSingleBandAPConfigurator):
15    """Derived class to control Netgear WNR2000v3 router."""
16
17    def _alert_handler(self, alert):
18        """Checks for any modal dialogs which popup to alert the user and
19        either raises a RuntimeError or ignores the alert.
20
21        @param alert: The modal dialog's contents.
22        """
23        text = alert.text
24        if 'The WEP security can only be supported on one SSID' in text:
25            alert.accept()
26        elif 'WPA-PSK [TKIP] only operates at "Up to 54Mbps"' in text:
27            raise RuntimeError('Security and mode do not match:', text)
28        elif '40 Mhz and 20 Mhz coexistence' in text:
29            alert.accept()
30        elif 'Are you sure that you do not want any wireless security' in text:
31            alert.accept()
32        elif 'WPS requires SSID broadcasting in order to work' in text:
33            alert.accept()
34        elif 'WPS is going to become inaccessible' in text:
35            alert.accept()
36        else:
37            super(Netgear2000APConfigurator, self)._alert_handler(alert)
38
39
40    def logout_from_previous_netgear(self):
41        """Some netgear routers dislike you being logged into another
42           one of their kind. So make sure that you are not."""
43        self.click_button_by_id('yes', alert_handler=self._alert_handler)
44        self.navigate_to_page(1)
45
46
47    def navigate_to_page(self, page_number):
48        """Navigates to the given page.
49
50        @param page_number: the page to navigate to.
51        """
52        try:
53            self.get_url(urlparse.urljoin(self.admin_interface_url,
54                         'adv_index.htm'), page_title='WNR2000v3')
55            self.click_button_by_id('setup_bt')
56            self.wait_for_object_by_id('wireless')
57            self.click_button_by_id('wireless')
58        except Exception as e:
59            if os.path.basename(self.driver.current_url) != 'adv_index.htm':
60                raise RuntimeError('Invalid url %s' % self.driver.current_url)
61            elif os.path.basename(
62                 self.driver.current_url) == 'multi_login.html':
63                self.logout_from_previous_netgear()
64        setframe = self.driver.find_element_by_xpath(
65                   '//iframe[@name="formframe"]')
66        settings = self.driver.switch_to_frame(setframe)
67        self.wait_for_object_by_xpath('//input[@name="ssid"]')
68
69
70    def get_supported_modes(self):
71        return [{'band': ap_spec.BAND_2GHZ,
72                 'modes': [ap_spec.MODE_G, ap_spec.MODE_N]}]
73
74
75    def set_mode(self, mode):
76        # The mode popup changes based on the security mode.  Set to no
77        # security to get the right popup.
78        self.add_item_to_command_list(self._set_security_disabled, (), 1, 799)
79        self.add_item_to_command_list(self._set_mode, (mode, ), 1, 800)
80
81
82    def _set_mode(self, mode):
83        if mode == ap_spec.MODE_G:
84            mode = 'Up to 54 Mbps'
85        elif mode == ap_spec.MODE_N:
86            mode = 'Up to 150 Mbps'
87        else:
88            raise RuntimeError('Unsupported mode passed.')
89        xpath = '//select[@name="opmode"]'
90        self.select_item_from_popup_by_xpath(mode, xpath)
91
92
93    def set_visibility(self, visible=True):
94        self.add_item_to_command_list(self._set_visibility, (visible,), 1, 900)
95
96
97    def _set_visibility(self, visible=True):
98        xpath = '//input[@name="ssid_bc" and @type="checkbox"]'
99        check_box = self.wait_for_object_by_xpath(xpath)
100        # These check boxes behave different from other APs.
101        value = check_box.is_selected()
102        if (visible and not value) or (not visible and value):
103            check_box.click()
104
105        self.set_check_box_selected_by_xpath(xpath, selected=visible,
106                                             wait_for_xpath=None,
107                                             alert_handler=self._alert_handler)
108