Skip to content

Commit a45fa76

Browse files
committed
test: add comprehensive test suite and example notifications for flet python
1 parent 5f5b61a commit a45fa76

7 files changed

Lines changed: 296 additions & 0 deletions

File tree

docs/tests/flet/adv/main.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import os,traceback,io,sys,unittest
2+
import flet as ft
3+
from contextlib import redirect_stdout
4+
from android_notify.core import get_app_root_path,asks_permission_if_needed
5+
6+
md1=''
7+
i=0
8+
def main(page: ft.Page):
9+
page.scroll = ft.ScrollMode.ADAPTIVE
10+
page.add(ft.Text("1111111111 Android Notify Test Results", size=24, weight=ft.FontWeight.BOLD))
11+
logs_path=os.path.join(get_app_root_path(),'last.txt')
12+
mdObj = ft.Markdown(
13+
md1,
14+
selectable=True,
15+
extension_set=ft.MarkdownExtensionSet.GITHUB_WEB,
16+
on_tap_link=lambda e: page.launch_url(e.data),
17+
)
18+
page.add(mdObj)
19+
def console(a):
20+
global md1,i
21+
i+=1
22+
print('Testing print visibility: ',i,'\n')
23+
try:
24+
if os.getenv("FLET_APP_CONSOLE"):
25+
with open(os.getenv("FLET_APP_CONSOLE"), "r") as f:
26+
md1 = f.read()
27+
mdObj.value = md1
28+
29+
with open(logs_path, 'r') as logf:
30+
mdObj.value = logf.read() + md1
31+
except Exception as err:
32+
mdObj.value = f"Error reading log: {err}"
33+
finally:
34+
mdObj.update()
35+
36+
37+
def send_basic(e):
38+
"""Send a notification to verify android_notify works"""
39+
try:
40+
from android_notify import Notification
41+
Notification(title="Hello World", message="From android_notify").send()
42+
except Exception as err:
43+
mdObj.value = f"Notification error: {err}"
44+
mdObj.update()
45+
def asks_permission_if_needed_(e):
46+
asks_permission_if_needed()
47+
48+
49+
def ensure_tests_folder():
50+
try:
51+
base_path = get_app_root_path()
52+
except Exception:
53+
base_path = os.path.dirname(__file__)
54+
55+
tests_path = os.path.join(base_path, "tests")
56+
os.makedirs(tests_path, exist_ok=True)
57+
init_file = os.path.join(tests_path, "__init__.py")
58+
if not os.path.exists(init_file):
59+
open(init_file, "w").close()
60+
61+
page.add(ft.Text(f"{tests_path}", size=24, weight=ft.FontWeight.BOLD))
62+
return tests_path
63+
64+
page.add(ft.Text("2222222222 Android Notify Test Results", size=24, weight=ft.FontWeight.BOLD))
65+
66+
def run_tests(e=None):
67+
"""Run tests and log results to /sdcard/flet_app_console.txt"""
68+
tests_path = ensure_tests_folder()
69+
70+
try:
71+
with open(logs_path, "w") as logf, redirect_stdout(logf):
72+
loader = unittest.TestLoader()
73+
suite = loader.discover(start_dir=tests_path, pattern="test_*.py")
74+
print("Discovered tests:",suite.countTestCases())
75+
if suite.countTestCases() ==0:
76+
print("No tests found")
77+
78+
79+
runner = unittest.TextTestRunner(stream=logf, verbosity=2)
80+
runner.run(suite)
81+
82+
mdObj.value = f"Tests complete. Log saved to:\n`{logs_path}`"
83+
except Exception as err:
84+
mdObj.value = f"Test error:\n{traceback.format_exc()}"
85+
mdObj.update()
86+
def has_per(e=None):
87+
from android_notify import NotificationHandler
88+
print('permmm:',NotificationHandler.has_permission())
89+
page.add(
90+
ft.OutlinedButton("🔔 Send Basic Notification", on_click=send_basic),
91+
ft.OutlinedButton("🔁 Refresh Prints", on_click=console),
92+
ft.OutlinedButton("🧪 Run Tests", on_click=run_tests),
93+
ft.OutlinedButton("🧪permmm", on_click=has_per),
94+
ft.OutlinedButton("🧪asks_permission_if_needed", on_click=asks_permission_if_needed_),
95+
)
96+
97+
ft.app(main)

docs/tests/flet/adv/tests/__init__.py

Whitespace-only changes.
Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
"""
2+
Comprehensive test suite for Android-Notify Tester (Laner.use_android_notify)
3+
Covers all notification styles, callbacks, and behaviors.
4+
"""
5+
import unittest
6+
import time
7+
import traceback
8+
9+
from android_notify import Notification, NotificationStyles, NotificationHandler, send_notification
10+
11+
12+
class TestAndroidNotifyFull(unittest.TestCase):
13+
def tearDown(self):
14+
time.sleep(3)
15+
16+
def setUp(self):
17+
max_int = 2_147_483_647
18+
self.uid = int(time.time() * 1000) % max_int
19+
20+
def test_simple(self):
21+
try:
22+
n = Notification( id=self.uid,title="Simple", message="Simple message test")
23+
n.send()
24+
except Exception as e:
25+
self.fail(f"Simple notification failed: {e}")
26+
27+
def test_progress(self):
28+
try:
29+
n = Notification( id=self.uid,title="Downloading...", message="0% downloaded", progress_current_value=0, progress_max_value=100)
30+
n.send()
31+
for i in range(0, 101, 10):
32+
time.sleep(2)
33+
n.updateProgressBar(i, f"{i}% done")
34+
n.removeProgressBar(message="Done", title="Download Complete")
35+
except Exception as e:
36+
self.fail(f"Progress notification failed: {e}")
37+
38+
def test_big_picture(self):
39+
try:
40+
n = Notification( id=self.uid,title="Big Picture", message="Testing big picture")
41+
n.setBigPicture("assets/icon.png")
42+
n.send()
43+
except Exception as e:
44+
self.fail(f"Big picture failed: {e}")
45+
46+
def test_inbox(self):
47+
try:
48+
n = Notification( id=self.uid,title="Inbox", message="Inbox notification")
49+
n.setLines(["Line 1", "Line 2", "Line 3"])
50+
n.send()
51+
except Exception as e:
52+
self.fail(f"Inbox notification failed: {e}")
53+
54+
def test_inbox_from_file_message(self):
55+
try:
56+
lines = "Test1\nTest2\nTest3"
57+
n = Notification( id=self.uid,title="Inbox File", message="File Inbox Test", lines_txt=lines)
58+
n.send()
59+
except Exception as e:
60+
self.fail(f"Inbox from file failed: {e}")
61+
62+
def test_inbox_add_line(self):
63+
try:
64+
n = Notification( id=self.uid,title="Inbox AddLine", message="Testing addLine()")
65+
n.addLine("First line")
66+
n.addLine("Second line")
67+
n.addLine("Third line")
68+
n.send()
69+
except Exception as e:
70+
self.fail(f"Inbox addLine failed: {e}")
71+
72+
def test_large_icon(self):
73+
try:
74+
n = Notification( id=self.uid,title="Large Icon", message="Testing large icon")
75+
n.setLargeIcon("assets/icon.png")
76+
n.send()
77+
except Exception as e:
78+
self.fail(f"Large icon failed: {e}")
79+
80+
def test_big_text(self):
81+
try:
82+
n = Notification( id=self.uid,title="Big Text", message="Testing big text")
83+
n.setBigText("Lorem Ipsum is dummy text for testing BigTextStyle display.")
84+
n.send()
85+
except Exception as e:
86+
self.fail(f"Big text failed: {e}")
87+
88+
def test_buttons(self):
89+
try:
90+
n = Notification( id=self.uid,title="With Buttons", message="Testing action buttons")
91+
n.addButton(text="Play", on_release=lambda: print("Playing"))
92+
n.addButton(text="Pause", on_release=lambda: print("Paused"))
93+
n.addButton(text="Stop", on_release=lambda: print("Stopped"))
94+
n.send()
95+
except Exception as e:
96+
self.fail(f"Buttons failed: {e}")
97+
98+
def test_both_imgs(self):
99+
try:
100+
n = Notification( id=self.uid,title="Both Images", message="Testing both large & big picture")
101+
n.setLargeIcon("assets/icon.png")
102+
n.setBigPicture("assets/icon.png")
103+
n.send()
104+
except Exception as e:
105+
self.fail(f"Both imgs failed: {e}")
106+
107+
def test_custom_icon(self):
108+
try:
109+
n = Notification( id=self.uid,title="Custom Icon", message="Testing custom app icon")
110+
n.setSmallIcon("assets/icon.png")
111+
n.send()
112+
except Exception as e:
113+
self.fail(f"Custom icon failed: {e}")
114+
115+
def test_title_message_update(self):
116+
try:
117+
n = Notification( id=self.uid,title="Old Title", message="Old Message")
118+
n.send()
119+
time.sleep(2)
120+
n.updateTitle("New Title")
121+
n.updateMessage("New Message")
122+
except Exception as e:
123+
self.fail(f"Update title/message failed: {e}")
124+
125+
def test_download_channel(self):
126+
try:
127+
n = Notification( id=self.uid,
128+
title="Download Done",
129+
message="Your file finished downloading.",
130+
channel_name="Download Notifications",
131+
channel_id="downloads_notifications"
132+
)
133+
n.send()
134+
except Exception as e:
135+
self.fail(f"Download channel failed: {e}")
136+
137+
def test_channel_generate_id(self):
138+
try:
139+
n = Notification( id=self.uid,
140+
title="Generated Channel",
141+
message="Channel creation test",
142+
channel_name="Custom Channel"
143+
)
144+
n.send()
145+
except Exception as e:
146+
self.fail(f"Channel generating ID failed: {e}")
147+
148+
def test_custom_id(self):
149+
try:
150+
n = Notification( id=self.uid,title="Custom ID", message="Click to trigger handler", name="change_app_page")
151+
n.send()
152+
except Exception as e:
153+
self.fail(f"Custom ID failed: {e}")
154+
155+
def test_callback(self):
156+
try:
157+
n = Notification( id=self.uid,title="With Callback", message="Tap to run callback", callback=lambda: print("Callback invoked"))
158+
n.send()
159+
except Exception as e:
160+
self.fail(f"Callback failed: {e}")
161+
162+
def test_custom_channel_name(self):
163+
try:
164+
n = Notification( id=self.uid,title="Custom Channel", message="Testing custom name", channel_name="MyChannelName")
165+
n.send()
166+
except Exception as e:
167+
self.fail(f"Custom channel name failed: {e}")
168+
169+
def test_cancel_all(self):
170+
try:
171+
Notification.cancelAll()
172+
except Exception as e:
173+
self.fail(f"Cancel all failed: {e}")
174+
175+
def test_create_channel_lifespan(self):
176+
try:
177+
n = Notification( id=self.uid,title="Check ID Lifespan", message="Testing channel id reuse")
178+
cid = "test_channel_id"
179+
n.createChannel(id=cid, name="Test Channel", description="Lifespan check")
180+
n.send()
181+
except Exception as e:
182+
self.fail(f"Create channel lifespan failed: {e}")
183+
184+
def test_persistent(self):
185+
try:
186+
n = Notification( id=self.uid,title="Persistent", message="This notification should persist")
187+
n.send(persistent=True)
188+
except Exception as e:
189+
self.fail(f"Persistent notification failed: {e}")
190+
191+
# def test_get_active_notifications(self):
192+
# try:
193+
# Notification( id=self.uid,).get_active_notifications()
194+
# except Exception as e:
195+
# self.fail(f"Get active notifications failed: {e}")
196+
197+
198+
if __name__ == "__main__":
199+
unittest.main(verbosity=2)
File renamed without changes.

0 commit comments

Comments
 (0)