@@ -1725,29 +1725,35 @@ - (void)testRestoringPlatformFirstResponderNotifiesFrameworkOnce {
17251725
17261726 XCTAssertTrue([inputView becomeFirstResponderFromFramework]);
17271727 OCMVerify(never(), [engine flutterTextInputView:inputView
1728- didRestoreFirstResponderWithTextInputClient:123]);
1728+ didRestoreFirstResponderWithTextInputClient:123
1729+ result:[OCMArg any]]);
17291730
17301731 XCTAssertTrue([inputView resignFirstResponder]);
17311732 OCMVerify(times(1), [engine flutterTextInputView:inputView
17321733 didResignFirstResponderWithTextInputClient:123]);
17331734 OCMVerify(never(), [engine flutterTextInputView:inputView
1734- didRestoreFirstResponderWithTextInputClient:123]);
1735+ didRestoreFirstResponderWithTextInputClient:123
1736+ result:[OCMArg any]]);
17351737 XCTAssertTrue([inputView becomeFirstResponder]);
17361738 OCMVerify(times(1), [engine flutterTextInputView:inputView
1737- didRestoreFirstResponderWithTextInputClient:123]);
1739+ didRestoreFirstResponderWithTextInputClient:123
1740+ result:[OCMArg any]]);
17381741
17391742 XCTAssertTrue([inputView becomeFirstResponder]);
17401743 OCMVerify(times(1), [engine flutterTextInputView:inputView
1741- didRestoreFirstResponderWithTextInputClient:123]);
1744+ didRestoreFirstResponderWithTextInputClient:123
1745+ result:[OCMArg any]]);
17421746
17431747 XCTAssertTrue([inputView resignFirstResponder]);
17441748 OCMVerify(times(2), [engine flutterTextInputView:inputView
17451749 didResignFirstResponderWithTextInputClient:123]);
17461750 OCMVerify(times(1), [engine flutterTextInputView:inputView
1747- didRestoreFirstResponderWithTextInputClient:123]);
1751+ didRestoreFirstResponderWithTextInputClient:123
1752+ result:[OCMArg any]]);
17481753 XCTAssertTrue([inputView becomeFirstResponder]);
17491754 OCMVerify(times(2), [engine flutterTextInputView:inputView
1750- didRestoreFirstResponderWithTextInputClient:123]);
1755+ didRestoreFirstResponderWithTextInputClient:123
1756+ result:[OCMArg any]]);
17511757
17521758 [inputView resignFirstResponderFromFramework];
17531759 [inputView removeFromSuperview];
@@ -1766,7 +1772,8 @@ - (void)testFrameworkResponderChangesCancelPendingFocusRestore {
17661772 XCTAssertTrue([inputView becomeFirstResponderFromFramework]);
17671773
17681774 OCMVerify(never(), [engine flutterTextInputView:inputView
1769- didRestoreFirstResponderWithTextInputClient:123]);
1775+ didRestoreFirstResponderWithTextInputClient:123
1776+ result:[OCMArg any]]);
17701777
17711778 [inputView resignFirstResponderFromFramework];
17721779 [inputView removeFromSuperview];
@@ -1786,7 +1793,8 @@ - (void)testInteractiveKeyboardResignDoesNotArmFocusRestore {
17861793 OCMVerify(never(), [engine flutterTextInputView:inputView
17871794 didResignFirstResponderWithTextInputClient:123]);
17881795 OCMVerify(never(), [engine flutterTextInputView:inputView
1789- didRestoreFirstResponderWithTextInputClient:123]);
1796+ didRestoreFirstResponderWithTextInputClient:123
1797+ result:[OCMArg any]]);
17901798
17911799 [inputView resignFirstResponderFromFramework];
17921800 [inputView removeFromSuperview];
@@ -1803,9 +1811,11 @@ - (void)testChangingClientCancelsPendingFocusRestore {
18031811 XCTAssertTrue([inputView becomeFirstResponder]);
18041812
18051813 OCMVerify(never(), [engine flutterTextInputView:inputView
1806- didRestoreFirstResponderWithTextInputClient:123]);
1814+ didRestoreFirstResponderWithTextInputClient:123
1815+ result:[OCMArg any]]);
18071816 OCMVerify(never(), [engine flutterTextInputView:inputView
1808- didRestoreFirstResponderWithTextInputClient:456]);
1817+ didRestoreFirstResponderWithTextInputClient:456
1818+ result:[OCMArg any]]);
18091819
18101820 [inputView resignFirstResponderFromFramework];
18111821 [inputView removeFromSuperview];
@@ -1822,7 +1832,95 @@ - (void)testResettingSameClientCancelsPendingFocusRestore {
18221832 XCTAssertTrue([inputView becomeFirstResponder]);
18231833
18241834 OCMVerify(never(), [engine flutterTextInputView:inputView
1825- didRestoreFirstResponderWithTextInputClient:123]);
1835+ didRestoreFirstResponderWithTextInputClient:123
1836+ result:[OCMArg any]]);
1837+
1838+ [inputView resignFirstResponderFromFramework];
1839+ [inputView removeFromSuperview];
1840+ }
1841+
1842+ // Resigns and restores the first responder of |inputView| (client 123) from the platform side, and
1843+ // returns the callback that delivers the framework's reply to the resulting focus restore
1844+ // notification.
1845+ - (FlutterResult)restorePlatformFirstResponderOfInputView:(FlutterTextInputView*)inputView {
1846+ __block FlutterResult restoreResult;
1847+ OCMStub([engine flutterTextInputView:inputView
1848+ didRestoreFirstResponderWithTextInputClient:123
1849+ result:[OCMArg any]])
1850+ .andDo(^(NSInvocation* invocation) {
1851+ __unsafe_unretained FlutterResult resultUnsafe;
1852+ [invocation getArgument:&resultUnsafe atIndex:4];
1853+ restoreResult = resultUnsafe;
1854+ });
1855+
1856+ XCTAssertTrue([inputView becomeFirstResponderFromFramework]);
1857+ XCTAssertTrue([inputView resignFirstResponder]);
1858+ XCTAssertTrue([inputView becomeFirstResponder]);
1859+ XCTAssertNotNil(restoreResult);
1860+ return restoreResult;
1861+ }
1862+
1863+ - (void)testDeclinedFocusRestoreResignsFirstResponder {
1864+ FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin];
1865+ [UIApplication.sharedApplication.keyWindow addSubview:inputView];
1866+ [inputView setTextInputClient:123];
1867+
1868+ FlutterResult restoreResult = [self restorePlatformFirstResponderOfInputView:inputView];
1869+ restoreResult(@NO);
1870+
1871+ XCTAssertFalse(inputView.isFirstResponder);
1872+ OCMVerify(times(2), [engine flutterTextInputView:inputView
1873+ didResignFirstResponderWithTextInputClient:123]);
1874+
1875+ // The resign goes through the framework path, so a later restore does not notify again.
1876+ XCTAssertTrue([inputView becomeFirstResponder]);
1877+ OCMVerify(times(1), [engine flutterTextInputView:inputView
1878+ didRestoreFirstResponderWithTextInputClient:123
1879+ result:[OCMArg any]]);
1880+
1881+ [inputView resignFirstResponderFromFramework];
1882+ [inputView removeFromSuperview];
1883+ }
1884+
1885+ - (void)testAcceptedFocusRestoreKeepsFirstResponder {
1886+ FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin];
1887+ [UIApplication.sharedApplication.keyWindow addSubview:inputView];
1888+ [inputView setTextInputClient:123];
1889+
1890+ FlutterResult restoreResult = [self restorePlatformFirstResponderOfInputView:inputView];
1891+ restoreResult(@YES);
1892+
1893+ XCTAssertTrue(inputView.isFirstResponder);
1894+ OCMVerify(times(1), [engine flutterTextInputView:inputView
1895+ didResignFirstResponderWithTextInputClient:123]);
1896+
1897+ [inputView resignFirstResponderFromFramework];
1898+ [inputView removeFromSuperview];
1899+ }
1900+
1901+ - (void)testFailedFocusRestoreReplyResignsFirstResponder {
1902+ FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin];
1903+ [UIApplication.sharedApplication.keyWindow addSubview:inputView];
1904+ [inputView setTextInputClient:123];
1905+
1906+ FlutterResult restoreResult = [self restorePlatformFirstResponderOfInputView:inputView];
1907+ restoreResult([FlutterError errorWithCode:@"error" message:nil details:nil]);
1908+
1909+ XCTAssertFalse(inputView.isFirstResponder);
1910+
1911+ [inputView removeFromSuperview];
1912+ }
1913+
1914+ - (void)testChangingClientIgnoresPendingFocusRestoreReply {
1915+ FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin];
1916+ [UIApplication.sharedApplication.keyWindow addSubview:inputView];
1917+ [inputView setTextInputClient:123];
1918+
1919+ FlutterResult restoreResult = [self restorePlatformFirstResponderOfInputView:inputView];
1920+ [inputView setTextInputClient:456];
1921+ restoreResult(@NO);
1922+
1923+ XCTAssertTrue(inputView.isFirstResponder);
18261924
18271925 [inputView resignFirstResponderFromFramework];
18281926 [inputView removeFromSuperview];
0 commit comments