"""
novelWriter – Lorem Ipsum Tool Tester
=====================================

This file is a part of novelWriter
Copyright 2018–2023, Veronica Berglyd Olsen

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

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
General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""

import pytest

from tools import C, getGuiItem, buildTestProject

from PyQt5.QtWidgets import QAction

from novelwriter.tools.lipsum import GuiLipsum


@pytest.mark.gui
def testToolLipsum_Main(qtbot, nwGUI, projPath, mockRnd):
    """Test the Lorem Ipsum tool.
    """
    # Check that we cannot open when there is no project
    nwGUI.mainMenu.aLipsumText.activate(QAction.Trigger)
    assert getGuiItem("GuiLipsum") is None

    # Create a new project
    buildTestProject(nwGUI, projPath)
    assert nwGUI.openDocument(C.hSceneDoc) is True
    assert len(nwGUI.docEditor.getText()) == 15

    # Open the tool
    nwGUI.mainMenu.aLipsumText.activate(QAction.Trigger)
    qtbot.waitUntil(lambda: getGuiItem("GuiLipsum") is not None, timeout=1000)

    nwLipsum = getGuiItem("GuiLipsum")
    assert isinstance(nwLipsum, GuiLipsum)

    # Insert paragraphs
    nwGUI.docEditor.setCursorPosition(100)  # End of document
    nwLipsum.paraCount.setValue(2)
    nwLipsum._doInsert()
    theText = nwGUI.docEditor.getText()
    assert "Lorem ipsum" in theText
    assert len(theText) == 965

    # Insert random paragraph
    nwGUI.docEditor.setCursorPosition(1000)  # End of document
    nwLipsum.randSwitch.setChecked(True)
    nwLipsum.paraCount.setValue(1)
    nwLipsum._doInsert()
    theText = nwGUI.docEditor.getText()
    assert len(theText) > 965

    # Close
    nwLipsum._doClose()

    # qtbot.stop()

# END Test testToolLipsum_Main
