| 1 | use std::collections::HashSet; |
| 2 | use std::fs; |
| 3 | use std::path::{Path, PathBuf}; |
| 4 | |
| 5 | use regex::Regex; |
| 6 | |
| 7 | const WIDGETS_FILE: &str = "widgets.rs"; |
| 8 | const SCAN_ALLOWLIST: &[(&str, &str)] = &[ |
| 9 | ("views/toolbar_placement.rs", ".top(px(-SLOT_HEIGHT))"), |
| 10 | ("views/conversation.rs", ".bottom(px(-1.))"), |
| 11 | ]; |
| 12 | |
| 13 | fn source_roots() -> Vec<PathBuf> { |
| 14 | let crates_directory = Path::new(env!("CARGO_MANIFEST_DIR")).join(".."); |
| 15 | let mut roots: Vec<PathBuf> = fs::read_dir(&crates_directory) |
| 16 | .expect("read crates directory") |
| 17 | .map(|entry| entry.expect("directory entry").path()) |
| 18 | .filter(|path| { |
| 19 | path.file_name() |
| 20 | .is_some_and(|name| name.to_string_lossy().starts_with("app")) |
| 21 | }) |
| 22 | .map(|path| path.join("src")) |
| 23 | .filter(|path| path.is_dir()) |
| 24 | .collect(); |
| 25 | roots.sort(); |
| 26 | roots |
| 27 | } |
| 28 | |
| 29 | fn source_files(directory: &Path, files: &mut Vec<PathBuf>) { |
| 30 | for entry in fs::read_dir(directory).expect("read source directory") { |
| 31 | let path = entry.expect("directory entry").path(); |
| 32 | if path.is_dir() { |
| 33 | source_files(&path, files); |
| 34 | } else if path.extension().is_some_and(|extension| extension == "rs") { |
| 35 | files.push(path); |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | fn negative_offsets( |
| 41 | pattern: &Regex, |
| 42 | source: &str, |
| 43 | relative: &str, |
| 44 | ) -> Vec<(usize, Option<usize>, String)> { |
| 45 | pattern |
| 46 | .find_iter(source) |
| 47 | .map(|found| { |
| 48 | let line_number = source[..found.start()].matches('\n').count() + 1; |
| 49 | let from_match = &source[found.start()..]; |
| 50 | let allowed = SCAN_ALLOWLIST.iter().position(|(allowed_file, snippet)| { |
| 51 | *allowed_file == relative && from_match.starts_with(snippet) |
| 52 | }); |
| 53 | let line_end = from_match.find('\n').unwrap_or(from_match.len()); |
| 54 | ( |
| 55 | line_number, |
| 56 | allowed, |
| 57 | from_match[..line_end].trim().to_owned(), |
| 58 | ) |
| 59 | }) |
| 60 | .collect() |
| 61 | } |
| 62 | |
| 63 | fn offset_pattern() -> Regex { |
| 64 | Regex::new(r"\.(top|right|bottom|left|inset)\(\s*(px\(\s*)?-").expect("pattern") |
| 65 | } |
| 66 | |
| 67 | #[test] |
| 68 | fn matcher_catches_wrapped_offsets() { |
| 69 | let source = "let a = 1;\nlet b = div()\n .top(\n px(-4.))\n .left(px(2.));\n"; |
| 70 | let found = negative_offsets(&offset_pattern(), source, "views/other.rs"); |
| 71 | assert_eq!(found, [(3, None, ".top(".to_owned())]); |
| 72 | } |
| 73 | |
| 74 | #[test] |
| 75 | fn matcher_matches_allowlist_from_match_start() { |
| 76 | let source = "x\n .bottom(px(-1.))\n"; |
| 77 | let found = negative_offsets(&offset_pattern(), source, "views/conversation.rs"); |
| 78 | assert_eq!(found, [(2, Some(1), ".bottom(px(-1.))".to_owned())]); |
| 79 | } |
| 80 | |
| 81 | #[test] |
| 82 | fn negative_offsets_go_through_corner_badge() { |
| 83 | let pattern = offset_pattern(); |
| 84 | let mut matched_allowlist = HashSet::new(); |
| 85 | let mut violations = Vec::new(); |
| 86 | for root in source_roots() { |
| 87 | let mut files = Vec::new(); |
| 88 | source_files(&root, &mut files); |
| 89 | for file in files { |
| 90 | let relative = file |
| 91 | .strip_prefix(&root) |
| 92 | .expect("inside src") |
| 93 | .to_string_lossy() |
| 94 | .replace('\\', "/"); |
| 95 | if relative == WIDGETS_FILE || relative == "layout_guard.rs" { |
| 96 | continue; |
| 97 | } |
| 98 | let source = fs::read_to_string(&file).expect("read source file"); |
| 99 | for (line_number, position, text) in negative_offsets(&pattern, &source, &relative) { |
| 100 | match position { |
| 101 | Some(position) => { |
| 102 | matched_allowlist.insert(position); |
| 103 | } |
| 104 | None => violations.push(format!( |
| 105 | "{relative}:{line_number}: negative absolute offset, use widgets::corner_badge: {text}" |
| 106 | )), |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | let stale: Vec<String> = SCAN_ALLOWLIST |
| 112 | .iter() |
| 113 | .enumerate() |
| 114 | .filter(|(position, _)| !matched_allowlist.contains(position)) |
| 115 | .map(|(_, (file, snippet))| format!("stale allowlist entry, remove it: {file} {snippet}")) |
| 116 | .collect(); |
| 117 | assert!( |
| 118 | violations.is_empty() && stale.is_empty(), |
| 119 | "{}", |
| 120 | violations |
| 121 | .into_iter() |
| 122 | .chain(stale) |
| 123 | .collect::<Vec<_>>() |
| 124 | .join("\n") |
| 125 | ); |
| 126 | } |
| 127 | |
| 128 | #[test] |
| 129 | fn hover_styles_never_toggle_display() { |
| 130 | let toggle = |
| 131 | Regex::new(r"hover\([^|]*\|\w+\|\s*\w+\s*\.\s*(hidden|flex|block|grid)\(\)").unwrap(); |
| 132 | let mut files = Vec::new(); |
| 133 | for root in source_roots() { |
| 134 | source_files(&root, &mut files); |
| 135 | } |
| 136 | let offenders: Vec<String> = files |
| 137 | .iter() |
| 138 | .flat_map(|path| { |
| 139 | let source = fs::read_to_string(path).unwrap(); |
| 140 | source |
| 141 | .lines() |
| 142 | .enumerate() |
| 143 | .filter(|(_, line)| toggle.is_match(line)) |
| 144 | .map(|(index, _)| format!("{}:{}", path.display(), index + 1)) |
| 145 | .collect::<Vec<_>>() |
| 146 | }) |
| 147 | .collect(); |
| 148 | assert!( |
| 149 | offenders.is_empty(), |
| 150 | "hover styles must toggle visible()/invisible(), not display (gpui panics \"must call prepaint before paint\"): {offenders:?}" |
| 151 | ); |
| 152 | } |
| 153 | |
| 154 | mod frames { |
| 155 | use std::collections::HashSet; |
| 156 | use std::sync::Arc; |
| 157 | use std::time::Duration; |
| 158 | |
| 159 | use gpui_kit::test::TestWindowExt as _; |
| 160 | use gpui_kit::{ |
| 161 | AppContext as _, Bounds, Pixels, ScrollDelta, ScrollWheelEvent, TestAppContext, |
| 162 | VisualTestContext, WindowOptions, point, px, size, |
| 163 | }; |
| 164 | use store::Store; |
| 165 | |
| 166 | use std::time::Instant; |
| 167 | |
| 168 | use calling::{CallState, CallUpdate, CaptionEntry, CaptionState, RosterEntry, call_channel}; |
| 169 | use gpui_kit::Entity; |
| 170 | |
| 171 | use crate::app_state::{AppHandle, AppState, Mode, Selection}; |
| 172 | use crate::call::{ActiveCall, CallModel, ConsultCall}; |
| 173 | use crate::demo; |
| 174 | use crate::settings::{item_selector, label, label_selector, metrics}; |
| 175 | use crate::settings_section::SettingsSection; |
| 176 | use crate::user_activity::UserInput; |
| 177 | use crate::views::call_transcript::TranscriptPanel; |
| 178 | use crate::views::call_view::CHAT_PANEL_TAG; |
| 179 | use crate::views::composer::{COMPOSER_BOX_TAG, COMPOSER_LEADING_TAG, COMPOSER_SEND_TAG}; |
| 180 | use crate::views::conversation::MEMBERS_BUTTON; |
| 181 | use crate::views::message_row::BUBBLE_TAGS; |
| 182 | use crate::views::proposal_card::{PROPOSAL_BUTTONS, PROPOSAL_CARD, PROPOSAL_THUMBNAILS}; |
| 183 | use crate::views::reaction_proposal_pill::REACTION_PILL; |
| 184 | use crate::views::recap_tab::RECAP_REGION; |
| 185 | use crate::views::shell::{ |
| 186 | AppShell, Startup, bind_keys, install_globals, sidebar_hidden_in_call, |
| 187 | }; |
| 188 | use crate::views::shortcuts::{ |
| 189 | OpenActivity, OpenAgents, OpenCallsTab, OpenChannelsTab, OpenSaved, |
| 190 | }; |
| 191 | |
| 192 | const GUARDED: &[&str] = &[ |
| 193 | "shell", |
| 194 | "shell/title-bar", |
| 195 | "shell/title-bar/search", |
| 196 | "shell/title-bar/own-status", |
| 197 | "shell/title-bar/own-status/avatar", |
| 198 | "shell/islands", |
| 199 | "shell/islands/rail", |
| 200 | "shell/islands/sidebar", |
| 201 | "shell/islands/main", |
| 202 | ]; |
| 203 | const CALL_GUARDED: &[&str] = &[ |
| 204 | "shell/islands/main/call-header", |
| 205 | "shell/islands/main/call-stage", |
| 206 | "shell/islands/main/call-controls", |
| 207 | "shell/islands/main/call-controls/mic", |
| 208 | "shell/islands/main/call-controls/leave", |
| 209 | ]; |
| 210 | const AGENTS_LIST: &str = "shell/islands/sidebar/agents"; |
| 211 | const CALLS_TAGS: &[&str] = &[ |
| 212 | "shell/islands/sidebar/calls-actions", |
| 213 | "shell/islands/sidebar/calls-actions/meet-now", |
| 214 | "shell/islands/sidebar/calls-actions/new-meeting", |
| 215 | ]; |
| 216 | const PREJOIN_TAGS: &[&str] = &[ |
| 217 | "shell/islands/main/prejoin", |
| 218 | "shell/islands/main/prejoin/preview", |
| 219 | "shell/islands/main/prejoin/controls", |
| 220 | "shell/islands/main/prejoin/devices", |
| 221 | "shell/islands/main/prejoin/audio", |
| 222 | "shell/islands/main/prejoin/actions", |
| 223 | ]; |
| 224 | const PREJOIN_LINK: &str = "https://teams.microsoft.com/l/meetup-join/19%3ameeting_guard%40thread.v2/0?context=%7b%22Tid%22%3a%22t%22%2c%22Oid%22%3a%22o%22%7d"; |
| 225 | const CONSULT_GUARDED: &[&str] = &[ |
| 226 | "shell/islands/main/call-header/call-consult", |
| 227 | "shell/islands/main/call-header/call-consult/transfer", |
| 228 | "shell/islands/main/call-header/call-consult/resume", |
| 229 | ]; |
| 230 | const CALL_TRANSCRIPT: &str = "shell/islands/main/call-transcript"; |
| 231 | const PEOPLE_PANEL: &str = "shell/islands/main/call-people"; |
| 232 | const SHARE_GUARDED: &[&str] = &[ |
| 233 | "shell/islands/main/call-stage/share-content", |
| 234 | "shell/islands/main/call-stage/share-content/zoom-chip", |
| 235 | "shell/islands/main/call-stage/share-divider", |
| 236 | "shell/islands/main/call-stage/share-strip", |
| 237 | ]; |
| 238 | const OVERLAY_PANELS: &[&str] = &[CALL_TRANSCRIPT, PEOPLE_PANEL]; |
| 239 | const OVERLAY_CHROME: &[&str] = &[ |
| 240 | "shell/islands/main/call-header", |
| 241 | "shell/islands/main/call-stage", |
| 242 | ]; |
| 243 | const MENU: &str = "popup-menu"; |
| 244 | const OWN_STATUS: &str = "shell/title-bar/own-status"; |
| 245 | const RECAP_TAB: &str = "shell/islands/main/recap-tab"; |
| 246 | const RECAP_MEETING_CHAT: &str = "demo-chat-meeting-standup"; |
| 247 | const PROPOSAL_CHAT: &str = "demo-chat-release"; |
| 248 | const REACTION_CHAT: &str = "demo-chat-mara"; |
| 249 | const AGENT_TAGS: [&str; 4] = [ |
| 250 | PROPOSAL_CARD, |
| 251 | PROPOSAL_THUMBNAILS, |
| 252 | PROPOSAL_BUTTONS, |
| 253 | REACTION_PILL, |
| 254 | ]; |
| 255 | const SETTINGS_TAGS: &[&str] = &[ |
| 256 | "settings", |
| 257 | "settings/nav", |
| 258 | "settings/nav/divider", |
| 259 | "settings/nav/about", |
| 260 | "settings/main", |
| 261 | "settings/main/header", |
| 262 | "settings/main/header/close", |
| 263 | "settings/main/body", |
| 264 | ]; |
| 265 | const SETTINGS_BODY: &str = "settings/main/body"; |
| 266 | const SETTINGS_NAV: &str = "settings/nav"; |
| 267 | const SETTINGS_NAV_ABOUT: &str = "settings/nav/about"; |
| 268 | const NAV_LABEL_SIZE: f32 = 14.; |
| 269 | const NAV_LINE_HEIGHT: f32 = 20.; |
| 270 | const SETTINGS_CONTENT: &str = "settings/main/body/content"; |
| 271 | const MEMBERS_TAGS: &[&str] = &["chat-members-popup", "chat-members-popup/divider"]; |
| 272 | const MEMBERS_CHAT: &str = "demo-chat-release"; |
| 273 | const DIVIDER_SEGMENT: &str = "divider"; |
| 274 | const DIVIDER_THICKNESS: f32 = 1.; |
| 275 | const INSET_TOLERANCE: f32 = 1.; |
| 276 | const INSET_RULES: &[InsetRule] = &[ |
| 277 | InsetRule { |
| 278 | panel: "settings/nav", |
| 279 | content: "settings/nav/about", |
| 280 | edges: &[Edge::Left, Edge::Right, Edge::Bottom], |
| 281 | }, |
| 282 | InsetRule { |
| 283 | panel: "settings/main", |
| 284 | content: "settings/main/header/close", |
| 285 | edges: &[Edge::Top, Edge::Right], |
| 286 | }, |
| 287 | ]; |
| 288 | const SCROLLED_INSET_RULE: InsetRule = InsetRule { |
| 289 | panel: SETTINGS_BODY, |
| 290 | content: SETTINGS_CONTENT, |
| 291 | edges: &[Edge::Left, Edge::Bottom], |
| 292 | }; |
| 293 | const SCROLLED_INSET_ALLOWLIST: &[(SettingsSection, &str)] = &[( |
| 294 | SettingsSection::Agents, |
| 295 | "Agents pages carry their own PAGE_PADDING side inset, the body narrows its side padding to keep the text edge", |
| 296 | )]; |
| 297 | const SETTINGS_SECTIONS: [SettingsSection; 9] = [ |
| 298 | SettingsSection::General, |
| 299 | SettingsSection::Notifications, |
| 300 | SettingsSection::Appearance, |
| 301 | SettingsSection::Chats, |
| 302 | SettingsSection::Calls, |
| 303 | SettingsSection::Devices, |
| 304 | SettingsSection::Agents, |
| 305 | SettingsSection::Mcp, |
| 306 | SettingsSection::About, |
| 307 | ]; |
| 308 | const SIZES: [(f32, f32); 3] = [(640., 480.), (1280., 800.), (1920., 1080.)]; |
| 309 | const TOLERANCE: f32 = 0.5; |
| 310 | const KNOWN_VIOLATIONS: &[&str] = &[]; |
| 311 | |
| 312 | type Tag = (&'static str, Bounds<Pixels>); |
| 313 | |
| 314 | #[derive(Clone, Copy, Debug)] |
| 315 | enum Edge { |
| 316 | Top, |
| 317 | Right, |
| 318 | Bottom, |
| 319 | Left, |
| 320 | } |
| 321 | |
| 322 | struct InsetRule { |
| 323 | panel: &'static str, |
| 324 | content: &'static str, |
| 325 | edges: &'static [Edge], |
| 326 | } |
| 327 | |
| 328 | fn inset(panel: Bounds<Pixels>, content: Bounds<Pixels>, edge: Edge) -> f32 { |
| 329 | f32::from(match edge { |
| 330 | Edge::Top => content.top() - panel.top(), |
| 331 | Edge::Right => panel.right() - content.right(), |
| 332 | Edge::Bottom => panel.bottom() - content.bottom(), |
| 333 | Edge::Left => content.left() - panel.left(), |
| 334 | }) |
| 335 | } |
| 336 | |
| 337 | fn inset_violation( |
| 338 | rule: &InsetRule, |
| 339 | panel: Bounds<Pixels>, |
| 340 | content: Bounds<Pixels>, |
| 341 | ) -> Option<(String, String)> { |
| 342 | let insets: Vec<(Edge, f32)> = rule |
| 343 | .edges |
| 344 | .iter() |
| 345 | .map(|edge| (*edge, inset(panel, content, *edge))) |
| 346 | .collect(); |
| 347 | let values = insets.iter().map(|(_, value)| *value); |
| 348 | let spread = values.clone().fold(f32::MIN, f32::max) - values.fold(f32::MAX, f32::min); |
| 349 | (spread > INSET_TOLERANCE).then(|| { |
| 350 | ( |
| 351 | format!("{}:uneven-inset", rule.panel), |
| 352 | format!( |
| 353 | "{} insets {} by {insets:?}, they must match", |
| 354 | rule.panel, rule.content |
| 355 | ), |
| 356 | ) |
| 357 | }) |
| 358 | } |
| 359 | |
| 360 | fn inset_violations(tags: &[Tag], rules: &[InsetRule]) -> Vec<(String, String)> { |
| 361 | let bounds_of = |path: &str| { |
| 362 | tags.iter() |
| 363 | .find(|(tag, _)| *tag == path) |
| 364 | .map(|(_, bounds)| *bounds) |
| 365 | }; |
| 366 | rules |
| 367 | .iter() |
| 368 | .filter_map( |
| 369 | |rule| match (bounds_of(rule.panel), bounds_of(rule.content)) { |
| 370 | (Some(panel), Some(content)) => inset_violation(rule, panel, content), |
| 371 | _ => None, |
| 372 | }, |
| 373 | ) |
| 374 | .collect() |
| 375 | } |
| 376 | |
| 377 | fn divider_violations(tags: &[Tag]) -> Vec<(String, String)> { |
| 378 | let mut found = Vec::new(); |
| 379 | for (path, bounds) in tags { |
| 380 | let Some((parent, segment)) = path.rsplit_once('/') else { |
| 381 | continue; |
| 382 | }; |
| 383 | if !segment.starts_with(DIVIDER_SEGMENT) { |
| 384 | continue; |
| 385 | } |
| 386 | let Some((_, panel)) = tags.iter().find(|(tag, _)| *tag == parent) else { |
| 387 | found.push(( |
| 388 | format!("{path}:no-panel"), |
| 389 | format!("{path} needs its panel {parent} tagged"), |
| 390 | )); |
| 391 | continue; |
| 392 | }; |
| 393 | let thin = |length: Pixels| f32::from(length) <= DIVIDER_THICKNESS + TOLERANCE; |
| 394 | let near = |first: Pixels, second: Pixels| f32::from(first - second).abs() <= TOLERANCE; |
| 395 | let spans = if thin(bounds.size.height) { |
| 396 | near(bounds.left(), panel.left()) && near(bounds.right(), panel.right()) |
| 397 | } else if thin(bounds.size.width) { |
| 398 | near(bounds.top(), panel.top()) && near(bounds.bottom(), panel.bottom()) |
| 399 | } else { |
| 400 | false |
| 401 | }; |
| 402 | if !spans { |
| 403 | found.push(( |
| 404 | format!("{path}:short-divider"), |
| 405 | format!( |
| 406 | "{path} {} must be a {DIVIDER_THICKNESS} px line from edge to edge of {parent} {}", |
| 407 | describe(*bounds), |
| 408 | describe(*panel) |
| 409 | ), |
| 410 | )); |
| 411 | } |
| 412 | } |
| 413 | found |
| 414 | } |
| 415 | |
| 416 | enum Screen { |
| 417 | Tab(&'static str, Option<fn(&mut VisualTestContext)>), |
| 418 | StatusMenu, |
| 419 | Call, |
| 420 | Prejoin, |
| 421 | CallTranscript, |
| 422 | CallPeople, |
| 423 | CallShare, |
| 424 | CallConsult, |
| 425 | } |
| 426 | |
| 427 | fn parent_of(path: &str) -> Option<&str> { |
| 428 | path.rsplit_once('/').map(|(parent, _)| parent) |
| 429 | } |
| 430 | |
| 431 | fn contains(outer: Bounds<Pixels>, inner: Bounds<Pixels>) -> bool { |
| 432 | let tolerance = px(TOLERANCE); |
| 433 | inner.left() >= outer.left() - tolerance |
| 434 | && inner.top() >= outer.top() - tolerance |
| 435 | && inner.right() <= outer.right() + tolerance |
| 436 | && inner.bottom() <= outer.bottom() + tolerance |
| 437 | } |
| 438 | |
| 439 | fn overlaps(first: Bounds<Pixels>, second: Bounds<Pixels>) -> bool { |
| 440 | let tolerance = px(TOLERANCE); |
| 441 | first.left() < second.right() - tolerance |
| 442 | && second.left() < first.right() - tolerance |
| 443 | && first.top() < second.bottom() - tolerance |
| 444 | && second.top() < first.bottom() - tolerance |
| 445 | } |
| 446 | |
| 447 | fn describe(bounds: Bounds<Pixels>) -> String { |
| 448 | format!( |
| 449 | "[{:.1},{:.1} {:.1}x{:.1}]", |
| 450 | f32::from(bounds.origin.x), |
| 451 | f32::from(bounds.origin.y), |
| 452 | f32::from(bounds.size.width), |
| 453 | f32::from(bounds.size.height) |
| 454 | ) |
| 455 | } |
| 456 | |
| 457 | fn violations( |
| 458 | tags: &[Tag], |
| 459 | window: Bounds<Pixels>, |
| 460 | expected: &[&str], |
| 461 | ) -> Vec<(String, String)> { |
| 462 | let mut found = Vec::new(); |
| 463 | let bounds_of = |path: &str| { |
| 464 | tags.iter() |
| 465 | .find(|(tag, _)| *tag == path) |
| 466 | .map(|(_, bounds)| *bounds) |
| 467 | }; |
| 468 | for path in expected { |
| 469 | if bounds_of(path).is_none() { |
| 470 | found.push(( |
| 471 | format!("{path}:missing"), |
| 472 | format!("{path} was not rendered"), |
| 473 | )); |
| 474 | } |
| 475 | } |
| 476 | for (path, bounds) in tags { |
| 477 | if !contains(window, *bounds) { |
| 478 | found.push(( |
| 479 | format!("{path}:outside-window"), |
| 480 | format!( |
| 481 | "{path} {} leaves the window {}", |
| 482 | describe(*bounds), |
| 483 | describe(window) |
| 484 | ), |
| 485 | )); |
| 486 | } |
| 487 | if let Some(parent) = |
| 488 | parent_of(path).and_then(|parent| bounds_of(parent).map(|bounds| (parent, bounds))) |
| 489 | { |
| 490 | if !contains(parent.1, *bounds) { |
| 491 | found.push(( |
| 492 | format!("{path}:outside-parent"), |
| 493 | format!( |
| 494 | "{path} {} leaves {} {}", |
| 495 | describe(*bounds), |
| 496 | parent.0, |
| 497 | describe(parent.1) |
| 498 | ), |
| 499 | )); |
| 500 | } |
| 501 | } |
| 502 | for (other, other_bounds) in tags { |
| 503 | let overlays = |first: &str, second: &str| { |
| 504 | OVERLAY_PANELS.contains(&first) && OVERLAY_CHROME.contains(&second) |
| 505 | }; |
| 506 | let siblings = path < other |
| 507 | && parent_of(path).is_some() |
| 508 | && parent_of(path) == parent_of(other) |
| 509 | && !overlays(path, other) |
| 510 | && !overlays(other, path); |
| 511 | if siblings && overlaps(*bounds, *other_bounds) { |
| 512 | found.push(( |
| 513 | format!("{path}:overlaps:{other}"), |
| 514 | format!( |
| 515 | "{path} {} overlaps {other} {}", |
| 516 | describe(*bounds), |
| 517 | describe(*other_bounds) |
| 518 | ), |
| 519 | )); |
| 520 | } |
| 521 | } |
| 522 | } |
| 523 | found.extend(divider_violations(tags)); |
| 524 | found |
| 525 | } |
| 526 | |
| 527 | const SETTLE_FRAMES: usize = 2; |
| 528 | const SETTLE_LIMIT: usize = 60; |
| 529 | const SETTLE_STEP: Duration = Duration::from_millis(50); |
| 530 | |
| 531 | fn collect(visual: &mut VisualTestContext, awaited: Option<&'static str>) -> Vec<Tag> { |
| 532 | let mut frames = 0; |
| 533 | while frames < SETTLE_FRAMES |
| 534 | || awaited.is_some_and(|path| visual.debug_bounds(path).is_none()) |
| 535 | { |
| 536 | assert!(frames < SETTLE_LIMIT, "{awaited:?} never rendered"); |
| 537 | visual.executor().advance_clock(SETTLE_STEP); |
| 538 | std::thread::sleep(SETTLE_STEP); |
| 539 | visual.run_until_parked(); |
| 540 | visual.update(|window, cx| window.render_frame(cx)); |
| 541 | frames += 1; |
| 542 | } |
| 543 | GUARDED |
| 544 | .iter() |
| 545 | .chain(CALL_GUARDED) |
| 546 | .chain(CALLS_TAGS) |
| 547 | .chain(PREJOIN_TAGS) |
| 548 | .chain(SHARE_GUARDED) |
| 549 | .chain(CONSULT_GUARDED) |
| 550 | .chain([ |
| 551 | &AGENTS_LIST, |
| 552 | &CALL_TRANSCRIPT, |
| 553 | &MENU, |
| 554 | &RECAP_TAB, |
| 555 | &RECAP_REGION, |
| 556 | &PEOPLE_PANEL, |
| 557 | ]) |
| 558 | .chain(&AGENT_TAGS) |
| 559 | .chain(SETTINGS_TAGS) |
| 560 | .chain(MEMBERS_TAGS) |
| 561 | .filter_map(|path| visual.debug_bounds(path).map(|bounds| (*path, bounds))) |
| 562 | .collect() |
| 563 | } |
| 564 | |
| 565 | fn sharing_call() -> ActiveCall { |
| 566 | let mut call = gallery_call(); |
| 567 | call.model |
| 568 | .apply(CallUpdate::ScreenShare(Some("8:orgid:guard-1".to_owned()))); |
| 569 | call.share_stage.view.step_in((800., 450.)); |
| 570 | call |
| 571 | } |
| 572 | |
| 573 | fn gallery_call() -> ActiveCall { |
| 574 | let (handle, _control) = call_channel(); |
| 575 | let mut model = CallModel::meeting("Standup", false); |
| 576 | model.apply(CallUpdate::State(CallState::Connected { |
| 577 | since: Instant::now(), |
| 578 | })); |
| 579 | model.apply(CallUpdate::Roster( |
| 580 | (0..11) |
| 581 | .map(|index| RosterEntry { |
| 582 | mri: format!("8:orgid:guard-{index}"), |
| 583 | display_name: format!("Guest {index}"), |
| 584 | muted: false, |
| 585 | in_lobby: false, |
| 586 | has_video: false, |
| 587 | sharing: false, |
| 588 | hand: None, |
| 589 | spotlight: None, |
| 590 | organizer: false, |
| 591 | ..Default::default() |
| 592 | }) |
| 593 | .collect(), |
| 594 | )); |
| 595 | ActiveCall::new(1, model, handle.commands, None, handle.video) |
| 596 | } |
| 597 | |
| 598 | fn consult_calls() -> (ActiveCall, ActiveCall) { |
| 599 | let connected = |id: u64, name: &str, mri: &str| { |
| 600 | let (handle, _control) = call_channel(); |
| 601 | let mut model = CallModel::people(name, &[(mri.to_owned(), name.to_owned())]); |
| 602 | model.apply(CallUpdate::State(CallState::Connected { |
| 603 | since: Instant::now(), |
| 604 | })); |
| 605 | ActiveCall::new(id, model, handle.commands, None, handle.video) |
| 606 | }; |
| 607 | let held = connected(1, "Christopher Montgomery-Lindqvist", "8:orgid:guard-held"); |
| 608 | let mut consult = connected(2, "Jonas Ortega", "8:orgid:guard-consult"); |
| 609 | consult.consult = Some(ConsultCall::new( |
| 610 | ( |
| 611 | "8:orgid:guard-consult".to_owned(), |
| 612 | "Jonas Ortega".to_owned(), |
| 613 | ), |
| 614 | held.model.peer_name.clone(), |
| 615 | )); |
| 616 | (consult, held) |
| 617 | } |
| 618 | |
| 619 | fn mount(cx: &mut TestAppContext) -> (gpui_kit::AnyWindowHandle, Entity<AppState>) { |
| 620 | mount_with(cx, false) |
| 621 | } |
| 622 | |
| 623 | fn mount_with( |
| 624 | cx: &mut TestAppContext, |
| 625 | agent_api: bool, |
| 626 | ) -> (gpui_kit::AnyWindowHandle, Entity<AppState>) { |
| 627 | cx.update(gpui_kit::init); |
| 628 | cx.update(|cx| { |
| 629 | crate::theme::apply(cx); |
| 630 | bind_keys(cx); |
| 631 | let store = Store::open_in_memory().expect("in-memory store"); |
| 632 | demo::seed(&store); |
| 633 | let store = Arc::new(store); |
| 634 | if agent_api { |
| 635 | crate::agent_api::init(store.clone(), true, cx); |
| 636 | } |
| 637 | let mode = Mode { |
| 638 | demo: true, |
| 639 | read_only: false, |
| 640 | demo_sync: None, |
| 641 | }; |
| 642 | let state = cx.new(|_| AppState::new(store, mode)); |
| 643 | install_globals(&state, UserInput::new(), cx); |
| 644 | crate::agents::install(state.clone(), cx); |
| 645 | demo::start_session(&state, cx); |
| 646 | let shared_state = state.clone(); |
| 647 | let startup = Startup { |
| 648 | switcher_query: None, |
| 649 | composer_text: None, |
| 650 | reply_to_last: false, |
| 651 | }; |
| 652 | let window = gpui_kit::open_window(WindowOptions::default(), cx, move |window, cx| { |
| 653 | cx.new(|cx| AppShell::new(state, None, startup, window, cx)) |
| 654 | }) |
| 655 | .expect("open test window") |
| 656 | .0; |
| 657 | (window, shared_state) |
| 658 | }) |
| 659 | } |
| 660 | |
| 661 | fn rectangle(left: f32, top: f32, width: f32, height: f32) -> Bounds<Pixels> { |
| 662 | Bounds { |
| 663 | origin: point(px(left), px(top)), |
| 664 | size: size(px(width), px(height)), |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | #[test] |
| 669 | fn rules_flag_overlap_and_overflow() { |
| 670 | let window = rectangle(0., 0., 100., 100.); |
| 671 | let tags: Vec<Tag> = vec![ |
| 672 | ("shell", rectangle(0., 0., 100., 100.)), |
| 673 | ("shell/islands", rectangle(0., 0., 100., 50.)), |
| 674 | ("shell/islands/rail", rectangle(0., 0., 60., 50.)), |
| 675 | ("shell/islands/sidebar", rectangle(50., 0., 60., 50.)), |
| 676 | ]; |
| 677 | let keys: Vec<String> = violations(&tags, window, &["shell/status-bar"]) |
| 678 | .into_iter() |
| 679 | .map(|(key, _)| key) |
| 680 | .collect(); |
| 681 | let line_tags: Vec<Tag> = vec![ |
| 682 | ("panel", rectangle(0., 0., 100., 100.)), |
| 683 | ("panel/divider", rectangle(0., 50., 100., 1.)), |
| 684 | ("panel/divider-inset", rectangle(12., 60., 76., 1.)), |
| 685 | ("panel/divider-side", rectangle(40., 0., 1., 100.)), |
| 686 | ("panel/about", rectangle(12., 76., 76., 12.)), |
| 687 | ("panel/footer", rectangle(12., 64., 76., 12.)), |
| 688 | ]; |
| 689 | let line_keys: Vec<String> = divider_violations(&line_tags) |
| 690 | .into_iter() |
| 691 | .chain(inset_violations( |
| 692 | &line_tags, |
| 693 | &[ |
| 694 | InsetRule { |
| 695 | panel: "panel", |
| 696 | content: "panel/about", |
| 697 | edges: &[Edge::Left, Edge::Right, Edge::Bottom], |
| 698 | }, |
| 699 | InsetRule { |
| 700 | panel: "panel", |
| 701 | content: "panel/footer", |
| 702 | edges: &[Edge::Left, Edge::Bottom], |
| 703 | }, |
| 704 | ], |
| 705 | )) |
| 706 | .map(|(key, _)| key) |
| 707 | .collect(); |
| 708 | assert_eq!( |
| 709 | line_keys, |
| 710 | ["panel/divider-inset:short-divider", "panel:uneven-inset"] |
| 711 | ); |
| 712 | assert_eq!( |
| 713 | keys, |
| 714 | [ |
| 715 | "shell/status-bar:missing", |
| 716 | "shell/islands/rail:overlaps:shell/islands/sidebar", |
| 717 | "shell/islands/sidebar:outside-window", |
| 718 | "shell/islands/sidebar:outside-parent", |
| 719 | ] |
| 720 | ); |
| 721 | } |
| 722 | |
| 723 | #[gpui_kit::test] |
| 724 | fn no_element_overlaps_or_leaves_its_container(cx: &mut TestAppContext) { |
| 725 | let (handle, app) = mount(cx); |
| 726 | let mut visual = VisualTestContext::from_window(handle, cx); |
| 727 | let mut seen_known = HashSet::new(); |
| 728 | let mut unexpected = Vec::new(); |
| 729 | for (width, height) in SIZES { |
| 730 | visual.simulate_resize(size(px(width), px(height))); |
| 731 | let window = Bounds { |
| 732 | origin: point(px(0.), px(0.)), |
| 733 | size: size(px(width), px(height)), |
| 734 | }; |
| 735 | let screens = [ |
| 736 | Screen::Tab("chats", None), |
| 737 | Screen::Tab( |
| 738 | "activity", |
| 739 | Some(|visual| visual.dispatch_action(OpenActivity)), |
| 740 | ), |
| 741 | Screen::Tab( |
| 742 | "channels", |
| 743 | Some(|visual| visual.dispatch_action(OpenChannelsTab)), |
| 744 | ), |
| 745 | Screen::Tab("saved", Some(|visual| visual.dispatch_action(OpenSaved))), |
| 746 | Screen::Tab("calls", Some(|visual| visual.dispatch_action(OpenCallsTab))), |
| 747 | Screen::Tab("agents", Some(|visual| visual.dispatch_action(OpenAgents))), |
| 748 | Screen::Prejoin, |
| 749 | Screen::StatusMenu, |
| 750 | Screen::Call, |
| 751 | Screen::CallTranscript, |
| 752 | Screen::CallPeople, |
| 753 | Screen::CallShare, |
| 754 | Screen::CallConsult, |
| 755 | ]; |
| 756 | for screen in screens { |
| 757 | let (screen_name, status_menu) = match screen { |
| 758 | Screen::Tab(name, open) => { |
| 759 | if let Some(open) = open { |
| 760 | open(&mut visual); |
| 761 | } |
| 762 | (name, false) |
| 763 | } |
| 764 | Screen::Call | Screen::CallPeople => { |
| 765 | let people_open = matches!(screen, Screen::CallPeople); |
| 766 | visual.update(|_, cx| { |
| 767 | app.update(cx, |state, cx| { |
| 768 | let mut call = gallery_call(); |
| 769 | call.people_open = people_open; |
| 770 | state.call = Some(call); |
| 771 | cx.notify(); |
| 772 | }) |
| 773 | }); |
| 774 | (if people_open { "call-people" } else { "call" }, false) |
| 775 | } |
| 776 | Screen::Prejoin => { |
| 777 | let state = visual.update(|_, cx| cx.global::<AppHandle>().0.clone()); |
| 778 | visual.update(|_, cx| { |
| 779 | state.update(cx, |state, cx| state.join_meeting_link(PREJOIN_LINK, cx)) |
| 780 | }); |
| 781 | ("prejoin", false) |
| 782 | } |
| 783 | Screen::CallTranscript => { |
| 784 | visual.update(|window, cx| { |
| 785 | app.update(cx, |state, cx| { |
| 786 | let mut call = gallery_call(); |
| 787 | call.model.apply(CallUpdate::Captions(CaptionState::On)); |
| 788 | for index in 0..40 { |
| 789 | call.model.note_caption( |
| 790 | CaptionEntry { |
| 791 | id: format!("guard-{index}"), |
| 792 | user_id: "8:orgid:guard-0".into(), |
| 793 | display_name: "Guest 0".into(), |
| 794 | text: "A long guarded sentence that has to wrap inside the panel".into(), |
| 795 | is_final: true, |
| 796 | }, |
| 797 | Instant::now(), |
| 798 | ); |
| 799 | } |
| 800 | call.transcript_open = true; |
| 801 | state.call = Some(call); |
| 802 | let app_entity = cx.entity(); |
| 803 | state.transcript_panel = Some(cx.new(|cx| TranscriptPanel::new(app_entity, window, cx)).into()); |
| 804 | cx.notify(); |
| 805 | }) |
| 806 | }); |
| 807 | ("call-transcript", false) |
| 808 | } |
| 809 | Screen::CallShare => { |
| 810 | visual.update(|_, cx| { |
| 811 | app.update(cx, |state, cx| { |
| 812 | state.call = Some(sharing_call()); |
| 813 | cx.notify(); |
| 814 | }) |
| 815 | }); |
| 816 | ("call-share", false) |
| 817 | } |
| 818 | Screen::CallConsult => { |
| 819 | visual.update(|_, cx| { |
| 820 | app.update(cx, |state, cx| { |
| 821 | let (consult, held) = consult_calls(); |
| 822 | state.call = Some(consult); |
| 823 | state.held_call = Some(held); |
| 824 | cx.notify(); |
| 825 | }) |
| 826 | }); |
| 827 | ("call-consult", false) |
| 828 | } |
| 829 | Screen::StatusMenu => { |
| 830 | let tags = collect(&mut visual, None); |
| 831 | let button = tags |
| 832 | .iter() |
| 833 | .find(|(path, _)| *path == OWN_STATUS) |
| 834 | .map(|(_, bounds)| *bounds) |
| 835 | .expect("own status button rendered"); |
| 836 | visual.simulate_click(button.center(), Default::default()); |
| 837 | ("status-menu", true) |
| 838 | } |
| 839 | }; |
| 840 | let is_call = matches!( |
| 841 | screen_name, |
| 842 | "call" | "call-people" | "call-transcript" | "call-share" | "call-consult" |
| 843 | ); |
| 844 | let tags = collect(&mut visual, status_menu.then_some(MENU)); |
| 845 | let mut expected = GUARDED.to_vec(); |
| 846 | if is_call { |
| 847 | expected.extend(CALL_GUARDED); |
| 848 | let panel_open = matches!(screen_name, "call-people" | "call-transcript"); |
| 849 | if sidebar_hidden_in_call(width, panel_open) { |
| 850 | expected.retain(|path| *path != "shell/islands/sidebar"); |
| 851 | } |
| 852 | } |
| 853 | if screen_name == "call-transcript" { |
| 854 | expected.push(CALL_TRANSCRIPT); |
| 855 | } |
| 856 | if screen_name == "call-people" { |
| 857 | expected.push(PEOPLE_PANEL); |
| 858 | } |
| 859 | if screen_name == "call-share" { |
| 860 | expected.extend(SHARE_GUARDED); |
| 861 | } |
| 862 | if screen_name == "call-consult" { |
| 863 | expected.extend(CONSULT_GUARDED); |
| 864 | } |
| 865 | if status_menu { |
| 866 | expected.push(MENU); |
| 867 | } |
| 868 | match screen_name { |
| 869 | "calls" => expected.extend(CALLS_TAGS), |
| 870 | "agents" => expected.push(AGENTS_LIST), |
| 871 | "prejoin" => expected.extend(PREJOIN_TAGS), |
| 872 | _ => {} |
| 873 | } |
| 874 | let mut found = violations(&tags, window, &expected); |
| 875 | if status_menu { |
| 876 | let menu = tags |
| 877 | .iter() |
| 878 | .find(|(path, _)| *path == MENU) |
| 879 | .map(|(_, bounds)| *bounds); |
| 880 | let button = tags |
| 881 | .iter() |
| 882 | .find(|(path, _)| *path == OWN_STATUS) |
| 883 | .map(|(_, bounds)| *bounds); |
| 884 | if let (Some(menu), Some(button)) = (menu, button) { |
| 885 | if overlaps(menu, button) { |
| 886 | found.push(( |
| 887 | format!("{MENU}:covers:{OWN_STATUS}"), |
| 888 | format!( |
| 889 | "{MENU} {} covers {OWN_STATUS} {}", |
| 890 | describe(menu), |
| 891 | describe(button) |
| 892 | ), |
| 893 | )); |
| 894 | } |
| 895 | } |
| 896 | visual.simulate_keystrokes("escape"); |
| 897 | visual.run_until_parked(); |
| 898 | } |
| 899 | if is_call { |
| 900 | visual.update(|_, cx| { |
| 901 | app.update(cx, |state, cx| { |
| 902 | state.call = None; |
| 903 | state.held_call = None; |
| 904 | cx.notify(); |
| 905 | }) |
| 906 | }); |
| 907 | } |
| 908 | if screen_name == "prejoin" { |
| 909 | let state = visual.update(|_, cx| cx.global::<AppHandle>().0.clone()); |
| 910 | visual.update(|_, cx| state.update(cx, |state, cx| state.cancel_prejoin(cx))); |
| 911 | } |
| 912 | for (key, message) in found { |
| 913 | let full_key = format!("{screen_name}@{width}x{height}:{key}"); |
| 914 | if KNOWN_VIOLATIONS.contains(&full_key.as_str()) { |
| 915 | seen_known.insert(full_key); |
| 916 | } else { |
| 917 | unexpected.push(format!("{full_key}: {message}")); |
| 918 | } |
| 919 | } |
| 920 | } |
| 921 | } |
| 922 | let stale: Vec<String> = KNOWN_VIOLATIONS |
| 923 | .iter() |
| 924 | .filter(|key| !seen_known.contains(**key)) |
| 925 | .map(|key| format!("stale known violation, remove it: {key}")) |
| 926 | .collect(); |
| 927 | assert!( |
| 928 | unexpected.is_empty() && stale.is_empty(), |
| 929 | "{}", |
| 930 | unexpected |
| 931 | .into_iter() |
| 932 | .chain(stale) |
| 933 | .collect::<Vec<_>>() |
| 934 | .join("\n") |
| 935 | ); |
| 936 | } |
| 937 | const TITLE_SEARCH: &str = "shell/title-bar/search"; |
| 938 | const TITLE_NEIGHBOURS: &[&str] = &[ |
| 939 | "shell/title-bar/leading", |
| 940 | "shell/title-bar/mode-pill", |
| 941 | "shell/title-bar/trailing", |
| 942 | "shell/title-bar/update", |
| 943 | OWN_STATUS, |
| 944 | ]; |
| 945 | const TITLE_SEARCH_SIZES: [(f32, f32, bool); 3] = [ |
| 946 | (1920., 1080., true), |
| 947 | (1280., 800., true), |
| 948 | (640., 480., false), |
| 949 | ]; |
| 950 | |
| 951 | #[gpui_kit::test] |
| 952 | fn title_search_is_centered_on_the_window(cx: &mut TestAppContext) { |
| 953 | let (handle, _) = mount(cx); |
| 954 | let mut visual = VisualTestContext::from_window(handle, cx); |
| 955 | let shell = visual.update(|window, cx| { |
| 956 | window |
| 957 | .root::<gpui_kit::base::Root>() |
| 958 | .flatten() |
| 959 | .and_then(|root| root.read(cx).view().clone().downcast::<AppShell>().ok()) |
| 960 | .expect("app shell is the root view") |
| 961 | }); |
| 962 | visual.update(|_, cx| { |
| 963 | shell.update(cx, |shell, cx| { |
| 964 | shell.set_update_status(crate::updater::UpdateStatus::Ready, cx) |
| 965 | }) |
| 966 | }); |
| 967 | let mut found = Vec::new(); |
| 968 | for (width, height, centered) in TITLE_SEARCH_SIZES { |
| 969 | visual.simulate_resize(size(px(width), px(height))); |
| 970 | collect(&mut visual, Some(TITLE_SEARCH)); |
| 971 | let search = visual |
| 972 | .debug_bounds(TITLE_SEARCH) |
| 973 | .expect("title search rendered"); |
| 974 | let search_center = f32::from(search.center().x); |
| 975 | eprintln!( |
| 976 | "title search @{width}x{height}: {} center {search_center:.1}, window center {:.1}", |
| 977 | describe(search), |
| 978 | width / 2. |
| 979 | ); |
| 980 | if centered && (search_center - width / 2.).abs() > 1. { |
| 981 | found.push(format!( |
| 982 | "@{width}x{height}: search center {search_center:.1} is not the window center {:.1}", |
| 983 | width / 2. |
| 984 | )); |
| 985 | } |
| 986 | if !contains(rectangle(0., 0., width, height), search) { |
| 987 | found.push(format!( |
| 988 | "@{width}x{height}: search {} leaves the window", |
| 989 | describe(search) |
| 990 | )); |
| 991 | } |
| 992 | for neighbour in TITLE_NEIGHBOURS { |
| 993 | match visual.debug_bounds(neighbour) { |
| 994 | None => found.push(format!("@{width}x{height}: {neighbour} was not rendered")), |
| 995 | Some(bounds) if overlaps(search, bounds) => found.push(format!( |
| 996 | "@{width}x{height}: search {} overlaps {neighbour} {}", |
| 997 | describe(search), |
| 998 | describe(bounds) |
| 999 | )), |
| 1000 | Some(_) => {} |
| 1001 | } |
| 1002 | } |
| 1003 | } |
| 1004 | assert!(found.is_empty(), "{}", found.join("\n")); |
| 1005 | } |
| 1006 | |
| 1007 | #[gpui_kit::test] |
| 1008 | fn call_chat_bubbles_stay_inside_the_panel(cx: &mut TestAppContext) { |
| 1009 | let (handle, app) = mount(cx); |
| 1010 | let mut visual = VisualTestContext::from_window(handle, cx); |
| 1011 | for (width, height) in SIZES { |
| 1012 | visual.simulate_resize(size(px(width), px(height))); |
| 1013 | visual.update(|_, cx| { |
| 1014 | app.update(cx, |state, cx| { |
| 1015 | let mut call = gallery_call(); |
| 1016 | call.conversation_id = Some(RECAP_MEETING_CHAT.to_owned()); |
| 1017 | call.chat_open = true; |
| 1018 | state.call = Some(call); |
| 1019 | cx.notify(); |
| 1020 | }) |
| 1021 | }); |
| 1022 | collect(&mut visual, Some(CHAT_PANEL_TAG)); |
| 1023 | let panel = visual |
| 1024 | .debug_bounds(CHAT_PANEL_TAG) |
| 1025 | .expect("chat panel rendered"); |
| 1026 | let bubbles: Vec<(usize, Bounds<Pixels>)> = BUBBLE_TAGS |
| 1027 | .iter() |
| 1028 | .enumerate() |
| 1029 | .filter_map(|(index, tag)| visual.debug_bounds(tag).map(|bounds| (index, bounds))) |
| 1030 | .collect(); |
| 1031 | assert!(!bubbles.is_empty(), "no bubbles rendered in the call chat"); |
| 1032 | let found: Vec<String> = bubbles |
| 1033 | .into_iter() |
| 1034 | .filter(|(_, bounds)| !contains(panel, *bounds)) |
| 1035 | .map(|(index, bounds)| { |
| 1036 | format!( |
| 1037 | "call-chat@{width}x{height}: bubble {index} {} leaves the chat panel {}", |
| 1038 | describe(bounds), |
| 1039 | describe(panel) |
| 1040 | ) |
| 1041 | }) |
| 1042 | .collect(); |
| 1043 | visual.update(|_, cx| { |
| 1044 | app.update(cx, |state, cx| { |
| 1045 | state.call = None; |
| 1046 | cx.notify(); |
| 1047 | }) |
| 1048 | }); |
| 1049 | assert!(found.is_empty(), "{}", found.join("\n")); |
| 1050 | } |
| 1051 | } |
| 1052 | |
| 1053 | #[gpui_kit::test] |
| 1054 | fn recap_tab_stays_inside_the_main_island(cx: &mut TestAppContext) { |
| 1055 | let (handle, state) = mount(cx); |
| 1056 | let mut visual = VisualTestContext::from_window(handle, cx); |
| 1057 | for (width, height) in SIZES { |
| 1058 | visual.simulate_resize(size(px(width), px(height))); |
| 1059 | let window = Bounds { |
| 1060 | origin: point(px(0.), px(0.)), |
| 1061 | size: size(px(width), px(height)), |
| 1062 | }; |
| 1063 | visual.update(|_, cx| { |
| 1064 | state.update(cx, |state, cx| { |
| 1065 | state.select(Selection::Chat(RECAP_MEETING_CHAT.to_owned()), cx) |
| 1066 | }) |
| 1067 | }); |
| 1068 | collect(&mut visual, Some(RECAP_TAB)); |
| 1069 | let tab = visual.debug_bounds(RECAP_TAB).expect("recap tab rendered"); |
| 1070 | visual.simulate_click(tab.center(), Default::default()); |
| 1071 | let tags = collect(&mut visual, Some(RECAP_REGION)); |
| 1072 | let mut expected = GUARDED.to_vec(); |
| 1073 | expected.extend([RECAP_TAB, RECAP_REGION]); |
| 1074 | let found: Vec<String> = violations(&tags, window, &expected) |
| 1075 | .into_iter() |
| 1076 | .map(|(key, message)| format!("recap@{width}x{height}:{key}: {message}")) |
| 1077 | .collect(); |
| 1078 | assert!(found.is_empty(), "{}", found.join("\n")); |
| 1079 | } |
| 1080 | } |
| 1081 | |
| 1082 | #[gpui_kit::test] |
| 1083 | fn settings_dialog_keeps_its_inset_at_every_size(cx: &mut TestAppContext) { |
| 1084 | check_settings_dialog(cx, &SETTINGS_SECTIONS); |
| 1085 | } |
| 1086 | |
| 1087 | #[gpui_kit::test] |
| 1088 | fn settings_dialog_keeps_its_inset_in_the_teams_scheme(cx: &mut TestAppContext) { |
| 1089 | crate::theme::set_thread_scheme(Some("teams")); |
| 1090 | check_settings_dialog(cx, &[SettingsSection::Appearance, SettingsSection::General]); |
| 1091 | crate::theme::set_thread_scheme(None); |
| 1092 | } |
| 1093 | |
| 1094 | fn check_settings_dialog(cx: &mut TestAppContext, sections: &[SettingsSection]) { |
| 1095 | let (handle, state) = mount_with(cx, true); |
| 1096 | let mut visual = VisualTestContext::from_window(handle, cx); |
| 1097 | for (width, height) in SIZES.into_iter().chain([(960., 640.)]) { |
| 1098 | visual.simulate_resize(size(px(width), px(height))); |
| 1099 | let window = Bounds { |
| 1100 | origin: point(px(0.), px(0.)), |
| 1101 | size: size(px(width), px(height)), |
| 1102 | }; |
| 1103 | let inset = metrics(width, height).inset; |
| 1104 | let label_sizes = NAV_LABEL_SIZES.contains(&(width, height)); |
| 1105 | for §ion in sections { |
| 1106 | visual |
| 1107 | .update(|_, cx| state.update(cx, |state, cx| state.open_settings(section, cx))); |
| 1108 | let tags = collect(&mut visual, Some(SETTINGS_TAGS[0])); |
| 1109 | let mut found: Vec<(String, String)> = violations(&tags, window, SETTINGS_TAGS) |
| 1110 | .into_iter() |
| 1111 | .filter(|(key, _)| key.starts_with("settings")) |
| 1112 | .chain(inset_violations(&tags, INSET_RULES)) |
| 1113 | .collect(); |
| 1114 | if label_sizes { |
| 1115 | found.extend(nav_label_violations(&mut visual)); |
| 1116 | } |
| 1117 | let scrolled_exempt = SCROLLED_INSET_ALLOWLIST |
| 1118 | .iter() |
| 1119 | .any(|(exempt, _)| *exempt == section); |
| 1120 | if !scrolled_exempt { |
| 1121 | found.extend(scrolled_inset_violation(&mut visual)); |
| 1122 | } |
| 1123 | let found: Vec<String> = found |
| 1124 | .into_iter() |
| 1125 | .map(|(key, message)| format!("{section:?}@{width}x{height}:{key}: {message}")) |
| 1126 | .collect(); |
| 1127 | assert!(found.is_empty(), "{}", found.join("\n")); |
| 1128 | let dialog = visual |
| 1129 | .debug_bounds(SETTINGS_TAGS[0]) |
| 1130 | .expect("settings dialog"); |
| 1131 | let edges = [ |
| 1132 | f32::from(dialog.left()), |
| 1133 | f32::from(dialog.top()), |
| 1134 | width - f32::from(dialog.right()), |
| 1135 | height - f32::from(dialog.bottom()), |
| 1136 | ]; |
| 1137 | assert!( |
| 1138 | edges.iter().all(|edge| (edge - inset).abs() <= TOLERANCE), |
| 1139 | "{section:?}@{width}x{height}: dialog edges {edges:?}, expected {inset}" |
| 1140 | ); |
| 1141 | } |
| 1142 | } |
| 1143 | } |
| 1144 | |
| 1145 | const NAV_LABEL_SIZES: [(f32, f32); 3] = [(960., 640.), (1280., 800.), (1920., 1080.)]; |
| 1146 | |
| 1147 | fn nav_label_violations(visual: &mut VisualTestContext) -> Vec<(String, String)> { |
| 1148 | let Some(nav) = visual.debug_bounds(SETTINGS_NAV) else { |
| 1149 | return vec![(SETTINGS_NAV.to_owned(), "not rendered".to_owned())]; |
| 1150 | }; |
| 1151 | let mut found = Vec::new(); |
| 1152 | for section in SETTINGS_SECTIONS { |
| 1153 | let label_path: &'static str = Box::leak(label_selector(section).into_boxed_str()); |
| 1154 | let item_path: &'static str = if section == SettingsSection::About { |
| 1155 | SETTINGS_NAV_ABOUT |
| 1156 | } else { |
| 1157 | Box::leak(item_selector(section).into_boxed_str()) |
| 1158 | }; |
| 1159 | let (Some(label_bounds), Some(item)) = ( |
| 1160 | visual.debug_bounds(label_path), |
| 1161 | visual.debug_bounds(item_path), |
| 1162 | ) else { |
| 1163 | if section != SettingsSection::Agents { |
| 1164 | found.push((label_path.to_owned(), "not rendered".to_owned())); |
| 1165 | } |
| 1166 | continue; |
| 1167 | }; |
| 1168 | let text = label(section); |
| 1169 | let text_width = visual.update(|window, _| { |
| 1170 | let run = window.text_style().to_run(text.len()); |
| 1171 | f32::from( |
| 1172 | window |
| 1173 | .text_system() |
| 1174 | .shape_line(text.into(), px(NAV_LABEL_SIZE), &[run], None) |
| 1175 | .width, |
| 1176 | ) |
| 1177 | }); |
| 1178 | let label_width = f32::from(label_bounds.size.width); |
| 1179 | let label_height = f32::from(label_bounds.size.height); |
| 1180 | if text_width > label_width + TOLERANCE |
| 1181 | && label_height < 2. * NAV_LINE_HEIGHT - TOLERANCE |
| 1182 | { |
| 1183 | found.push(( |
| 1184 | label_path.to_owned(), |
| 1185 | format!("{text:?} needs {text_width} px, has {label_width} px on one line: truncated"), |
| 1186 | )); |
| 1187 | } |
| 1188 | let outside = |inner: Bounds<Pixels>, outer: Bounds<Pixels>| { |
| 1189 | inner.left() < outer.left() - px(TOLERANCE) |
| 1190 | || inner.right() > outer.right() + px(TOLERANCE) |
| 1191 | || inner.top() < outer.top() - px(TOLERANCE) |
| 1192 | || inner.bottom() > outer.bottom() + px(TOLERANCE) |
| 1193 | }; |
| 1194 | if outside(label_bounds, item) { |
| 1195 | found.push(( |
| 1196 | label_path.to_owned(), |
| 1197 | format!("label {label_bounds:?} outside item {item:?}"), |
| 1198 | )); |
| 1199 | } |
| 1200 | if item.left() < nav.left() || item.right() > nav.right() { |
| 1201 | found.push(( |
| 1202 | item_path.to_owned(), |
| 1203 | format!("item {item:?} outside nav {nav:?}"), |
| 1204 | )); |
| 1205 | } |
| 1206 | } |
| 1207 | found |
| 1208 | } |
| 1209 | |
| 1210 | fn scrolled_inset_violation(visual: &mut VisualTestContext) -> Option<(String, String)> { |
| 1211 | let body = visual.debug_bounds(SETTINGS_BODY)?; |
| 1212 | let content = visual.debug_bounds(SETTINGS_CONTENT)?; |
| 1213 | if content.bottom() <= body.bottom() { |
| 1214 | return None; |
| 1215 | } |
| 1216 | visual.simulate_event(ScrollWheelEvent { |
| 1217 | position: body.center(), |
| 1218 | delta: ScrollDelta::Pixels(point(px(0.), px(-100_000.))), |
| 1219 | ..Default::default() |
| 1220 | }); |
| 1221 | collect(visual, None); |
| 1222 | let body = visual.debug_bounds(SETTINGS_BODY)?; |
| 1223 | let content = visual.debug_bounds(SETTINGS_CONTENT)?; |
| 1224 | inset_violation(&SCROLLED_INSET_RULE, body, content) |
| 1225 | } |
| 1226 | |
| 1227 | #[gpui_kit::test] |
| 1228 | fn members_popup_lines_reach_the_edges(cx: &mut TestAppContext) { |
| 1229 | let (handle, state) = mount(cx); |
| 1230 | let mut visual = VisualTestContext::from_window(handle, cx); |
| 1231 | for (width, height) in SIZES { |
| 1232 | visual.simulate_resize(size(px(width), px(height))); |
| 1233 | let window = Bounds { |
| 1234 | origin: point(px(0.), px(0.)), |
| 1235 | size: size(px(width), px(height)), |
| 1236 | }; |
| 1237 | visual.update(|_, cx| { |
| 1238 | state.update(cx, |state, cx| { |
| 1239 | state.select(Selection::Chat(MEMBERS_CHAT.to_owned()), cx) |
| 1240 | }) |
| 1241 | }); |
| 1242 | collect(&mut visual, Some(MEMBERS_BUTTON)); |
| 1243 | let button = visual |
| 1244 | .debug_bounds(MEMBERS_BUTTON) |
| 1245 | .expect("members button rendered"); |
| 1246 | visual.simulate_click(button.center(), Default::default()); |
| 1247 | let tags = collect(&mut visual, Some(MEMBERS_TAGS[0])); |
| 1248 | let found: Vec<String> = violations(&tags, window, MEMBERS_TAGS) |
| 1249 | .into_iter() |
| 1250 | .filter(|(key, _)| key.starts_with("chat-members")) |
| 1251 | .map(|(key, message)| format!("members@{width}x{height}:{key}: {message}")) |
| 1252 | .collect(); |
| 1253 | visual.simulate_keystrokes("escape"); |
| 1254 | visual.run_until_parked(); |
| 1255 | assert!(found.is_empty(), "{}", found.join("\n")); |
| 1256 | } |
| 1257 | } |
| 1258 | |
| 1259 | fn composer_button_insets(visual: &mut VisualTestContext) -> [f32; 6] { |
| 1260 | collect(visual, Some(COMPOSER_SEND_TAG)); |
| 1261 | let mut bounds_of = |path: &'static str| { |
| 1262 | visual |
| 1263 | .debug_bounds(path) |
| 1264 | .unwrap_or_else(|| panic!("{path} rendered")) |
| 1265 | }; |
| 1266 | let composer = bounds_of(COMPOSER_BOX_TAG); |
| 1267 | let leading = bounds_of(COMPOSER_LEADING_TAG); |
| 1268 | let send = bounds_of(COMPOSER_SEND_TAG); |
| 1269 | [ |
| 1270 | f32::from(leading.left() - composer.left()), |
| 1271 | f32::from(leading.top() - composer.top()), |
| 1272 | f32::from(composer.bottom() - leading.bottom()), |
| 1273 | f32::from(composer.right() - send.right()), |
| 1274 | f32::from(send.top() - composer.top()), |
| 1275 | f32::from(composer.bottom() - send.bottom()), |
| 1276 | ] |
| 1277 | } |
| 1278 | |
| 1279 | #[gpui_kit::test] |
| 1280 | fn composer_buttons_keep_equal_insets(cx: &mut TestAppContext) { |
| 1281 | let (handle, _) = mount(cx); |
| 1282 | let mut visual = VisualTestContext::from_window(handle, cx); |
| 1283 | visual.simulate_resize(size(px(1280.), px(800.))); |
| 1284 | let single_line = composer_button_insets(&mut visual); |
| 1285 | let left = single_line[0]; |
| 1286 | assert!( |
| 1287 | single_line |
| 1288 | .iter() |
| 1289 | .all(|inset| (inset - left).abs() < TOLERANCE), |
| 1290 | "single line: insets left, top, bottom of the first button and right, top, bottom of send {single_line:?}" |
| 1291 | ); |
| 1292 | let leading = visual |
| 1293 | .debug_bounds(COMPOSER_LEADING_TAG) |
| 1294 | .expect("leading buttons"); |
| 1295 | let send = visual.debug_bounds(COMPOSER_SEND_TAG).expect("send button"); |
| 1296 | visual.simulate_click( |
| 1297 | point((leading.right() + send.left()) / 2., send.center().y), |
| 1298 | Default::default(), |
| 1299 | ); |
| 1300 | visual.simulate_keystrokes("o n e shift-enter t w o shift-enter t h r e e"); |
| 1301 | let three_lines = composer_button_insets(&mut visual); |
| 1302 | assert!( |
| 1303 | three_lines[1] > single_line[1] + 30., |
| 1304 | "three lines did not grow the composer: {three_lines:?}" |
| 1305 | ); |
| 1306 | for inset in [ |
| 1307 | three_lines[0], |
| 1308 | three_lines[2], |
| 1309 | three_lines[3], |
| 1310 | three_lines[5], |
| 1311 | ] { |
| 1312 | assert!( |
| 1313 | (inset - left).abs() < TOLERANCE, |
| 1314 | "three lines: insets left, top, bottom of the first button and right, top, bottom of send {three_lines:?}, expected {left}" |
| 1315 | ); |
| 1316 | } |
| 1317 | } |
| 1318 | |
| 1319 | #[gpui_kit::test] |
| 1320 | fn agent_proposals_stay_inside_the_main_island(cx: &mut TestAppContext) { |
| 1321 | let (handle, state) = mount_with(cx, true); |
| 1322 | let mut visual = VisualTestContext::from_window(handle, cx); |
| 1323 | for (width, height) in SIZES { |
| 1324 | visual.simulate_resize(size(px(width), px(height))); |
| 1325 | let window = Bounds { |
| 1326 | origin: point(px(0.), px(0.)), |
| 1327 | size: size(px(width), px(height)), |
| 1328 | }; |
| 1329 | for (chat, expected_tags) in [ |
| 1330 | (PROPOSAL_CHAT, &AGENT_TAGS[..3]), |
| 1331 | (REACTION_CHAT, &AGENT_TAGS[3..]), |
| 1332 | ] { |
| 1333 | visual.update(|_, cx| { |
| 1334 | state.update(cx, |state, cx| { |
| 1335 | state.select(Selection::Chat(chat.to_owned()), cx) |
| 1336 | }) |
| 1337 | }); |
| 1338 | let tags = collect(&mut visual, Some(expected_tags[0])); |
| 1339 | let mut expected = GUARDED.to_vec(); |
| 1340 | expected.extend(expected_tags); |
| 1341 | let found: Vec<String> = violations(&tags, window, &expected) |
| 1342 | .into_iter() |
| 1343 | .map(|(key, message)| format!("{chat}@{width}x{height}:{key}: {message}")) |
| 1344 | .collect(); |
| 1345 | assert!(found.is_empty(), "{}", found.join("\n")); |
| 1346 | } |
| 1347 | } |
| 1348 | } |
| 1349 | } |
| 1350 | |
| 1351 | #[test] |
| 1352 | fn lines_are_solid() { |
| 1353 | let mut files = Vec::new(); |
| 1354 | for root in source_roots() { |
| 1355 | source_files(&root, &mut files); |
| 1356 | } |
| 1357 | let violations: Vec<String> = files |
| 1358 | .iter() |
| 1359 | .filter(|file| !file.ends_with("layout_guard.rs")) |
| 1360 | .flat_map(|file| { |
| 1361 | let source = fs::read_to_string(file).expect("read source file"); |
| 1362 | source |
| 1363 | .lines() |
| 1364 | .enumerate() |
| 1365 | .filter(|(_, line)| { |
| 1366 | line.contains(".border_dashed(") || line.contains(".border_dotted(") |
| 1367 | }) |
| 1368 | .map(|(index, line)| { |
| 1369 | format!( |
| 1370 | "{}:{}: lines are always solid: {}", |
| 1371 | file.display(), |
| 1372 | index + 1, |
| 1373 | line.trim() |
| 1374 | ) |
| 1375 | }) |
| 1376 | .collect::<Vec<_>>() |
| 1377 | }) |
| 1378 | .collect(); |
| 1379 | assert!(violations.is_empty(), "{}", violations.join("\n")); |
| 1380 | } |
| 1381 | |
| 1382 | const STATE_RING_COLORS: &[&str] = &[ |
| 1383 | "if focused", |
| 1384 | "if selected", |
| 1385 | "if speaking", |
| 1386 | "if current", |
| 1387 | "if open", |
| 1388 | "if state.failed", |
| 1389 | ]; |
| 1390 | const OUTLINE_ALLOWLIST: &[(&str, &str, &str)] = &[ |
| 1391 | ( |
| 1392 | "notify/ring_view.rs", |
| 1393 | "theme::green()", |
| 1394 | "state signal (pulsing ring around the caller while it rings), not a resting outline", |
| 1395 | ), |
| 1396 | ( |
| 1397 | "notify/meeting_view.rs", |
| 1398 | "theme::border_strong()", |
| 1399 | "outlined button, not a surface", |
| 1400 | ), |
| 1401 | ( |
| 1402 | "settings/mcp.rs", |
| 1403 | "theme::border_strong()", |
| 1404 | "outlined button, not a surface", |
| 1405 | ), |
| 1406 | ( |
| 1407 | "settings/rows.rs", |
| 1408 | "theme::border_strong()", |
| 1409 | "outlined button, not a surface", |
| 1410 | ), |
| 1411 | ( |
| 1412 | "views/card_widgets.rs", |
| 1413 | "border)", |
| 1414 | "message content drawn as Teams renders it", |
| 1415 | ), |
| 1416 | ( |
| 1417 | "views/card_widgets.rs", |
| 1418 | "theme::border_strong()", |
| 1419 | "message content drawn as Teams renders it", |
| 1420 | ), |
| 1421 | ("views/shared_tab.rs", "theme::accent()", "input field"), |
| 1422 | ("views/card_input.rs", "if error.is_some()", "input field"), |
| 1423 | ("views/format_toolbar.rs", "if refused", "input field"), |
| 1424 | ( |
| 1425 | "views/card_code_block.rs", |
| 1426 | "theme::code_header_border()", |
| 1427 | "message content drawn as Teams renders it", |
| 1428 | ), |
| 1429 | ("views/call_transcript.rs", "theme::border()", "input field"), |
| 1430 | ( |
| 1431 | "views/conversation.rs", |
| 1432 | "theme::accent()", |
| 1433 | "state signal (speaking, sharing, drag, drop, failed), not a resting outline", |
| 1434 | ), |
| 1435 | ( |
| 1436 | "views/attachment_tray.rs", |
| 1437 | "theme::accent()", |
| 1438 | "state signal (speaking, sharing, drag, drop, failed), not a resting outline", |
| 1439 | ), |
| 1440 | ( |
| 1441 | "views/card_carousel.rs", |
| 1442 | "theme::border_strong()", |
| 1443 | "message content drawn as Teams renders it", |
| 1444 | ), |
| 1445 | ( |
| 1446 | "views/card_table.rs", |
| 1447 | "grid_color", |
| 1448 | "message content drawn as Teams renders it", |
| 1449 | ), |
| 1450 | ( |
| 1451 | "views/activity_panel.rs", |
| 1452 | "theme::surface()", |
| 1453 | "cutout ring in the background color, invisible against its surface", |
| 1454 | ), |
| 1455 | ( |
| 1456 | "views/avatar.rs", |
| 1457 | "theme::surface()", |
| 1458 | "cutout ring in the background color, invisible against its surface", |
| 1459 | ), |
| 1460 | ( |
| 1461 | "views/avatar.rs", |
| 1462 | "theme::text_muted()", |
| 1463 | "offline presence glyph, Teams icon", |
| 1464 | ), |
| 1465 | ( |
| 1466 | "views/message_row.rs", |
| 1467 | "theme::border_strong()", |
| 1468 | "deleted message placeholder, Teams look", |
| 1469 | ), |
| 1470 | ( |
| 1471 | "views/message_row.rs", |
| 1472 | "theme::red()", |
| 1473 | "state signal (speaking, sharing, drag, drop, failed), not a resting outline", |
| 1474 | ), |
| 1475 | ( |
| 1476 | "views/message_row.rs", |
| 1477 | "if row.delivery", |
| 1478 | "state signal (speaking, sharing, drag, drop, failed), not a resting outline", |
| 1479 | ), |
| 1480 | ( |
| 1481 | "views/new_meeting_dialog.rs", |
| 1482 | "theme::border()", |
| 1483 | "input field", |
| 1484 | ), |
| 1485 | ( |
| 1486 | "views/call_mini_window.rs", |
| 1487 | "theme::border_strong()", |
| 1488 | "self view over live video, needs an edge on any picture", |
| 1489 | ), |
| 1490 | ("views/transfer_picker.rs", "theme::border()", "input field"), |
| 1491 | ( |
| 1492 | "views/call_notice.rs", |
| 1493 | "theme::border_strong()", |
| 1494 | "outlined button, not a surface", |
| 1495 | ), |
| 1496 | ( |
| 1497 | "views/composer/schedule.rs", |
| 1498 | "theme::accent()", |
| 1499 | "input field", |
| 1500 | ), |
| 1501 | ( |
| 1502 | "views/call_mini.rs", |
| 1503 | "border)", |
| 1504 | "state signal (speaking, sharing, drag, drop, failed), not a resting outline", |
| 1505 | ), |
| 1506 | ( |
| 1507 | "views/sidebar.rs", |
| 1508 | "theme::accent()", |
| 1509 | "state signal (speaking, sharing, drag, drop, failed), not a resting outline", |
| 1510 | ), |
| 1511 | ( |
| 1512 | "views/adaptive_card.rs", |
| 1513 | "theme::border()", |
| 1514 | "message content drawn as Teams renders it", |
| 1515 | ), |
| 1516 | ( |
| 1517 | "views/adaptive_card.rs", |
| 1518 | "border)", |
| 1519 | "message content drawn as Teams renders it", |
| 1520 | ), |
| 1521 | ( |
| 1522 | "views/share_bar.rs", |
| 1523 | "theme::red()", |
| 1524 | "state signal (speaking, sharing, drag, drop, failed), not a resting outline", |
| 1525 | ), |
| 1526 | ("settings_controls.rs", "if invalid", "input field"), |
| 1527 | ]; |
| 1528 | |
| 1529 | fn outlines(source: &str) -> Vec<(usize, String)> { |
| 1530 | let pattern = Regex::new(r"\.border(_1\(\)|_2\(\)|\(px)").expect("pattern"); |
| 1531 | pattern |
| 1532 | .find_iter(source) |
| 1533 | .map(|found| { |
| 1534 | let line_number = source[..found.start()].matches('\n').count() + 1; |
| 1535 | let after = &source[found.end()..]; |
| 1536 | let color = after |
| 1537 | .find(".border_color(") |
| 1538 | .filter(|position| *position < 120) |
| 1539 | .map(|position| { |
| 1540 | let start = &after[position + ".border_color(".len()..]; |
| 1541 | start |
| 1542 | .lines() |
| 1543 | .map(str::trim) |
| 1544 | .find(|line| !line.is_empty()) |
| 1545 | .unwrap_or_default() |
| 1546 | .to_owned() |
| 1547 | }) |
| 1548 | .unwrap_or_default(); |
| 1549 | (line_number, color) |
| 1550 | }) |
| 1551 | .collect() |
| 1552 | } |
| 1553 | |
| 1554 | #[test] |
| 1555 | fn outline_matcher_reads_the_border_color() { |
| 1556 | let source = "div()\n .border_1()\n .border_color(theme::amber())\n .border_t_1()\n"; |
| 1557 | assert_eq!(outlines(source), [(2, "theme::amber())".to_owned())]); |
| 1558 | } |
| 1559 | |
| 1560 | #[test] |
| 1561 | fn surfaces_are_soft() { |
| 1562 | let mut matched_allowlist = HashSet::new(); |
| 1563 | let mut violations = Vec::new(); |
| 1564 | for root in source_roots() { |
| 1565 | let mut files = Vec::new(); |
| 1566 | source_files(&root, &mut files); |
| 1567 | for file in files { |
| 1568 | let relative = file |
| 1569 | .strip_prefix(&root) |
| 1570 | .expect("inside src") |
| 1571 | .to_string_lossy() |
| 1572 | .replace('\\', "/"); |
| 1573 | if relative == "layout_guard.rs" { |
| 1574 | continue; |
| 1575 | } |
| 1576 | let source = fs::read_to_string(&file).expect("read source file"); |
| 1577 | for (line_number, color) in outlines(&source) { |
| 1578 | if STATE_RING_COLORS |
| 1579 | .iter() |
| 1580 | .any(|prefix| color.starts_with(prefix)) |
| 1581 | { |
| 1582 | continue; |
| 1583 | } |
| 1584 | match OUTLINE_ALLOWLIST.iter().position(|(allowed_file, prefix, _)| { |
| 1585 | *allowed_file == relative && color.starts_with(prefix) |
| 1586 | }) { |
| 1587 | Some(position) => { |
| 1588 | matched_allowlist.insert(position); |
| 1589 | } |
| 1590 | None => violations.push(format!( |
| 1591 | "{relative}:{line_number}: hard outline on a surface, use a soft fill or shadow (border_color {color})" |
| 1592 | )), |
| 1593 | } |
| 1594 | } |
| 1595 | } |
| 1596 | } |
| 1597 | let stale = OUTLINE_ALLOWLIST |
| 1598 | .iter() |
| 1599 | .enumerate() |
| 1600 | .filter(|(position, _)| !matched_allowlist.contains(position)) |
| 1601 | .map(|(_, (file, prefix, _))| { |
| 1602 | format!("stale outline allowlist entry, remove it: {file} {prefix}") |
| 1603 | }); |
| 1604 | let report: Vec<String> = violations.into_iter().chain(stale).collect(); |
| 1605 | assert!(report.is_empty(), "{}", report.join("\n")); |
| 1606 | } |
| 1607 | |
| 1608 | fn indentation(line: &str) -> usize { |
| 1609 | line.len() - line.trim_start().len() |
| 1610 | } |
| 1611 | |
| 1612 | fn chain_lines<'a>(lines: &[&'a str], index: usize) -> Vec<&'a str> { |
| 1613 | let indent = indentation(lines[index]); |
| 1614 | let belongs = |line: &&str| line.trim().is_empty() || indentation(line) >= indent; |
| 1615 | let start = lines[..index] |
| 1616 | .iter() |
| 1617 | .rposition(|line| !belongs(line)) |
| 1618 | .map_or(0, |position| position + 1); |
| 1619 | let end = lines[index..] |
| 1620 | .iter() |
| 1621 | .position(|line| !belongs(line)) |
| 1622 | .map_or(lines.len(), |position| index + position); |
| 1623 | lines[start..end] |
| 1624 | .iter() |
| 1625 | .filter(|line| indentation(line) == indent) |
| 1626 | .copied() |
| 1627 | .collect() |
| 1628 | } |
| 1629 | |
| 1630 | fn shrinking_rows(row_height: &Regex, source: &str) -> Vec<usize> { |
| 1631 | let lines: Vec<&str> = source.lines().collect(); |
| 1632 | (0..lines.len()) |
| 1633 | .filter(|&index| row_height.is_match(lines[index])) |
| 1634 | .filter(|&index| { |
| 1635 | !chain_lines(&lines, index).iter().any(|line| { |
| 1636 | let line = line.trim(); |
| 1637 | line.starts_with(".flex_none()") || line.starts_with(".flex_shrink_0()") |
| 1638 | }) |
| 1639 | }) |
| 1640 | .map(|index| index + 1) |
| 1641 | .collect() |
| 1642 | } |
| 1643 | |
| 1644 | fn row_height_pattern() -> Regex { |
| 1645 | Regex::new(r"^\s*\.h\(px\(((\w+_)?(ROW|ENTRY)_HEIGHT|row_height)\)\)").expect("pattern") |
| 1646 | } |
| 1647 | |
| 1648 | #[test] |
| 1649 | fn matcher_finds_rows_without_flex_none() { |
| 1650 | let source = "fn rows() {\n h_flex()\n .h(px(ROW_HEIGHT))\n .child(\n div()\n .flex_none(),\n );\n div()\n .id(1)\n .h(px(OWN_ROW_HEIGHT))\n .flex_none();\n}\n"; |
| 1651 | assert_eq!(shrinking_rows(&row_height_pattern(), source), [3]); |
| 1652 | } |
| 1653 | |
| 1654 | #[test] |
| 1655 | fn fixed_height_rows_never_shrink() { |
| 1656 | let row_height = row_height_pattern(); |
| 1657 | let mut files = Vec::new(); |
| 1658 | for root in source_roots() { |
| 1659 | source_files(&root, &mut files); |
| 1660 | } |
| 1661 | let violations: Vec<String> = files |
| 1662 | .iter() |
| 1663 | .filter(|file| !file.ends_with("layout_guard.rs")) |
| 1664 | .flat_map(|file| { |
| 1665 | let source = fs::read_to_string(file).expect("read source file"); |
| 1666 | shrinking_rows(&row_height, &source) |
| 1667 | .into_iter() |
| 1668 | .map(|line| format!("{}:{line}", file.display())) |
| 1669 | .collect::<Vec<_>>() |
| 1670 | }) |
| 1671 | .collect(); |
| 1672 | assert!( |
| 1673 | violations.is_empty(), |
| 1674 | "a fixed-height row needs .flex_none(), or a max_h scroll list shrinks it to its content: {violations:?}" |
| 1675 | ); |
| 1676 | } |