Skip to content

Commit c5f7a61

Browse files
committed
Fix: add tests for cursor movement.
1 parent 1dbf577 commit c5f7a61

File tree

2 files changed

+188
-36
lines changed
  • server/tests

2 files changed

+188
-36
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# The contents of this file don't matter -- tests will supply the content,
2+
# instead of loading it from disk. However, it does need to exist for
3+
# `canonicalize` to find the correct path to this file.

server/tests/overall_core/mod.rs

Lines changed: 185 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,54 @@ fn get_version(msg: &EditorMessage) -> f64 {
257257
.version
258258
}
259259

260+
async fn goto_line(
261+
codechat_server: &CodeChatEditorServer,
262+
driver_ref: &WebDriver,
263+
client_id: &mut f64,
264+
path_str: &str,
265+
line: u32,
266+
) -> Result<(), Box<dyn Error + Send + Sync>> {
267+
let code_line_css = ".CodeChat-CodeMirror .cm-line";
268+
let code_line = driver_ref.find(By::Css(code_line_css)).await.unwrap();
269+
code_line
270+
.send_keys(
271+
Key::Alt
272+
+ if cfg!(target_os = "macos") {
273+
Key::Command
274+
} else {
275+
Key::Control
276+
}
277+
+ "g",
278+
)
279+
.await
280+
.unwrap();
281+
// Enter a line in the dialog that pops up.
282+
driver_ref
283+
.find(By::Css("input.cm-textfield"))
284+
.await
285+
.unwrap()
286+
.send_keys(line.to_string() + Key::Enter)
287+
.await
288+
.unwrap();
289+
// The cursor movement produces a cursor/scroll position update after an
290+
// autosave delay.
291+
assert_eq!(
292+
codechat_server.get_message_timeout(TIMEOUT).await.unwrap(),
293+
EditorMessage {
294+
id: *client_id,
295+
message: EditorMessageContents::Update(UpdateMessageContents {
296+
file_path: path_str.to_string(),
297+
contents: None,
298+
cursor_position: Some(line),
299+
scroll_position: Some(1.0)
300+
})
301+
}
302+
);
303+
codechat_server.send_result(*client_id, None).await.unwrap();
304+
*client_id += MESSAGE_ID_INCREMENT;
305+
306+
Ok(())
307+
}
260308
// Tests
261309
// -----------------------------------------------------------------------------
262310
//
@@ -1183,46 +1231,13 @@ async fn test_client_updates_core(
11831231
}
11841232
);
11851233

1186-
// Insert a character to check the insertion point.
1187-
let code_line_css = ".CodeChat-CodeMirror .cm-line";
1188-
let code_line = driver_ref.find(By::Css(code_line_css)).await.unwrap();
1189-
code_line
1190-
.send_keys(
1191-
Key::Alt
1192-
+ if cfg!(target_os = "macos") {
1193-
Key::Command
1194-
} else {
1195-
Key::Control
1196-
}
1197-
+ "g",
1198-
)
1234+
goto_line(&codechat_server, driver_ref, &mut client_id, &path_str, 4)
11991235
.await
12001236
.unwrap();
1201-
// Enter a line in the dialog that pops up.
1202-
driver_ref
1203-
.find(By::Css("input.cm-textfield"))
1204-
.await
1205-
.unwrap()
1206-
.send_keys("4" + Key::Enter)
1207-
.await
1208-
.unwrap();
1209-
// The cursor movement produces a cursor/scroll position update after an
1210-
// autosave delay.
1211-
assert_eq!(
1212-
codechat_server.get_message_timeout(TIMEOUT).await.unwrap(),
1213-
EditorMessage {
1214-
id: client_id,
1215-
message: EditorMessageContents::Update(UpdateMessageContents {
1216-
file_path: path_str.clone(),
1217-
contents: None,
1218-
cursor_position: Some(4),
1219-
scroll_position: Some(1.0)
1220-
})
1221-
}
1222-
);
1223-
client_id += MESSAGE_ID_INCREMENT;
12241237

12251238
// Add an indented comment.
1239+
let code_line_css = ".CodeChat-CodeMirror .cm-line";
1240+
let code_line = driver_ref.find(By::Css(code_line_css)).await.unwrap();
12261241
code_line.send_keys(Key::Home + "# ").await.unwrap();
12271242
// This should edit the (new) third line of the file after word wrap: `def
12281243
// foo():`.
@@ -1258,3 +1273,137 @@ async fn test_client_updates_core(
12581273

12591274
Ok(())
12601275
}
1276+
1277+
mod test4 {
1278+
use super::*;
1279+
harness!(test_4_core);
1280+
}
1281+
1282+
#[tokio::test]
1283+
async fn test_4() -> Result<(), Box<dyn Error + Send + Sync>> {
1284+
test4::harness(test_4_core, prep_test_dir!()).await
1285+
}
1286+
1287+
// Some of the thirtyfour calls are marked as deprecated, though they aren't
1288+
// marked that way in the Selenium docs.
1289+
#[allow(deprecated)]
1290+
async fn test_4_core(
1291+
codechat_server: CodeChatEditorServer,
1292+
driver_ref: &WebDriver,
1293+
test_dir: PathBuf,
1294+
) -> Result<(), WebDriverError> {
1295+
let mut expected_messages = ExpectedMessages::new();
1296+
let path = canonicalize(test_dir.join("test.py")).unwrap();
1297+
let path_str = path.to_str().unwrap().to_string();
1298+
let current_file_id = codechat_server
1299+
.send_message_current_file(path_str.clone())
1300+
.await
1301+
.unwrap();
1302+
// The ordering of these messages isn't fixed -- one can come first, or the
1303+
// other.
1304+
expected_messages.insert(EditorMessage {
1305+
id: current_file_id,
1306+
message: EditorMessageContents::Result(Ok(ResultOkTypes::Void)),
1307+
});
1308+
let mut server_id = 6.0;
1309+
expected_messages.insert(EditorMessage {
1310+
id: server_id,
1311+
message: EditorMessageContents::LoadFile(path.clone()),
1312+
});
1313+
expected_messages
1314+
.assert_all_messages(&codechat_server, TIMEOUT)
1315+
.await;
1316+
1317+
// Respond to the load request.
1318+
let ide_version = 0.0;
1319+
codechat_server
1320+
.send_result_loadfile(
1321+
server_id,
1322+
Some((
1323+
indoc!(
1324+
"
1325+
# 1
1326+
2
1327+
# 3
1328+
4
1329+
# 5
1330+
"
1331+
)
1332+
.to_string(),
1333+
ide_version,
1334+
)),
1335+
)
1336+
.await
1337+
.unwrap();
1338+
server_id += MESSAGE_ID_INCREMENT;
1339+
1340+
// The loadfile produces a message to the client, which comes back here. We
1341+
// don't need to acknowledge it.
1342+
assert_eq!(
1343+
codechat_server.get_message_timeout(TIMEOUT).await.unwrap(),
1344+
EditorMessage {
1345+
id: server_id,
1346+
message: EditorMessageContents::Result(Ok(ResultOkTypes::Void))
1347+
}
1348+
);
1349+
1350+
// Target the iframe containing the Client.
1351+
let codechat_iframe = driver_ref.find(By::Css("#CodeChat-iframe")).await.unwrap();
1352+
driver_ref
1353+
.switch_to()
1354+
.frame_element(&codechat_iframe)
1355+
.await
1356+
.unwrap();
1357+
1358+
// Switch from one doc block to another. It should produce an update with only cursor/scroll info (no contents).
1359+
let mut client_id = INITIAL_CLIENT_MESSAGE_ID;
1360+
let doc_blocks = driver_ref.find_all(By::Css(".CodeChat-doc")).await.unwrap();
1361+
doc_blocks[0].click().await.unwrap();
1362+
assert_eq!(
1363+
codechat_server.get_message_timeout(TIMEOUT).await.unwrap(),
1364+
EditorMessage {
1365+
id: client_id,
1366+
message: EditorMessageContents::Update(UpdateMessageContents {
1367+
file_path: path_str.clone(),
1368+
contents: None,
1369+
cursor_position: Some(1),
1370+
scroll_position: Some(1.0)
1371+
})
1372+
}
1373+
);
1374+
codechat_server.send_result(client_id, None).await.unwrap();
1375+
client_id += MESSAGE_ID_INCREMENT;
1376+
1377+
doc_blocks[1].click().await.unwrap();
1378+
assert_eq!(
1379+
codechat_server.get_message_timeout(TIMEOUT).await.unwrap(),
1380+
EditorMessage {
1381+
id: client_id,
1382+
message: EditorMessageContents::Update(UpdateMessageContents {
1383+
file_path: path_str.clone(),
1384+
contents: None,
1385+
cursor_position: Some(3),
1386+
scroll_position: Some(1.0)
1387+
})
1388+
}
1389+
);
1390+
codechat_server.send_result(client_id, None).await.unwrap();
1391+
client_id += MESSAGE_ID_INCREMENT;
1392+
1393+
doc_blocks[2].click().await.unwrap();
1394+
assert_eq!(
1395+
codechat_server.get_message_timeout(TIMEOUT).await.unwrap(),
1396+
EditorMessage {
1397+
id: client_id,
1398+
message: EditorMessageContents::Update(UpdateMessageContents {
1399+
file_path: path_str.clone(),
1400+
contents: None,
1401+
cursor_position: Some(5),
1402+
scroll_position: Some(1.0)
1403+
})
1404+
}
1405+
);
1406+
codechat_server.send_result(client_id, None).await.unwrap();
1407+
1408+
Ok(())
1409+
}

0 commit comments

Comments
 (0)