| 1 | struct Explorer { |
| 2 | app: ArchiveApp, |
| 3 | ctx: egui::Context, |
| 4 | factory: FakeFactory, |
| 5 | time: f64, |
| 6 | opened: std::rc::Rc<std::cell::RefCell<Vec<PathBuf>>>, |
| 7 | } |
| 8 | impl Explorer { |
| 9 | fn new(scratch: &Folder) -> Self { |
| 10 | let factory = FakeFactory::default(); |
| 11 | let mut app = loaded(scratch, &factory); |
| 12 | app.model.entries = [ |
| 13 | (0, "docs/child.txt", Kind::File), |
| 14 | (1, "alpha.txt", Kind::File), |
| 15 | (2, "beta.txt", Kind::File), |
| 16 | (3, "gamma.txt", Kind::File), |
| 17 | (4, "omega.txt", Kind::File), |
| 18 | (5, "docs", Kind::Directory), |
| 19 | ] |
| 20 | .map(|(id, name, kind)| Entry { |
| 21 | id, |
| 22 | name: name.into(), |
| 23 | kind, |
| 24 | bytes: Some(7), |
| 25 | packed: None, |
| 26 | encrypted: false, |
| 27 | }) |
| 28 | .into(); |
| 29 | let opened = std::rc::Rc::new(std::cell::RefCell::new(Vec::new())); |
| 30 | let record = opened.clone(); |
| 31 | app.set_opener(Box::new(move |path| { |
| 32 | record.borrow_mut().push(path.to_path_buf()); |
| 33 | Ok(()) |
| 34 | })); |
| 35 | let mut screen = Self { |
| 36 | app, |
| 37 | ctx: egui::Context::default(), |
| 38 | factory, |
| 39 | time: 0.0, |
| 40 | opened, |
| 41 | }; |
| 42 | screen.frame(egui::Modifiers::NONE, vec![]); |
| 43 | screen.frame(egui::Modifiers::NONE, vec![]); |
| 44 | screen |
| 45 | } |
| 46 | fn frame(&mut self, modifiers: egui::Modifiers, events: Vec<egui::Event>) -> egui::FullOutput { |
| 47 | self.time += 0.05; |
| 48 | let mut events = events; |
| 49 | events.insert(0, egui::Event::ModifiersChanged(modifiers)); |
| 50 | let mut output = self.ctx.run_ui( |
| 51 | egui::RawInput { |
| 52 | screen_rect: Some(egui::Rect::from_min_size( |
| 53 | egui::Pos2::ZERO, |
| 54 | egui::vec2(760.0, 540.0), |
| 55 | )), |
| 56 | time: Some(self.time), |
| 57 | events, |
| 58 | ..Default::default() |
| 59 | }, |
| 60 | |ui| self.app.show(ui), |
| 61 | ); |
| 62 | output.textures_delta.clear(); |
| 63 | output |
| 64 | } |
| 65 | fn row(&mut self, path: &str) -> egui::Rect { |
| 66 | self.frame(egui::Modifiers::NONE, vec![]); |
| 67 | self.ctx |
| 68 | .read_response(egui::Id::unique(("archive-row", path))) |
| 69 | .unwrap() |
| 70 | .rect |
| 71 | } |
| 72 | fn body(&self) -> egui::Rect { |
| 73 | self.ctx |
| 74 | .read_response(egui::Id::unique("archive-list")) |
| 75 | .unwrap() |
| 76 | .rect |
| 77 | } |
| 78 | fn pointer( |
| 79 | &mut self, |
| 80 | point: egui::Pos2, |
| 81 | pressed: bool, |
| 82 | modifiers: egui::Modifiers, |
| 83 | ) -> egui::FullOutput { |
| 84 | self.frame( |
| 85 | modifiers, |
| 86 | vec![ |
| 87 | egui::Event::PointerMoved(point), |
| 88 | egui::Event::PointerButton { |
| 89 | pos: point, |
| 90 | button: egui::PointerButton::Primary, |
| 91 | pressed, |
| 92 | modifiers, |
| 93 | }, |
| 94 | ], |
| 95 | ) |
| 96 | } |
| 97 | fn click(&mut self, path: &str, modifiers: egui::Modifiers) { |
| 98 | self.time += 1.0; |
| 99 | let point = self.row(path).center(); |
| 100 | self.pointer(point, true, modifiers); |
| 101 | self.pointer(point, false, modifiers); |
| 102 | self.frame(egui::Modifiers::NONE, vec![]); |
| 103 | } |
| 104 | fn key(&mut self, key: egui::Key, modifiers: egui::Modifiers) { |
| 105 | self.frame( |
| 106 | modifiers, |
| 107 | vec![egui::Event::Key { |
| 108 | key, |
| 109 | physical_key: None, |
| 110 | pressed: true, |
| 111 | repeat: false, |
| 112 | modifiers, |
| 113 | }], |
| 114 | ); |
| 115 | self.frame( |
| 116 | modifiers, |
| 117 | vec![egui::Event::Key { |
| 118 | key, |
| 119 | physical_key: None, |
| 120 | pressed: false, |
| 121 | repeat: false, |
| 122 | modifiers, |
| 123 | }], |
| 124 | ); |
| 125 | } |
| 126 | fn button(&mut self, point: egui::Pos2, button: egui::PointerButton) { |
| 127 | self.time += 1.0; |
| 128 | for pressed in [true, false] { |
| 129 | self.frame( |
| 130 | egui::Modifiers::NONE, |
| 131 | vec![ |
| 132 | egui::Event::PointerMoved(point), |
| 133 | egui::Event::PointerButton { |
| 134 | pos: point, |
| 135 | button, |
| 136 | pressed, |
| 137 | modifiers: egui::Modifiers::NONE, |
| 138 | }, |
| 139 | ], |
| 140 | ); |
| 141 | } |
| 142 | self.frame(egui::Modifiers::NONE, vec![]); |
| 143 | } |
| 144 | fn double_click(&mut self, path: &str) { |
| 145 | self.time += 1.0; |
| 146 | let point = self.row(path).center(); |
| 147 | for _ in 0..2 { |
| 148 | self.pointer(point, true, egui::Modifiers::NONE); |
| 149 | self.pointer(point, false, egui::Modifiers::NONE); |
| 150 | } |
| 151 | for _ in 0..30 { |
| 152 | self.frame(egui::Modifiers::NONE, vec![]); |
| 153 | } |
| 154 | } |
| 155 | fn click_text(&mut self, label: &str) { |
| 156 | let output = self.frame(egui::Modifiers::NONE, vec![]); |
| 157 | let rect = layout_text(&output) |
| 158 | .into_iter() |
| 159 | .find(|(text, _, _)| text == label) |
| 160 | .unwrap_or_else(|| panic!("{label} 없음")) |
| 161 | .1; |
| 162 | self.button(rect.center(), egui::PointerButton::Primary); |
| 163 | } |
| 164 | #[track_caller] |
| 165 | fn selected(&self, ids: &[u32]) { |
| 166 | assert_eq!(self.app.model.selected, ids.iter().copied().collect()); |
| 167 | } |
| 168 | } |
| 169 | fn explorer_ctrl() -> egui::Modifiers { |
| 170 | egui::Modifiers { |
| 171 | ctrl: true, |
| 172 | ..egui::Modifiers::NONE |
| 173 | } |
| 174 | } |
| 175 | fn explorer_shift() -> egui::Modifiers { |
| 176 | egui::Modifiers { |
| 177 | shift: true, |
| 178 | ..egui::Modifiers::NONE |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | #[test] |
| 183 | fn explorer_click_ctrl_shift_and_blank_match_visible_selection_count() { |
| 184 | let scratch = Folder::new(); |
| 185 | let mut s = Explorer::new(&scratch); |
| 186 | s.click("alpha.txt", egui::Modifiers::NONE); |
| 187 | s.selected(&[1]); |
| 188 | s.click("beta.txt", explorer_ctrl()); |
| 189 | s.selected(&[1, 2]); |
| 190 | s.click("alpha.txt", explorer_ctrl()); |
| 191 | s.selected(&[2]); |
| 192 | s.click("alpha.txt", egui::Modifiers::NONE); |
| 193 | s.click("gamma.txt", explorer_shift()); |
| 194 | s.selected(&[1, 2, 3]); |
| 195 | let output = s.frame(egui::Modifiers::NONE, vec![]); |
| 196 | assert!( |
| 197 | layout_text(&output) |
| 198 | .iter() |
| 199 | .any(|(text, _, _)| text == "선택 3개") |
| 200 | ); |
| 201 | s.click( |
| 202 | "omega.txt", |
| 203 | egui::Modifiers { |
| 204 | ctrl: true, |
| 205 | shift: true, |
| 206 | ..egui::Modifiers::NONE |
| 207 | }, |
| 208 | ); |
| 209 | s.selected(&[1, 2, 3, 4]); |
| 210 | s.click("docs", egui::Modifiers::NONE); |
| 211 | s.selected(&[0, 5]); |
| 212 | let output = s.frame(egui::Modifiers::NONE, vec![]); |
| 213 | assert!( |
| 214 | layout_text(&output) |
| 215 | .iter() |
| 216 | .any(|(text, _, _)| text == "선택 1개") |
| 217 | ); |
| 218 | let point = egui::pos2(100.0, s.body().bottom() - 4.0); |
| 219 | s.pointer(point, true, explorer_ctrl()); |
| 220 | s.pointer(point, false, explorer_ctrl()); |
| 221 | s.selected(&[0, 5]); |
| 222 | s.pointer(point, true, egui::Modifiers::NONE); |
| 223 | s.pointer(point, false, egui::Modifiers::NONE); |
| 224 | s.selected(&[]); |
| 225 | } |
| 226 | |
| 227 | #[test] |
| 228 | fn explorer_keyboard_ranges_enter_backspace_and_escape_are_scoped_to_the_list() { |
| 229 | let scratch = Folder::new(); |
| 230 | let mut s = Explorer::new(&scratch); |
| 231 | s.ctx |
| 232 | .memory_mut(|m| m.request_focus(egui::Id::unique("archive-list"))); |
| 233 | s.frame(egui::Modifiers::NONE, vec![]); |
| 234 | s.key(egui::Key::ArrowDown, egui::Modifiers::NONE); |
| 235 | s.selected(&[0, 5]); |
| 236 | s.key(egui::Key::ArrowDown, explorer_shift()); |
| 237 | s.selected(&[0, 1, 5]); |
| 238 | s.key(egui::Key::ArrowUp, explorer_shift()); |
| 239 | s.selected(&[0, 5]); |
| 240 | s.key(egui::Key::ArrowDown, explorer_shift()); |
| 241 | s.selected(&[0, 1, 5]); |
| 242 | s.key(egui::Key::A, explorer_ctrl()); |
| 243 | s.selected(&[0, 1, 2, 3, 4, 5]); |
| 244 | s.key(egui::Key::Escape, egui::Modifiers::NONE); |
| 245 | s.selected(&[]); |
| 246 | s.key(egui::Key::ArrowUp, egui::Modifiers::NONE); |
| 247 | s.selected(&[0, 5]); |
| 248 | s.key(egui::Key::Enter, egui::Modifiers::NONE); |
| 249 | s.selected(&[]); |
| 250 | assert!( |
| 251 | layout_text(&s.frame(egui::Modifiers::NONE, vec![])) |
| 252 | .iter() |
| 253 | .any(|(text, _, _)| text == "child.txt") |
| 254 | ); |
| 255 | s.key(egui::Key::Backspace, egui::Modifiers::NONE); |
| 256 | s.selected(&[]); |
| 257 | assert!( |
| 258 | layout_text(&s.frame(egui::Modifiers::NONE, vec![])) |
| 259 | .iter() |
| 260 | .any(|(text, _, _)| text == "alpha.txt") |
| 261 | ); |
| 262 | s.ctx |
| 263 | .memory_mut(|m| m.request_focus(egui::Id::unique("archive-search"))); |
| 264 | s.key(egui::Key::A, explorer_ctrl()); |
| 265 | s.selected(&[]); |
| 266 | s.key(egui::Key::Enter, egui::Modifiers::NONE); |
| 267 | assert_eq!(s.factory.state.borrow().starts.len(), 1); |
| 268 | } |
| 269 | |
| 270 | #[test] |
| 271 | fn explorer_double_click_opens_files_with_default_app_and_folders_in_place() { |
| 272 | let scratch = Folder::new(); |
| 273 | let mut s = Explorer::new(&scratch); |
| 274 | let destination = s.app.model.archive.clone().unwrap().with_file_name("sample"); |
| 275 | // 실제 작업자처럼 새로 만든 상위 폴더도 출력으로 알림. |
| 276 | s.factory.state.borrow_mut().outputs = Some(vec!["docs".into(), "docs/child.txt".into()]); |
| 277 | s.app.browse_folder("docs"); |
| 278 | s.double_click("docs/child.txt"); |
| 279 | s.selected(&[0]); |
| 280 | // 파일은 압축파일 옆 숨김 임시 폴더에 그 파일만 풀고 기본 프로그램으로 엶. |
| 281 | let root = scratch.0.join(zipelo_ui_memory_app::archive_present::open_root_name("sample.zip")); |
| 282 | { |
| 283 | let state = s.factory.state.borrow(); |
| 284 | assert_eq!(state.starts.len(), 2); |
| 285 | let Start::Extract { |
| 286 | destination: folder, |
| 287 | selection, |
| 288 | .. |
| 289 | } = state.starts.last().unwrap() |
| 290 | else { |
| 291 | panic!("{:?}", state.starts.last()); |
| 292 | }; |
| 293 | assert_eq!(selection, &Selection::EntryIds(vec![0])); |
| 294 | assert_eq!(folder, &root.join("1")); |
| 295 | } |
| 296 | assert_eq!( |
| 297 | *s.opened.borrow(), |
| 298 | vec![root.join("1").join("docs").join("child.txt")] |
| 299 | ); |
| 300 | let output = s.frame(egui::Modifiers::NONE, vec![]); |
| 301 | assert!( |
| 302 | !layout_text(&output) |
| 303 | .iter() |
| 304 | .any(|(text, _, _)| text == "작업 결과") |
| 305 | ); |
| 306 | // 같은 파일을 다시 열면 새 하위 폴더를 씀. |
| 307 | s.double_click("docs/child.txt"); |
| 308 | assert!(matches!( |
| 309 | s.factory.state.borrow().starts.last(), |
| 310 | Some(Start::Extract { destination, .. }) if destination == &root.join("2") |
| 311 | )); |
| 312 | assert_eq!(s.opened.borrow().len(), 2); |
| 313 | // 열 파일이 출력에 없으면 조용히 넘어가지 않고 알림. |
| 314 | s.factory.state.borrow_mut().outputs = Some(vec!["docs".into()]); |
| 315 | s.double_click("docs/child.txt"); |
| 316 | assert_eq!(s.opened.borrow().len(), 2); |
| 317 | assert!( |
| 318 | layout_text(&s.frame(egui::Modifiers::NONE, vec![])) |
| 319 | .iter() |
| 320 | .any(|(text, _, _)| text == "풀린 파일을 찾지 못해 열지 못했습니다") |
| 321 | ); |
| 322 | // 열기용 폴더는 압축파일 이름 폴더와 섞이지 않음. |
| 323 | assert!(!destination.exists()); |
| 324 | // 앱을 닫으면 열기용 폴더를 지움. |
| 325 | assert!(root.is_dir()); |
| 326 | drop(s); |
| 327 | assert!(!root.exists()); |
| 328 | } |
| 329 | |
| 330 | /// 열기용 풀기가 한도 초과로 실패한 상태까지 진행함. 결과 창을 그리는 문맥도 함께 돌려줌. |
| 331 | fn double_click_open_hits_limit(s: &mut Explorer) -> egui::Context { |
| 332 | s.factory.state.borrow_mut().manual = true; |
| 333 | s.double_click("alpha.txt"); |
| 334 | assert_eq!(s.factory.state.borrow().starts.len(), 2); |
| 335 | finish_limit(s); |
| 336 | let ctx = egui::Context::default(); |
| 337 | ctx.set_embed_viewports(false); |
| 338 | for _ in 0..12 { |
| 339 | policy_render(&ctx, &mut s.app, vec![]); |
| 340 | } |
| 341 | assert!(s.app.model.can_retry_expanded()); |
| 342 | assert!(s.opened.borrow().is_empty()); |
| 343 | ctx |
| 344 | } |
| 345 | |
| 346 | fn finish_limit(s: &mut Explorer) { |
| 347 | { |
| 348 | let mut worker = s.factory.state.borrow_mut(); |
| 349 | worker.frame(Event::Accepted { encryption: false }); |
| 350 | worker.frame(Event::Terminal(Terminal { |
| 351 | outcome: Outcome::Failed, |
| 352 | error: Some(ErrorCode::Limit), |
| 353 | publication: Publication::None, |
| 354 | created_count: 0, |
| 355 | retained_partial_count: 0, |
| 356 | durability: Durability::NotApplicable, |
| 357 | cleanup: Cleanup::Complete, |
| 358 | })); |
| 359 | worker.queue.extend([ |
| 360 | HostEvent::StdoutEof, |
| 361 | HostEvent::StderrEof, |
| 362 | HostEvent::Exited { success: true }, |
| 363 | ]); |
| 364 | } |
| 365 | for _ in 0..30 { |
| 366 | s.frame(egui::Modifiers::NONE, vec![]); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | #[test] |
| 371 | fn explorer_open_keeps_pending_open_when_limit_retry_succeeds() { |
| 372 | let scratch = Folder::new(); |
| 373 | let mut s = Explorer::new(&scratch); |
| 374 | let root = scratch.0.join(zipelo_ui_memory_app::archive_present::open_root_name("sample.zip")); |
| 375 | let ctx = double_click_open_hits_limit(&mut s); |
| 376 | policy_click(&ctx, &mut s.app, "한도를 늘려 다시 시도"); |
| 377 | assert!(s.app.model.busy()); |
| 378 | assert!(s.factory.state.borrow().expanded); |
| 379 | assert!(matches!( |
| 380 | s.factory.state.borrow().starts.last(), |
| 381 | Some(Start::Extract { destination, .. }) if destination == &root.join("1") |
| 382 | )); |
| 383 | { |
| 384 | let mut worker = s.factory.state.borrow_mut(); |
| 385 | worker.frame(Event::Accepted { encryption: false }); |
| 386 | worker.frame(Event::Output { |
| 387 | entry_id: None, |
| 388 | relative_name: "alpha.txt".into(), |
| 389 | disposition: Disposition::Created, |
| 390 | }); |
| 391 | worker.finish(Outcome::Completed, Publication::Complete); |
| 392 | } |
| 393 | for _ in 0..30 { |
| 394 | s.frame(egui::Modifiers::NONE, vec![]); |
| 395 | } |
| 396 | // 다시 시도가 성공하면 기본 프로그램이 열림. |
| 397 | assert_eq!(*s.opened.borrow(), vec![root.join("1").join("alpha.txt")]); |
| 398 | // 열기가 끝나면 풀 위치는 원래대로 돌아옴. |
| 399 | s.factory.state.borrow_mut().manual = false; |
| 400 | assert!(s.app.begin_extract()); |
| 401 | assert!(matches!( |
| 402 | s.factory.state.borrow().starts.last(), |
| 403 | Some(Start::Extract { destination, .. }) if !destination.starts_with(&root) |
| 404 | )); |
| 405 | } |
| 406 | |
| 407 | #[test] |
| 408 | fn explorer_open_restores_destination_when_limit_result_is_dismissed() { |
| 409 | let scratch = Folder::new(); |
| 410 | let mut s = Explorer::new(&scratch); |
| 411 | let root = scratch.0.join(zipelo_ui_memory_app::archive_present::open_root_name("sample.zip")); |
| 412 | let ctx = double_click_open_hits_limit(&mut s); |
| 413 | policy_click(&ctx, &mut s.app, "확인"); |
| 414 | for _ in 0..3 { |
| 415 | policy_render(&ctx, &mut s.app, vec![]); |
| 416 | } |
| 417 | assert!(s.opened.borrow().is_empty()); |
| 418 | s.factory.state.borrow_mut().manual = false; |
| 419 | assert!(s.app.begin_extract()); |
| 420 | let state = s.factory.state.borrow(); |
| 421 | let Some(Start::Extract { destination, .. }) = state.starts.last() else { |
| 422 | panic!("{:?}", state.starts.last()); |
| 423 | }; |
| 424 | assert_eq!(destination, &scratch.0.join("sample")); |
| 425 | assert!(!destination.starts_with(&root)); |
| 426 | } |
| 427 | |
| 428 | #[test] |
| 429 | fn explorer_open_restores_destination_when_expanded_retry_fails_again() { |
| 430 | let scratch = Folder::new(); |
| 431 | let mut s = Explorer::new(&scratch); |
| 432 | let ctx = double_click_open_hits_limit(&mut s); |
| 433 | policy_click(&ctx, &mut s.app, "한도를 늘려 다시 시도"); |
| 434 | assert!(s.app.model.busy()); |
| 435 | finish_limit(&mut s); |
| 436 | assert!(s.opened.borrow().is_empty()); |
| 437 | assert!(!s.app.model.can_retry_expanded()); |
| 438 | s.factory.state.borrow_mut().manual = false; |
| 439 | assert!(s.app.begin_extract()); |
| 440 | assert!(matches!( |
| 441 | s.factory.state.borrow().starts.last(), |
| 442 | Some(Start::Extract { destination, .. }) if destination == &scratch.0.join("sample") |
| 443 | )); |
| 444 | } |
| 445 | |
| 446 | #[test] |
| 447 | fn explorer_open_pending_after_limit_is_dropped_when_zip_creation_starts() { |
| 448 | let scratch = Folder::new(); |
| 449 | let mut s = Explorer::new(&scratch); |
| 450 | let root = scratch.0.join(zipelo_ui_memory_app::archive_present::open_root_name("sample.zip")); |
| 451 | let _limit_ctx = double_click_open_hits_limit(&mut s); |
| 452 | // 한도 초과 결과를 닫지 않고 폴더를 끌어 놓아 ZIP 만들기를 시작함. |
| 453 | let folder = scratch.0.join("photos"); |
| 454 | std::fs::create_dir_all(&folder).unwrap(); |
| 455 | std::fs::write(folder.join("a.txt"), b"abc").unwrap(); |
| 456 | s.app.add_sources(vec![folder]); |
| 457 | let ctx = egui::Context::default(); |
| 458 | policy_click(&ctx, &mut s.app, "압축하기"); |
| 459 | assert!(s.app.model.busy()); |
| 460 | assert!(matches!( |
| 461 | s.factory.state.borrow().starts.last(), |
| 462 | Some(Start::CreateZip { .. }) |
| 463 | )); |
| 464 | { |
| 465 | let mut worker = s.factory.state.borrow_mut(); |
| 466 | worker.frame(Event::Accepted { encryption: false }); |
| 467 | worker.frame(Event::Output { |
| 468 | entry_id: None, |
| 469 | relative_name: "photos.zip".into(), |
| 470 | disposition: Disposition::Created, |
| 471 | }); |
| 472 | worker.finish(Outcome::Completed, Publication::Complete); |
| 473 | } |
| 474 | for _ in 0..30 { |
| 475 | s.frame(egui::Modifiers::NONE, vec![]); |
| 476 | } |
| 477 | // 만들기가 끝나도 이전 열기가 적용되지 않아 오류가 뜨지 않음. |
| 478 | for _ in 0..3 { |
| 479 | policy_render(&ctx, &mut s.app, vec![]); |
| 480 | } |
| 481 | let shown = policy_render(&ctx, &mut s.app, vec![]); |
| 482 | assert!( |
| 483 | !shown.iter().any(|(text, _)| text.contains("열지 못했습니다")), |
| 484 | "{shown:?}" |
| 485 | ); |
| 486 | assert!(s.opened.borrow().is_empty()); |
| 487 | // 풀 위치도 원래대로 돌아옴. |
| 488 | s.factory.state.borrow_mut().manual = false; |
| 489 | assert!(s.app.begin_extract()); |
| 490 | let state = s.factory.state.borrow(); |
| 491 | let Some(Start::Extract { destination, .. }) = state.starts.last() else { |
| 492 | panic!("{:?}", state.starts.last()); |
| 493 | }; |
| 494 | assert_eq!(destination, &scratch.0.join("sample")); |
| 495 | assert!(!destination.starts_with(&root)); |
| 496 | } |
| 497 | |
| 498 | #[test] |
| 499 | fn explorer_double_click_does_not_open_executables() { |
| 500 | let scratch = Folder::new(); |
| 501 | let mut s = Explorer::new(&scratch); |
| 502 | s.app.model.entries[2].name = "setup.EXE".into(); |
| 503 | s.double_click("setup.EXE"); |
| 504 | assert_eq!(s.factory.state.borrow().starts.len(), 1); |
| 505 | assert!(s.opened.borrow().is_empty()); |
| 506 | let output = s.frame(egui::Modifiers::NONE, vec![]); |
| 507 | assert!( |
| 508 | layout_text(&output) |
| 509 | .iter() |
| 510 | .any(|(text, _, _)| text == "실행 파일(.EXE)은 안전을 위해 바로 열 수 없습니다") |
| 511 | ); |
| 512 | // 실행 파일은 압축을 풀지 않으므로 임시 폴더도 만들지 않음. |
| 513 | assert!(!scratch.0.join(zipelo_ui_memory_app::archive_present::open_root_name("sample.zip")).exists()); |
| 514 | } |
| 515 | |
| 516 | #[test] |
| 517 | fn explorer_dragging_in_parent_work_window_does_not_select_rows() { |
| 518 | let scratch = Folder::new(); |
| 519 | let mut s = Explorer::new(&scratch); |
| 520 | s.app.set_work_window_in_parent(true); |
| 521 | s.app.model.entries[2].name = "setup.EXE".into(); |
| 522 | s.double_click("setup.EXE"); |
| 523 | s.selected(&[2]); |
| 524 | let close = s |
| 525 | .ctx |
| 526 | .read_response(egui::Id::unique(("window-button", "work-in-parent", "닫기"))) |
| 527 | .expect("부모 안 작업 창") |
| 528 | .rect; |
| 529 | // 행이 하나뿐인 폴더로 옮겨 작업 창 제목 줄 아래가 목록의 빈 곳이 되게 함. |
| 530 | s.app.browse_folder("docs"); |
| 531 | let row = s.row("docs/child.txt"); |
| 532 | s.selected(&[]); |
| 533 | // 작업 창 제목 줄을 누르고 목록 행 위로 끌어도 목록 선택 사각형이 생기지 않아야 함. |
| 534 | let start = egui::pos2(close.left() - 120.0, close.center().y); |
| 535 | assert!(start.y > row.bottom(), "{start:?} {row:?}"); |
| 536 | s.pointer(start, true, egui::Modifiers::NONE); |
| 537 | let end = egui::pos2(row.center().x, row.center().y); |
| 538 | for step in 1..=6 { |
| 539 | let t = step as f32 / 6.0; |
| 540 | s.frame( |
| 541 | egui::Modifiers::NONE, |
| 542 | vec![egui::Event::PointerMoved(start + (end - start) * t)], |
| 543 | ); |
| 544 | } |
| 545 | let output = s.frame(egui::Modifiers::NONE, vec![]); |
| 546 | s.selected(&[]); |
| 547 | assert!(!output.shapes.iter().any(|shape| matches!( |
| 548 | &shape.shape, |
| 549 | egui::epaint::Shape::Rect(rect) if rect.fill == theme_blue_fill() |
| 550 | ))); |
| 551 | s.pointer(end, false, egui::Modifiers::NONE); |
| 552 | s.selected(&[]); |
| 553 | } |
| 554 | |
| 555 | fn theme_blue_fill() -> egui::Color32 { |
| 556 | zipelo_ui_memory_app::archive_theme::BLUE.gamma_multiply(0.16) |
| 557 | } |
| 558 | |
| 559 | #[test] |
| 560 | fn explorer_context_menu_extracts_selection_into_numbered_archive_folder() { |
| 561 | let scratch = Folder::new(); |
| 562 | let mut s = Explorer::new(&scratch); |
| 563 | fs::create_dir(scratch.0.join("sample")).unwrap(); |
| 564 | s.click("alpha.txt", egui::Modifiers::NONE); |
| 565 | // 선택하지 않은 행을 오른쪽 클릭하면 그 행만 선택하고 메뉴를 엶. |
| 566 | let point = s.row("docs").center(); |
| 567 | s.button(point, egui::PointerButton::Secondary); |
| 568 | s.selected(&[0, 5]); |
| 569 | s.click_text("압축 파일명으로 새 폴더를 만들어 압축 풀기"); |
| 570 | for _ in 0..30 { |
| 571 | s.frame(egui::Modifiers::NONE, vec![]); |
| 572 | } |
| 573 | let state = s.factory.state.borrow(); |
| 574 | let Some(Start::Extract { |
| 575 | destination, |
| 576 | selection, |
| 577 | .. |
| 578 | }) = state.starts.last() |
| 579 | else { |
| 580 | panic!("{:?}", state.starts.last()); |
| 581 | }; |
| 582 | assert_eq!(destination, &scratch.0.join("sample (1)")); |
| 583 | assert_eq!(selection, &Selection::EntryIds(vec![0, 5])); |
| 584 | assert!(scratch.0.join("sample (1)").is_dir()); |
| 585 | assert!(s.opened.borrow().is_empty()); |
| 586 | } |
| 587 | |
| 588 | #[test] |
| 589 | fn explorer_context_menu_asks_for_folder_and_extracts_only_selection() { |
| 590 | let scratch = Folder::new(); |
| 591 | let mut s = Explorer::new(&scratch); |
| 592 | let dialogs = crate::dialogs::TestDialogs::default(); |
| 593 | let target = scratch.0.join("target"); |
| 594 | fs::create_dir(&target).unwrap(); |
| 595 | dialogs |
| 596 | .0 |
| 597 | .borrow_mut() |
| 598 | .answers |
| 599 | .push_back(Some(vec![target.clone()])); |
| 600 | s.app.set_dialogs(Box::new(dialogs.clone())); |
| 601 | s.click("alpha.txt", egui::Modifiers::NONE); |
| 602 | s.click("beta.txt", explorer_ctrl()); |
| 603 | let point = s.row("beta.txt").center(); |
| 604 | s.button(point, egui::PointerButton::Secondary); |
| 605 | s.selected(&[1, 2]); |
| 606 | s.click_text("특정 폴더에 압축 풀기"); |
| 607 | for _ in 0..30 { |
| 608 | s.frame(egui::Modifiers::NONE, vec![]); |
| 609 | } |
| 610 | assert_eq!( |
| 611 | dialogs.0.borrow().requests.last().unwrap().purpose, |
| 612 | zipelo_ui_memory_app::archive_dialog::Purpose::Destination |
| 613 | ); |
| 614 | assert!(matches!( |
| 615 | s.factory.state.borrow().starts.last(), |
| 616 | Some(Start::Extract { destination, selection: Selection::EntryIds(ids), .. }) |
| 617 | if destination == &target && ids == &vec![1, 2] |
| 618 | )); |
| 619 | } |
| 620 | |
| 621 | #[test] |
| 622 | fn explorer_mouse_back_button_goes_to_parent_folder() { |
| 623 | let scratch = Folder::new(); |
| 624 | let mut s = Explorer::new(&scratch); |
| 625 | s.app.browse_folder("docs"); |
| 626 | let point = s.row("docs/child.txt").center(); |
| 627 | s.button(point, egui::PointerButton::Extra2); |
| 628 | assert!(s.ctx.read_response(egui::Id::unique(("archive-row", "docs/child.txt"))).is_some()); |
| 629 | s.button(point, egui::PointerButton::Extra1); |
| 630 | assert!( |
| 631 | layout_text(&s.frame(egui::Modifiers::NONE, vec![])) |
| 632 | .iter() |
| 633 | .any(|(text, _, _)| text == "alpha.txt") |
| 634 | ); |
| 635 | // 최상위에서는 아무 일도 하지 않음. |
| 636 | s.button(point, egui::PointerButton::Extra1); |
| 637 | assert!( |
| 638 | layout_text(&s.frame(egui::Modifiers::NONE, vec![])) |
| 639 | .iter() |
| 640 | .any(|(text, _, _)| text == "alpha.txt") |
| 641 | ); |
| 642 | } |
| 643 | |
| 644 | #[test] |
| 645 | fn explorer_double_click_opens_only_folders_and_resets_selection() { |
| 646 | let scratch = Folder::new(); |
| 647 | let mut s = Explorer::new(&scratch); |
| 648 | s.time += 1.0; |
| 649 | let point = s.row("docs").center(); |
| 650 | for _ in 0..2 { |
| 651 | s.pointer(point, true, egui::Modifiers::NONE); |
| 652 | s.pointer(point, false, egui::Modifiers::NONE); |
| 653 | } |
| 654 | s.selected(&[]); |
| 655 | assert!( |
| 656 | layout_text(&s.frame(egui::Modifiers::NONE, vec![])) |
| 657 | .iter() |
| 658 | .any(|(text, _, _)| text == "child.txt") |
| 659 | ); |
| 660 | } |
| 661 | |
| 662 | #[test] |
| 663 | fn explorer_marquee_selects_intersections_and_ctrl_adds_with_a_translucent_blue_rectangle() { |
| 664 | let scratch = Folder::new(); |
| 665 | let mut s = Explorer::new(&scratch); |
| 666 | s.click("docs", egui::Modifiers::NONE); |
| 667 | let row = s.row("alpha.txt"); |
| 668 | let start = egui::pos2(row.right() + 8.0, s.body().bottom() - 5.0); |
| 669 | let end = egui::pos2(150.0, row.top() + 2.0); |
| 670 | s.pointer(start, true, explorer_ctrl()); |
| 671 | let output = s.frame(explorer_ctrl(), vec![egui::Event::PointerMoved(end)]); |
| 672 | s.selected(&[0, 1, 2, 3, 4, 5]); |
| 673 | assert!(output.shapes.iter().any(|shape| matches!( |
| 674 | &shape.shape, |
| 675 | Shape::Rect(rect) |
| 676 | if rect.fill == zipelo_ui_memory_app::archive_theme::BLUE.gamma_multiply(0.16) |
| 677 | && rect.rect.height() > 100.0 |
| 678 | && rect.fill.a() > 0 |
| 679 | && rect.fill.a() < 255 |
| 680 | && rect.stroke == egui::Stroke::new(1.0, zipelo_ui_memory_app::archive_theme::BLUE) |
| 681 | && rect.stroke_kind == egui::StrokeKind::Inside |
| 682 | ))); |
| 683 | s.pointer(end, false, explorer_ctrl()); |
| 684 | s.selected(&[0, 1, 2, 3, 4, 5]); |
| 685 | let start = egui::pos2(row.right() + 8.0, s.body().bottom() - 5.0); |
| 686 | s.pointer(start, true, egui::Modifiers::NONE); |
| 687 | s.frame(egui::Modifiers::NONE, vec![egui::Event::PointerMoved(end)]); |
| 688 | s.pointer(end, false, egui::Modifiers::NONE); |
| 689 | s.selected(&[1, 2, 3, 4]); |
| 690 | } |
| 691 | |
| 692 | #[test] |
| 693 | fn explorer_marquee_autoscrolls_thousands_of_rows_without_rendering_them_all() { |
| 694 | let scratch = Folder::new(); |
| 695 | let mut s = Explorer::new(&scratch); |
| 696 | s.app.model.entries = (0..5000) |
| 697 | .map(|id| Entry { |
| 698 | id, |
| 699 | name: format!("item-{id:05}.txt"), |
| 700 | kind: Kind::File, |
| 701 | bytes: Some(1), |
| 702 | packed: None, |
| 703 | encrypted: false, |
| 704 | }) |
| 705 | .collect(); |
| 706 | s.frame(egui::Modifiers::NONE, vec![]); |
| 707 | let row = s.row("item-00000.txt"); |
| 708 | let start = egui::pos2(row.right() + 8.0, row.center().y); |
| 709 | s.pointer(start, true, egui::Modifiers::NONE); |
| 710 | let end = egui::pos2(100.0, s.body().bottom() + 45.0); |
| 711 | s.frame(egui::Modifiers::NONE, vec![egui::Event::PointerMoved(end)]); |
| 712 | let started = std::time::Instant::now(); |
| 713 | let mut output = s.frame(egui::Modifiers::NONE, vec![]); |
| 714 | for _ in 0..40 { |
| 715 | output = s.frame(egui::Modifiers::NONE, vec![]); |
| 716 | } |
| 717 | assert!(started.elapsed() < std::time::Duration::from_secs(5)); |
| 718 | assert!( |
| 719 | s.app.model.selected.len() > 20, |
| 720 | "{:?}", |
| 721 | s.app.model.selected |
| 722 | ); |
| 723 | assert!( |
| 724 | layout_text(&output) |
| 725 | .iter() |
| 726 | .filter(|(text, _, _)| text.starts_with("item-")) |
| 727 | .count() |
| 728 | < 30 |
| 729 | ); |
| 730 | assert!( |
| 731 | layout_text(&output) |
| 732 | .iter() |
| 733 | .filter(|(text, _, _)| text.starts_with("item-")) |
| 734 | .all(|(text, _, _)| text != "item-00000.txt") |
| 735 | ); |
| 736 | let first_visible = |output: &egui::FullOutput| { |
| 737 | layout_text(output) |
| 738 | .into_iter() |
| 739 | .filter_map(|(text, _, _)| { |
| 740 | text.strip_prefix("item-") |
| 741 | .and_then(|name| name.strip_suffix(".txt")) |
| 742 | .and_then(|number| number.parse::<u32>().ok()) |
| 743 | }) |
| 744 | .min() |
| 745 | .unwrap() |
| 746 | }; |
| 747 | let down = first_visible(&output); |
| 748 | let up = egui::pos2(100.0, s.body().top() - 45.0); |
| 749 | s.frame(egui::Modifiers::NONE, vec![egui::Event::PointerMoved(up)]); |
| 750 | for _ in 0..12 { |
| 751 | output = s.frame(egui::Modifiers::NONE, vec![]); |
| 752 | } |
| 753 | assert!(first_visible(&output) < down); |
| 754 | s.pointer(up, false, egui::Modifiers::NONE); |
| 755 | let selected = s.app.model.selected.clone(); |
| 756 | s.frame(egui::Modifiers::NONE, vec![egui::Event::PointerMoved(end)]); |
| 757 | assert_eq!(s.app.model.selected, selected); |
| 758 | } |
| 759 | |
| 760 | #[test] |
| 761 | fn explorer_selected_folder_and_empty_selection_reach_the_exact_extraction_request() { |
| 762 | let scratch = Folder::new(); |
| 763 | let mut s = Explorer::new(&scratch); |
| 764 | s.click("docs", egui::Modifiers::NONE); |
| 765 | s.app.choose_destination(scratch.0.clone()); |
| 766 | assert!(s.app.begin_extract()); |
| 767 | assert!( |
| 768 | 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()) |
| 769 | ); |
| 770 | for _ in 0..30 { |
| 771 | s.frame(egui::Modifiers::NONE, vec![]); |
| 772 | } |
| 773 | s.app.model.selected.clear(); |
| 774 | assert!(s.app.begin_extract()); |
| 775 | assert!(matches!( |
| 776 | s.factory.state.borrow().starts.last(), |
| 777 | Some(Start::Extract { |
| 778 | selection: Selection::All, |
| 779 | .. |
| 780 | }) |
| 781 | )); |
| 782 | } |
| 783 | |
| 784 | #[test] |
| 785 | fn explorer_fast_forward_marquee_uses_the_original_press_position() { |
| 786 | let scratch = Folder::new(); |
| 787 | let mut s = Explorer::new(&scratch); |
| 788 | let first = s.row("docs"); |
| 789 | let last = s.row("beta.txt"); |
| 790 | let start = egui::pos2(first.right() + 8.0, first.top() + 2.0); |
| 791 | let end = egui::pos2(100.0, last.bottom() - 1.0); |
| 792 | s.frame( |
| 793 | egui::Modifiers::NONE, |
| 794 | vec![ |
| 795 | egui::Event::PointerMoved(start), |
| 796 | egui::Event::PointerButton { |
| 797 | pos: start, |
| 798 | button: egui::PointerButton::Primary, |
| 799 | pressed: true, |
| 800 | modifiers: egui::Modifiers::NONE, |
| 801 | }, |
| 802 | egui::Event::PointerMoved(end), |
| 803 | ], |
| 804 | ); |
| 805 | s.selected(&[0, 1, 2, 5]); |
| 806 | s.pointer(end, false, egui::Modifiers::NONE); |
| 807 | s.selected(&[0, 1, 2, 5]); |
| 808 | } |
| 809 | |
| 810 | #[test] |
| 811 | fn explorer_ctrl_removes_a_large_directory_subtree_without_ancestor_rescans() { |
| 812 | let scratch = Folder::new(); |
| 813 | let mut s = Explorer::new(&scratch); |
| 814 | s.app.model.entries = (0..5001) |
| 815 | .map(|id| Entry { |
| 816 | id, |
| 817 | name: if id == 0 { |
| 818 | "docs".into() |
| 819 | } else { |
| 820 | format!("docs/child-{id:05}") |
| 821 | }, |
| 822 | kind: Kind::Directory, |
| 823 | bytes: None, |
| 824 | packed: None, |
| 825 | encrypted: false, |
| 826 | }) |
| 827 | .collect(); |
| 828 | s.frame(egui::Modifiers::NONE, vec![]); |
| 829 | s.click("docs", egui::Modifiers::NONE); |
| 830 | assert_eq!(s.app.model.selected.len(), 5001); |
| 831 | let started = std::time::Instant::now(); |
| 832 | s.click("docs", explorer_ctrl()); |
| 833 | assert!(s.app.model.selected.is_empty()); |
| 834 | assert!(started.elapsed() < std::time::Duration::from_secs(2)); |
| 835 | } |
| 836 | |
| 837 | #[test] |
| 838 | fn explorer_mouse_click_gives_list_focus_for_keyboard_selection_and_navigation() { |
| 839 | let scratch = Folder::new(); |
| 840 | let mut s = Explorer::new(&scratch); |
| 841 | assert!(s.ctx.memory(|memory| memory.focused()).is_none()); |
| 842 | s.click("alpha.txt", egui::Modifiers::NONE); |
| 843 | assert!(s.ctx.memory(|memory| memory.focused()).is_some()); |
| 844 | s.key(egui::Key::ArrowDown, egui::Modifiers::NONE); |
| 845 | s.selected(&[2]); |
| 846 | s.key(egui::Key::ArrowDown, explorer_shift()); |
| 847 | s.selected(&[2, 3]); |
| 848 | s.key(egui::Key::A, explorer_ctrl()); |
| 849 | s.selected(&[0, 1, 2, 3, 4, 5]); |
| 850 | s.click("docs", egui::Modifiers::NONE); |
| 851 | s.key(egui::Key::Enter, egui::Modifiers::NONE); |
| 852 | assert!( |
| 853 | layout_text(&s.frame(egui::Modifiers::NONE, vec![])) |
| 854 | .iter() |
| 855 | .any(|(text, _, _)| text == "child.txt") |
| 856 | ); |
| 857 | s.key(egui::Key::Backspace, egui::Modifiers::NONE); |
| 858 | assert!( |
| 859 | layout_text(&s.frame(egui::Modifiers::NONE, vec![])) |
| 860 | .iter() |
| 861 | .any(|(text, _, _)| text == "alpha.txt") |
| 862 | ); |
| 863 | s.click("alpha.txt", egui::Modifiers::NONE); |
| 864 | s.key(egui::Key::Escape, egui::Modifiers::NONE); |
| 865 | s.selected(&[]); |
| 866 | } |
| 867 | |
| 868 | #[test] |
| 869 | fn explorer_retry_preserves_original_scope_after_selection_changes_and_conflicts() { |
| 870 | for selected in [false, true] { |
| 871 | for conflict in [false, true] { |
| 872 | let scratch = Folder::new(); |
| 873 | let mut s = Explorer::new(&scratch); |
| 874 | s.factory.state.borrow_mut().manual = true; |
| 875 | if selected { |
| 876 | s.click("docs", egui::Modifiers::NONE); |
| 877 | } |
| 878 | assert!(s.app.begin_extract()); |
| 879 | let original = match s.factory.state.borrow().starts.last().unwrap() { |
| 880 | Start::Extract { selection, .. } => selection.clone(), |
| 881 | _ => panic!("추출 요청이 필요함"), |
| 882 | }; |
| 883 | { |
| 884 | let mut worker = s.factory.state.borrow_mut(); |
| 885 | worker.frame(Event::Accepted { encryption: false }); |
| 886 | worker.finish(Outcome::Failed, Publication::None); |
| 887 | for event in &mut worker.queue { |
| 888 | if let HostEvent::Frame { |
| 889 | event: Event::Terminal(t), |
| 890 | .. |
| 891 | } = event |
| 892 | { |
| 893 | t.error = Some(if conflict { |
| 894 | ErrorCode::Conflict |
| 895 | } else { |
| 896 | ErrorCode::Io |
| 897 | }); |
| 898 | } |
| 899 | } |
| 900 | } |
| 901 | for _ in 0..30 { |
| 902 | s.frame(egui::Modifiers::NONE, vec![]); |
| 903 | } |
| 904 | s.click("alpha.txt", egui::Modifiers::NONE); |
| 905 | s.selected(&[1]); |
| 906 | let chosen = scratch.0.join("retry-output"); |
| 907 | if conflict { |
| 908 | fs::create_dir(&chosen).unwrap(); |
| 909 | let dialogs = TestDialogs::default(); |
| 910 | dialogs |
| 911 | .0 |
| 912 | .borrow_mut() |
| 913 | .answers |
| 914 | .push_back(Some(vec![chosen.clone()])); |
| 915 | s.app.set_dialogs(Box::new(dialogs)); |
| 916 | } |
| 917 | policy_click( |
| 918 | &s.ctx, |
| 919 | &mut s.app, |
| 920 | if conflict { |
| 921 | "다른 폴더에 풀기" |
| 922 | } else { |
| 923 | "다시 시도" |
| 924 | }, |
| 925 | ); |
| 926 | assert!(matches!(s.factory.state.borrow().starts.last(), |
| 927 | Some(Start::Extract { selection, destination, .. }) |
| 928 | if selection == &original && (!conflict || destination == &chosen))); |
| 929 | } |
| 930 | } |
| 931 | } |