|
32 | 32 | #import "TestWKWebView.h"
|
33 | 33 | #import <WebKit/WKWebViewPrivate.h>
|
34 | 34 |
|
| 35 | +#if PLATFORM(IOS) |
| 36 | +#import <UIKit/UIKit.h> |
| 37 | +#endif |
| 38 | + |
35 | 39 | namespace TestWebKitAPI {
|
36 | 40 |
|
37 | 41 | static RetainPtr<EditingTestHarness> setUpEditorStateTestHarness()
|
|
229 | 233 | EXPECT_WK_STREQ("https://www.apple.com/", [[testHarness webView] stringByEvaluatingJavaScript:@"document.querySelector('a').href"]);
|
230 | 234 | }
|
231 | 235 |
|
| 236 | +#if PLATFORM(IOS) |
| 237 | + |
| 238 | +static void checkContentViewHasTextWithFailureDescription(TestWKWebView *webView, BOOL expectedToHaveText, NSString *description) |
| 239 | +{ |
| 240 | + BOOL hasText = webView.textInputContentView.hasText; |
| 241 | + if (expectedToHaveText) |
| 242 | + EXPECT_TRUE(hasText); |
| 243 | + else |
| 244 | + EXPECT_FALSE(hasText); |
| 245 | + |
| 246 | + if (expectedToHaveText != hasText) |
| 247 | + NSLog(@"Expected -[%@ hasText] to be %@, but observed: %@ (%@)", [webView.textInputContentView class], expectedToHaveText ? @"YES" : @"NO", hasText ? @"YES" : @"NO", description); |
| 248 | +} |
| 249 | + |
| 250 | +TEST(EditorStateTests, ContentViewHasTextInContentEditableElement) |
| 251 | +{ |
| 252 | + auto testHarness = setUpEditorStateTestHarness(); |
| 253 | + TestWKWebView *webView = [testHarness webView]; |
| 254 | + |
| 255 | + checkContentViewHasTextWithFailureDescription(webView, NO, @"before inserting any content"); |
| 256 | + [testHarness insertHTML:@"<img src='icon.png'></img>"]; |
| 257 | + checkContentViewHasTextWithFailureDescription(webView, NO, @"after inserting an image element"); |
| 258 | + [testHarness insertText:@"A"]; |
| 259 | + checkContentViewHasTextWithFailureDescription(webView, YES, @"after inserting text"); |
| 260 | + [testHarness selectAll]; |
| 261 | + checkContentViewHasTextWithFailureDescription(webView, YES, @"after selecting everything"); |
| 262 | + [testHarness deleteBackwards]; |
| 263 | + checkContentViewHasTextWithFailureDescription(webView, NO, @"after deleting everything"); |
| 264 | + [testHarness insertParagraph]; |
| 265 | + checkContentViewHasTextWithFailureDescription(webView, YES, @"after inserting a newline"); |
| 266 | + [testHarness deleteBackwards]; |
| 267 | + checkContentViewHasTextWithFailureDescription(webView, NO, @"after deleting the newline"); |
| 268 | + [testHarness insertText:@"B"]; |
| 269 | + checkContentViewHasTextWithFailureDescription(webView, YES, @"after inserting text again"); |
| 270 | + [webView stringByEvaluatingJavaScript:@"document.body.blur()"]; |
| 271 | + [webView waitForNextPresentationUpdate]; |
| 272 | + checkContentViewHasTextWithFailureDescription(webView, NO, @"after losing focus"); |
| 273 | +} |
| 274 | + |
| 275 | +TEST(EditorStateTests, ContentViewHasTextInTextarea) |
| 276 | +{ |
| 277 | + auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:NSMakeRect(0, 0, 400, 400)]); |
| 278 | + auto testHarness = adoptNS([[EditingTestHarness alloc] initWithWebView:webView.get()]); |
| 279 | + [webView synchronouslyLoadHTMLString:@"<textarea id='textarea'></textarea>"]; |
| 280 | + [webView stringByEvaluatingJavaScript:@"textarea.focus()"]; |
| 281 | + [webView waitForNextPresentationUpdate]; |
| 282 | + |
| 283 | + checkContentViewHasTextWithFailureDescription(webView.get(), NO, @"before inserting any content"); |
| 284 | + [testHarness insertText:@"A"]; |
| 285 | + checkContentViewHasTextWithFailureDescription(webView.get(), YES, @"after inserting text"); |
| 286 | + [testHarness selectAll]; |
| 287 | + checkContentViewHasTextWithFailureDescription(webView.get(), YES, @"after selecting everything"); |
| 288 | + [testHarness deleteBackwards]; |
| 289 | + checkContentViewHasTextWithFailureDescription(webView.get(), NO, @"after deleting everything"); |
| 290 | + [testHarness insertParagraph]; |
| 291 | + checkContentViewHasTextWithFailureDescription(webView.get(), YES, @"after inserting a newline"); |
| 292 | + [testHarness deleteBackwards]; |
| 293 | + checkContentViewHasTextWithFailureDescription(webView.get(), NO, @"after deleting the newline"); |
| 294 | + [testHarness insertText:@"B"]; |
| 295 | + checkContentViewHasTextWithFailureDescription(webView.get(), YES, @"after inserting text again"); |
| 296 | + [webView stringByEvaluatingJavaScript:@"textarea.blur()"]; |
| 297 | + [webView waitForNextPresentationUpdate]; |
| 298 | + checkContentViewHasTextWithFailureDescription(webView.get(), NO, @"after losing focus"); |
| 299 | +} |
| 300 | + |
| 301 | +#endif |
| 302 | + |
232 | 303 | } // namespace TestWebKitAPI
|
233 | 304 |
|
234 | 305 | #endif // WK_API_ENABLED
|
0 commit comments