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
5
6import ap_spec
7import belkinF9K_ap_configurator
8import logging
9from selenium.common.exceptions import TimeoutException as \
10SeleniumTimeoutException
11
12
13class BelkinF6D4230APConfigurator(
14        belkinF9K_ap_configurator.BelkinF9KAPConfigurator):
15    """Class to configure Belkin F6D4230-4 router."""
16
17
18    def __init__(self, ap_config):
19        super(BelkinF6D4230APConfigurator, self).__init__(ap_config)
20        self._dhcp_delay = 0
21
22
23    def _set_channel(self, channel):
24        position = self._get_channel_popup_position(channel)
25        channel_choices = ['1', '2', '3', '4', '5', '6', '7', '8',
26                           '9', '10', '11']
27        xpath = '//select[@name="wchan"]'
28        self.select_item_from_popup_by_xpath(channel_choices[position], xpath)
29
30
31    def _set_mode(self, mode):
32        mode_mapping = {ap_spec.MODE_N: '1x1 802.11n',
33                        ap_spec.MODE_G: '802.11g',
34                        ap_spec.MODE_B | ap_spec.MODE_G | ap_spec.MODE_N:
35                        '802.11b&802.11g&802.11n'}
36        mode_name = mode_mapping.get(mode)
37        if not mode_name:
38            raise RuntimeError('The mode %d not supported by router %s. ',
39                               hex(mode), self.name)
40        xpath = '//select[@name="wbr"]'
41        self.select_item_from_popup_by_xpath(mode_name, xpath,
42                                             wait_for_xpath=None,
43                                             alert_handler=self._security_alert)
44
45
46    def _set_security_wpapsk(self, security, shared_key, update_interval=None):
47        key_field = '//input[@name="wpa_key_pass"]'
48        psk = '//select[@name="authentication"]'
49        self.select_item_from_popup_by_xpath('WPA/WPA2-Personal (PSK)',
50                                             self.security_popup,
51                                             wait_for_xpath=key_field,
52                                             alert_handler=self._security_alert)
53        auth_type = 'WPA-PSK'
54        if security == ap_spec.SECURITY_TYPE_WPA2PSK:
55            auth_type = 'WPA2-PSK'
56        self.select_item_from_popup_by_xpath(auth_type, psk,
57                                             alert_handler=self._security_alert)
58        self.set_content_of_text_field_by_xpath(shared_key, key_field,
59                                                abort_check=True)
60
61
62    def save_page(self, page_number):
63        """Save changes and logout from the router.
64        This router has different behaviors while saving the changes everytime.
65        Hence I cover all the three possibilities below.
66
67        @param page_number: the page number to save as an integer.
68
69        """
70        apply_button = '//input[@type="submit" and @value="Apply Changes"]'
71        self.click_button_by_xpath(apply_button,
72                                   alert_handler=self._security_alert)
73        try:
74            self.wait_for_object_by_xpath(apply_button)
75        except SeleniumTimeoutException, e:
76            try:
77                self.set_wait_time(30)
78                self.wait.until(lambda _:'setup.htm' in self.driver.current_url)
79            except SeleniumTimeoutException, e:
80                xpath= '//h1[contains(text(), "Duplicate Administrator")]'
81                if (self.driver.find_element_by_xpath(xpath)):
82                    logging.debug('We got a \'Duplicate Administrator\' page '
83                                  'when we saved the changes.')
84            finally:
85                self.restore_default_wait_time()
86