| 1 | struct Explorer { |
| 2 | app: ArchiveApp, |
| 3 | ctx: egui::Context, |
| 4 | factory: FakeFactory, |
| 5 | time: f64, |
| 6 | } |
| 7 | impl Explorer { |
| 8 | fn new(scratch: &Folder) -> Self { |
| 9 | let factory = FakeFactory::default(); |
| 10 | let mut app = loaded(scratch, &factory); |
| 11 | app.model.entries = [ |
| 12 | (0, "docs/child.txt", Kind::File), |
| 13 | (1, "alpha.txt", Kind::File), |
| 14 | (2, "beta.txt", Kind::File), |
| 15 | (3, "gamma.txt", Kind::File), |
| 16 | (4, "omega.txt", Kind::File), |
| 17 | (5, "docs", Kind::Directory), |
| 18 | ] |
| 19 | .map(|(id, name, kind)| Entry { |
| 20 | id, |
| 21 | name: name.into(), |
| 22 | kind, |
| 23 | bytes: Some(7), |
| 24 | packed: None, |
| 25 | encrypted: false, |
| 26 | }) |
| 27 | .into(); |
| 28 | let mut screen = Self { |
| 29 | app, |
| 30 | ctx: egui::Context::default(), |
| 31 | factory, |
| 32 | time: 0.0, |
| 33 | }; |
| 34 | screen.frame(egui::Modifiers::NONE, vec![]); |
| 35 | screen.frame(egui::Modifiers::NONE, vec![]); |
| 36 | screen |
| 37 | } |
| 38 | fn frame(&mut self, modifiers: egui::Modifiers, events: Vec<egui::Event>) -> egui::FullOutput { |
| 39 | self.time += 0.05; |
| 40 | let mut events = events; |
| 41 | events.insert(0, egui::Event::ModifiersChanged(modifiers)); |
| 42 | let mut output = self.ctx.run_ui( |
| 43 | egui::RawInput { |
| 44 | screen_rect: Some(egui::Rect::from_min_size( |
| 45 | egui::Pos2::ZERO, |
| 46 | egui::vec2(760.0, 540.0), |
| 47 | )), |
| 48 | time: Some(self.time), |
| 49 | events, |
| 50 | ..Default::default() |
| 51 | }, |
| 52 | |ui| self.app.show(ui), |
| 53 | ); |
| 54 | output.textures_delta.clear(); |
| 55 | output |
| 56 | } |
| 57 | fn row(&mut self, path: &str) -> egui::Rect { |
| 58 | self.frame(egui::Modifiers::NONE, vec![]); |
| 59 | self.ctx |
| 60 | .read_response(egui::Id::unique(("archive-row", path))) |
| 61 | .unwrap() |
| 62 | .rect |
| 63 | } |
| 64 | fn body(&self) -> egui::Rect { |
| 65 | self.ctx |
| 66 | .read_response(egui::Id::unique("archive-list")) |
| 67 | .unwrap() |
| 68 | .rect |
| 69 | } |
| 70 | fn pointer( |
| 71 | &mut self, |
| 72 | point: egui::Pos2, |
| 73 | pressed: bool, |
| 74 | modifiers: egui::Modifiers, |
| 75 | ) -> egui::FullOutput { |
| 76 | self.frame( |
| 77 | modifiers, |
| 78 | vec![ |
| 79 | egui::Event::PointerMoved(point), |
| 80 | egui::Event::PointerButton { |
| 81 | pos: point, |
| 82 | button: egui::PointerButton::Primary, |
| 83 | pressed, |
| 84 | modifiers, |
| 85 | }, |
| 86 | ], |
| 87 | ) |
| 88 | } |
| 89 | fn click(&mut self, path: &str, modifiers: egui::Modifiers) { |
| 90 | self.time += 1.0; |
| 91 | let point = self.row(path).center(); |
| 92 | self.pointer(point, true, modifiers); |
| 93 | self.pointer(point, false, modifiers); |
| 94 | self.frame(egui::Modifiers::NONE, vec![]); |
| 95 | } |
| 96 | fn key(&mut self, key: egui::Key, modifiers: egui::Modifiers) { |
| 97 | self.frame( |
| 98 | modifiers, |
| 99 | vec![egui::Event::Key { |
| 100 | key, |
| 101 | physical_key: None, |
| 102 | pressed: true, |
| 103 | repeat: false, |
| 104 | modifiers, |
| 105 | }], |
| 106 | ); |
| 107 | self.frame( |
| 108 | modifiers, |
| 109 | vec![egui::Event::Key { |
| 110 | key, |
| 111 | physical_key: None, |
| 112 | pressed: false, |
| 113 | repeat: false, |
| 114 | modifiers, |
| 115 | }], |
| 116 | ); |
| 117 | } |
| 118 | #[track_caller] |
| 119 | fn selected(&self, ids: &[u32]) { |
| 120 | assert_eq!(self.app.model.selected, ids.iter().copied().collect()); |
| 121 | } |
| 122 | } |
| 123 | fn explorer_ctrl() -> egui::Modifiers { |
| 124 | egui::Modifiers { |
| 125 | ctrl: true, |
| 126 | ..egui::Modifiers::NONE |
| 127 | } |
| 128 | } |
| 129 | fn explorer_shift() -> egui::Modifiers { |
| 130 | egui::Modifiers { |
| 131 | shift: true, |
| 132 | ..egui::Modifiers::NONE |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | #[test] |
| 137 | fn explorer_click_ctrl_shift_and_blank_match_visible_selection_count() { |
| 138 | let scratch = Folder::new(); |
| 139 | let mut s = Explorer::new(&scratch); |
| 140 | s.click("alpha.txt", egui::Modifiers::NONE); |
| 141 | s.selected(&[1]); |
| 142 | s.click("beta.txt", explorer_ctrl()); |
| 143 | s.selected(&[1, 2]); |
| 144 | s.click("alpha.txt", explorer_ctrl()); |
| 145 | s.selected(&[2]); |
| 146 | s.click("alpha.txt", egui::Modifiers::NONE); |
| 147 | s.click("gamma.txt", explorer_shift()); |
| 148 | s.selected(&[1, 2, 3]); |
| 149 | let output = s.frame(egui::Modifiers::NONE, vec![]); |
| 150 | assert!( |
| 151 | layout_text(&output) |
| 152 | .iter() |
| 153 | .any(|(text, _, _)| text == "선택 3개") |
| 154 | ); |
| 155 | s.click( |
| 156 | "omega.txt", |
| 157 | egui::Modifiers { |
| 158 | ctrl: true, |
| 159 | shift: true, |
| 160 | ..egui::Modifiers::NONE |
| 161 | }, |
| 162 | ); |
| 163 | s.selected(&[1, 2, 3, 4]); |
| 164 | s.click("docs", egui::Modifiers::NONE); |
| 165 | s.selected(&[0, 5]); |
| 166 | let output = s.frame(egui::Modifiers::NONE, vec![]); |
| 167 | assert!( |
| 168 | layout_text(&output) |
| 169 | .iter() |
| 170 | .any(|(text, _, _)| text == "선택 1개") |
| 171 | ); |
| 172 | let point = egui::pos2(100.0, s.body().bottom() - 4.0); |
| 173 | s.pointer(point, true, explorer_ctrl()); |
| 174 | s.pointer(point, false, explorer_ctrl()); |
| 175 | s.selected(&[0, 5]); |
| 176 | s.pointer(point, true, egui::Modifiers::NONE); |
| 177 | s.pointer(point, false, egui::Modifiers::NONE); |
| 178 | s.selected(&[]); |
| 179 | } |
| 180 | |
| 181 | #[test] |
| 182 | fn explorer_keyboard_ranges_enter_backspace_and_escape_are_scoped_to_the_list() { |
| 183 | let scratch = Folder::new(); |
| 184 | let mut s = Explorer::new(&scratch); |
| 185 | s.ctx |
| 186 | .memory_mut(|m| m.request_focus(egui::Id::unique("archive-list"))); |
| 187 | s.frame(egui::Modifiers::NONE, vec![]); |
| 188 | s.key(egui::Key::ArrowDown, egui::Modifiers::NONE); |
| 189 | s.selected(&[0, 5]); |
| 190 | s.key(egui::Key::ArrowDown, explorer_shift()); |
| 191 | s.selected(&[0, 1, 5]); |
| 192 | s.key(egui::Key::ArrowUp, explorer_shift()); |
| 193 | s.selected(&[0, 5]); |
| 194 | s.key(egui::Key::ArrowDown, explorer_shift()); |
| 195 | s.selected(&[0, 1, 5]); |
| 196 | s.key(egui::Key::A, explorer_ctrl()); |
| 197 | s.selected(&[0, 1, 2, 3, 4, 5]); |
| 198 | s.key(egui::Key::Escape, egui::Modifiers::NONE); |
| 199 | s.selected(&[]); |
| 200 | s.key(egui::Key::ArrowUp, egui::Modifiers::NONE); |
| 201 | s.selected(&[0, 5]); |
| 202 | s.key(egui::Key::Enter, egui::Modifiers::NONE); |
| 203 | s.selected(&[]); |
| 204 | assert!( |
| 205 | layout_text(&s.frame(egui::Modifiers::NONE, vec![])) |
| 206 | .iter() |
| 207 | .any(|(text, _, _)| text == "child.txt") |
| 208 | ); |
| 209 | s.key(egui::Key::Backspace, egui::Modifiers::NONE); |
| 210 | s.selected(&[]); |
| 211 | assert!( |
| 212 | layout_text(&s.frame(egui::Modifiers::NONE, vec![])) |
| 213 | .iter() |
| 214 | .any(|(text, _, _)| text == "alpha.txt") |
| 215 | ); |
| 216 | s.ctx |
| 217 | .memory_mut(|m| m.request_focus(egui::Id::unique("archive-search"))); |
| 218 | s.key(egui::Key::A, explorer_ctrl()); |
| 219 | s.selected(&[]); |
| 220 | s.key(egui::Key::Enter, egui::Modifiers::NONE); |
| 221 | assert_eq!(s.factory.state.borrow().starts.len(), 1); |
| 222 | } |
| 223 | |
| 224 | #[test] |
| 225 | fn explorer_double_click_opens_only_folders_and_resets_selection() { |
| 226 | let scratch = Folder::new(); |
| 227 | let mut s = Explorer::new(&scratch); |
| 228 | let point = s.row("alpha.txt").center(); |
| 229 | for _ in 0..2 { |
| 230 | s.pointer(point, true, egui::Modifiers::NONE); |
| 231 | s.pointer(point, false, egui::Modifiers::NONE); |
| 232 | } |
| 233 | s.selected(&[1]); |
| 234 | assert_eq!(s.factory.state.borrow().starts.len(), 1); |
| 235 | s.time += 1.0; |
| 236 | let point = s.row("docs").center(); |
| 237 | for _ in 0..2 { |
| 238 | s.pointer(point, true, egui::Modifiers::NONE); |
| 239 | s.pointer(point, false, egui::Modifiers::NONE); |
| 240 | } |
| 241 | s.selected(&[]); |
| 242 | assert!( |
| 243 | layout_text(&s.frame(egui::Modifiers::NONE, vec![])) |
| 244 | .iter() |
| 245 | .any(|(text, _, _)| text == "child.txt") |
| 246 | ); |
| 247 | } |
| 248 | |
| 249 | #[test] |
| 250 | fn explorer_marquee_selects_intersections_and_ctrl_adds_with_a_translucent_blue_rectangle() { |
| 251 | let scratch = Folder::new(); |
| 252 | let mut s = Explorer::new(&scratch); |
| 253 | s.click("docs", egui::Modifiers::NONE); |
| 254 | let row = s.row("alpha.txt"); |
| 255 | let start = egui::pos2(row.right() + 8.0, s.body().bottom() - 5.0); |
| 256 | let end = egui::pos2(150.0, row.top() + 2.0); |
| 257 | s.pointer(start, true, explorer_ctrl()); |
| 258 | let output = s.frame(explorer_ctrl(), vec![egui::Event::PointerMoved(end)]); |
| 259 | s.selected(&[0, 1, 2, 3, 4, 5]); |
| 260 | assert!(output.shapes.iter().any(|shape| matches!( |
| 261 | &shape.shape, |
| 262 | Shape::Rect(rect) |
| 263 | if rect.fill == zipelo_ui_memory_app::archive_theme::BLUE.gamma_multiply(0.16) |
| 264 | && rect.rect.height() > 100.0 |
| 265 | && rect.fill.a() > 0 |
| 266 | && rect.fill.a() < 255 |
| 267 | && rect.stroke == egui::Stroke::new(1.0, zipelo_ui_memory_app::archive_theme::BLUE) |
| 268 | && rect.stroke_kind == egui::StrokeKind::Inside |
| 269 | ))); |
| 270 | s.pointer(end, false, explorer_ctrl()); |
| 271 | s.selected(&[0, 1, 2, 3, 4, 5]); |
| 272 | let start = egui::pos2(row.right() + 8.0, s.body().bottom() - 5.0); |
| 273 | s.pointer(start, true, egui::Modifiers::NONE); |
| 274 | s.frame(egui::Modifiers::NONE, vec![egui::Event::PointerMoved(end)]); |
| 275 | s.pointer(end, false, egui::Modifiers::NONE); |
| 276 | s.selected(&[1, 2, 3, 4]); |
| 277 | } |
| 278 | |
| 279 | #[test] |
| 280 | fn explorer_marquee_autoscrolls_thousands_of_rows_without_rendering_them_all() { |
| 281 | let scratch = Folder::new(); |
| 282 | let mut s = Explorer::new(&scratch); |
| 283 | s.app.model.entries = (0..5000) |
| 284 | .map(|id| Entry { |
| 285 | id, |
| 286 | name: format!("item-{id:05}.txt"), |
| 287 | kind: Kind::File, |
| 288 | bytes: Some(1), |
| 289 | packed: None, |
| 290 | encrypted: false, |
| 291 | }) |
| 292 | .collect(); |
| 293 | s.frame(egui::Modifiers::NONE, vec![]); |
| 294 | let row = s.row("item-00000.txt"); |
| 295 | let start = egui::pos2(row.right() + 8.0, row.center().y); |
| 296 | s.pointer(start, true, egui::Modifiers::NONE); |
| 297 | let end = egui::pos2(100.0, s.body().bottom() + 45.0); |
| 298 | s.frame(egui::Modifiers::NONE, vec![egui::Event::PointerMoved(end)]); |
| 299 | let started = std::time::Instant::now(); |
| 300 | let mut output = s.frame(egui::Modifiers::NONE, vec![]); |
| 301 | for _ in 0..40 { |
| 302 | output = s.frame(egui::Modifiers::NONE, vec![]); |
| 303 | } |
| 304 | assert!(started.elapsed() < std::time::Duration::from_secs(5)); |
| 305 | assert!( |
| 306 | s.app.model.selected.len() > 20, |
| 307 | "{:?}", |
| 308 | s.app.model.selected |
| 309 | ); |
| 310 | assert!( |
| 311 | layout_text(&output) |
| 312 | .iter() |
| 313 | .filter(|(text, _, _)| text.starts_with("item-")) |
| 314 | .count() |
| 315 | < 30 |
| 316 | ); |
| 317 | assert!( |
| 318 | layout_text(&output) |
| 319 | .iter() |
| 320 | .filter(|(text, _, _)| text.starts_with("item-")) |
| 321 | .all(|(text, _, _)| text != "item-00000.txt") |
| 322 | ); |
| 323 | let first_visible = |output: &egui::FullOutput| { |
| 324 | layout_text(output) |
| 325 | .into_iter() |
| 326 | .filter_map(|(text, _, _)| { |
| 327 | text.strip_prefix("item-") |
| 328 | .and_then(|name| name.strip_suffix(".txt")) |
| 329 | .and_then(|number| number.parse::<u32>().ok()) |
| 330 | }) |
| 331 | .min() |
| 332 | .unwrap() |
| 333 | }; |
| 334 | let down = first_visible(&output); |
| 335 | let up = egui::pos2(100.0, s.body().top() - 45.0); |
| 336 | s.frame(egui::Modifiers::NONE, vec![egui::Event::PointerMoved(up)]); |
| 337 | for _ in 0..12 { |
| 338 | output = s.frame(egui::Modifiers::NONE, vec![]); |
| 339 | } |
| 340 | assert!(first_visible(&output) < down); |
| 341 | s.pointer(up, false, egui::Modifiers::NONE); |
| 342 | let selected = s.app.model.selected.clone(); |
| 343 | s.frame(egui::Modifiers::NONE, vec![egui::Event::PointerMoved(end)]); |
| 344 | assert_eq!(s.app.model.selected, selected); |
| 345 | } |
| 346 | |
| 347 | #[test] |
| 348 | fn explorer_selected_folder_and_empty_selection_reach_the_exact_extraction_request() { |
| 349 | let scratch = Folder::new(); |
| 350 | let mut s = Explorer::new(&scratch); |
| 351 | s.click("docs", egui::Modifiers::NONE); |
| 352 | s.app.choose_destination(scratch.0.clone()); |
| 353 | assert!(s.app.begin_extract()); |
| 354 | assert!( |
| 355 | matches!(s.factory.state.borrow().starts.last(), Some(Start::Extract { selection: Selection::EntryIds(ids), .. }) if ids.iter().copied().collect::<BTreeSet<_>>() == [0,5].into_iter().collect()) |
| 356 | ); |
| 357 | for _ in 0..30 { |
| 358 | s.frame(egui::Modifiers::NONE, vec![]); |
| 359 | } |
| 360 | s.app.model.selected.clear(); |
| 361 | assert!(s.app.begin_extract()); |
| 362 | assert!(matches!( |
| 363 | s.factory.state.borrow().starts.last(), |
| 364 | Some(Start::Extract { |
| 365 | selection: Selection::All, |
| 366 | .. |
| 367 | }) |
| 368 | )); |
| 369 | } |
| 370 | |
| 371 | #[test] |
| 372 | fn explorer_fast_forward_marquee_uses_the_original_press_position() { |
| 373 | let scratch = Folder::new(); |
| 374 | let mut s = Explorer::new(&scratch); |
| 375 | let first = s.row("docs"); |
| 376 | let last = s.row("beta.txt"); |
| 377 | let start = egui::pos2(first.right() + 8.0, first.top() + 2.0); |
| 378 | let end = egui::pos2(100.0, last.bottom() - 1.0); |
| 379 | s.frame( |
| 380 | egui::Modifiers::NONE, |
| 381 | vec![ |
| 382 | egui::Event::PointerMoved(start), |
| 383 | egui::Event::PointerButton { |
| 384 | pos: start, |
| 385 | button: egui::PointerButton::Primary, |
| 386 | pressed: true, |
| 387 | modifiers: egui::Modifiers::NONE, |
| 388 | }, |
| 389 | egui::Event::PointerMoved(end), |
| 390 | ], |
| 391 | ); |
| 392 | s.selected(&[0, 1, 2, 5]); |
| 393 | s.pointer(end, false, egui::Modifiers::NONE); |
| 394 | s.selected(&[0, 1, 2, 5]); |
| 395 | } |
| 396 | |
| 397 | #[test] |
| 398 | fn explorer_ctrl_removes_a_large_directory_subtree_without_ancestor_rescans() { |
| 399 | let scratch = Folder::new(); |
| 400 | let mut s = Explorer::new(&scratch); |
| 401 | s.app.model.entries = (0..5001) |
| 402 | .map(|id| Entry { |
| 403 | id, |
| 404 | name: if id == 0 { |
| 405 | "docs".into() |
| 406 | } else { |
| 407 | format!("docs/child-{id:05}") |
| 408 | }, |
| 409 | kind: Kind::Directory, |
| 410 | bytes: None, |
| 411 | packed: None, |
| 412 | encrypted: false, |
| 413 | }) |
| 414 | .collect(); |
| 415 | s.frame(egui::Modifiers::NONE, vec![]); |
| 416 | s.click("docs", egui::Modifiers::NONE); |
| 417 | assert_eq!(s.app.model.selected.len(), 5001); |
| 418 | let started = std::time::Instant::now(); |
| 419 | s.click("docs", explorer_ctrl()); |
| 420 | assert!(s.app.model.selected.is_empty()); |
| 421 | assert!(started.elapsed() < std::time::Duration::from_secs(2)); |
| 422 | } |
| 423 | |
| 424 | #[test] |
| 425 | fn explorer_mouse_click_gives_list_focus_for_keyboard_selection_and_navigation() { |
| 426 | let scratch = Folder::new(); |
| 427 | let mut s = Explorer::new(&scratch); |
| 428 | assert!(s.ctx.memory(|memory| memory.focused()).is_none()); |
| 429 | s.click("alpha.txt", egui::Modifiers::NONE); |
| 430 | assert!(s.ctx.memory(|memory| memory.focused()).is_some()); |
| 431 | s.key(egui::Key::ArrowDown, egui::Modifiers::NONE); |
| 432 | s.selected(&[2]); |
| 433 | s.key(egui::Key::ArrowDown, explorer_shift()); |
| 434 | s.selected(&[2, 3]); |
| 435 | s.key(egui::Key::A, explorer_ctrl()); |
| 436 | s.selected(&[0, 1, 2, 3, 4, 5]); |
| 437 | s.click("docs", egui::Modifiers::NONE); |
| 438 | s.key(egui::Key::Enter, egui::Modifiers::NONE); |
| 439 | assert!( |
| 440 | layout_text(&s.frame(egui::Modifiers::NONE, vec![])) |
| 441 | .iter() |
| 442 | .any(|(text, _, _)| text == "child.txt") |
| 443 | ); |
| 444 | s.key(egui::Key::Backspace, egui::Modifiers::NONE); |
| 445 | assert!( |
| 446 | layout_text(&s.frame(egui::Modifiers::NONE, vec![])) |
| 447 | .iter() |
| 448 | .any(|(text, _, _)| text == "alpha.txt") |
| 449 | ); |
| 450 | s.click("alpha.txt", egui::Modifiers::NONE); |
| 451 | s.key(egui::Key::Escape, egui::Modifiers::NONE); |
| 452 | s.selected(&[]); |
| 453 | } |
| 454 | |
| 455 | #[test] |
| 456 | fn explorer_retry_preserves_original_scope_after_selection_changes_and_conflicts() { |
| 457 | for selected in [false, true] { |
| 458 | for conflict in [false, true] { |
| 459 | let scratch = Folder::new(); |
| 460 | let mut s = Explorer::new(&scratch); |
| 461 | s.factory.state.borrow_mut().manual = true; |
| 462 | if selected { |
| 463 | s.click("docs", egui::Modifiers::NONE); |
| 464 | } |
| 465 | assert!(s.app.begin_extract()); |
| 466 | let original = match s.factory.state.borrow().starts.last().unwrap() { |
| 467 | Start::Extract { selection, .. } => selection.clone(), |
| 468 | _ => panic!("추출 요청이 필요함"), |
| 469 | }; |
| 470 | { |
| 471 | let mut worker = s.factory.state.borrow_mut(); |
| 472 | worker.frame(Event::Accepted { encryption: false }); |
| 473 | worker.finish(Outcome::Failed, Publication::None); |
| 474 | for event in &mut worker.queue { |
| 475 | if let HostEvent::Frame { |
| 476 | event: Event::Terminal(t), |
| 477 | .. |
| 478 | } = event |
| 479 | { |
| 480 | t.error = Some(if conflict { |
| 481 | ErrorCode::Conflict |
| 482 | } else { |
| 483 | ErrorCode::Io |
| 484 | }); |
| 485 | } |
| 486 | } |
| 487 | } |
| 488 | for _ in 0..30 { |
| 489 | s.frame(egui::Modifiers::NONE, vec![]); |
| 490 | } |
| 491 | s.click("alpha.txt", egui::Modifiers::NONE); |
| 492 | s.selected(&[1]); |
| 493 | let chosen = scratch.0.join("retry-output"); |
| 494 | if conflict { |
| 495 | fs::create_dir(&chosen).unwrap(); |
| 496 | let dialogs = TestDialogs::default(); |
| 497 | dialogs |
| 498 | .0 |
| 499 | .borrow_mut() |
| 500 | .answers |
| 501 | .push_back(Some(vec![chosen.clone()])); |
| 502 | s.app.set_dialogs(Box::new(dialogs)); |
| 503 | } |
| 504 | policy_click( |
| 505 | &s.ctx, |
| 506 | &mut s.app, |
| 507 | if conflict { |
| 508 | "다른 폴더에 풀기" |
| 509 | } else { |
| 510 | "다시 시도" |
| 511 | }, |
| 512 | ); |
| 513 | assert!(matches!(s.factory.state.borrow().starts.last(), |
| 514 | Some(Start::Extract { selection, destination, .. }) |
| 515 | if selection == &original && (!conflict || destination == &chosen))); |
| 516 | } |
| 517 | } |
| 518 | } |