<s> from __future__ import absolute_import <EOL> import random <EOL> from jinja2 import Template <EOL> from bokeh . charts import Area , Histogram <EOL> from bokeh . io import save <EOL> from bokeh . plotting import figure <EOL> from bokeh . sampledata . autompg import autompg as df <EOL> from bokeh . resources import INLINE <EOL> from bokeh . embed import components <EOL> from selenium . webdriver . support . ui import WebDriverWait <EOL> from selenium . common . exceptions import TimeoutException <EOL> import pytest <EOL> from . . utils import element_to_start_resizing , element_to_finish_resizing <EOL> pytestmark = pytest . mark . integration <EOL> def wait_for_canvas_resize ( canvas , test_driver ) : <EOL> try : <EOL> wait = WebDriverWait ( test_driver , <NUM_LIT:5> ) <EOL> wait . until ( element_to_start_resizing ( canvas ) ) <EOL> wait . until ( element_to_finish_resizing ( canvas ) ) <EOL> except TimeoutException : <EOL> pass <EOL> def test_responsive_resizes_plot_while_maintaining_aspect_ratio ( output_file_url , selenium ) : <EOL> aspect_ratio = <NUM_LIT:2> <EOL> plot_height = <NUM_LIT> <EOL> plot_width = <NUM_LIT> * aspect_ratio <EOL> lower_bound = aspect_ratio * <NUM_LIT> <EOL> upper_bound = aspect_ratio * <NUM_LIT> <EOL> initial_window_width = <NUM_LIT> <EOL> final_window_width = <NUM_LIT> <EOL> window_ratio = initial_window_width / final_window_width <EOL> plot = figure ( plot_width = plot_width , plot_height = plot_height , responsive = True ) <EOL> plot . scatter ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , [ <NUM_LIT:3> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> save ( plot ) <EOL> selenium . set_window_size ( width = initial_window_width , height = <NUM_LIT> ) <EOL> selenium . get ( output_file_url ) <EOL> canvas = selenium . find_element_by_tag_name ( '<STR_LIT>' ) <EOL> initial_height = canvas . size [ '<STR_LIT>' ] <EOL> initial_width = canvas . size [ '<STR_LIT:width>' ] <EOL> initial_aspect_ratio = initial_width / initial_height <EOL> assert initial_aspect_ratio > lower_bound <EOL> assert initial_aspect_ratio < upper_bound <EOL> selenium . set_window_size ( width = final_window_width , height = <NUM_LIT> ) <EOL> wait_for_canvas_resize ( canvas , selenium ) <EOL> final_height = canvas . size [ '<STR_LIT>' ] <EOL> final_width = canvas . size [ '<STR_LIT:width>' ] <EOL> final_aspect_ratio = final_width / final_height <EOL> assert final_aspect_ratio > lower_bound <EOL> assert final_aspect_ratio < upper_bound <EOL> assert final_width <= initial_width / window_ratio <EOL> assert final_height <= initial_height / window_ratio <EOL> def test_responsive_maintains_a_minimum_width ( output_file_url , selenium ) : <EOL> plot = figure ( plot_width = <NUM_LIT> , plot_height = <NUM_LIT> , responsive = True ) <EOL> plot . scatter ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , [ <NUM_LIT:3> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> save ( plot ) <EOL> selenium . set_window_size ( width = <NUM_LIT:100> , height = <NUM_LIT> ) <EOL> selenium . get ( output_file_url ) <EOL> canvas = selenium . find_element_by_tag_name ( '<STR_LIT>' ) <EOL> wait_for_canvas_resize ( canvas , selenium ) <EOL> assert canvas . size [ '<STR_LIT:width>' ] < <NUM_LIT> <EOL> assert canvas . size [ '<STR_LIT:width>' ] >= <NUM_LIT:100> <EOL> def test_responsive_maintains_a_minimum_height ( output_file_url , selenium ) : <EOL> plot = figure ( plot_width = <NUM_LIT> , plot_height = <NUM_LIT> , responsive = True ) <EOL> plot . scatter ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , [ <NUM_LIT:3> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> save ( plot ) <EOL> selenium . set_window_size ( width = <NUM_LIT:100> , height = <NUM_LIT> ) <EOL> selenium . get ( output_file_url ) <EOL> canvas = selenium . find_element_by_tag_name ( '<STR_LIT>' ) <EOL> wait_for_canvas_resize ( canvas , selenium ) <EOL> assert canvas . size [ '<STR_LIT>' ] < <NUM_LIT> <EOL> assert canvas . size [ '<STR_LIT>' ] >= <NUM_LIT:100> <EOL> def test_responsive_chart_starts_at_correct_size ( output_file_url , selenium ) : <EOL> hist = Histogram ( df [ '<STR_LIT>' ] , title = "<STR_LIT>" , responsive = True ) <EOL> save ( hist ) <EOL> selenium . set_window_size ( width = <NUM_LIT:1000> , height = <NUM_LIT> ) <EOL> selenium . get ( output_file_url ) <EOL> canvas = selenium . find_element_by_tag_name ( '<STR_LIT>' ) <EOL> wait_for_canvas_resize ( canvas , selenium ) <EOL> assert canvas . size [ '<STR_LIT:width>' ] > <NUM_LIT> <EOL> assert canvas . size [ '<STR_LIT:width>' ] < <NUM_LIT:1000> <EOL> def test_responsive_legacy_chart_starts_at_correct_size ( output_file_url , selenium ) : <EOL> values = dict ( <EOL> apples = [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:7> , <NUM_LIT:5> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:2> , <NUM_LIT> , <NUM_LIT:10> , <NUM_LIT:11> ] , <EOL> oranges = [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT:10> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> ) <EOL> area = Area ( values , title = "<STR_LIT>" , responsive = True ) <EOL> save ( area ) <EOL> selenium . set_window_size ( width = <NUM_LIT:1000> , height = <NUM_LIT> ) <EOL> selenium . get ( output_file_url ) <EOL> canvas = selenium . find_element_by_tag_name ( '<STR_LIT>' ) <EOL> wait_for_canvas_resize ( canvas , selenium ) <EOL> assert canvas . size [ '<STR_LIT:width>' ] > <NUM_LIT> <EOL> assert canvas . size [ '<STR_LIT:width>' ] < <NUM_LIT:1000> <EOL> def test_responsive_plot_starts_at_correct_size ( output_file_url , selenium ) : <EOL> plot = figure ( responsive = True , title = "<STR_LIT>" ) <EOL> plot . scatter ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , [ <NUM_LIT:3> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> save ( plot ) <EOL> selenium . set_window_size ( width = <NUM_LIT:1000> , height = <NUM_LIT> ) <EOL> selenium . get ( output_file_url ) <EOL> canvas = selenium . find_element_by_tag_name ( '<STR_LIT>' ) <EOL> wait_for_canvas_resize ( canvas , selenium ) <EOL> assert canvas . size [ '<STR_LIT:width>' ] > <NUM_LIT> <EOL> assert canvas . size [ '<STR_LIT:width>' ] < <NUM_LIT:1000> <EOL> def test_responsive_resizes_width_and_height ( output_file_url , selenium ) : <EOL> aspect_ratio = <NUM_LIT:2> <EOL> plot_height = <NUM_LIT> <EOL> plot_width = <NUM_LIT> * aspect_ratio <EOL> lower_bound = aspect_ratio * <NUM_LIT> <EOL> upper_bound = aspect_ratio * <NUM_LIT> <EOL> template = Template ( '''<STR_LIT>''' ) <EOL> PLOT_OPTIONS = dict ( plot_width = plot_width , plot_height = plot_height ) <EOL> SCATTER_OPTIONS = dict ( size = <NUM_LIT:12> , alpha = <NUM_LIT:0.5> ) <EOL> data = lambda : [ random . choice ( [ i for i in range ( <NUM_LIT:100> ) ] ) for r in range ( <NUM_LIT:10> ) ] <EOL> red = figure ( responsive = False , tools = '<STR_LIT>' , ** PLOT_OPTIONS ) <EOL> red . scatter ( data ( ) , data ( ) , color = "<STR_LIT>" , ** SCATTER_OPTIONS ) <EOL> resources = INLINE <EOL> js_resources = resources . render_js ( ) <EOL> css_resources = resources . render_css ( ) <EOL> script , div = components ( { '<STR_LIT>' : red } ) <EOL> html = template . render ( js_resources = js_resources , <EOL> css_resources = css_resources , <EOL> plot_script = script , <EOL> plot_div = div ) <EOL> filename = output_file_url . split ( '<STR_LIT:/>' , <NUM_LIT:3> ) [ - <NUM_LIT:1> ] <EOL> with open ( filename , '<STR_LIT:w>' ) as f : <EOL> f . write ( html ) <EOL> selenium . set_window_size ( width = <NUM_LIT> , height = <NUM_LIT> ) <EOL> selenium . get ( output_file_url ) <EOL> canvas = selenium . find_element_by_tag_name ( '<STR_LIT>' ) <EOL> wait_for_canvas_resize ( canvas , selenium ) <EOL> height1 = canvas . size [ '<STR_LIT>' ] <EOL> width1 = canvas . size [ '<STR_LIT:width>' ] <EOL> aspect_ratio1 = width1 / height1 <EOL> assert aspect_ratio1 > lower_bound <EOL> assert aspect_ratio1 < upper_bound <EOL> selenium . set_window_size ( width = <NUM_LIT> , height = <NUM_LIT> ) <EOL> wait_for_canvas_resize ( canvas , selenium ) <EOL> height2 = canvas . size [ '<STR_LIT>' ] <EOL> width2 = canvas . size [ '<STR_LIT:width>' ] <EOL> aspect_ratio2 = width2 / height2 <EOL> assert aspect_ratio2 > lower_bound <EOL> assert aspect_ratio2 < upper_bound <EOL> assert width2 < width1 - <NUM_LIT:20> <EOL> assert height2 < height1 - <NUM_LIT:20> <EOL> selenium . set_window_size ( width = <NUM_LIT> , height = <NUM_LIT> ) <EOL> wait_for_canvas_resize ( canvas , selenium ) <EOL> height3 = canvas . size [ '<STR_LIT>' ] <EOL> width3 = canvas . size [ '<STR_LIT:width>' ] <EOL> assert width3 == width1 <EOL> assert height3 == height1 <EOL> selenium . set_window_size ( width = <NUM_LIT> , height = <NUM_LIT> ) <EOL> wait_for_canvas_resize ( canvas , selenium ) <EOL> height4 = canvas . size [ '<STR_LIT>' ] <EOL> width4 = canvas . size [ '<STR_LIT:width>' ] <EOL> aspect_ratio4 = width4 / height4 <EOL> assert aspect_ratio4 > lower_bound <EOL> assert aspect_ratio4 < upper_bound <EOL> assert width4 < width1 - <NUM_LIT:20> <EOL> assert height4 < height1 - <NUM_LIT:20> <EOL> selenium . set_window_size ( width = <NUM_LIT> , height = <NUM_LIT> ) <EOL> wait_for_canvas_resize ( canvas , selenium ) <EOL> height5 = canvas . size [ '<STR_LIT>' ] <EOL> width5 = canvas . size [ '<STR_LIT:width>' ] <EOL> assert width5 == width1 <EOL> assert height5 == height1 </s>
