#!/usr/bin/python

"""Tests to verify bug #517048 - Orca does not always speak the correct
   information when navigating and/or selecting text across object
   boundaries in OOo Writer.
"""

from macaroon.playback import *
import utils

sequence = MacroSequence()

###########################################################################
# Wait for oowriter to startup.
#
sequence.append(WaitForWindowActivate("Untitled1 - OpenOffice.org Writer",None))

###########################################################################
# Create a document containing:
#
# This is a test.
# So is this.
#
sequence.append(TypeAction("This is a test."))
sequence.append(KeyComboAction("Return"))
sequence.append(WaitForFocus("", acc_role=pyatspi.ROLE_PARAGRAPH))
sequence.append(TypeAction("So is this."))
sequence.append(KeyComboAction("Return"))
sequence.append(WaitForFocus("", acc_role=pyatspi.ROLE_PARAGRAPH))

###########################################################################
# Test 1. Position the cursor at the start of the document. Use Shift-Right
#         Arrow to select character by character. Once you've selected the
#         period, press Shift+Right Arrow again (selecting just the newline).
#
sequence.append(KeyComboAction("<Control>Home"))
#sequence.append(WaitForFocus("", acc_role=pyatspi.ROLE_PARAGRAPH))
sequence.append(KeyComboAction("<Shift>Right"))
sequence.append(KeyComboAction("<Shift>Right"))
sequence.append(KeyComboAction("<Shift>Right"))
sequence.append(KeyComboAction("<Shift>Right"))
sequence.append(KeyComboAction("<Shift>Right"))
sequence.append(KeyComboAction("<Shift>Right"))
sequence.append(KeyComboAction("<Shift>Right"))
sequence.append(KeyComboAction("<Shift>Right"))
sequence.append(KeyComboAction("<Shift>Right"))
sequence.append(KeyComboAction("<Shift>Right"))
sequence.append(KeyComboAction("<Shift>Right"))
sequence.append(KeyComboAction("<Shift>Right"))
sequence.append(KeyComboAction("<Shift>Right"))
sequence.append(KeyComboAction("<Shift>Right"))
sequence.append(KeyComboAction("<Shift>Right"))
sequence.append(KeyComboAction("<Shift>Right"))
sequence.append(PauseAction(3000))
sequence.append(utils.AssertPresentationAction(
    "Selecting newline",
    ["XXX: to complete."]))

###########################################################################
# Test 2. Press Shift+Left Arrow to unselect the newline.
#
sequence.append(KeyComboAction("<Shift>Left"))
sequence.append(PauseAction(3000))
sequence.append(utils.AssertPresentationAction(
    "Unselecting newline",
    ["XXX: to complete."]))

###########################################################################
# Test 3. Press Control-Home to unselect everything. Then use 
#         Control-Shift-Right Arrow to select word by word from the beginning
#         through "test" (i.e. "test" is selected but the "." is not)
#
sequence.append(KeyComboAction("<Control>Home"))
sequence.append(KeyComboAction("<Shift><Control>Right"))
sequence.append(KeyComboAction("<Shift><Control>Right"))
sequence.append(KeyComboAction("<Shift><Control>Right"))
sequence.append(KeyComboAction("<Shift><Control>Right"))
sequence.append(PauseAction(3000))
sequence.append(utils.AssertPresentationAction(
    "Selecting 'test' by word",
    ["XXX: to complete."]))

###########################################################################
# Test 4. Continue to press Control-Shift-Arrow to select forward by word 
#         to select the period.
#
sequence.append(KeyComboAction("<Shift><Control>Right"))
sequence.append(PauseAction(3000))
sequence.append(utils.AssertPresentationAction(
    "Selecting period by word",
    ["XXX: to complete."]))

###########################################################################
# Test 5. Press Control-Shift-Right Arrow to select forward by word to 
#         select the newline/ paragraph boundary.
#
sequence.append(KeyComboAction("<Shift><Control>Right"))
sequence.append(PauseAction(3000))
sequence.append(utils.AssertPresentationAction(
    "Selecting newline by word",
    ["XXX: to complete."]))

###########################################################################
# Test 6. Press Control-End to move to the end of the document. Press 
#         Control+Shift+Left to select the period. Then press it again 
#         to select "this".
#
sequence.append(KeyComboAction("<Control>End"))
sequence.append(KeyComboAction("<Shift><Control>Left"))
sequence.append(KeyComboAction("<Shift><Control>Left"))
sequence.append(PauseAction(3000))
sequence.append(utils.AssertPresentationAction(
    "Selecting 'this' by word, from end of document",
    ["XXX: to complete."]))

###########################################################################
# Test 7. Continue pressing Control-Shift-Left Arrow to select backwards
#         by word. After "So is this." is selected, press
#         Control-Shift-Left Arrow again.
#
sequence.append(KeyComboAction("<Shift><Control>Left"))
sequence.append(KeyComboAction("<Shift><Control>Left"))
sequence.append(KeyComboAction("<Shift><Control>Left"))
sequence.append(PauseAction(3000))
sequence.append(utils.AssertPresentationAction(
    "Selecting newline before 'So', by word, from end of document",
    ["XXX: to complete."]))

###########################################################################
# Test 8. Continue pressing Control-Shift-Left Arrow to select backwards 
#         by word until "test" is selected.
#
sequence.append(KeyComboAction("<Shift><Control>Left"))
sequence.append(KeyComboAction("<Shift><Control>Left"))
sequence.append(PauseAction(3000))
sequence.append(utils.AssertPresentationAction(
    "Selecting 'test', by word, from end of document",
    ["XXX: to complete."]))

###########################################################################
# Test 9. Press Control-Home to start from the beginning of the document.
#         Press Shift-Down Arrow to select the current line.
#
sequence.append(KeyComboAction("<Control>Home"))
sequence.append(KeyComboAction("<Shift>Down"))
sequence.append(PauseAction(3000))
sequence.append(utils.AssertPresentationAction(
    "Select the current line from start of document",
    ["XXX: to complete."]))

###########################################################################
# Test 10. Start from the end of the document (by pressing Control+End).
#          Press Shift-Up Arrow to select a line's worth (thus "So is this."
#          on the last line is selected, along with "est." at the end of the
#          previous line).
#
sequence.append(KeyComboAction("<Control>End"))
sequence.append(KeyComboAction("<Shift>Up"))
sequence.append(PauseAction(3000))
sequence.append(utils.AssertPresentationAction(
    "Select the current line from end of document",
    ["XXX: to complete."]))

###########################################################################
# Test 11. Press Control-Home to get to the beginning of the document.
#          Navigate by word by pressing Control-Right Arrow until you 
#          land on the period.
#
sequence.append(KeyComboAction("<Control>Home"))
sequence.append(KeyComboAction("<Control>Right"))
sequence.append(KeyComboAction("<Control>Right"))
sequence.append(KeyComboAction("<Control>Right"))
sequence.append(KeyComboAction("<Control>Right"))
sequence.append(PauseAction(3000))
sequence.append(utils.AssertPresentationAction(
    "Navigating by word from start of document",
    ["XXX: to complete."]))

###########################################################################
# Adjust the document to now contain:
#
# This is a test.
# So is this.
# And this.
#
sequence.append(KeyComboAction("<Control>End"))
sequence.append(WaitForFocus("", acc_role=pyatspi.ROLE_PARAGRAPH))
sequence.append(TypeAction("And this."))
sequence.append(KeyComboAction("Return"))
sequence.append(WaitForFocus("", acc_role=pyatspi.ROLE_PARAGRAPH))

# Test 12. Press Control-Home to move to the start of the document. Select 
#          line by line with Shift-Down Arrow. Having selected it all, 
#          press Shift-Up Arrow twice. "So is this." should just have been
#          unselected.
#
sequence.append(KeyComboAction("<Control>Home"))
sequence.append(KeyComboAction("<Shift>Down"))
sequence.append(KeyComboAction("<Shift>Down"))
sequence.append(KeyComboAction("<Shift>Down"))
sequence.append(KeyComboAction("<Shift>Up"))
sequence.append(KeyComboAction("<Shift>Up"))
sequence.append(PauseAction(3000))
sequence.append(utils.AssertPresentationAction(
    "Unselecting by line from end of document",
    ["XXX: to complete."]))

sequence.append(utils.AssertionSummaryAction())

sequence.start()
