<commit_before># Copyright 2015, 2016 Reahl Software Services (Pty) Ltd. All rights reserved.
#-*- encoding: utf-8 -*-
#
#    This file is part of Reahl.
#
#    Reahl is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation; version 3 of the License.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

from __future__ import print_function, unicode_literals, absolute_import, division

import six

from reahl.tofu import scenario, Fixture, uses
from reahl.tofu.pytestsupport import with_fixtures

from reahl.webdev.tools import WidgetTester, XPath

from reahl.web.fw import Url
from reahl.web.bootstrap.ui import P
from reahl.web.bootstrap.tabbedpanel import TabbedPanel, MultiTab, Tab


from reahl.webdev.fixtures import WebServerFixture
from reahl.web_dev.fixtures import WebFixture


@uses(web_fixture=WebFixture)
class   TabbedPanelAjaxFixture(Fixture):

    def new_PopulatedTabbedPanel(self):
        class PopulatedTabbedPanel(TabbedPanel):
            def __init__(self, view):
                super(PopulatedTabbedPanel, self).__init__(view)
                multi_tab = MultiTab(view, 'multitab name', 'multi-main')
                tab1 = Tab(view, 'tab 1 name', 'multi1', P.factory(text='tab 1/1 content'))
                tab2 = Tab(view, 'tab 2 name', 'multi2', P.factory(text='tab 1/2 content'))
                multi_tab.add_tab(tab1)
                multi_tab.add_tab(tab2)
                self.add_tab(multi_tab)

                tab3 = Tab(view, 'tab 3 name', 'tab3', P.factory(text='tab 3 content'))
                self.add_tab(tab3)
                tab4 = Tab(view, 'tab 4 name', 'tab4', P.factory(text='tab 4 content'))
                self.add_tab(tab4)
        return PopulatedTabbedPanel

    def new_wsgi_app(self, enable_js=True):
        return self.web_fixture.new_wsgi_app(enable_js=enable_js, child_factory=self.PopulatedTabbedPanel.factory())

    def tab_is_active(self, tab_name):
        element = self.web_fixture.driver_browser.find_element('//a[contains(text(), "%s")]' % tab_name)
        return 'active' in element.get_attribute('class')

    def tab_contents_equals(self, expected_contents):
        current_contents = self.web_fixture.driver_browser.get_inner_html_for('//div[contains(@class, "active")]')
        return current_contents == expected_contents


@with_fixtures(WebFixture)
def test_basic_rendering(web_fixture):
    """A TabbedPanel consists of a Nav (its tabs) and a Div in which tab contents are displayed."""


    web_fixture.request.query_string = 'tab=tab1'
    tabbed_panel = TabbedPanel(web_fixture.view)
    tabbed_panel.add_tab(Tab(web_fixture.view, 'tab 1 name', 'tab1', P.factory(text='tab 1 content')))

    tester = WidgetTester(tabbed_panel)

    expected_html = \
      '''<ul class="nav nav-tabs reahl-menu">'''\
       '''<li class="nav-item">'''\
       '''<a data-target="#tab_tab1" data-toggle="tab" href="/?tab=tab1" class="active nav-link">tab 1 name</a>'''\
       '''</li>'''\
      '''</ul>'''\
      '''<div class="tab-content">'''\
       '''<div id="tab_tab1" class="active tab-pane"><p>tab 1 content</p></div>'''\
      '''</div>'''\

    actual = tester.render_html()
    assert actual == expected_html


@with_fixtures(WebFixture)
def test_tabs_with_sub_options(web_fixture):
    """A TabbedPanel can have Tabs that are each composed of multiple sub-options."""

    web_fixture.request.query_string = 'tab=mult2'
    tabbed_panel = TabbedPanel(web_fixture.view)
    multi_tab = MultiTab(web_fixture.view, 'tab 1 name', 'multitab-main')
    multi_tab.add_tab(Tab(web_fixture.view, 'multi tab 1', 'mult1', P.factory(text='tab 1/1 content')))
    multi_tab.add_tab(Tab(web_fixture.view, 'multi tab 2', 'mult2', P.factory(text='tab 1/2 content')))
    tabbed_panel.add_tab(multi_tab)

    tester = WidgetTester(tabbed_panel)

    expected_html = \
     '''<ul class="nav nav-tabs reahl-menu">'''\
     '''<li class="dropdown nav-item">'''\
      '''<a aria-haspopup="true" data-target="-" data-toggle="dropdown" href="/?open_item=tab+1+name&amp;tab=mult2" role="button" class="active dropdown-toggle nav-link reahl-ajaxlink">tab 1 name<span class="caret"></span></a>'''\
      '''<div class="dropdown-menu">'''\
       '''<a data-target="#tab_mult1" data-toggle="tab" href="/?tab=mult1" class="dropdown-item">multi tab 1</a>'''\
       '''<a data-target="#tab_mult2" data-toggle="tab" href="/?tab=mult2" class="active dropdown-item">multi tab 2</a>'''\
      '''</div>'''\
     '''</li>'''\
    '''</ul>'''\
    '''<div class="tab-content">'''\
     '''<div id="tab_mult1" class="tab-pane"><p>tab 1/1 content</p></div>'''\
     '''<div id="tab_mult2" class="active tab-pane"><p>tab 1/2 content</p></div>'''\
    '''</div>'''

    actual = tester.render_html()
    assert actual == expected_html


@uses(web_fixture=WebFixture)
class DefaultTabScenarios(Fixture):

    @scenario<commit_msg>got working after bootstrap beta2 changes<commit_after># Copyright 2015, 2016 Reahl Software Services (Pty) Ltd. All rights reserved.
#-*- encoding: utf-8 -*-
#
#    This file is part of Reahl.
#
#    Reahl is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation; version 3 of the License.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

from __future__ import print_function, unicode_literals, absolute_import, division

import six

from reahl.tofu import scenario, Fixture, uses
from reahl.tofu.pytestsupport import with_fixtures

from reahl.webdev.tools import WidgetTester, XPath

from reahl.web.fw import Url
from reahl.web.bootstrap.ui import P
from reahl.web.bootstrap.tabbedpanel import TabbedPanel, MultiTab, Tab


from reahl.webdev.fixtures import WebServerFixture
from reahl.web_dev.fixtures import WebFixture


@uses(web_fixture=WebFixture)
class   TabbedPanelAjaxFixture(Fixture):

    def new_PopulatedTabbedPanel(self):
        class PopulatedTabbedPanel(TabbedPanel):
            def __init__(self, view):
                super(PopulatedTabbedPanel, self).__init__(view)
                multi_tab = MultiTab(view, 'multitab name', 'multi-main')
                tab1 = Tab(view, 'tab 1 name', 'multi1', P.factory(text='tab 1/1 content'))
                tab2 = Tab(view, 'tab 2 name', 'multi2', P.factory(text='tab 1/2 content'))
                multi_tab.add_tab(tab1)
                multi_tab.add_tab(tab2)
                self.add_tab(multi_tab)

                tab3 = Tab(view, 'tab 3 name', 'tab3', P.factory(text='tab 3 content'))
                self.add_tab(tab3)
                tab4 = Tab(view, 'tab 4 name', 'tab4', P.factory(text='tab 4 content'))
                self.add_tab(tab4)
        return PopulatedTabbedPanel

    def new_wsgi_app(self, enable_js=True):
        return self.web_fixture.new_wsgi_app(enable_js=enable_js, child_factory=self.PopulatedTabbedPanel.factory())

    def tab_is_active(self, tab_name):
        element = self.web_fixture.driver_browser.find_element('//a[contains(text(), "%s")]' % tab_name)
        return 'active' in element.get_attribute('class')

    def tab_contents_equals(self, expected_contents):
        current_contents = self.web_fixture.driver_browser.get_inner_html_for('//div[contains(@class, "active")]')
        return current_contents == expected_contents


@with_fixtures(WebFixture)
def test_basic_rendering(web_fixture):
    """A TabbedPanel consists of a Nav (its tabs) and a Div in which tab contents are displayed."""


    web_fixture.request.query_string = 'tab=tab1'
    tabbed_panel = TabbedPanel(web_fixture.view)
    tabbed_panel.add_tab(Tab(web_fixture.view, 'tab 1 name', 'tab1', P.factory(text='tab 1 content')))

    tester = WidgetTester(tabbed_panel)

    expected_html = \
      '''<ul class="nav nav-tabs reahl-menu">'''\
       '''<li class="nav-item">'''\
       '''<a data-target="#tab_tab1" data-toggle="tab" href="/?tab=tab1" class="active nav-link">tab 1 name</a>'''\
       '''</li>'''\
      '''</ul>'''\
      '''<div class="tab-content">'''\
       '''<div id="tab_tab1" class="active tab-pane"><p>tab 1 content</p></div>'''\
      '''</div>'''\

    actual = tester.render_html()
    assert actual == expected_html


@with_fixtures(WebFixture)
def test_tabs_with_sub_options(web_fixture):
    """A TabbedPanel can have Tabs that are each composed of multiple sub-options."""

    web_fixture.request.query_string = 'tab=mult2'
    tabbed_panel = TabbedPanel(web_fixture.view)
    multi_tab = MultiTab(web_fixture.view, 'tab 1 name', 'multitab-main')
    multi_tab.add_tab(Tab(web_fixture.view, 'multi tab 1', 'mult1', P.factory(text='tab 1/1 content')))
    multi_tab.add_tab(Tab(web_fixture.view, 'multi tab 2', 'mult2', P.factory(text='tab 1/2 content')))
    tabbed_panel.add_tab(multi_tab)

    tester = WidgetTester(tabbed_panel)

    expected_html = \
     '''<ul class="nav nav-tabs reahl-menu">'''\
     '''<li class="dropdown nav-item">'''\
      '''<a aria-haspopup="true" data-toggle="dropdown" href="/?open_item=tab+1+name&amp;tab=mult2" role="button" class="active dropdown-toggle nav-link reahl-ajaxlink">tab 1 name</a>'''\
      '''<div class="dropdown-menu">'''\
       '''<a data-target="#tab_mult1" data-toggle="tab" href="/?tab=mult1" class="dropdown-item">multi tab 1</a>'''\
       '''<a data-target="#tab_mult2" data-toggle="tab" href="/?tab=mult2" class="active dropdown-item">multi tab 2</a>'''\
      '''</div>'''\
     '''</li>'''\
    '''</ul>'''\
    '''<div class="tab-content">'''\
     '''<div id="tab_mult1" class="tab-pane"><p>tab 1/1 content</p></div>'''\
     '''<div id="tab_mult2" class="active tab-pane"><p>tab 1/2 content</p></div>'''\
    '''</div>'''

    actual = tester.render_html()
    assert actual == expected_html


@uses(web_fixture=WebFixture)
class DefaultTabScenarios(Fixture):

    @scenario
{'fraction_non_alphanumeric': 0.14798541647190802, 'fraction_numerical': 0.012433392539964476, 'mean_word_length': 3.6655037069341474, 'pattern_counts': {'":': 0, '<': 75, '<?xml version=': 0, '>': 75, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '251175', 'n_tokens_mistral': 3601, 'n_tokens_neox': 3465, 'n_words': 838}