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_WNDR_dual_band_configurator
10from netgear_WNDR_dual_band_configurator import *
11
12
13class Netgear4300APConfigurator(netgear_WNDR_dual_band_configurator.
14                                NetgearDualBandAPConfigurator):
15    """Derived class to control Netgear WNDR4300 router."""
16
17
18    def _alert_handler(self, alert):
19        """Checks for any modal dialogs which popup to alert the user and
20        either raises a RuntimeError or ignores the alert.
21
22        @param alert: The modal dialog's contents.
23        """
24        text = alert.text
25        #  We ignore warnings that we get when we disable visibility or security
26        #  changed to WEP, WPA Personal or WPA Enterprise.
27        if 'The WEP security can only be supported on one SSID' in text:
28            alert.accept()
29        elif '40 Mhz and 20 Mhz coexistence' in text:
30            alert.accept()
31        elif 'WPS is going to become inaccessible' in text:
32            alert.accept()
33        elif 'Keys length should be 10 Hex' in text:
34            alert.accept()
35            raise RuntimeError('We got a dialog with Invalid Key Error. '
36                               + text)
37        else:
38            super(Netgear4300APConfigurator, self)._alert_handler(alert)
39
40
41    def get_supported_bands(self):
42        return [{'band': ap_spec.BAND_2GHZ,
43                 'channels': ['Auto', 1, 2, 3, 4, 5, 6, 7, 8, 9 , 10, 11]},
44                {'band': ap_spec.BAND_5GHZ,
45                 'channels': [36, 40, 44, 48, 149, 153, 157, 161]}]
46
47
48    def get_supported_modes(self):
49        return [{'band': ap_spec.BAND_5GHZ,
50                 'modes': [ap_spec.MODE_A, ap_spec.MODE_N]},
51                {'band': ap_spec.BAND_2GHZ,
52                 'modes': [ap_spec.MODE_G, ap_spec.MODE_N]}]
53
54
55    def logout_from_previous_netgear(self):
56        """Some netgear routers dislike you being logged into another
57           one of their kind. So make sure that you are not."""
58        self.click_button_by_id('yes')
59
60
61    def navigate_to_page(self, page_number):
62        """Navigate to the given page.
63
64        @param page_number: the page to navigate to.
65        """
66        self.set_wait_time(30)
67        try:
68            self.get_url(urlparse.urljoin(self.admin_interface_url,
69                         'adv_index.htm'), page_title='WNDR4300')
70            self.driver.execute_script("click_adv_action('wireless')")
71        except Exception as e:
72            if os.path.basename(self.driver.current_url) != 'adv_index.htm':
73                raise RuntimeError('Invalid url %s' % self.driver.current_url)
74            elif os.path.basename(
75                self.driver.current_url) == 'multi_login.html':
76                self.logout_from_previous_netgear()
77        setframe = self.wait_for_object_by_xpath('//iframe[@name="formframe"]')
78        settings = self.driver.switch_to_frame(setframe)
79        xpath = '//input[@name="security_type_an" and @value="WPA-ENTER"]'
80        self.wait.until(lambda _:
81                        self.wait_for_object_by_xpath(xpath).is_displayed())
82        self.driver.execute_script('window.stop()')
83        self.restore_default_wait_time()
84
85
86    def save_page(self, page_number):
87        """Saves the given page.
88
89        @param page_number: the page to save.
90        """
91        # Router throws 3 different alerts when the settings are applied.
92        try:
93            self.click_button_by_xpath('//input[@name="Apply"]',
94                                       alert_handler=self._alert_handler)
95        except Exception, e1:
96            try:
97                self._check_for_alert_in_message(str(e1),
98                        alert_handler=self._alert_handler)
99            except Exception, e2:
100                self._check_for_alert_in_message(str(e2),
101                        alert_handler=self._alert_handler)
102
103
104    def _set_mode(self, mode, band=None):
105        if mode == ap_spec.MODE_G or mode == ap_spec.MODE_A:
106            mode_selection = 'Up to 54 Mbps'
107        elif mode == ap_spec.MODE_N:
108            if self.current_band == ap_spec.BAND_2GHZ:
109                mode_selection = 'Up to 300 Mbps'
110            else:
111                mode_selection = 'Up to 450 Mbps'
112        else:
113            raise RuntimeError('Unsupported mode passed.')
114        xpath = '//select[@name="opmode"]'
115        if self.current_band == ap_spec.BAND_5GHZ:
116            xpath = '//select[@name="opmode_an"]'
117        self.wait_for_object_by_xpath(xpath)
118        self.select_item_from_popup_by_xpath(mode_selection, xpath)
119
120
121    def set_channel(self, channel):
122        self.add_item_to_command_list(self._set_channel, (channel,), 1, 900)
123
124
125    def _set_channel(self, channel):
126        position = self._get_channel_popup_position(channel)
127        channel_choices = ['Auto', '01', '02', '03', '04', '05', '06', '07',
128                           '08', '09', '10', '11']
129        xpath = '//select[@name="w_channel"]'
130        if self.current_band == ap_spec.BAND_5GHZ:
131            xpath = '//select[@name="w_channel_an"]'
132            channel_choices = ['36', '40', '44', '48', '149', '153',
133                               '157', '161']
134        self.select_item_from_popup_by_xpath(channel_choices[position], xpath)
135
136
137    def set_security_wep(self, key_value, authentication):
138        # The button name seems to differ in various Netgear routers
139        self.add_item_to_command_list(self._set_security_wep,
140                                      (key_value, authentication), 1, 900)
141
142
143    def _set_security_wep(self, key_value, authentication):
144        self.set_wait_time(30)
145        xpath = ('//input[@name="security_type" and @value="WEP" and '
146                 '@type="radio"]')
147        text_field = '//input[@name="KEY1"]'
148        if self.current_band == ap_spec.BAND_5GHZ:
149            xpath = '//input[@name="security_type_an" and @value="WEP" and\
150                     @type="radio"]'
151            text_field = '//input[@name="KEY1_an"]'
152        try:
153            self.wait_for_object_by_xpath(xpath)
154            self.click_button_by_xpath(xpath, alert_handler=self._alert_handler)
155        except Exception, e:
156            raise RuntimeError('We got an exception: "%s". Check the mode. '
157                               'It should be \'Up to 54 Mbps\'.' % str(e))
158        self.wait_for_object_by_xpath(text_field)
159        self.set_content_of_text_field_by_xpath(key_value, text_field,
160                                                abort_check=True)
161        self.restore_default_wait_time()
162