Repository: nvaccess/nvda

Issue:
Focus movement changing between focus and browse mode with UIA in Word 365
<!--
Please thoroughly read NVDA's wiki article on how to fill in this template, including how to provide the required files.
Issues may be closed if the required information is not present.
https://github.com/nvaccess/nvda/wiki/Github-issue-template-explanation-and-examples
-->

### Steps to reproduce:
1. Open Word 365 with a document longer than visually fits on screen.  Have the option to use UIA in Word checked in NVDA's advanced options.
2. At the top of the document, press NVDA+spacebar to put NVDA in Browse mode.
3. Press CONTROL+END to move to the end of the document.
4. Press NVDA+spacebar to put NVDA in Focus mode.
5. Press the up arrow.

### Actual behavior:

A) At Step 3, the screen does NOT scroll to show the current location of the cursor.
B) At Step 5, the focus jumps back to the start of the first line.  Note that if the left arrow is pressed in Step 5, the focus moves to the END of the first line.  If the right arrow or down arrow are pressed, the focus moves to a spot just under the visible text (eg if one full page is visible, the focus moves to the start of page 2).

C) At Step 3, instead of moving to the end of the document, if the focus is moved within the visible text on screen, the focus is where it would be expected to be after step 5.

### Expected behavior:
At Step 3, the screen should scroll to show the end of the document.
At Step 5, the focus should be at the end of the document (one character from the end if left arrow is pressed, on the second last line if the up arrow is pressed).

### System configuration
#### NVDA installed/portable/running from source:

#### NVDA version:
2019.1.1 installed

#### Windows version:
Windows 10 Version: 
18362.1 
#### Name and version of other software in use when reproducing the issue:
Office 365 Version: 
16.0.11601.20184 

#### Other information about your system:

### Other questions
#### Does the issue still occur after restarting your PC?
Yes.  Also reported by a user.

#### Have you tried any other versions of NVDA? If so, please report their behaviors.
NVDA alpha-17311,0d29265e - same behaviour.


Discussion:
@Qchristensen  Please test this try build: https://ci.appveyor.com/api/buildjobs/bsjf2a8c6t9879wf/artifacts/output%2Fnvda_snapshot_try-i9611-23673%2C38a7b5ac.exe
Browse mode in MS word with both UIA enabled and disabled should scroll the document to the exact browse mode position, even across pages.
It may be an idea to also check that scrolling in Firefox and Chrome still works as expected.
Possibly even Edge with UIA enabled.
That try build seems to do the trick in Word, and scrolling in FF, Chrome and Edge seems to work as expected as well.

Resolving patch:
diff --git a/source/NVDAObjects/UIA/wordDocument.py b/source/NVDAObjects/UIA/wordDocument.py
--- a/source/NVDAObjects/UIA/wordDocument.py
+++ b/source/NVDAObjects/UIA/wordDocument.py
@@ -118,6 +118,28 @@ def label(self):
 
 class WordDocumentTextInfo(UIATextInfo):
 
+	def _ensureRangeVisibility(self):
+		try:
+			inView = self.pointAtStart in self.obj.location
+		except LookupError:
+			inView = False
+		if not inView:
+			self._rangeObj.ScrollIntoView(True)
+
+	def updateSelection(self):
+		# #9611: The document must be scrolled so that the range is visible on screen
+		# Otherwise trying to set the selection to the range
+		# may cause the selection to remain on the wrong page.
+		self._ensureRangeVisibility()
+		super().updateSelection()
+
+	def updateCaret(self):
+		# #9611: The document must be scrolled so that the range is visible on screen
+		# Otherwise trying to set the caret to the range
+		# may cause the caret to remain on the wrong page.
+		self._ensureRangeVisibility()
+		super().updateCaret()
+
 	def _get_locationText(self):
 		point = self.pointAtStart
 		# UIA has no good way yet to convert coordinates into user-configured distances such as inches or centimetres.


Test patch: