*** Settings ***
Variables           variables.py
Resource            plone/app/robotframework/selenium.robot
Resource            plone/app/robotframework/keywords.robot
Resource            ../lib/keywords.robot
Library             Remote    ${PLONE_URL}/RobotRemote
Library             DebugLibrary

Test Setup          Prepare test browser
Test Teardown       Close all browsers


*** Test Cases ***
Alice can add a personal task
    Given I am logged in as the user alice_lindstrom
    Then I can go to the application    Todo
    And I have no personal tasks
    Then I create a personal task    My first task    Christian Stoney
    Then I delete the current document
    And I have no personal tasks

Allan can add and remove task attachments
    Given I am logged in as the user allan_neece
    Then I can go to the application    Todo
    Then I create a personal task    My first task    Christian Stoney
    And I can look in all workspaces for the attachment    Minutes
    Then I can select the first found attachment
    And I cannot select the first found attachment a second time
    And I can remove the selected attachment
    Then I delete the current document

Allan can add a workspace task
    Given I am logged in as the user allan_neece
    Then I can go to the application    Todo
    Then I create a workspace task    My first task    Christian Stoney
    Then I delete the current document

Allan can use the todo apps filters
    [Documentation]    The filters are persistent through a cookie
    Given I am logged in as the user allan_neece
    Then I can go to the application    Todo
    Then I can see that the first task is    File report
    When I sort the results    Alphabetically
    Then I can see that the first task is    Budget
    When I filter the results by state    Closed
    Then I can see that the first task is    Identify the requirements
    Then I can go to the application    Todo
    And I can see that the first task is    Identify the requirements
    When I reset the filters
    Then I can see that the first task is    File report

Moderator can modify someone else's task
    When I make Allan a moderator and create a task
    Given I am logged in as the user allan_neece
    And I browse to the workspace    Open Market Committee
    Then I can edit the task    Ciao Allan
    Then Revoke Allan's role and delete task and close browser


*** Keywords ***
# See lib/keywords.robot in the section "case related keywords"

I have no personal tasks
    Wait until page contains element    jquery=#search-result .notice:contains(No results found)

I create a workspace task
    [Arguments]    ${title}    ${assignee}
    Click link    Create new task
    I create a task    My first task    Christian Stoney

I create a personal task
    [Arguments]    ${title}    ${assignee}
    Click Element    css=.quick-functions a.create
    I create a task    My first task    Christian Stoney

I create a task
    [Arguments]    ${title}    ${assignee}
    Wait for injection to be finished
    Input Text    xpath=//div[@class='panel-body']//input[@name='title']    text=${title}
    Input Text    xpath=//div[@class='panel-body']//textarea[@name='description']    text=Plan for success
    Click Element    jquery=label.assignee .select2-container
    Wait Until Element Is visible    css=.select2-input.select2-focused
    Input Text    css=.select2-input.select2-focused    ${assignee}
    Wait Until Element Is visible    xpath=//span[@class='select2-match'][text()='${assignee}']
    Click Element    xpath=//span[@class='select2-match'][text()='${assignee}']
    Click Button    Create
    Close notification
    Wait until page contains    ${title}
    Click Link    ${title}
    Wait for injection to be finished

I delete the current document
    Wait until element is visible    css=#document-form div.quick-functions a.icon-ellipsis.tooltip-inactive
    Click Element    css=#document-form div.quick-functions a.icon-ellipsis.tooltip-inactive
    Wait until element is visible    css=a.icon-trash
    Click Element    css=a.icon-trash
    Wait for injection to be finished
    Click Button    I am sure, delete now
    Wait for injection to be finished

I can see that the first task is
    [Arguments]    ${title}
    Wait for injection to be finished
    Wait until element is visible    jquery=#search-result .panel-content :first a:contains(${title})

I sort the results
    [Arguments]    ${title}
    Select from pat-select    sort-mode    ${title}

I filter the results by state
    [Arguments]    ${title}
    Select from pat-select    state-mode    ${title} tasks

I reset the filters
    Click Button    Reset search and filters

I make Allan a moderator and create a task
    Given I am in a workspace as a workspace admin
    I give the Moderator role to Allan
    I create a task for    Allan

Delete task
    [Arguments]    ${title}
    I can go to the sidebar tasks tile of my case
    Click Link    ${title}
    Wait for injection to be finished
    Click Link    Show extra context actions
    Wait for injection to be finished
    Click Link    Delete this document
    Wait for injection to be finished
    Click Button    I am sure, delete now
    Wait Until Page Contains    has been deleted

Revoke Allan's role and delete task and close browser
    Given I am in a workspace as a workspace admin
    Delete task    Ciao Allan
    Close notification
    I can remove the Moderator role from Allan
    Close all browsers

I can edit the task
    [Arguments]    ${title}
    I can go to the sidebar tasks tile of my case
    Click Link    ${title}
    Wait for injection to be finished
    Select from list by Label    priority    Medium priority
    Click button    Save
    I see the changes have been saved

I open the attachment selector
    Wait Until Keyword Succeeds    10x    100ms    Scroll Element into view    css=.attached-documents.show-more
    Click Element    css=.attached-documents.show-more .button-add-document
    Wait for injection to be finished

I close the attachment selector
    Click Element    jquery=#attach-documents-2 .close-button

I can look in all workspaces for the attachment
    [Arguments]    ${title}
    I open the attachment selector
    Input Text    jquery=#attach-documents-2 input[name=SearchableText]    ${title}
    Wait for injection to be finished
    Run Keyword and Ignore Error    Scroll Element Into View    jquery=#attach-documents-2-results .button-add

I can select the first found attachment
    Execute Javascript    jQuery('#attach-documents-2-results .button-add').first().click()
    Wait for injection to be finished
    Page should contain    Attachment added
    Close notification

I cannot select the first found attachment a second time
    Click Element    jquery=#attach-documents-2-results .button-add:first
    Wait for injection to be finished
    Page should contain    Attachment already present
    Close notification

I can remove the selected attachment
    I close the attachment selector
    Click element    jquery=#attached-documents-attachments-2 .icon-trash
    Click button    Remove
    Wait for injection to be finished
    Page should contain    Attachment removed
    Close notification
