<s> from __future__ import unicode_literals <EOL> from contextlib import contextmanager <EOL> import time <EOL> from django import forms <EOL> from django . conf import settings <EOL> from django . contrib . admin import site <EOL> from django . contrib . admin . tests import AdminSeleniumWebDriverTestCase <EOL> from django . contrib . auth . models import User <EOL> from django . core . urlresolvers import reverse <EOL> from django . test import SimpleTestCase <EOL> from admin_enhancer import widgets <EOL> from . models import Book <EOL> class InteractionTest ( AdminSeleniumWebDriverTestCase ) : <EOL> available_apps = settings . INSTALLED_APPS <EOL> def setUp ( self ) : <EOL> super ( InteractionTest , self ) . setUp ( ) <EOL> User . objects . create_superuser ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def wait_for_popup ( self , name ) : <EOL> def popup_is_loaded ( driver ) : <EOL> return driver . current_window_handle == name <EOL> self . wait_until ( popup_is_loaded ) <EOL> @ contextmanager <EOL> def handle_popup ( self , trigger ) : <EOL> initial_window_handle = self . selenium . current_window_handle <EOL> window_handles = set ( self . selenium . window_handles ) <EOL> try : <EOL> trigger ( ) <EOL> self . wait_until ( lambda driver : set ( driver . window_handles ) != window_handles ) <EOL> new_window_handle = ( set ( self . selenium . window_handles ) - window_handles ) . pop ( ) <EOL> self . selenium . switch_to . window ( new_window_handle ) <EOL> yield new_window_handle <EOL> finally : <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> self . selenium . switch_to . window ( initial_window_handle ) <EOL> def test_widget_interactions ( self ) : <EOL> self . admin_login ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> driver = self . selenium <EOL> driver . set_page_load_timeout ( <NUM_LIT:10> ) <EOL> driver . get ( "<STR_LIT>" % ( self . live_server_url , reverse ( '<STR_LIT>' ) ) ) <EOL> author_select = driver . find_element_by_id ( '<STR_LIT>' ) <EOL> edit_author_btn = driver . find_element_by_id ( '<STR_LIT>' ) <EOL> add_author_btn = driver . find_element_by_id ( '<STR_LIT>' ) <EOL> delete_author_btn = driver . find_element_by_id ( '<STR_LIT>' ) <EOL> self . assertIsNone ( edit_author_btn . get_attribute ( '<STR_LIT>' ) ) <EOL> self . assertIsNone ( delete_author_btn . get_attribute ( '<STR_LIT>' ) ) <EOL> def author_options ( ) : <EOL> author_options = author_select . find_elements_by_tag_name ( '<STR_LIT>' ) <EOL> options_label = [ ] <EOL> selected_option_label = None <EOL> for option in author_options : <EOL> label = option . get_attribute ( '<STR_LIT>' ) <EOL> options_label . append ( label ) <EOL> if option . get_attribute ( '<STR_LIT>' ) : <EOL> selected_option_label = label <EOL> return selected_option_label , options_label <EOL> def interact ( button , name ) : <EOL> with self . handle_popup ( button . click ) : <EOL> driver . implicitly_wait ( <NUM_LIT:1> ) <EOL> driver . find_element_by_id ( '<STR_LIT>' ) . clear ( ) <EOL> driver . find_element_by_id ( '<STR_LIT>' ) . send_keys ( name ) <EOL> driver . find_element_by_name ( '<STR_LIT>' ) . click ( ) <EOL> selected_option_label , options_label = author_options ( ) <EOL> self . assertEqual ( [ '<STR_LIT>' , name ] , options_label ) <EOL> self . assertEqual ( name , selected_option_label ) <EOL> interact ( add_author_btn , '<STR_LIT>' ) <EOL> self . assertIsNotNone ( edit_author_btn . get_attribute ( '<STR_LIT>' ) ) <EOL> self . assertIsNotNone ( delete_author_btn . get_attribute ( '<STR_LIT>' ) ) <EOL> interact ( edit_author_btn , '<STR_LIT>' ) <EOL> with self . handle_popup ( delete_author_btn . click ) : <EOL> driver . find_element_by_css_selector ( '<STR_LIT>' ) . click ( ) <EOL> selected_option_label , options_label = author_options ( ) <EOL> self . assertEqual ( [ '<STR_LIT>' ] , options_label ) <EOL> self . assertEqual ( '<STR_LIT>' , selected_option_label ) <EOL> self . assertIsNone ( edit_author_btn . get_attribute ( '<STR_LIT>' ) ) <EOL> self . assertIsNone ( delete_author_btn . get_attribute ( '<STR_LIT>' ) ) <EOL> class RelatedFieldWidgetWrapperTests ( SimpleTestCase ) : <EOL> def test_select_multiple_widget_cant_change_delete_related ( self ) : <EOL> rel = Book . _meta . get_field ( '<STR_LIT>' ) . rel <EOL> widget = forms . SelectMultiple ( ) <EOL> wrapper = widgets . RelatedFieldWidgetWrapper ( <EOL> widget , rel , site , <EOL> can_add_related = True , <EOL> can_change_related = True , <EOL> can_delete_related = True , <EOL> ) <EOL> self . assertTrue ( wrapper . can_add_related ) <EOL> self . assertFalse ( wrapper . can_change_related ) <EOL> self . assertFalse ( wrapper . can_delete_related ) <EOL> def test_on_delete_cascade_rel_cant_delete_related ( self ) : <EOL> rel = Book . _meta . get_field ( '<STR_LIT>' ) . rel <EOL> widget = forms . Select ( ) <EOL> wrapper = widgets . RelatedFieldWidgetWrapper ( <EOL> widget , rel , site , <EOL> can_add_related = True , <EOL> can_change_related = True , <EOL> can_delete_related = True , <EOL> ) <EOL> self . assertTrue ( wrapper . can_add_related ) <EOL> self . assertTrue ( wrapper . can_change_related ) <EOL> self . assertFalse ( wrapper . can_delete_related ) </s>
