1+ import base64
2+ import io
3+
4+ from PIL import Image
5+
6+ from odoo import exceptions
7+
18# Copyright 2024 Tecnativa - Víctor Martínez
29# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
3-
410from odoo .tests .common import tagged
511
612from odoo .addons .base .tests .common import HttpCaseWithUserDemo
@@ -24,3 +30,89 @@ def test_webmanifest_customize(self):
2430 self .assertEqual (data ["short_name" ], "SHORT-NAME" )
2531 self .assertEqual (data ["background_color" ], "#2E69B5" )
2632 self .assertEqual (data ["theme_color" ], "#2E69B4" )
33+
34+ def test_pwa_settings_processing (self ):
35+ """Test the settings logic and icon attachment generation"""
36+
37+ # 1. Test PNG (valid 512x512)
38+ img = Image .new ("RGB" , (512 , 512 ), color = "red" )
39+ img_byte_arr = io .BytesIO ()
40+ img .save (img_byte_arr , format = "PNG" )
41+ icon_base64 = base64 .b64encode (img_byte_arr .getvalue ())
42+
43+ config = self .env ["res.config.settings" ].create (
44+ {
45+ "pwa_short_name" : "New Name" ,
46+ "pwa_icon" : icon_base64 ,
47+ }
48+ )
49+ config .execute ()
50+
51+ # Check manifest endpoint with icons
52+ response = self .url_open ("/web/manifest.webmanifest" )
53+ data = response .json ()
54+ self .assertIn ("icons" , data )
55+ self .assertGreaterEqual (len (data ["icons" ]), 6 )
56+
57+ # 2. Test SVG (valid)
58+ svg_content = (
59+ '<svg width="100" height="100">'
60+ '<circle cx="50" cy="50" r="40" stroke="green" '
61+ 'stroke-width="4" fill="yellow" />'
62+ "</svg>"
63+ )
64+ svg_base64 = base64 .b64encode (svg_content )
65+ config .write ({"pwa_icon" : svg_base64 })
66+ config .execute ()
67+
68+ response = self .url_open ("/web/manifest.webmanifest" )
69+ data = response .json ()
70+ self .assertEqual (len (data ["icons" ]), 1 )
71+ self .assertEqual (data ["icons" ][0 ]["type" ], "image/svg+xml" )
72+
73+ # 3. Test Clearing Icons
74+ config .write ({"pwa_icon" : False })
75+ config .execute ()
76+ attachments = self .env ["ir.attachment" ].search (
77+ [("url" , "like" , "/web_pwa_customize/icon" )]
78+ )
79+ self .assertEqual (len (attachments ), 0 )
80+
81+ # 4. Test PNG too small (< 512x512)
82+ small_img = Image .new ("RGB" , (100 , 100 ), color = "blue" )
83+ small_img_byte_arr = io .BytesIO ()
84+ small_img .save (small_img_byte_arr , format = "PNG" )
85+ small_icon_base64 = base64 .b64encode (small_img_byte_arr .getvalue ())
86+
87+ with self .assertRaises (exceptions .UserError ):
88+ config .write ({"pwa_icon" : small_icon_base64 })
89+ config .execute ()
90+
91+ # 5. Test File too large (> 2 MB)
92+ large_content = b"0" * (2 * 1024 * 1024 + 1024 ) # Slightly over 2MB
93+ large_base64 = base64 .b64encode (large_content )
94+ with self .assertRaises (exceptions .UserError ):
95+ config .write ({"pwa_icon" : large_base64 })
96+ config .execute ()
97+
98+ # 6. Test Invalid Image type (gif)
99+ gif_img = Image .new ("RGB" , (512 , 512 ), color = "green" )
100+ gif_byte_arr = io .BytesIO ()
101+ gif_img .save (gif_byte_arr , format = "GIF" )
102+ gif_base64 = base64 .b64encode (gif_byte_arr .getvalue ())
103+ with self .assertRaises (exceptions .UserError ):
104+ config .write ({"pwa_icon" : gif_base64 })
105+ config .execute ()
106+
107+ def test_default_get_colors (self ):
108+ """Test default colors are set in res.config.settings"""
109+ # Clear ICP to test defaults
110+ icp = self .env ["ir.config_parameter" ].sudo ()
111+ icp .set_param ("pwa.manifest.background_color" , False )
112+ icp .set_param ("pwa.manifest.theme_color" , False )
113+
114+ res = self .env ["res.config.settings" ].default_get (
115+ ["pwa_background_color" , "pwa_theme_color" ]
116+ )
117+ self .assertEqual (res .get ("pwa_background_color" ), "#714B67" )
118+ self .assertEqual (res .get ("pwa_theme_color" ), "#714B67" )
0 commit comments