1# -*- coding: UTF-8 -*-
2from behave import step, then
3
4from common_steps import *
5from time import sleep
6from dogtail.rawinput import keyCombo
7from subprocess import Popen, PIPE
8from dogtail import i18n
9
10
11@step(u'Open About dialog')
12def open_about_dialog(context):
13    context.execute_steps(u"""
14        * Click "About" in GApplication menu
15    """)
16    context.about_dialog = context.app.dialog(translate('About Image Viewer'))
17
18
19@step(u'Open and close About dialog')
20def open_and_close_about_dialog(context):
21    context.execute_steps(u'* Click "About" in GApplication menu')
22    keyCombo("<Esc>")
23
24
25@then(u'Website link to wiki is displayed')
26def website_link_to_wiki_is_displayed(context):
27    assert context.about_dialog.child(translate('Website')).showing
28
29
30@then(u'GPL 2.0 link is displayed')
31def gpl_license_link_is_displayed(context):
32    assert context.about_dialog.child(translate("Image Viewer")).showing, "App name is not displayed"
33    assert context.about_dialog.child(translate("The GNOME image viewer.")).showing, "App description is not displayed"
34    assert context.about_dialog.child(translate("Website")).showing, "Website link is not displayed"
35    assert context.about_dialog.child(roleName='radio button', name=translate("About")).checked, "About tab is not selected"
36    assert not context.about_dialog.child(roleName='radio button', name=translate("Credits")).checked, "Credits tab is selected"
37
38
39@step(u'Open "{filename}" via menu')
40def open_file_via_menu(context, filename):
41    keyCombo("<Ctrl>O")
42    context.execute_steps(u"""
43        * file select dialog with name "Open Image" is displayed
44        * In file select dialog select "%s"
45    """ % filename)
46    sleep(0.5)
47
48
49@then(u'image size is {width:d}x{height:d}')
50def image_size_is(context, width, height):
51    size_text = None
52    for attempt in range(0, 10):
53        size_child = context.app.child(roleName='page tab list').child(translate('Size'))
54        size_text = size_child.parent.children[11].text
55        if size_text == '':
56            sleep(0.5)
57            continue
58        else:
59            break
60    try:
61        actual_width = size_text.split(' \xc3\x97 ')[0].strip()
62        actual_height = size_text.split(' \xc3\x97 ')[1].split(' ')[0].strip()
63    except Exception:
64        raise Exception("Incorrect width/height is been displayed")
65    assert int(actual_width) == width, "Expected width to be '%s', but was '%s'" % (width, actual_width)
66    assert int(actual_height) == height, "Expected height to be '%s', but was '%s'" % (height, actual_height)
67
68
69@step(u'Rotate the image clockwise')
70def rotate_image_clockwise(context):
71    btn = context.app.child(description=translate('Rotate the image 90 degrees to the left'))
72    context.app.child(roleName='drawing area').point()
73    sleep(1)
74    btn.click()
75
76
77@step(u'Click Fullscreen button on headerbar')
78def click_fullscreen(context):
79    context.app.child(translate('Fullscreen')).click()
80
81
82@step(u'Open context menu for current image')
83def open_context_menu(context):
84    context.app.child(roleName='drawing area').click(button=3)
85    sleep(0.1)
86
87
88@step(u'Select "{item}" from context menu')
89def select_item_from_context_menu(context, item):
90    context.app.child(roleName='drawing area').click(button=3)
91    sleep(0.1)
92    context.app.child(roleName='window').menuItem(item).click()
93
94
95@then(u'sidepanel is {state:w}')
96def sidepanel_displayed(context, state):
97    sleep(0.5)
98    assert state in ['displayed', 'hidden'], "Incorrect state: %s" % state
99    actual = context.app.child(roleName='page tab list').showing
100    assert actual == (state == 'displayed')
101
102
103@then(u'application is {negative:w} fullscreen anymore')
104@then(u'application is displayed fullscreen')
105def app_displayed_fullscreen(context, negative=None):
106    sleep(0.5)
107    actual = context.app.child(roleName='drawing area').position[1] == 0
108    assert actual == (negative is None)
109
110
111@step(u'Wait a second')
112def wait_a_second(context):
113    sleep(1)
114
115
116@step(u'Click "Hide" in wallpaper popup')
117def hide_wallapper_popup(context):
118    context.app.button(translate('Hide')).click()
119
120
121@then(u'wallpaper is set to "{filename}"')
122def wallpaper_is_set_to(context, filename):
123    wallpaper_path = Popen(["gsettings", "get", "org.gnome.desktop.background", "picture-uri"], stdout=PIPE).stdout.read()
124    actual_filename = wallpaper_path.split('/')[-1].split("'")[0]
125    assert filename == actual_filename
126
127
128@then(u'"{filename}" file exists')
129def file_exists(context, filename):
130    assert os.path.isfile(os.path.expanduser(filename))
131
132
133@then(u'image type is "{mimetype}"')
134def image_type_is(context, mimetype):
135    imagetype = context.app.child(roleName='page tab list').child(translate('Type:')).parent.children[-1].text
136    assert imagetype == mimetype
137
138
139@step(u'Select "{menu}" menu')
140def select_menuitem(context, menu):
141    menu_item = menu.split(' -> ')
142    # First level menu
143    current = context.app.menu(translate(menu_item[0]))
144    current.click()
145    if len(menu_item) == 1:
146        return
147    # Intermediate menus - point them but don't click
148    for item in menu_item[1:-1]:
149        current = context.app.menu(translate(item))
150        current.point()
151    # Last level menu item
152    current.menuItem(translate(menu_item[-1])).click()
153
154
155@step(u'Open and close hamburger menu')
156def select_hamburger_and_close_it(context):
157    context.app.child('Menu').click()
158    keyCombo("<Esc>")
159
160
161@step(u'Select "{name}" window')
162def select_name_window(context, name):
163    context.app = context.app.child(roleName='frame', name=translate(name))
164    context.app.grab_focus()
165