92b1b9eeae85beab9ed859f72bfdce2ce609304c
[WebKit-https.git] / Tools / TestWebKitAPI / EditingTestHarness.mm
1 /*
2  * Copyright (C) 2017 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26
27 #include "config.h"
28 #include "EditingTestHarness.h"
29
30 #if WK_API_ENABLED
31
32 #import "PlatformUtilities.h"
33 #import <WebKit/WKWebViewPrivate.h>
34
35 @implementation EditingTestHarness
36
37 - (instancetype)initWithWebView:(TestWKWebView *)webView
38 {
39     if (self = [super init]) {
40         _webView = webView;
41         [_webView setUIDelegate:self];
42         _editorStateHistory = adoptNS([[NSMutableArray alloc] init]);
43     }
44     return self;
45 }
46
47 - (void)dealloc
48 {
49     if ([_webView UIDelegate] == self)
50         [_webView setUIDelegate:nil];
51
52     [super dealloc];
53 }
54
55 - (TestWKWebView *)webView
56 {
57     return _webView.get();
58 }
59
60 - (NSDictionary *)latestEditorState
61 {
62     return self.editorStateHistory.lastObject;
63 }
64
65 - (NSArray<NSDictionary *> *)editorStateHistory
66 {
67     return _editorStateHistory.get();
68 }
69
70 - (void)insertParagraph
71 {
72     [self insertParagraphAndExpectEditorStateWith:nil];
73 }
74
75 - (void)insertText:(NSString *)text
76 {
77     [self insertText:text andExpectEditorStateWith:nil];
78 }
79
80 - (void)insertHTML:(NSString *)html
81 {
82     [self insertHTML:html andExpectEditorStateWith:nil];
83 }
84
85 - (void)selectAll
86 {
87     [self selectAllAndExpectEditorStateWith:nil];
88 }
89
90 - (void)deleteBackwards
91 {
92     [self deleteBackwardAndExpectEditorStateWith:nil];
93 }
94
95 - (void)insertText:(NSString *)text andExpectEditorStateWith:(NSDictionary<NSString *, id> *)entries
96 {
97     [self _execCommand:@"InsertText" argument:text expectEntries:entries];
98 }
99
100 - (void)insertHTML:(NSString *)html andExpectEditorStateWith:(NSDictionary<NSString *, id> *)entries
101 {
102     [self _execCommand:@"InsertHTML" argument:html expectEntries:entries];
103 }
104
105 - (void)selectAllAndExpectEditorStateWith:(NSDictionary<NSString *, id> *)entries
106 {
107     [self _execCommand:@"SelectAll" argument:nil expectEntries:entries];
108 }
109
110 - (void)moveBackwardAndExpectEditorStateWith:(NSDictionary<NSString *, id> *)entries
111 {
112     [self _execCommand:@"MoveBackward" argument:nil expectEntries:entries];
113 }
114
115 - (void)moveWordBackwardAndExpectEditorStateWith:(NSDictionary<NSString *, id> *)entries
116 {
117     [self _execCommand:@"MoveWordBackward" argument:nil expectEntries:entries];
118 }
119
120 - (void)toggleBold
121 {
122     [self _execCommand:@"ToggleBold" argument:nil expectEntries:nil];
123 }
124
125 - (void)toggleItalic
126 {
127     [self _execCommand:@"ToggleItalic" argument:nil expectEntries:nil];
128 }
129
130 - (void)toggleUnderline
131 {
132     [self _execCommand:@"ToggleUnderline" argument:nil expectEntries:nil];
133 }
134
135 - (void)setForegroundColor:(NSString *)colorAsString
136 {
137     [self _execCommand:@"ForeColor" argument:colorAsString expectEntries:nil];
138 }
139
140 - (void)alignJustifiedAndExpectEditorStateWith:(NSDictionary<NSString *, id> *)entries
141 {
142     [self _execCommand:@"AlignJustified" argument:nil expectEntries:entries];
143 }
144
145 - (void)alignLeftAndExpectEditorStateWith:(NSDictionary<NSString *, id> *)entries
146 {
147     [self _execCommand:@"AlignLeft" argument:nil expectEntries:entries];
148 }
149
150 - (void)alignCenterAndExpectEditorStateWith:(NSDictionary<NSString *, id> *)entries
151 {
152     [self _execCommand:@"AlignCenter" argument:nil expectEntries:entries];
153 }
154
155 - (void)alignRightAndExpectEditorStateWith:(NSDictionary<NSString *, id> *)entries
156 {
157     [self _execCommand:@"AlignRight" argument:nil expectEntries:entries];
158 }
159
160 - (void)insertParagraphAndExpectEditorStateWith:(NSDictionary<NSString *, id> *)entries
161 {
162     [self _execCommand:@"InsertParagraph" argument:nil expectEntries:entries];
163 }
164
165 - (void)deleteBackwardAndExpectEditorStateWith:(NSDictionary<NSString *, id> *)entries
166 {
167     [self _execCommand:@"DeleteBackward" argument:nil expectEntries:entries];
168 }
169
170 - (void)_execCommand:(NSString *)command argument:(NSString *)argument expectEntries:(NSDictionary<NSString *, id> *)entries
171 {
172     __block BOOL result = false;
173     __block bool done = false;
174     [_webView _executeEditCommand:command argument:argument completion:^(BOOL success) {
175         result = success;
176         done = true;
177     }];
178     TestWebKitAPI::Util::run(&done);
179
180     [_webView waitForNextPresentationUpdate];
181
182     EXPECT_TRUE(result);
183     if (!result)
184         NSLog(@"Failed to execute editing command: ('%@', '%@')", command, argument ?: @"");
185
186     BOOL containsEntries = [self latestEditorStateContains:entries];
187     EXPECT_TRUE(containsEntries);
188     if (!containsEntries)
189         NSLog(@"Expected %@ to contain %@", self.latestEditorState, entries);
190 }
191
192 - (BOOL)latestEditorStateContains:(NSDictionary<NSString *, id> *)entries
193 {
194     NSDictionary *latestEditorState = self.latestEditorState;
195     for (NSString *key in entries) {
196         if (![latestEditorState[key] isEqual:entries[key]])
197             return NO;
198     }
199     return latestEditorState.count || !entries.count;
200 }
201
202 #pragma mark - WKUIDelegatePrivate
203
204 - (void)_webView:(WKWebView *)webView editorStateDidChange:(NSDictionary *)editorState
205 {
206     if (![editorState[@"post-layout-data"] boolValue])
207         return;
208
209     if (![self.latestEditorState isEqualToDictionary:editorState])
210         [_editorStateHistory addObject:editorState];
211 }
212
213 @end
214
215 #endif // WK_API_ENABLED