/** * Gets the selection container * @return The selection container * @throws AutomationException Something has gone wrong in automation */ public AutomationElement getSelectionContainer() throws AutomationException { PointerByReference pbr = new PointerByReference(); final int res = this.getPattern().getCurrentSelectionContainer(pbr); if (res != 0) { throw new AutomationException(res); } Unknown unkConditionA = makeUnknown(pbr.getValue()); PointerByReference pUnknownA = new PointerByReference(); WinNT.HRESULT resultA = unkConditionA.QueryInterface(new Guid.REFIID(IUIAutomationElement.IID), pUnknownA); if (COMUtils.SUCCEEDED(resultA)) { return new AutomationElement(convertPointerToElementInterface(pUnknownA)); } else { throw new AutomationException(resultA.intValue()); } }
/** * Gets the element associated with the grid cell * @param x X position * @param y Y position * @return The Element from the grid * @throws AutomationException Something amiss with automation */ public AutomationElement getItem(int x, int y) throws AutomationException { PointerByReference cell = this.getRawItem(x, y); Unknown uRoot = makeUnknown(cell.getValue()); PointerByReference pbr = new PointerByReference(); WinNT.HRESULT result0 = uRoot.QueryInterface(new Guid.REFIID(IUIAutomationElement.IID), pbr); if (COMUtils.SUCCEEDED(result0)) { return new AutomationElement(convertPointerToElementInterface(pbr)); } else { throw new AutomationException(result0.intValue()); } }
/** * Gets the raw pattern from the given element. * @param item The Element * @return The raw collapse pattern * @throws AutomationException Failed to get pattern */ public IUIAutomationExpandCollapsePattern getExpandCollapsePatternFromItem(AutomationElement item) throws AutomationException { PointerByReference pElement = item.getPattern(PatternID.ExpandCollapse.getValue()); Unknown unkConditionA = makeUnknown(pElement.getValue()); PointerByReference pUnknownA = new PointerByReference(); WinNT.HRESULT resultA = unkConditionA.QueryInterface(new Guid.REFIID(IUIAutomationExpandCollapsePattern.IID), pUnknownA); if (COMUtils.SUCCEEDED(resultA)) { return IUIAutomationExpandCollapsePatternConverter.PointerToInterface(pUnknownA); } else { throw new AutomationException("QueryInterface failed"); } }
@Test @Ignore("Need to build up the mocking") public void test_getItem_Throws_Exception_When_Pattern_Returns_Error_From_GetItem() throws Exception { Grid pattern = new Grid(rawPattern); Grid spyPattern = Mockito.spy(pattern); doAnswer(invocation -> new WinNT.HRESULT(0)).when(mockUnknown).QueryInterface(any(Guid.REFIID.class), any(PointerByReference.class)); IUIAutomationGridPattern mockGrid = Mockito.mock(IUIAutomationGridPattern.class); doReturn(mockUnknown) .when(spyPattern) .makeUnknown(any()); AutomationElement element = spyPattern.getItem(0,0); }
@Test public void testCreateFalseCondtion() { UIAutomation instance = UIAutomation.getInstance(); try { PointerByReference condition = instance.createFalseCondition(); Unknown unk = new Unknown(condition.getValue()); PointerByReference pUnknown1 = new PointerByReference(); WinNT.HRESULT result = unk.QueryInterface(new Guid.REFIID(IUIAutomationCondition.IID), pUnknown1); assertTrue("Create FalseCondition:" + COMUtils.SUCCEEDED(result), COMUtils.SUCCEEDED(result)); } catch (AutomationException ex) { assertTrue("Ouch", false); } }
@Test public void testCreatePropertyCondition() { UIAutomation instance = UIAutomation.getInstance(); Variant.VARIANT.ByValue variant = new Variant.VARIANT.ByValue(); WTypes.BSTR sysAllocated = OleAuto.INSTANCE.SysAllocString("SOMETHING"); variant.setValue(Variant.VT_BSTR, sysAllocated); try { try { PointerByReference pCondition = instance.createPropertyCondition(PropertyID.AutomationId.getValue(), variant); Unknown unk = new Unknown(pCondition.getValue()); PointerByReference pUnknown1 = new PointerByReference(); WinNT.HRESULT result = unk.QueryInterface(new Guid.REFIID(IUIAutomationCondition.IID), pUnknown1); assertTrue("CreatePropertyCondition:" + COMUtils.SUCCEEDED(result), COMUtils.SUCCEEDED(result)); } catch (AutomationException ex) { assertTrue("Exception", false); } } finally { OleAuto.INSTANCE.SysFreeString(sysAllocated); } }
@Test public void testCreateTrueCondition() { UIAutomation instance = UIAutomation.getInstance(); try { PointerByReference pCondition = instance.createTrueCondition(); PointerByReference first = new PointerByReference(); // Check whether it is a condition Unknown unk = new Unknown(pCondition.getValue()); PointerByReference pUnk = new PointerByReference(); Guid.REFIID refiid3 = new Guid.REFIID(IUIAutomationCondition.IID); PointerByReference pUnknown1 = new PointerByReference(); WinNT.HRESULT result = unk.QueryInterface(refiid3, pUnknown1); assertTrue("Create TrueCondition:" + COMUtils.SUCCEEDED(result), COMUtils.SUCCEEDED(result)); } catch (AutomationException ex) { assertTrue("Exception", false); } }
@Nullable private static String getUnityUserPath() { if(SystemInfo.isWinVistaOrNewer) { PointerByReference pointerByReference = new PointerByReference(); WinNT.HRESULT hresult = Shell32.INSTANCE.SHGetKnownFolderPath(Guid.GUID.fromString("{A520A1A4-1780-4FF6-BD18-167343C5AF16}"), 0, null, pointerByReference); if(hresult.longValue() != 0) { return null; } return pointerByReference.getValue().getWideString(0) + "\\Unity"; } else if(SystemInfo.isMac) { return SystemProperties.getUserHome() + "/Library/Unity"; } else if(SystemInfo.isLinux) { return SystemProperties.getUserHome() + "/.config/unity3d"; } return null; }
/** * Gets the text from the pattern. * * @return The text. * @throws AutomationException Something has gone wrong */ public String getText() throws AutomationException { PointerByReference pbr = new PointerByReference(); final int res = this.getPattern().getDocumentRange(pbr); if (res != 0) { throw new AutomationException(res); } Unknown unkConditionA = makeUnknown(pbr.getValue()); PointerByReference pUnknownA = new PointerByReference(); WinNT.HRESULT resultA = unkConditionA.QueryInterface(new Guid.REFIID(IUIAutomationTextRange.IID), pUnknownA); if (COMUtils.SUCCEEDED(resultA)) { IUIAutomationTextRange range = convertPointerToInterface(pUnknownA); PointerByReference sr = new PointerByReference(); final int res1 = range.getText(-1, sr); if (res1 != 0) { throw new AutomationException(res1); } return sr.getValue().getWideString(0); } else { throw new AutomationException(resultA.intValue()); } }
/** * Convert a raw PointerByReference to a IUIAutomationElement. * * @param pbr The raw pointer. * @return The IUIAutomationElement. * @throws AutomationException Automation library has thrown an error. */ public IUIAutomationElement getAutomationElementFromReference(final PointerByReference pbr) throws AutomationException { Unknown uElement = makeUnknown(pbr.getValue()); WinNT.HRESULT result0 = uElement.QueryInterface(new Guid.REFIID(IUIAutomationElement.IID), pbr); if (COMUtils.FAILED(result0)) { throw new AutomationException(result0.intValue()); } return IUIAutomationElementConverter.PointerToInterface(pbr); }
/** * Convert a raw PointerByReference to a IUIAutomationElementArray. * @param pbr The raw pointer. * @return The IUIAutomationElementArray. * @throws AutomationException Automation library has thrown an error. */ public IUIAutomationElementArray getAutomationElementArrayFromReference(final PointerByReference pbr) throws AutomationException { Unknown uElement = this.makeUnknown(pbr.getValue()); PointerByReference pUnknown = new PointerByReference(); WinNT.HRESULT result0 = uElement.QueryInterface(new Guid.REFIID(IUIAutomationElementArray.IID), pUnknown); if (COMUtils.FAILED(result0)) { throw new AutomationException(result0.intValue()); } return IUIAutomationElementArrayConverter.PointerToInterface(pUnknown); }
/** * Turns a collection (array) of automation elements, into a collection. * * @param collection The ElementArray. * @return The List. * @throws AutomationException Error in the automation library. */ public List<AutomationElement> collectionToList(final IUIAutomationElementArray collection) throws AutomationException { IntByReference ibr = new IntByReference(); final int res = collection.getLength(ibr); if (res != 0) { throw new AutomationException(res); } List<AutomationElement> list = new ArrayList<>(); for (int count = 0; count < ibr.getValue(); count++) { PointerByReference pbr = new PointerByReference(); final int res1 = collection.getElement(count, pbr); if (res1 != 0) { throw new AutomationException(res1); } Unknown uElement = new Unknown(pbr.getValue()); WinNT.HRESULT result0 = uElement.QueryInterface(new Guid.REFIID(IUIAutomationElement.IID), pbr); if (COMUtils.SUCCEEDED(result0)) { IUIAutomationElement element = IUIAutomationElementConverter.PointerToInterface(pbr); list.add(new AutomationElement(element)); } } return list; }
/** * Gets the raw pointer to the element. * * @param element The underlying element. * @return Pointer The raw pointer. * @throws AutomationException An error has occurred in the automation library. */ protected Pointer getPointerFromElement(final IUIAutomationElement element) throws AutomationException { PointerByReference pElement = new PointerByReference(); WinNT.HRESULT result1 = element.QueryInterface(new Guid.REFIID(IUIAutomationElement.IID), pElement); if (!COMUtils.SUCCEEDED(result1)) { throw new AutomationException(result1.intValue()); } return pElement.getValue(); }
/** * Gets the IUIAutomationElement3 interface, if possible * @return The IUIAutomationElement3 interface * @throws AutomationException Not able to convert interface */ public final IUIAutomationElement3 getElement3() throws AutomationException { PointerByReference pUnknown = new PointerByReference(); WinNT.HRESULT result = this.element.QueryInterface( new Guid.REFIID(IUIAutomationElement3.IID), pUnknown); if (COMUtils.SUCCEEDED(result)) { return IUIAutomationElement3Converter.PointerToInterface(pUnknown); } throw new AutomationException("Failed to convert to IUIAutomationElement6"); }
/** * Gets the IUIAutomationElement6 interface, if possible * @return The IUIAutomationElement6 interface * @throws AutomationException Not able to convert interface */ public final IUIAutomationElement6 getElement6() throws AutomationException { PointerByReference pUnknown = new PointerByReference(); WinNT.HRESULT result = this.element.QueryInterface( new Guid.REFIID(IUIAutomationElement6.IID), pUnknown); if (COMUtils.SUCCEEDED(result)) { return IUIAutomationElement6Converter.PointerToInterface(pUnknown); } throw new AutomationException("Failed to convert to IUIAutomationElement6"); }
@Test @Ignore("Needs better tests") public void test_That_getPattern_Gets_Pattern_When_No_Pattern_Set() throws Exception { doAnswer(invocation -> new WinNT.HRESULT(0)).when(mockUnknown).QueryInterface(any(Guid.REFIID.class), any(PointerByReference.class)); Selection pattern = new Selection(); Selection spyPattern = Mockito.spy(pattern); IUIAutomationSelectionPattern mockPattern = Mockito.mock(IUIAutomationSelectionPattern.class); IUIAutomationElementArray mockArray = Mockito.mock(IUIAutomationElementArray.class); doReturn(mockUnknown) .when(spyPattern) .makeUnknown(any()); doReturn(mockPattern) .when(spyPattern) .convertPointerToInterface(any()); doReturn(mockArray) .when(spyPattern) .convertPointerToElementArray(any()); spyPattern.getCurrentSelection(); verify(spyPattern, atLeastOnce()).getCurrentSelection(); }
@Test public void testCreateNotCondition() { UIAutomation instance = UIAutomation.getInstance(); Variant.VARIANT.ByValue variant = new Variant.VARIANT.ByValue(); WTypes.BSTR sysAllocated = OleAuto.INSTANCE.SysAllocString("SOMETHING"); variant.setValue(Variant.VT_BSTR, sysAllocated); try { try { // Create first condition to use PointerByReference pCondition = instance.createPropertyCondition(PropertyID.AutomationId.getValue(), variant); // Create the actual condition PointerByReference notCondition = instance.createNotCondition(pCondition); // Checking Unknown unk = new Unknown(notCondition.getValue()); PointerByReference pUnknown1 = new PointerByReference(); WinNT.HRESULT result = unk.QueryInterface(new Guid.REFIID(IUIAutomationCondition.IID), pUnknown1); assertTrue("CreateNotCondition:" + COMUtils.SUCCEEDED(result), COMUtils.SUCCEEDED(result)); } catch (AutomationException ex) { assertTrue("Exception", false); } } finally { OleAuto.INSTANCE.SysFreeString(sysAllocated); } }
@Test public void testCreateAndCondition() { UIAutomation instance = UIAutomation.getInstance(); Variant.VARIANT.ByValue variant = new Variant.VARIANT.ByValue(); WTypes.BSTR sysAllocated = OleAuto.INSTANCE.SysAllocString("SOMETHING"); variant.setValue(Variant.VT_BSTR, sysAllocated); try { try { // Create first condition to use PointerByReference pCondition0 = instance.createPropertyCondition(PropertyID.AutomationId.getValue(), variant); // Create first condition to use PointerByReference pCondition1 = instance.createPropertyCondition(PropertyID.AutomationId.getValue(), variant); // Create the actual condition PointerByReference andCondition = instance.createAndCondition(pCondition0, pCondition1); // Checking Unknown unk = new Unknown(andCondition.getValue()); PointerByReference pUnk = new PointerByReference(); PointerByReference pUnknown1 = new PointerByReference(); WinNT.HRESULT result = unk.QueryInterface(new Guid.REFIID(IUIAutomationCondition.IID), pUnknown1); assertTrue("CreateAndCondition:" + COMUtils.SUCCEEDED(result), COMUtils.SUCCEEDED(result)); } catch (AutomationException ex) { assertTrue("Exception", false); } } finally { OleAuto.INSTANCE.SysFreeString(sysAllocated); } }
@Test public void testCreateOrCondition() { UIAutomation instance = UIAutomation.getInstance(); Variant.VARIANT.ByValue variant = new Variant.VARIANT.ByValue(); WTypes.BSTR sysAllocated = OleAuto.INSTANCE.SysAllocString("SOMETHING"); variant.setValue(Variant.VT_BSTR, sysAllocated); try { try { // Create first condition to use PointerByReference pCondition0 = instance.createPropertyCondition(PropertyID.AutomationId.getValue(), variant); // Create first condition to use PointerByReference pCondition1 = instance.createPropertyCondition(PropertyID.AutomationId.getValue(), variant); // Create the actual condition PointerByReference condition = instance.createOrCondition(pCondition0, pCondition1); // Checking Unknown unk = new Unknown(condition.getValue()); PointerByReference pUnk = new PointerByReference(); PointerByReference pUnknown1 = new PointerByReference(); WinNT.HRESULT result = unk.QueryInterface(new Guid.REFIID(IUIAutomationCondition.IID), pUnknown1); assertTrue("CreateOrCondition:" + COMUtils.SUCCEEDED(result), COMUtils.SUCCEEDED(result)); } catch (AutomationException ex) { assertTrue("Exception", false); } } finally { OleAuto.INSTANCE.SysFreeString(sysAllocated); } }
/** * Gets the raw pointer to the pattern. * @param pbr The raw pointer * @return Result of the call from the COM library */ public WinNT.HRESULT getRawPatternPointer( final PointerByReference pbr) { Unknown uElement = makeUnknown(this.pattern); return uElement.QueryInterface(new Guid.REFIID(this.IID), pbr); }
/** * Gets the selection. * * @return String of the selection * @throws AutomationException Something has gone wrong */ public String getSelection() throws AutomationException { PointerByReference pbr = new PointerByReference(); final int res = this.getPattern().getSelection(pbr); if (res != 0) { throw new AutomationException(res); } Unknown unkConditionA = makeUnknown(pbr.getValue()); PointerByReference pUnknownA = new PointerByReference(); String selectionResult = ""; WinNT.HRESULT resultA = unkConditionA.QueryInterface(new Guid.REFIID(IUIAutomationTextRangeArray.IID), pUnknownA); if (COMUtils.SUCCEEDED(resultA)) { IUIAutomationTextRangeArray selection = convertPointerToArrayInterface(pUnknownA); // OK, now what? IntByReference ibr = new IntByReference(); final int res1 = selection.getLength(ibr); if (res1 != 0) { throw new AutomationException(res1); } int count = ibr.getValue(); for (int i = 0; i < count; i++) { PointerByReference pbr0 = new PointerByReference(); final int res2 = selection.getElement(i, pbr0); if (res2 != 0) { throw new AutomationException(res2); } Unknown unknown = makeUnknown(pbr0.getValue()); PointerByReference pUnknown = new PointerByReference(); WinNT.HRESULT result = unknown.QueryInterface(new Guid.REFIID(IUIAutomationTextRange.IID), pUnknown); if (COMUtils.SUCCEEDED(result)) { IUIAutomationTextRange range = convertPointerToInterface(pUnknown); PointerByReference sr = new PointerByReference(); final int res3 = range.getText(-1, sr); if (res3 != 0) { throw new AutomationException(res3); } selectionResult = sr.getValue().getWideString(0); } else { throw new AutomationException(result.intValue()); } } } else { throw new AutomationException(resultA.intValue()); } return selectionResult; }
@Test(expected=AutomationException.class) public void test_That_getPattern_Throws_Exception_When_Pattern_Returns_Error() throws Exception { doAnswer(invocation -> new WinNT.HRESULT(-1)).when(mockUnknown).QueryInterface(any(Guid.REFIID.class), any(PointerByReference.class)); Grid pattern = new Grid(); Grid spyPattern = Mockito.spy(new Grid()); IUIAutomationGridPattern mockGrid = Mockito.mock(IUIAutomationGridPattern.class); doReturn(mockUnknown) .when(spyPattern) .makeUnknown(any()); spyPattern.getItem(0,0); verify(mockGrid, atLeastOnce()).getItem(any(), any(),any()); }
@Test(expected=AutomationException.class) public void test_That_getPattern_Throws_Exception_When_Pattern_Returns_Error() throws Exception { doAnswer(invocation -> new WinNT.HRESULT(-1)).when(mockUnknown).QueryInterface(any(Guid.REFIID.class), any(PointerByReference.class)); Toggle pattern = new Toggle(); Toggle spyPattern = Mockito.spy(new Toggle()); IUIAutomationTogglePattern mockRange = Mockito.mock(IUIAutomationTogglePattern.class); doReturn(mockUnknown) .when(spyPattern) .makeUnknown(any()); spyPattern.toggle(); verify(spyPattern, atLeastOnce()).toggle(); }
@Test public void test_That_getPattern_Gets_Pattern_When_No_Pattern_Set() throws Exception { doAnswer(invocation -> new WinNT.HRESULT(1)).when(mockUnknown).QueryInterface(any(Guid.REFIID.class), any(PointerByReference.class)); Toggle pattern = new Toggle(); Toggle spyPattern = Mockito.spy(new Toggle()); IUIAutomationTogglePattern mockRange = Mockito.mock(IUIAutomationTogglePattern.class); doReturn(mockUnknown) .when(spyPattern) .makeUnknown(any()); doReturn(mockRange) .when(spyPattern) .convertPointerToInterface(any()); spyPattern.toggle(); verify(spyPattern, atLeastOnce()).toggle(); }
@Test(expected=AutomationException.class) public void test_GetSelection_Throws_Exception_When_GetRange_Length_Fails() throws Exception { doAnswer(invocation -> 0).when(rawPattern).getSelection(any()); doAnswer(invocation -> new WinNT.HRESULT(0)).when(mockUnknown).QueryInterface(any(Guid.REFIID.class), any(PointerByReference.class)); Text pattern = new Text(rawPattern); Text spyPattern = Mockito.spy(new Text(rawPattern)); doReturn(mockUnknown) .when(spyPattern) .makeUnknown(any()); IUIAutomationTextRangeArray mockRangeArray = Mockito.mock(IUIAutomationTextRangeArray.class); doAnswer(invocation -> 1).when(mockRangeArray).getLength(any()); doReturn(mockRangeArray) .when(spyPattern) .convertPointerToArrayInterface(any()); String text = spyPattern.getSelection(); assertTrue(text.equals("")); }
@Test(expected=AutomationException.class) public void test_GetText_Throws_Exception_When_QueryInterface_Returns_Error() throws Exception { doAnswer(invocation -> new WinNT.HRESULT(-1)).when(mockUnknown).QueryInterface(any(Guid.REFIID.class), any(PointerByReference.class)); Text pattern = new Text(rawPattern); Text spyPattern = Mockito.spy(new Text(rawPattern)); doReturn(mockUnknown) .when(spyPattern) .makeUnknown(any()); IUIAutomationTextRange mockRange = Mockito.mock(IUIAutomationTextRange.class); String text = spyPattern.getText(); assertTrue(text.equals("")); }
@Test(expected=AutomationException.class) public void test_That_getPattern_Throws_Exception_When_Pattern_Returns_Error() throws Exception { doAnswer(invocation -> new WinNT.HRESULT(-1)).when(mockUnknown).QueryInterface(any(Guid.REFIID.class), any(PointerByReference.class)); Table pattern = new Table(); Table spyPattern = Mockito.spy(pattern); IUIAutomationTablePattern mockRange = Mockito.mock(IUIAutomationTablePattern.class); doReturn(mockUnknown) .when(spyPattern) .makeUnknown(any()); spyPattern.getRowOrColumnMajor(); verify(mockRange, atLeastOnce()).getCurrentRowOrColumnMajor(any()); }
@Test public void test_That_getPattern_Gets_Pattern_When_No_Pattern_Set() throws Exception { doAnswer(invocation -> new WinNT.HRESULT(1)).when(mockUnknown).QueryInterface(any(Guid.REFIID.class), any(PointerByReference.class)); Table pattern = new Table(); Table spyPattern = Mockito.spy(pattern); IUIAutomationTablePattern mockRange = Mockito.mock(IUIAutomationTablePattern.class); doReturn(mockUnknown) .when(spyPattern) .makeUnknown(any()); doReturn(mockRange) .when(spyPattern) .convertPointerToInterface(any()); spyPattern.getRowOrColumnMajor(); verify(mockRange, atLeastOnce()).getCurrentRowOrColumnMajor(any()); }
@Test(expected=AutomationException.class) public void test_That_getPattern_Throws_Exception_When_Pattern_Returns_Error() throws Exception { doAnswer(invocation -> new WinNT.HRESULT(-1)).when(mockUnknown).QueryInterface(any(Guid.REFIID.class), any(PointerByReference.class)); Selection pattern = new Selection(); Selection spyPattern = Mockito.spy(pattern); IUIAutomationSelectionPattern mockPattern = Mockito.mock(IUIAutomationSelectionPattern.class); doReturn(mockUnknown) .when(spyPattern) .makeUnknown(any()); spyPattern.getCurrentSelection(); verify(spyPattern, atLeastOnce()).getCurrentSelection(); }
@Test(expected=AutomationException.class) @Ignore("Throws Mockito exception") public void test_That_getPattern_Throws_Exception_When_Pattern_Returns_Error() throws Exception { doAnswer(invocation -> new WinNT.HRESULT(-1)).when(mockUnknown).QueryInterface(any(Guid.REFIID.class), any(PointerByReference.class)); Value pattern = new Value(); Value spyPattern = Mockito.spy(pattern); IUIAutomationValuePattern mockPattern = Mockito.mock(IUIAutomationValuePattern.class); doReturn(mockUnknown) .when(spyPattern) .makeUnknown(any(Pointer.class)); doReturn(mockPattern) .when(spyPattern) .convertPointerToInterface(any(PointerByReference.class)); spyPattern.value(); verify(mockPattern, atLeastOnce()).getValue(any()); }
@Test(expected=AutomationException.class) public void test_That_getPattern_Throws_Exception_When_Pattern_Returns_Error() throws Exception { doAnswer(invocation -> new WinNT.HRESULT(-1)).when(mockUnknown).QueryInterface(any(Guid.REFIID.class), any(PointerByReference.class)); Range pattern = new Range(); Range spyPattern = Mockito.spy(new Range()); IUIAutomationRangeValuePattern mockRange = Mockito.mock(IUIAutomationRangeValuePattern.class); doReturn(mockUnknown) .when(spyPattern) .makeUnknown(any()); spyPattern.getValue(); verify(mockRange, atLeastOnce()).getValue(any()); }
@Test public void test_That_getPattern_Gets_Pattern_When_No_Pattern_Set() throws Exception { doAnswer(invocation -> new WinNT.HRESULT(1)).when(mockUnknown).QueryInterface(any(Guid.REFIID.class), any(PointerByReference.class)); Range pattern = new Range(); Range spyPattern = Mockito.spy(new Range()); IUIAutomationRangeValuePattern mockRange = Mockito.mock(IUIAutomationRangeValuePattern.class); doReturn(mockUnknown) .when(spyPattern) .makeUnknown(any()); doReturn(mockRange) .when(spyPattern) .convertPointerToInterface(any()); spyPattern.getValue(); verify(mockRange, atLeastOnce()).getValue(any()); }
@Test(expected=AutomationException.class) public void test_That_getPattern_Throws_Exception_When_Pattern_Returns_Error() throws Exception { doAnswer(invocation -> new WinNT.HRESULT(-1)).when(mockUnknown).QueryInterface(any(Guid.REFIID.class), any(PointerByReference.class)); Styles pattern = new Styles(); Styles spyPattern = Mockito.spy(pattern); IUIAutomationStylesPattern mockPattern = Mockito.mock(IUIAutomationStylesPattern.class); doReturn(mockUnknown) .when(spyPattern) .makeUnknown(any()); spyPattern.getStyleName(); verify(spyPattern, atLeastOnce()).getStyleName(); }
@Test(expected=AutomationException.class) public void test_That_getPattern_Throws_Exception_When_Pattern_Returns_Error() throws Exception { doAnswer(invocation -> new WinNT.HRESULT(-1)).when(mockUnknown).QueryInterface(any(Guid.REFIID.class), any(PointerByReference.class)); Invoke pattern = new Invoke(); Invoke spyPattern = Mockito.spy(new Invoke()); IUIAutomationInvokePattern mockRange = Mockito.mock(IUIAutomationInvokePattern.class); doReturn(mockUnknown) .when(spyPattern) .makeUnknown(any()); spyPattern.invoke(); verify(mockRange, atLeastOnce()).invoke(); }
@Test public void test_That_getPattern_Gets_Pattern_When_No_Pattern_Set() throws Exception { doAnswer(invocation -> new WinNT.HRESULT(1)).when(mockUnknown).QueryInterface(any(Guid.REFIID.class), any(PointerByReference.class)); Invoke pattern = new Invoke(); Invoke spyPattern = Mockito.spy(new Invoke()); IUIAutomationInvokePattern mockRange = Mockito.mock(IUIAutomationInvokePattern.class); doReturn(mockUnknown) .when(spyPattern) .makeUnknown(any()); doReturn(mockRange) .when(spyPattern) .convertPointerToInterface(any()); spyPattern.invoke(); verify(mockRange, atLeastOnce()).invoke(); }
@Test(expected=AutomationException.class) @Ignore("Fails after mockito upgrade") public void test_That_getPattern_Throws_Exception_When_Pattern_Returns_Error() throws Exception { doAnswer(invocation -> new WinNT.HRESULT(-1)).when(mockUnknown).QueryInterface(any(Guid.REFIID.class), any(PointerByReference.class)); SelectionItem pattern = new SelectionItem(); SelectionItem spyPattern = Mockito.spy(pattern); IUIAutomationSelectionItemPattern mockPattern = Mockito.mock(IUIAutomationSelectionItemPattern.class); doReturn(mockUnknown) .when(spyPattern) .makeUnknown(any(Pointer.class)); doReturn(mockPattern) .when(spyPattern) .convertPointerToInterface(any(PointerByReference.class)); spyPattern.select(); verify(spyPattern, atLeastOnce()).select(); }
@Test @Ignore("Needs better tests") public void test_getCurrentSelection() throws Exception { doAnswer(invocation -> 0).when(rawPattern).getCurrentSelection(any()); doAnswer(invocation -> new WinNT.HRESULT(0)).when(mockUnknown).QueryInterface(any(Guid.REFIID.class), any(PointerByReference.class)); Selection pattern = new Selection(rawPattern); Selection spyPattern = Mockito.spy(new Selection(rawPattern)); doReturn(mockUnknown) .when(spyPattern) .makeUnknown(any()); IUIAutomationElementArray mockArray = Mockito.mock(IUIAutomationElementArray.class); doReturn(mockArray) .when(spyPattern) .convertPointerToElementArray(any()); spyPattern.getCurrentSelection(); }
@Test @Ignore("Mocking issues, need to be sorted") public void test_That_getPattern_Gets_Pattern_When_No_Pattern_Set() throws Exception { doAnswer(invocation -> new WinNT.HRESULT(0)).when(mockUnknown).QueryInterface(any(Guid.REFIID.class), any(PointerByReference.class)); Grid pattern = new Grid(); Grid spyPattern = Mockito.spy(new Grid()); IUIAutomationGridPattern mockGrid = Mockito.mock(IUIAutomationGridPattern.class); doReturn(mockUnknown) .when(spyPattern) .makeUnknown(any()); doReturn(mockGrid) .when(spyPattern) .convertPointerToInterface(any()); IUIAutomationElement mockElement = Mockito.mock(IUIAutomationElement.class); doReturn(mockElement) .when(spyPattern) .convertPointerToElementInterface(any()); spyPattern.getItem(0,0); verify(mockGrid, atLeastOnce()).getItem(any(), any(),any()); }
@Test(expected=AutomationException.class) @Ignore("Failures after Mockito upgrade") public void test_GetSelection_Throws_Exception_When_Error_Returned() throws Exception { doAnswer(invocation -> 0).when(rawPattern).getSelection(any(PointerByReference.class)); doAnswer(invocation -> new WinNT.HRESULT(-1)).when(mockUnknown).QueryInterface(any(Guid.REFIID.class), any(PointerByReference.class)); Text pattern = new Text(rawPattern); String text = pattern.getSelection(); assertTrue(text.equals("")); }