Skip to content

Commit 93322b6

Browse files
committed
Test for TerminalBox and absolute imports
1 parent 5189364 commit 93322b6

9 files changed

Lines changed: 168 additions & 57 deletions

File tree

openandroidinstaller/app_state.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from pathlib import Path
1717

1818
from flet import ProgressBar
19-
from installer_config import _load_config
19+
from openandroidinstaller.installer_config import _load_config
2020

2121

2222
class AppState:
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
from .base import BaseView # noqa
2-
from .start_view import StartView # noqa
3-
from .requirements_view import RequirementsView # noqa
4-
from .select_view import SelectFilesView # noqa
5-
from .step_view import StepView # noqa
6-
from .success_view import SuccessView # noqa
1+
from openandroidinstaller.views.base import BaseView # noqa
2+
from openandroidinstaller.views.start_view import StartView # noqa
3+
from openandroidinstaller.views.requirements_view import RequirementsView # noqa
4+
from openandroidinstaller.views.select_view import SelectFilesView # noqa
5+
from openandroidinstaller.views.step_view import StepView # noqa
6+
from openandroidinstaller.views.success_view import SuccessView # noqa

openandroidinstaller/views/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# If not, see <https://www.gnu.org/licenses/>."""
1414
# Author: Tobias Sterbak
1515

16-
from app_state import AppState
16+
from openandroidinstaller.app_state import AppState
1717

1818
from flet import (
1919
Column,

openandroidinstaller/views/requirements_view.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
icons,
2929
)
3030

31-
from views import BaseView
32-
from app_state import AppState
33-
from widgets import get_title
31+
from openandroidinstaller.views import BaseView
32+
from openandroidinstaller.app_state import AppState
33+
from openandroidinstaller.widgets import get_title
3434

3535

3636
class RequirementsView(BaseView):

openandroidinstaller/views/select_view.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,13 @@
2929
FilePickerResultEvent,
3030
)
3131

32-
from views import BaseView
33-
from app_state import AppState
34-
from widgets import get_title, confirm_button
35-
from utils import get_download_link, image_recovery_works_with_device
32+
from openandroidinstaller.views import BaseView
33+
from openandroidinstaller.app_state import AppState
34+
from openandroidinstaller.widgets import get_title, confirm_button
35+
from openandroidinstaller.utils import (
36+
get_download_link,
37+
image_recovery_works_with_device,
38+
)
3639

3740

3841
class SelectFilesView(BaseView):

openandroidinstaller/views/start_view.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@
3131
icons,
3232
)
3333

34-
from views import BaseView
35-
from app_state import AppState
36-
from widgets import get_title
37-
from tooling import search_device
34+
from openandroidinstaller.views import BaseView
35+
from openandroidinstaller.app_state import AppState
36+
from openandroidinstaller.widgets import get_title
37+
from openandroidinstaller.tooling import search_device
3838

3939

4040
class StartView(BaseView):

openandroidinstaller/views/step_view.py

Lines changed: 52 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
from loguru import logger
1717
from time import sleep
1818
from typing import Callable
19-
from pathlib import Path
2019
from functools import partial
2120

2221
from flet import (
@@ -34,10 +33,10 @@
3433
ProgressBar,
3534
)
3635

37-
from views import BaseView
38-
from installer_config import Step
39-
from app_state import AppState
40-
from tooling import (
36+
from openandroidinstaller.views import BaseView
37+
from openandroidinstaller.installer_config import Step
38+
from openandroidinstaller.app_state import AppState
39+
from openandroidinstaller.tooling import (
4140
adb_reboot,
4241
adb_reboot_bootloader,
4342
adb_reboot_download,
@@ -50,7 +49,12 @@
5049
fastboot_unlock_with_code,
5150
heimdall_flash_recovery,
5251
)
53-
from widgets import call_button, confirm_button, get_title, link_button
52+
from openandroidinstaller.widgets import (
53+
call_button,
54+
confirm_button,
55+
get_title,
56+
link_button,
57+
)
5458

5559

5660
class StepView(BaseView):
@@ -110,16 +114,18 @@ def check_advanced_switch(e):
110114
self.call_button = call_button(
111115
self.call_to_phone, command=self.step.command
112116
)
113-
self.right_view.controls.extend([
114-
Row([self.error_text]),
115-
Column(
116-
[
117-
self.advanced_switch,
118-
Row([self.call_button, self.confirm_button]),
119-
]
120-
),
121-
Row([self.terminal_box])
122-
])
117+
self.right_view.controls.extend(
118+
[
119+
Row([self.error_text]),
120+
Column(
121+
[
122+
self.advanced_switch,
123+
Row([self.call_button, self.confirm_button]),
124+
]
125+
),
126+
Row([self.terminal_box]),
127+
]
128+
)
123129
elif self.step.type == "call_button_with_input":
124130
self.confirm_button.disabled = True
125131
self.call_button = call_button(
@@ -135,7 +141,7 @@ def check_advanced_switch(e):
135141
Row([self.call_button, self.confirm_button]),
136142
]
137143
),
138-
Row([self.terminal_box])
144+
Row([self.terminal_box]),
139145
]
140146
)
141147
elif self.step.type == "link_button_with_confirm":
@@ -189,13 +195,23 @@ def call_to_phone(self, e, command: str):
189195
"adb_reboot_bootloader": adb_reboot_bootloader,
190196
"adb_reboot_download": adb_reboot_download,
191197
"adb_sideload": partial(adb_sideload, target=self.state.image_path),
192-
"adb_twrp_wipe_and_install": partial(adb_twrp_wipe_and_install, target=self.state.image_path, config_path=self.state.config_path),
198+
"adb_twrp_wipe_and_install": partial(
199+
adb_twrp_wipe_and_install,
200+
target=self.state.image_path,
201+
config_path=self.state.config_path,
202+
),
193203
"fastboot_unlock": fastboot_unlock,
194-
"fastboot_unlock_with_code": partial(fastboot_unlock_with_code, unlock_code=self.inputtext.value),
204+
"fastboot_unlock_with_code": partial(
205+
fastboot_unlock_with_code, unlock_code=self.inputtext.value
206+
),
195207
"fastboot_oem_unlock": fastboot_oem_unlock,
196-
"fastboot_flash_recovery": partial(fastboot_flash_recovery, recovery=self.state.recovery_path),
208+
"fastboot_flash_recovery": partial(
209+
fastboot_flash_recovery, recovery=self.state.recovery_path
210+
),
197211
"fastboot_reboot": fastboot_reboot,
198-
"heimdall_flash_recovery": partial(heimdall_flash_recovery, recovery=self.state.recovery_path),
212+
"heimdall_flash_recovery": partial(
213+
heimdall_flash_recovery, recovery=self.state.recovery_path
214+
),
199215
}
200216

201217
# run the right command
@@ -229,12 +245,11 @@ def call_to_phone(self, e, command: str):
229245

230246

231247
class TerminalBox(UserControl):
232-
233248
def __init__(self, expand: bool = True):
234249
super().__init__(expand=expand)
235250

236251
def build(self):
237-
self.box = Container(
252+
self._box = Container(
238253
content=Column(scroll="auto", expand=True),
239254
margin=10,
240255
padding=10,
@@ -243,28 +258,30 @@ def build(self):
243258
height=300,
244259
border_radius=2,
245260
expand=True,
246-
visible=False
261+
visible=False,
247262
)
248-
return self.box
263+
return self._box
249264

250265
def write_line(self, line: str):
251266
"""
252267
Write the line to the window box and update.
253-
268+
254269
Ignores empty lines.
255270
"""
256271
if (type(line) == str) and line.strip():
257-
self.box.content.controls.append(
258-
Text(f">{line.strip()}", selectable=True)
259-
)
260-
self.box.update()
261-
272+
self._box.content.controls.append(Text(f">{line.strip()}", selectable=True))
273+
self.update()
274+
262275
def toggle_visibility(self):
263276
"""Toogle the visibility of the terminal box."""
264-
self.box.visible = not self.box.visible
265-
self.box.update()
277+
self._box.visible = not self._box.visible
278+
self.update()
266279

267280
def clear(self):
268281
"""Clear terminal output."""
269-
self.box.content.controls = []
270-
self.box.update()
282+
self._box.content.controls = []
283+
self.update()
284+
285+
def update(self):
286+
"""Update the view."""
287+
self._box.update()

openandroidinstaller/views/success_view.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
Text,
2121
)
2222

23-
from views import BaseView
24-
from app_state import AppState
25-
from widgets import get_title
23+
from openandroidinstaller.views import BaseView
24+
from openandroidinstaller.app_state import AppState
25+
from openandroidinstaller.widgets import get_title
2626

2727

2828
class SuccessView(BaseView):

tests/test_terminal_box.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
"""Test the TerminalBox class."""
2+
3+
# This file is part of OpenAndroidInstaller.
4+
# OpenAndroidInstaller is free software: you can redistribute it and/or modify it under the terms of
5+
# the GNU General Public License as published by the Free Software Foundation,
6+
# either version 3 of the License, or (at your option) any later version.
7+
8+
# OpenAndroidInstaller is distributed in the hope that it will be useful, but WITHOUT ANY
9+
# WARRANTY; without even the implied warranty of MERCHANTABILITY or
10+
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
11+
12+
# You should have received a copy of the GNU General Public License along with OpenAndroidInstaller.
13+
# If not, see <https://www.gnu.org/licenses/>."""
14+
# Author: Tobias Sterbak
15+
16+
import pytest
17+
from flet import Container, Page
18+
19+
from openandroidinstaller.views.step_view import TerminalBox
20+
21+
22+
def test_init_box():
23+
"""Test if the box can be initialized properly."""
24+
terminal_box = TerminalBox(expand=True)
25+
build_box = terminal_box.build()
26+
27+
assert isinstance(build_box, Container)
28+
29+
30+
def test_write_lines(mocker):
31+
"""Test if we can write lines to the terminal and bools are ignored."""
32+
mocker.patch(
33+
"openandroidinstaller.views.step_view.TerminalBox.update",
34+
return_value=True,
35+
new_callable=mocker.Mock,
36+
)
37+
38+
terminal_box = TerminalBox(expand=True)
39+
_ = terminal_box.build()
40+
41+
# write some lines
42+
for line in ["test", "test_line2", True]:
43+
terminal_box.write_line(line)
44+
45+
# two text elements should appear
46+
assert len(terminal_box._box.content.controls) == 2
47+
48+
49+
def test_toggle_visibility(mocker):
50+
"""Test if the visibility toggle method works."""
51+
mocker.patch(
52+
"openandroidinstaller.views.step_view.TerminalBox.update",
53+
return_value=True,
54+
new_callable=mocker.Mock,
55+
)
56+
57+
terminal_box = TerminalBox(expand=True)
58+
_ = terminal_box.build()
59+
60+
# should be non-visible at the beginning
61+
assert terminal_box._box.visible == False
62+
# now toggle
63+
terminal_box.toggle_visibility()
64+
# now should be visible
65+
assert terminal_box._box.visible == True
66+
# now toggle again
67+
terminal_box.toggle_visibility()
68+
# now it should be non-visible again
69+
assert terminal_box._box.visible == False
70+
71+
72+
def test_clear_terminal(mocker):
73+
"""Test if the terminal can be cleared properly."""
74+
mocker.patch(
75+
"openandroidinstaller.views.step_view.TerminalBox.update",
76+
return_value=True,
77+
new_callable=mocker.Mock,
78+
)
79+
80+
terminal_box = TerminalBox(expand=True)
81+
_ = terminal_box.build()
82+
83+
# write some lines
84+
for line in ["test", "test_line2", True]:
85+
terminal_box.write_line(line)
86+
87+
# now clear
88+
terminal_box.clear()
89+
90+
# two text elements should appear
91+
assert len(terminal_box._box.content.controls) == 0

0 commit comments

Comments
 (0)