from .base import FunctionalTest
from .post_page import PostPage


class LayoutAndStylingTest(FunctionalTest):
    def test_layout_and_styling(self):
        self.create_pre_authenticated_session("disco@test.io")
        self.browser.get(self.live_server_url + '/billboard/')
        post_page = PostPage(self)

        self.browser.set_window_size(1024, 768)

        def section_center(el):
            return self.browser.execute_script("""
                var el = arguments[0];
                var ancestor = el.parentElement;
                while (ancestor && ancestor.tagName !== 'SECTION'
                       && !ancestor.classList.contains('container')) {
                    ancestor = ancestor.parentElement;
                }
                var a = ancestor || document.body;
                var s = a.getBoundingClientRect();
                var cs = window.getComputedStyle(a);
                var pl = parseFloat(cs.paddingLeft);
                var pr = parseFloat(cs.paddingRight);
                var r = el.getBoundingClientRect();
                return [r.x + r.width / 2, s.x + pl + (s.width - pl - pr) / 2];
            """, el)

        inputbox = post_page.get_line_input_box()
        input_c, section_c = section_center(inputbox)
        self.assertAlmostEqual(input_c, section_c, delta=10)

        post_page.add_post_line("testing")
        inputbox = post_page.get_line_input_box()
        input_c, section_c = section_center(inputbox)
        self.assertAlmostEqual(input_c, section_c, delta=10)
