*** 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 attach a file to a post
    Given I am logged in as the user alice_lindstrom
    And I open the Dashboard
    When I create a new post
    And I post the file    basic.txt
    Then I can see the file preview in the post box

Alice can submit a post with a file attachment
    Given I am logged in as the user alice_lindstrom
    And I open the Dashboard
    When I create a new post
    And I post the file    basic.txt
    And I can see the file preview in the post box
    And I submit the new post
    # injection
    Then I can see the file preview in the stream
    # issue #1234: injected element is not properly clickable in test
    # likely because pat-pasonry messes up a short stream
    #    And I can open the file from the stream preview
    # reload to verify
    When I open the Dashboard
    Then I can see the file preview in the stream
    And I can open the file from the stream preview

Alice can submit a post with a UTF-8 file attachment
    Given I am logged in as the user alice_lindstrom
    And I open the Dashboard
    When I create a new post
    And I post the file    bärtige_flößer.odt
    And I can see the UTF-8 file preview in the post box
    And I submit the new post
    Then I can see the UTF-8 file preview in the stream
    And I can open the UTF-8 file from the stream preview

Alice can submit a post with a file attachment and no text
    Given I am logged in as the user alice_lindstrom
    And I open the Dashboard
    When I post the file    basic.txt
    And I can see the file preview in the post box
    And I submit the new post
    # test injection working
    Then I can see the file preview in the stream


*** Keywords ***
I create a new post
    Wait for injection to be finished
    Wait Until Element Is visible    css=#post-box .ProseMirror
    Input Text    css=#post-box .ProseMirror    Look at this new doc

I post the file
    [Arguments]    ${filename}
    Click Element    css=#post-box .ProseMirror
    Execute javascript    jQuery("#post-box").addClass("status-attach")
    Choose File    css=#post-box input[name='form.widgets.attachments']    ${UPLOADS}/${filename}

I can see the file preview in the post box
    Wait Until Element Is visible    css=#post-box #post-box-attachment-previews img

I submit the new post
    Click Element    css=button[name='form.buttons.statusupdate']
    Wait for injection to be finished
    Sleep    2

I can see the file preview in the stream
    Wait Until Element Is visible    css=#activity-stream .document-preview img[src$='basic.txt/@@render.svg']

I can open the file from the stream preview
    Wait Until Element Is visible    css=#activity-stream .document-preview a[href$='/basic.txt/@@download']
    # We cannot test the actual downloading. Skip.

I can see the UTF-8 file preview in the post box
    Wait Until Element Is visible    css=#post-box #post-box-attachment-previews img

I can see the UTF-8 file preview in the stream
    # The link contains the normalized filename, but the title is still the original UTF-8 filename
    Wait Until Element Is visible
    ...    css=#activity-stream .document-preview img[src$='bartige_flosser.odt/@@render.svg']
    Wait Until Element Is visible
    ...    xpath=//div[@id='activity-stream']//strong[@class='title']//a[contains(text(),'bärtige_flößer.odt')]

I can open the UTF-8 file from the stream preview
    Wait Until Element Is visible    css=#activity-stream .document-preview a[href$='/bartige_flosser.odt/@@download']
    # clicking the link downloads the .odt rather than rendering it in-browser. Skip.
