-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtest_textinator.py
249 lines (207 loc) · 8.37 KB
/
test_textinator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
"""Tests for Textinator"""
import os
from time import sleep
from .conftest import (
click_menu_item,
click_window_button,
copy_to_desktop,
log_file,
mark_screenshot,
process_is_running,
)
from .loginitems import list_login_items, remove_login_item
TEST_FILE_HELLO_WORLD = "tests/data/hello_world.png"
TEST_FILE_HELLO_WORLD_LINEBREAK = "tests/data/hello_world_linebreaks.png"
TEST_FILE_HELLO = "tests/data/hello.png"
TEST_FILE_WORLD = "tests/data/world.png"
TEST_QRCODE = "tests/data/qrcode.png"
TEST_QRCODE_WITH_TEXT = "tests/data/qrcode_with_text.png"
def test_screenshot_basic(pb):
"""Test screenshot detection"""
pb.clear()
with log_file() as log:
with copy_to_desktop(TEST_FILE_HELLO_WORLD) as filepath:
mark_screenshot(filepath)
sleep(5)
assert pb.get_text() == "Hello World"
assert "notification: Processed Screenshot" in log.read()
def test_screenshot_linebreak(pb):
"""Test screenshot detection with linebreaks"""
pb.clear()
with log_file() as log:
with copy_to_desktop(TEST_FILE_HELLO_WORLD_LINEBREAK) as filepath:
mark_screenshot(filepath)
sleep(5)
assert pb.get_text() == "Hello\nWorld"
assert "notification: Processed Screenshot" in log.read()
def test_screenshot_no_notification(pb):
"""Test screenshot detection with no notification"""
assert click_menu_item("Notification")
pb.clear()
with log_file() as log:
with copy_to_desktop(TEST_FILE_HELLO_WORLD) as filepath:
mark_screenshot(filepath)
sleep(5)
assert pb.get_text() == "Hello World"
assert "notification:" not in log.read()
# turn notification back on
assert click_menu_item("Notification")
def test_screenshot_append(pb):
"""Test screenshot detection with append"""
assert click_menu_item("Append to Clipboard")
pb.clear()
with copy_to_desktop(TEST_FILE_HELLO) as filepath:
mark_screenshot(filepath)
sleep(5)
with copy_to_desktop(TEST_FILE_WORLD) as filepath:
mark_screenshot(filepath)
sleep(5)
assert pb.get_text() == "Hello\nWorld"
# turn append off
assert click_menu_item("Append to Clipboard")
def test_screenshot_qrcode(pb):
"""Test screenshot detection with QR code"""
assert click_menu_item("Detect QR Codes")
# set confidence to high because sometimes the QR code is detected as text
assert click_menu_item("Text Detection Confidence Threshold", "High")
pb.clear()
with copy_to_desktop(TEST_QRCODE) as filepath:
mark_screenshot(filepath)
sleep(5)
assert pb.get_text() == "https://github.com/RhetTbull/textinator"
assert click_menu_item("Detect QR Codes")
assert click_menu_item("Text Detection Confidence Threshold", "Low")
def test_screenshot_qrcode_with_text(pb):
"""Test screenshot detection with QR code and text"""
assert click_menu_item("Detect QR Codes")
pb.clear()
with copy_to_desktop(TEST_QRCODE_WITH_TEXT) as filepath:
mark_screenshot(filepath)
sleep(5)
text = pb.get_text()
assert "https://github.com/RhetTbull/textinator" in text
assert "SCAN ME" in text
assert click_menu_item("Detect QR Codes")
def test_screenshot_qrcode_with_text_no_detect(pb):
"""Test screenshot detection with QR code and text when QR code detection is off"""
pb.clear()
with copy_to_desktop(TEST_QRCODE_WITH_TEXT) as filepath:
mark_screenshot(filepath)
sleep(5)
text = pb.get_text()
assert "https://github.com/RhetTbull/textinator" not in text
assert "SCAN ME" in text
def test_pause(pb):
"""Test pause"""
pb.clear()
pb.set_text("Paused")
assert click_menu_item("Pause Text Detection")
with log_file() as log:
with copy_to_desktop(TEST_FILE_HELLO_WORLD) as filepath:
mark_screenshot(filepath)
sleep(5)
assert pb.get_text() == "Paused"
assert "skipping screenshot because app is paused:" in log.read()
with log_file() as log:
assert click_menu_item("Resume text detection")
with copy_to_desktop(TEST_FILE_HELLO_WORLD) as filepath:
mark_screenshot(filepath)
sleep(5)
assert pb.get_text() == "Hello World"
assert "notification: Processed Screenshot" in log.read()
def test_confidence(pb):
"""Test text detection confidence menu"""
pb.clear()
with log_file() as log:
assert click_menu_item("Text Detection Confidence Threshold", "Medium")
assert "'confidence': 'MEDIUM'" in log.read()
with copy_to_desktop(TEST_FILE_HELLO_WORLD) as filepath:
mark_screenshot(filepath)
sleep(5)
assert pb.get_text() == "Hello World"
assert click_menu_item("Text Detection Confidence Threshold", "Low")
assert "'confidence': 'LOW'" in log.read()
def test_clipboard_basic(pb):
"""Test clipboard detection"""
pb.clear()
pb.set_image(TEST_FILE_HELLO_WORLD, "PNG")
sleep(5)
assert pb.get_text() == "Hello World"
def test_clipboard_text_and_image(pb):
"""Test clipboard detection when clipboard has text and image (#16)"""
pb.clear()
with log_file() as log:
pb.set_text_and_image("Alt Text", TEST_FILE_HELLO_WORLD, "PNG")
sleep(5)
assert "clipboard has text, skipping" in log.read()
assert pb.get_text() == "Alt Text"
def test_clipboard_no_clipboard(pb):
"""Test clipboard detection does not run when "Detect Text in Images on Clipboard" is off"""
assert click_menu_item("Detect Text in Images on Clipboard")
pb.clear()
pb.set_image(TEST_FILE_HELLO_WORLD, "PNG")
sleep(5)
assert pb.get_text() == ""
assert click_menu_item("Detect Text in Images on Clipboard")
def test_clear_clipboard(pb):
"""Test Clear Clipboard menu item works"""
pb.set_text("Hello World")
assert click_menu_item("Clear Clipboard")
assert pb.get_text() == ""
def test_confirm_clipboard_changes_yes(pb):
"""Test Confirm Clipboard Changes menu item works when pressing Yes"""
pb.clear()
with log_file() as log:
assert click_menu_item("Confirm Clipboard Changes")
assert "'confirmation': 1" in log.read()
with copy_to_desktop(TEST_FILE_HELLO_WORLD) as filepath:
mark_screenshot(filepath)
sleep(5)
assert click_window_button(1, 2) # button 1 is Yes
sleep(5)
assert pb.get_text() == "Hello World"
assert click_menu_item("Confirm Clipboard Changes")
def test_confirm_clipboard_changes_no(pb):
"""Test Confirm Clipboard Changes menu item works when pressing No"""
pb.set_text("Nope")
with log_file() as log:
assert click_menu_item("Confirm Clipboard Changes")
assert "'confirmation': 1" in log.read()
with copy_to_desktop(TEST_FILE_HELLO_WORLD) as filepath:
mark_screenshot(filepath)
sleep(5)
assert click_window_button(1, 1) # button 2 is "No"
sleep(5)
assert pb.get_text() == "Nope"
assert click_menu_item("Confirm Clipboard Changes")
def test_show_last_text_detection(pb):
"""Test Show Last Text Detection menu item works"""
pb.clear()
with copy_to_desktop(TEST_FILE_HELLO_WORLD) as filepath:
mark_screenshot(filepath)
sleep(5)
with log_file() as log:
assert click_menu_item("Show Last Text Detection")
assert "Showing confirmation window" in log.read()
assert click_window_button(1, 2) # button 1 is Yes
sleep(5)
assert pb.get_text() == "Hello World"
assert click_menu_item("Confirm Clipboard Changes")
def test_enable_start_on_login():
"""Test Start Textinator on Login menu item works"""
# setup_teardown() should have removed the login item if it existed
assert "Textinator" not in list_login_items()
assert click_menu_item("Start Textinator on Login")
assert "Textinator" in list_login_items()
assert click_menu_item("Start Textinator on Login")
assert "Textinator" not in list_login_items()
def test_about():
"""Test About dialog"""
assert click_menu_item("About Textinator")
assert click_window_button(1, 1)
def test_quit():
"""Test Quit menu item"""
assert process_is_running("Textinator")
assert click_menu_item("Quit Textinator")
assert not process_is_running("Textinator")
os.system("open -a Textinator")