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