summaryrefslogtreecommitdiff
path: root/odoo/addons/base/tests/test_base_document_layout.py
diff options
context:
space:
mode:
authorJulien Mougenot <jum@odoo.com>2019-07-18 15:18:41 +0000
committerVincent Schippefilt <vsc@odoo.com>2019-07-29 08:29:26 +0000
commited18095127b20da516fa5ada943b2dd6d5c84652 (patch)
tree98529e7fd7dc90d1a48173114878594a43c31555 /odoo/addons/base/tests/test_base_document_layout.py
parenta8c93df9c8663ae90afbabeeffeff7d509aeaa26 (diff)
[IMP] base: Configure document layout
The onboarding modal for setting up the few base fields of a company has now been moved to a wizard It is accessible from the general settings, but also in the onboarding section of sale and account modules. The following company settings are editable with that wizard: - Set report **layout**: The user can chose the overall look of the report. The current choices are : *Standard* (default), *Background*, *Boxed* and *Clean*. - Set company **logo**: Changes the company logo. - Set report **colors**: The user can set the primary and secondary colors of the report through a newly added widget allowing to pick a custom color. When changing the **logo**, colors are automatically set to its most dominant colors. > A "Reset colors" button also triggers the color calculation. - Set report **font**: Changes the overall font of the report. Only Google Fonts are used for enhanced compatibility. - Company **tagline**, also called "header" - **Footer** - **Paper format** - Report **preview**: A mockup of a final report Automatically updates when changing **layout**, **logo**, **colors** or **font** Co-authored by: Julien Mougenot <jum@odoo.com> closes odoo/odoo#33863 Signed-off-by: VincentSchippefilt <VincentSchippefilt@users.noreply.github.com> Co-authored-by: Lucas Perais <lpe@odoo.com>
Diffstat (limited to 'odoo/addons/base/tests/test_base_document_layout.py')
-rw-r--r--odoo/addons/base/tests/test_base_document_layout.py206
1 files changed, 206 insertions, 0 deletions
diff --git a/odoo/addons/base/tests/test_base_document_layout.py b/odoo/addons/base/tests/test_base_document_layout.py
new file mode 100644
index 00000000000..ec785ea6216
--- /dev/null
+++ b/odoo/addons/base/tests/test_base_document_layout.py
@@ -0,0 +1,206 @@
+import os
+from PIL import Image
+from functools import partial
+
+from odoo.tests import TransactionCase, tagged, Form
+from odoo.tools import frozendict, image_to_base64, hex_to_rgb
+
+
+dir_path = os.path.dirname(os.path.realpath(__file__))
+_file_cache = {}
+
+
+class TestBaseDocumentLayoutHelpers(TransactionCase):
+ #
+ # Public
+ #
+ def setUp(self):
+ super(TestBaseDocumentLayoutHelpers, self).setUp()
+ self.color_fields = ['primary_color', 'secondary_color']
+ self.company = self.env.company
+ self.css_color_error = 0
+ self._set_templates_and_layouts()
+ self._set_images()
+
+ def assertColors(self, checked_obj, expected):
+ _expected_getter = expected.get if isinstance(expected, dict) else partial(getattr, expected)
+ for fname in self.color_fields:
+ color1 = getattr(checked_obj, fname)
+ color2 = _expected_getter(fname)
+ if self.css_color_error:
+ self._compare_colors_rgb(color1, color2)
+ else:
+ self.assertEqual(color1, color2)
+
+ #
+ # Private
+ #
+ def _compare_colors_rgb(self, color1, color2):
+ self.assertEqual(bool(color1), bool(color2))
+ if not color1:
+ return
+ color1 = hex_to_rgb(color1)
+ color2 = hex_to_rgb(color2)
+ self.assertEqual(len(color1), len(color2))
+ for i in range(len(color1)):
+ self.assertAlmostEqual(color1[i], color2[i], delta=self.css_color_error)
+
+ def _get_images_for_test(self):
+ return ['overwatch.jpg', 'odoo.jpg']
+
+ def _set_images(self):
+ for fname in self._get_images_for_test():
+ fname_split = fname.split('.')
+ if not fname_split[0] in _file_cache:
+ with Image.open(os.path.join(dir_path, fname), 'r') as img:
+ base64_img = image_to_base64(img, 'JPEG')
+ primary, secondary = self.env['base.document.layout'].create(
+ {})._parse_logo_colors(base64_img)
+ _img = frozendict({
+ 'img': base64_img,
+ 'colors': {
+ 'primary_color': primary,
+ 'secondary_color': secondary,
+ },
+ })
+ _file_cache[fname_split[0]] = _img
+ self.company_imgs = frozendict(_file_cache)
+
+ def _set_templates_and_layouts(self):
+ self.layout_template1 = self.env['ir.ui.view'].create({
+ 'name': 'layout_template1',
+ 'key': 'layout_template1',
+ 'type': 'qweb',
+ 'arch': '''<div></div>''',
+ })
+ self.env['ir.model.data'].create({
+ 'name': self.layout_template1.name,
+ 'model': 'ir.ui.view',
+ 'module': 'base',
+ 'res_id': self.layout_template1.id,
+ })
+ self.default_colors = {
+ 'primary_color': '#000000',
+ 'secondary_color': '#000000',
+ }
+ self.report_layout1 = self.env['report.layout'].create({
+ 'view_id': self.layout_template1.id,
+ 'name': 'report_%s' % self.layout_template1.name,
+ })
+ self.layout_template2 = self.env['ir.ui.view'].create({
+ 'name': 'layout_template2',
+ 'key': 'layout_template2',
+ 'type': 'qweb',
+ 'arch': '''<div></div>''',
+ })
+ self.env['ir.model.data'].create({
+ 'name': self.layout_template2.name,
+ 'model': 'ir.ui.view',
+ 'module': 'base',
+ 'res_id': self.layout_template2.id,
+ })
+ self.report_layout2 = self.env['report.layout'].create({
+ 'view_id': self.layout_template2.id,
+ 'name': 'report_%s' % self.layout_template2.name,
+ })
+
+
+@tagged('document_layout')
+class TestBaseDocumentLayout(TestBaseDocumentLayoutHelpers):
+ # Logo change Tests
+ def test_company_no_color_change_logo(self):
+ """When neither a logo nor the colors are set
+ The wizard displays the colors of the report layout
+ Changing logo means the colors on the wizard change too
+ Emptying the logo works and doesn't change the colors"""
+ self.company.write({
+ 'primary_color': False,
+ 'secondary_color': False,
+ 'logo': False,
+ 'external_report_layout_id': self.env.ref('base.layout_template1').id,
+ })
+ default_colors = self.default_colors
+ with Form(self.env['base.document.layout']) as doc_layout:
+ self.assertColors(doc_layout, default_colors)
+ self.assertEqual(doc_layout.company_id, self.company)
+ doc_layout.logo = self.company_imgs['overwatch']['img']
+
+ self.assertColors(doc_layout, self.company_imgs['overwatch']['colors'])
+
+ doc_layout.logo = ''
+ self.assertColors(doc_layout, self.company_imgs['overwatch']['colors'])
+ self.assertEqual(doc_layout.logo, '')
+
+ def test_company_no_color_but_logo_change_logo(self):
+ """When company colors are not set, but a logo is,
+ the wizard displays the computed colors from the logo"""
+ self.company.write({
+ 'primary_color': '#ff0080',
+ 'secondary_color': '#00ff00',
+ 'logo': self.company_imgs['overwatch']['img'],
+ })
+
+ with Form(self.env['base.document.layout']) as doc_layout:
+ self.assertColors(doc_layout, self.company)
+ doc_layout.logo = self.company_imgs['odoo']['img']
+ self.assertColors(doc_layout, self.company_imgs['odoo']['colors'])
+
+ def test_company_colors_change_logo(self):
+ """changes of the logo implies displaying the new computed colors"""
+ self.company.write({
+ 'primary_color': '#ff0080',
+ 'secondary_color': '#00ff00',
+ 'logo': False,
+ })
+
+ with Form(self.env['base.document.layout']) as doc_layout:
+ self.assertColors(doc_layout, self.company)
+ doc_layout.logo = self.company_imgs['odoo']['img']
+ self.assertColors(doc_layout, self.company_imgs['odoo']['colors'])
+
+ def test_company_colors_and_logo_change_logo(self):
+ """The colors of the company may differ from the one the logo computes
+ Opening the wizard in these condition displays the company's colors
+ When the logo changes, colors must change according to the logo"""
+ self.company.write({
+ 'primary_color': '#ff0080',
+ 'secondary_color': '#00ff00',
+ 'logo': self.company_imgs['overwatch']['img'],
+ })
+
+ with Form(self.env['base.document.layout']) as doc_layout:
+ self.assertColors(doc_layout, self.company)
+ doc_layout.logo = self.company_imgs['odoo']['img']
+ self.assertColors(doc_layout, self.company_imgs['odoo']['colors'])
+
+ # Layout change tests
+ def test_company_colors_reset_colors(self):
+ """Reset the colors when they differ from the ones originally
+ computed from the company logo"""
+ self.company.write({
+ 'primary_color': '#ff0080',
+ 'secondary_color': '#00ff00',
+ 'logo': self.company_imgs['overwatch']['img'],
+ })
+
+ with Form(self.env['base.document.layout']) as doc_layout:
+ self.assertColors(doc_layout, self.company)
+ doc_layout.custom_colors = False
+ self.assertColors(doc_layout, self.company_imgs['overwatch']['colors'])
+
+ # /!\ This case is NOT supported, and probably not supportable
+ # res.partner resizes manu-militari the image it is given
+ # so res.company._get_logo differs from res.partner.[default image]
+ # def test_company_no_colors_default_logo_and_layout_change_layout(self):
+ # """When the default YourCompany logo is set, and no colors are set on company:
+ # change wizard's color according to template"""
+ # self.company.write({
+ # 'primary_color': False,
+ # 'secondary_color': False,
+ # 'external_report_layout_id': self.layout_template1.id,
+ # })
+ # default_colors = self.default_colors
+ # with Form(self.env['base.document.layout']) as doc_layout:
+ # self.assertColors(doc_layout, default_colors)
+ # doc_layout.report_layout_id = self.report_layout2
+ # self.assertColors(doc_layout, self.report_layout2)