| 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::delivery::{ |
| 180 | COMPOSER_DELIVERY_TAG, COMPOSER_IMPORTANCE_LABEL_TAG, COMPOSER_URGENT_BANNER_TAG, |
| 181 | DELIVERY_MENU_TAG, |
| 182 | }; |
| 183 | use crate::views::composer::{ |
| 184 | COMPOSER_BOX_TAG, COMPOSER_FORMAT_BAR_TAG, COMPOSER_FORMAT_DIVIDER_TAG, |
| 185 | COMPOSER_FORMAT_TAG, COMPOSER_LEADING_TAG, COMPOSER_SEND_TAG, |
| 186 | }; |
| 187 | use crate::views::conversation::{CHANNEL_DETAILS_BUTTON, CHANNEL_DETAILS_TAG}; |
| 188 | use crate::views::conversation::{HEADER_DIVIDER, MEMBERS_BUTTON, TIMELINE_TAG}; |
| 189 | use crate::views::message_row::{BUBBLE_TAGS, IMPORTANCE_LABEL_TAG}; |
| 190 | use crate::views::pin_banner::PIN_BANNER_TAG; |
| 191 | use crate::views::proposal_card::{PROPOSAL_BUTTONS, PROPOSAL_CARD, PROPOSAL_THUMBNAILS}; |
| 192 | use crate::views::reaction_proposal_pill::REACTION_PILL; |
| 193 | use crate::views::recap_tab::RECAP_REGION; |
| 194 | use crate::views::shell::{ |
| 195 | AppShell, Startup, bind_keys, install_globals, sidebar_hidden_in_call, |
| 196 | }; |
| 197 | use crate::views::shortcuts::{ |
| 198 | OpenActivity, OpenAgents, OpenCallsTab, OpenChannelsTab, OpenSaved, |
| 199 | }; |
| 200 | use crate::views::sidebar::SIDEBAR_SCROLL_DIVIDER; |
| 201 | use crate::views::status_menu::{OWN_AVATAR_DISC, TITLE_AVATAR_SIZE}; |
| 202 | |
| 203 | const GUARDED: &[&str] = &[ |
| 204 | "shell", |
| 205 | "shell/title-bar", |
| 206 | "shell/title-bar/search", |
| 207 | "shell/title-bar/own-status", |
| 208 | "shell/title-bar/own-status/avatar", |
| 209 | OWN_AVATAR_DISC, |
| 210 | "shell/islands", |
| 211 | "shell/islands/rail", |
| 212 | "shell/islands/sidebar", |
| 213 | "shell/islands/main", |
| 214 | ]; |
| 215 | const CALL_GUARDED: &[&str] = &[ |
| 216 | "shell/islands/main/call-header", |
| 217 | "shell/islands/main/call-stage", |
| 218 | "shell/islands/main/call-controls", |
| 219 | "shell/islands/main/call-controls/mic", |
| 220 | "shell/islands/main/call-controls/leave", |
| 221 | ]; |
| 222 | const AGENTS_LIST: &str = "shell/islands/sidebar/agents"; |
| 223 | const CALLS_TAGS: &[&str] = &[ |
| 224 | "shell/islands/sidebar/calls-actions", |
| 225 | "shell/islands/sidebar/calls-actions/meet-now", |
| 226 | "shell/islands/sidebar/calls-actions/new-meeting", |
| 227 | ]; |
| 228 | const PREJOIN_TAGS: &[&str] = &[ |
| 229 | "shell/islands/main/prejoin", |
| 230 | "shell/islands/main/prejoin/preview", |
| 231 | "shell/islands/main/prejoin/controls", |
| 232 | "shell/islands/main/prejoin/devices", |
| 233 | "shell/islands/main/prejoin/audio", |
| 234 | "shell/islands/main/prejoin/actions", |
| 235 | ]; |
| 236 | 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"; |
| 237 | const CONSULT_GUARDED: &[&str] = &[ |
| 238 | "shell/islands/main/call-header/call-consult", |
| 239 | "shell/islands/main/call-header/call-consult/transfer", |
| 240 | "shell/islands/main/call-header/call-consult/resume", |
| 241 | ]; |
| 242 | const CALL_TRANSCRIPT: &str = "shell/islands/main/call-transcript"; |
| 243 | const PEOPLE_PANEL: &str = "shell/islands/main/call-people"; |
| 244 | const SHARE_GUARDED: &[&str] = &[ |
| 245 | "shell/islands/main/call-stage/share-content", |
| 246 | "shell/islands/main/call-stage/share-content/zoom-chip", |
| 247 | "shell/islands/main/call-stage/share-divider", |
| 248 | "shell/islands/main/call-stage/share-strip", |
| 249 | ]; |
| 250 | const OVERLAY_PANELS: &[&str] = &[CALL_TRANSCRIPT, PEOPLE_PANEL]; |
| 251 | const OVERLAY_CHROME: &[&str] = &[ |
| 252 | "shell/islands/main/call-header", |
| 253 | "shell/islands/main/call-stage", |
| 254 | ]; |
| 255 | const MENU: &str = "popup-menu"; |
| 256 | const OWN_STATUS: &str = "shell/title-bar/own-status"; |
| 257 | const RECAP_TAB: &str = "shell/islands/main/recap-tab"; |
| 258 | const RECAP_MEETING_CHAT: &str = "demo-chat-meeting-standup"; |
| 259 | const PROPOSAL_CHAT: &str = "demo-chat-release"; |
| 260 | const REACTION_CHAT: &str = "demo-chat-mara"; |
| 261 | const IMPORTANCE_CHATS: [&str; 2] = ["demo-chat-priya", "demo-chat-atlas"]; |
| 262 | const IMPORTANCE_LABEL_INSET_X: f32 = 12.; |
| 263 | const IMPORTANCE_LABEL_INSET_TOP: f32 = 8.; |
| 264 | const AGENT_TAGS: [&str; 4] = [ |
| 265 | PROPOSAL_CARD, |
| 266 | PROPOSAL_THUMBNAILS, |
| 267 | PROPOSAL_BUTTONS, |
| 268 | REACTION_PILL, |
| 269 | ]; |
| 270 | const SETTINGS_TAGS: &[&str] = &[ |
| 271 | "settings", |
| 272 | "settings/nav", |
| 273 | "settings/nav/divider", |
| 274 | "settings/nav/about", |
| 275 | "settings/main", |
| 276 | "settings/main/header", |
| 277 | "settings/main/header/close", |
| 278 | "settings/main/body", |
| 279 | ]; |
| 280 | const SETTINGS_BODY: &str = "settings/main/body"; |
| 281 | const SETTINGS_NAV: &str = "settings/nav"; |
| 282 | const SETTINGS_NAV_ABOUT: &str = "settings/nav/about"; |
| 283 | const NAV_LABEL_SIZE: f32 = 14.; |
| 284 | const NAV_LINE_HEIGHT: f32 = 20.; |
| 285 | const SETTINGS_CONTENT: &str = "settings/main/body/content"; |
| 286 | const MEMBERS_TAGS: &[&str] = &["chat-members-popup", "chat-members-popup/divider"]; |
| 287 | const MEMBERS_CHAT: &str = "demo-chat-release"; |
| 288 | const SIDEBAR: &str = "shell/islands/sidebar"; |
| 289 | const MAIN: &str = "shell/islands/main"; |
| 290 | const EDGE_DIVIDERS: &[&str] = &[SIDEBAR_SCROLL_DIVIDER, HEADER_DIVIDER]; |
| 291 | const DIVIDER_SEGMENT: &str = "divider"; |
| 292 | const DIVIDER_THICKNESS: f32 = 1.; |
| 293 | const INSET_TOLERANCE: f32 = 1.; |
| 294 | const INSET_RULES: &[InsetRule] = &[ |
| 295 | InsetRule { |
| 296 | panel: "settings/nav", |
| 297 | content: "settings/nav/about", |
| 298 | edges: &[Edge::Left, Edge::Right, Edge::Bottom], |
| 299 | }, |
| 300 | InsetRule { |
| 301 | panel: "settings/main", |
| 302 | content: "settings/main/header/close", |
| 303 | edges: &[Edge::Top, Edge::Right], |
| 304 | }, |
| 305 | ]; |
| 306 | const SCROLLED_INSET_RULE: InsetRule = InsetRule { |
| 307 | panel: SETTINGS_BODY, |
| 308 | content: SETTINGS_CONTENT, |
| 309 | edges: &[Edge::Left, Edge::Bottom], |
| 310 | }; |
| 311 | const SCROLLED_INSET_ALLOWLIST: &[(SettingsSection, &str)] = &[( |
| 312 | SettingsSection::Agents, |
| 313 | "Agents pages carry their own PAGE_PADDING side inset, the body narrows its side padding to keep the text edge", |
| 314 | )]; |
| 315 | const SETTINGS_SECTIONS: [SettingsSection; 9] = [ |
| 316 | SettingsSection::General, |
| 317 | SettingsSection::Notifications, |
| 318 | SettingsSection::Appearance, |
| 319 | SettingsSection::Chats, |
| 320 | SettingsSection::Calls, |
| 321 | SettingsSection::Devices, |
| 322 | SettingsSection::Agents, |
| 323 | SettingsSection::Mcp, |
| 324 | SettingsSection::About, |
| 325 | ]; |
| 326 | const SIZES: [(f32, f32); 3] = [(640., 480.), (1280., 800.), (1920., 1080.)]; |
| 327 | const TOLERANCE: f32 = 0.5; |
| 328 | const KNOWN_VIOLATIONS: &[&str] = &[]; |
| 329 | const ZOOM_STEP: f32 = 1.25; |
| 330 | |
| 331 | type Tag = (&'static str, Bounds<Pixels>); |
| 332 | |
| 333 | #[derive(Clone, Copy, Debug)] |
| 334 | enum Edge { |
| 335 | Top, |
| 336 | Right, |
| 337 | Bottom, |
| 338 | Left, |
| 339 | } |
| 340 | |
| 341 | struct InsetRule { |
| 342 | panel: &'static str, |
| 343 | content: &'static str, |
| 344 | edges: &'static [Edge], |
| 345 | } |
| 346 | |
| 347 | fn inset(panel: Bounds<Pixels>, content: Bounds<Pixels>, edge: Edge) -> f32 { |
| 348 | f32::from(match edge { |
| 349 | Edge::Top => content.top() - panel.top(), |
| 350 | Edge::Right => panel.right() - content.right(), |
| 351 | Edge::Bottom => panel.bottom() - content.bottom(), |
| 352 | Edge::Left => content.left() - panel.left(), |
| 353 | }) |
| 354 | } |
| 355 | |
| 356 | fn inset_violation( |
| 357 | rule: &InsetRule, |
| 358 | panel: Bounds<Pixels>, |
| 359 | content: Bounds<Pixels>, |
| 360 | ) -> Option<(String, String)> { |
| 361 | let insets: Vec<(Edge, f32)> = rule |
| 362 | .edges |
| 363 | .iter() |
| 364 | .map(|edge| (*edge, inset(panel, content, *edge))) |
| 365 | .collect(); |
| 366 | let values = insets.iter().map(|(_, value)| *value); |
| 367 | let spread = values.clone().fold(f32::MIN, f32::max) - values.fold(f32::MAX, f32::min); |
| 368 | (spread > INSET_TOLERANCE).then(|| { |
| 369 | ( |
| 370 | format!("{}:uneven-inset", rule.panel), |
| 371 | format!( |
| 372 | "{} insets {} by {insets:?}, they must match", |
| 373 | rule.panel, rule.content |
| 374 | ), |
| 375 | ) |
| 376 | }) |
| 377 | } |
| 378 | |
| 379 | fn inset_violations(tags: &[Tag], rules: &[InsetRule]) -> Vec<(String, String)> { |
| 380 | let bounds_of = |path: &str| { |
| 381 | tags.iter() |
| 382 | .find(|(tag, _)| *tag == path) |
| 383 | .map(|(_, bounds)| *bounds) |
| 384 | }; |
| 385 | rules |
| 386 | .iter() |
| 387 | .filter_map( |
| 388 | |rule| match (bounds_of(rule.panel), bounds_of(rule.content)) { |
| 389 | (Some(panel), Some(content)) => inset_violation(rule, panel, content), |
| 390 | _ => None, |
| 391 | }, |
| 392 | ) |
| 393 | .collect() |
| 394 | } |
| 395 | |
| 396 | fn divider_violations(tags: &[Tag]) -> Vec<(String, String)> { |
| 397 | let mut found = Vec::new(); |
| 398 | for (path, bounds) in tags { |
| 399 | let Some((parent, segment)) = path.rsplit_once('/') else { |
| 400 | continue; |
| 401 | }; |
| 402 | if !segment.starts_with(DIVIDER_SEGMENT) { |
| 403 | continue; |
| 404 | } |
| 405 | let Some((_, panel)) = tags.iter().find(|(tag, _)| *tag == parent) else { |
| 406 | found.push(( |
| 407 | format!("{path}:no-panel"), |
| 408 | format!("{path} needs its panel {parent} tagged"), |
| 409 | )); |
| 410 | continue; |
| 411 | }; |
| 412 | let thin = |length: Pixels| f32::from(length) <= DIVIDER_THICKNESS + TOLERANCE; |
| 413 | let near = |first: Pixels, second: Pixels| f32::from(first - second).abs() <= TOLERANCE; |
| 414 | let spans = if thin(bounds.size.height) { |
| 415 | near(bounds.left(), panel.left()) && near(bounds.right(), panel.right()) |
| 416 | } else if thin(bounds.size.width) { |
| 417 | near(bounds.top(), panel.top()) && near(bounds.bottom(), panel.bottom()) |
| 418 | } else { |
| 419 | false |
| 420 | }; |
| 421 | if !spans { |
| 422 | found.push(( |
| 423 | format!("{path}:short-divider"), |
| 424 | format!( |
| 425 | "{path} {} must be a {DIVIDER_THICKNESS} px line from edge to edge of {parent} {}", |
| 426 | describe(*bounds), |
| 427 | describe(*panel) |
| 428 | ), |
| 429 | )); |
| 430 | } |
| 431 | } |
| 432 | found |
| 433 | } |
| 434 | |
| 435 | enum Screen { |
| 436 | Tab(&'static str, Option<fn(&mut VisualTestContext)>), |
| 437 | StatusMenu, |
| 438 | Call, |
| 439 | Prejoin, |
| 440 | CallTranscript, |
| 441 | CallPeople, |
| 442 | CallShare, |
| 443 | CallConsult, |
| 444 | } |
| 445 | |
| 446 | fn parent_of(path: &str) -> Option<&str> { |
| 447 | path.rsplit_once('/').map(|(parent, _)| parent) |
| 448 | } |
| 449 | |
| 450 | fn contains(outer: Bounds<Pixels>, inner: Bounds<Pixels>) -> bool { |
| 451 | let tolerance = px(TOLERANCE); |
| 452 | inner.left() >= outer.left() - tolerance |
| 453 | && inner.top() >= outer.top() - tolerance |
| 454 | && inner.right() <= outer.right() + tolerance |
| 455 | && inner.bottom() <= outer.bottom() + tolerance |
| 456 | } |
| 457 | |
| 458 | fn overlaps(first: Bounds<Pixels>, second: Bounds<Pixels>) -> bool { |
| 459 | let tolerance = px(TOLERANCE); |
| 460 | first.left() < second.right() - tolerance |
| 461 | && second.left() < first.right() - tolerance |
| 462 | && first.top() < second.bottom() - tolerance |
| 463 | && second.top() < first.bottom() - tolerance |
| 464 | } |
| 465 | |
| 466 | fn describe(bounds: Bounds<Pixels>) -> String { |
| 467 | format!( |
| 468 | "[{:.1},{:.1} {:.1}x{:.1}]", |
| 469 | f32::from(bounds.origin.x), |
| 470 | f32::from(bounds.origin.y), |
| 471 | f32::from(bounds.size.width), |
| 472 | f32::from(bounds.size.height) |
| 473 | ) |
| 474 | } |
| 475 | |
| 476 | fn violations( |
| 477 | tags: &[Tag], |
| 478 | window: Bounds<Pixels>, |
| 479 | expected: &[&str], |
| 480 | ) -> Vec<(String, String)> { |
| 481 | let mut found = Vec::new(); |
| 482 | let bounds_of = |path: &str| { |
| 483 | tags.iter() |
| 484 | .find(|(tag, _)| *tag == path) |
| 485 | .map(|(_, bounds)| *bounds) |
| 486 | }; |
| 487 | for path in expected { |
| 488 | if bounds_of(path).is_none() { |
| 489 | found.push(( |
| 490 | format!("{path}:missing"), |
| 491 | format!("{path} was not rendered"), |
| 492 | )); |
| 493 | } |
| 494 | } |
| 495 | for (path, bounds) in tags { |
| 496 | if !contains(window, *bounds) { |
| 497 | found.push(( |
| 498 | format!("{path}:outside-window"), |
| 499 | format!( |
| 500 | "{path} {} leaves the window {}", |
| 501 | describe(*bounds), |
| 502 | describe(window) |
| 503 | ), |
| 504 | )); |
| 505 | } |
| 506 | if let Some(parent) = |
| 507 | parent_of(path).and_then(|parent| bounds_of(parent).map(|bounds| (parent, bounds))) |
| 508 | { |
| 509 | if !contains(parent.1, *bounds) { |
| 510 | found.push(( |
| 511 | format!("{path}:outside-parent"), |
| 512 | format!( |
| 513 | "{path} {} leaves {} {}", |
| 514 | describe(*bounds), |
| 515 | parent.0, |
| 516 | describe(parent.1) |
| 517 | ), |
| 518 | )); |
| 519 | } |
| 520 | } |
| 521 | for (other, other_bounds) in tags { |
| 522 | let overlays = |first: &str, second: &str| { |
| 523 | OVERLAY_PANELS.contains(&first) && OVERLAY_CHROME.contains(&second) |
| 524 | }; |
| 525 | let siblings = path < other |
| 526 | && parent_of(path).is_some() |
| 527 | && parent_of(path) == parent_of(other) |
| 528 | && !overlays(path, other) |
| 529 | && !overlays(other, path); |
| 530 | if siblings && overlaps(*bounds, *other_bounds) { |
| 531 | found.push(( |
| 532 | format!("{path}:overlaps:{other}"), |
| 533 | format!( |
| 534 | "{path} {} overlaps {other} {}", |
| 535 | describe(*bounds), |
| 536 | describe(*other_bounds) |
| 537 | ), |
| 538 | )); |
| 539 | } |
| 540 | } |
| 541 | } |
| 542 | found.extend(divider_violations(tags)); |
| 543 | found |
| 544 | } |
| 545 | |
| 546 | const SETTLE_FRAMES: usize = 2; |
| 547 | const SETTLE_LIMIT: usize = 60; |
| 548 | const SETTLE_STEP: Duration = Duration::from_millis(50); |
| 549 | |
| 550 | fn collect(visual: &mut VisualTestContext, awaited: Option<&'static str>) -> Vec<Tag> { |
| 551 | let mut frames = 0; |
| 552 | while frames < SETTLE_FRAMES |
| 553 | || awaited.is_some_and(|path| visual.debug_bounds(path).is_none()) |
| 554 | { |
| 555 | assert!(frames < SETTLE_LIMIT, "{awaited:?} never rendered"); |
| 556 | visual.executor().advance_clock(SETTLE_STEP); |
| 557 | std::thread::sleep(SETTLE_STEP); |
| 558 | visual.run_until_parked(); |
| 559 | visual.update(|window, cx| window.render_frame(cx)); |
| 560 | frames += 1; |
| 561 | } |
| 562 | GUARDED |
| 563 | .iter() |
| 564 | .chain(CALL_GUARDED) |
| 565 | .chain(CALLS_TAGS) |
| 566 | .chain(PREJOIN_TAGS) |
| 567 | .chain(SHARE_GUARDED) |
| 568 | .chain(CONSULT_GUARDED) |
| 569 | .chain([ |
| 570 | &AGENTS_LIST, |
| 571 | &CALL_TRANSCRIPT, |
| 572 | &MENU, |
| 573 | &RECAP_TAB, |
| 574 | &RECAP_REGION, |
| 575 | &PEOPLE_PANEL, |
| 576 | ]) |
| 577 | .chain(&AGENT_TAGS) |
| 578 | .chain(SETTINGS_TAGS) |
| 579 | .chain(MEMBERS_TAGS) |
| 580 | .chain(EDGE_DIVIDERS) |
| 581 | .filter_map(|path| visual.debug_bounds(path).map(|bounds| (*path, bounds))) |
| 582 | .collect() |
| 583 | } |
| 584 | |
| 585 | fn sharing_call() -> ActiveCall { |
| 586 | let mut call = gallery_call(); |
| 587 | call.model |
| 588 | .apply(CallUpdate::ScreenShare(Some("8:orgid:guard-1".to_owned()))); |
| 589 | call.share_stage.view.step_in((800., 450.)); |
| 590 | call |
| 591 | } |
| 592 | |
| 593 | fn gallery_call() -> ActiveCall { |
| 594 | let (handle, _control) = call_channel(); |
| 595 | let mut model = CallModel::meeting("Standup", false); |
| 596 | model.apply(CallUpdate::State(CallState::Connected { |
| 597 | since: Instant::now(), |
| 598 | })); |
| 599 | model.apply(CallUpdate::Roster( |
| 600 | (0..11) |
| 601 | .map(|index| RosterEntry { |
| 602 | mri: format!("8:orgid:guard-{index}"), |
| 603 | display_name: format!("Guest {index}"), |
| 604 | muted: false, |
| 605 | in_lobby: false, |
| 606 | has_video: false, |
| 607 | sharing: false, |
| 608 | hand: None, |
| 609 | spotlight: None, |
| 610 | organizer: false, |
| 611 | ..Default::default() |
| 612 | }) |
| 613 | .collect(), |
| 614 | )); |
| 615 | ActiveCall::new(1, model, handle.commands, None, handle.video) |
| 616 | } |
| 617 | |
| 618 | fn consult_calls() -> (ActiveCall, ActiveCall) { |
| 619 | let connected = |id: u64, name: &str, mri: &str| { |
| 620 | let (handle, _control) = call_channel(); |
| 621 | let mut model = CallModel::people(name, &[(mri.to_owned(), name.to_owned())]); |
| 622 | model.apply(CallUpdate::State(CallState::Connected { |
| 623 | since: Instant::now(), |
| 624 | })); |
| 625 | ActiveCall::new(id, model, handle.commands, None, handle.video) |
| 626 | }; |
| 627 | let held = connected(1, "Christopher Montgomery-Lindqvist", "8:orgid:guard-held"); |
| 628 | let mut consult = connected(2, "Jonas Ortega", "8:orgid:guard-consult"); |
| 629 | consult.consult = Some(ConsultCall::new( |
| 630 | ( |
| 631 | "8:orgid:guard-consult".to_owned(), |
| 632 | "Jonas Ortega".to_owned(), |
| 633 | ), |
| 634 | held.model.peer_name.clone(), |
| 635 | )); |
| 636 | (consult, held) |
| 637 | } |
| 638 | |
| 639 | fn mount(cx: &mut TestAppContext) -> (gpui_kit::AnyWindowHandle, Entity<AppState>) { |
| 640 | mount_with(cx, false) |
| 641 | } |
| 642 | |
| 643 | fn mount_with( |
| 644 | cx: &mut TestAppContext, |
| 645 | agent_api: bool, |
| 646 | ) -> (gpui_kit::AnyWindowHandle, Entity<AppState>) { |
| 647 | cx.update(gpui_kit::init); |
| 648 | cx.update(|cx| { |
| 649 | crate::theme::apply(cx); |
| 650 | bind_keys(cx); |
| 651 | let store = Store::open_in_memory().expect("in-memory store"); |
| 652 | demo::seed(&store); |
| 653 | let store = Arc::new(store); |
| 654 | if agent_api { |
| 655 | crate::agent_api::init(store.clone(), true, cx); |
| 656 | } |
| 657 | let mode = Mode { |
| 658 | demo: true, |
| 659 | read_only: false, |
| 660 | demo_sync: None, |
| 661 | }; |
| 662 | let state = cx.new(|_| AppState::new(store, mode)); |
| 663 | install_globals(&state, UserInput::new(), cx); |
| 664 | crate::agents::install(state.clone(), cx); |
| 665 | demo::start_session(&state, cx); |
| 666 | let shared_state = state.clone(); |
| 667 | let startup = Startup { |
| 668 | switcher_query: None, |
| 669 | composer_text: None, |
| 670 | reply_to_last: false, |
| 671 | }; |
| 672 | let window = gpui_kit::open_window(WindowOptions::default(), cx, move |window, cx| { |
| 673 | cx.new(|cx| AppShell::new(state, None, startup, window, cx)) |
| 674 | }) |
| 675 | .expect("open test window") |
| 676 | .0; |
| 677 | (window, shared_state) |
| 678 | }) |
| 679 | } |
| 680 | |
| 681 | fn rectangle(left: f32, top: f32, width: f32, height: f32) -> Bounds<Pixels> { |
| 682 | Bounds { |
| 683 | origin: point(px(left), px(top)), |
| 684 | size: size(px(width), px(height)), |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | #[test] |
| 689 | fn rules_flag_overlap_and_overflow() { |
| 690 | let window = rectangle(0., 0., 100., 100.); |
| 691 | let tags: Vec<Tag> = vec![ |
| 692 | ("shell", rectangle(0., 0., 100., 100.)), |
| 693 | ("shell/islands", rectangle(0., 0., 100., 50.)), |
| 694 | ("shell/islands/rail", rectangle(0., 0., 60., 50.)), |
| 695 | ("shell/islands/sidebar", rectangle(50., 0., 60., 50.)), |
| 696 | ]; |
| 697 | let keys: Vec<String> = violations(&tags, window, &["shell/status-bar"]) |
| 698 | .into_iter() |
| 699 | .map(|(key, _)| key) |
| 700 | .collect(); |
| 701 | let line_tags: Vec<Tag> = vec![ |
| 702 | ("panel", rectangle(0., 0., 100., 100.)), |
| 703 | ("panel/divider", rectangle(0., 50., 100., 1.)), |
| 704 | ("panel/divider-inset", rectangle(12., 60., 76., 1.)), |
| 705 | ("panel/divider-side", rectangle(40., 0., 1., 100.)), |
| 706 | ("panel/about", rectangle(12., 76., 76., 12.)), |
| 707 | ("panel/footer", rectangle(12., 64., 76., 12.)), |
| 708 | ]; |
| 709 | let line_keys: Vec<String> = divider_violations(&line_tags) |
| 710 | .into_iter() |
| 711 | .chain(inset_violations( |
| 712 | &line_tags, |
| 713 | &[ |
| 714 | InsetRule { |
| 715 | panel: "panel", |
| 716 | content: "panel/about", |
| 717 | edges: &[Edge::Left, Edge::Right, Edge::Bottom], |
| 718 | }, |
| 719 | InsetRule { |
| 720 | panel: "panel", |
| 721 | content: "panel/footer", |
| 722 | edges: &[Edge::Left, Edge::Bottom], |
| 723 | }, |
| 724 | ], |
| 725 | )) |
| 726 | .map(|(key, _)| key) |
| 727 | .collect(); |
| 728 | assert_eq!( |
| 729 | line_keys, |
| 730 | ["panel/divider-inset:short-divider", "panel:uneven-inset"] |
| 731 | ); |
| 732 | assert_eq!( |
| 733 | keys, |
| 734 | [ |
| 735 | "shell/status-bar:missing", |
| 736 | "shell/islands/rail:overlaps:shell/islands/sidebar", |
| 737 | "shell/islands/sidebar:outside-window", |
| 738 | "shell/islands/sidebar:outside-parent", |
| 739 | ] |
| 740 | ); |
| 741 | } |
| 742 | |
| 743 | #[gpui_kit::test] |
| 744 | fn no_element_overlaps_or_leaves_its_container(cx: &mut TestAppContext) { |
| 745 | check_layout(cx, &SIZES, 1., KNOWN_VIOLATIONS); |
| 746 | } |
| 747 | |
| 748 | #[gpui_kit::test] |
| 749 | fn layout_holds_at_a_zoomed_step(cx: &mut TestAppContext) { |
| 750 | let (width, height) = SIZES[1]; |
| 751 | let zoomed = [(width / ZOOM_STEP, height / ZOOM_STEP)]; |
| 752 | check_layout(cx, &zoomed, ZOOM_STEP, &[]); |
| 753 | } |
| 754 | |
| 755 | fn check_layout( |
| 756 | cx: &mut TestAppContext, |
| 757 | sizes: &[(f32, f32)], |
| 758 | zoom: f32, |
| 759 | known_violations: &[&str], |
| 760 | ) { |
| 761 | let (handle, app) = mount(cx); |
| 762 | let platform_scale = cx |
| 763 | .update_window(handle, |_, window, _| window.scale_factor()) |
| 764 | .expect("test window"); |
| 765 | cx.simulate_window_scale_factor_change(handle, platform_scale * zoom); |
| 766 | let mut visual = VisualTestContext::from_window(handle, cx); |
| 767 | let mut seen_known = HashSet::new(); |
| 768 | let mut unexpected = Vec::new(); |
| 769 | for &(width, height) in sizes { |
| 770 | visual.simulate_resize(size(px(width), px(height))); |
| 771 | let window = Bounds { |
| 772 | origin: point(px(0.), px(0.)), |
| 773 | size: size(px(width), px(height)), |
| 774 | }; |
| 775 | let screens = [ |
| 776 | Screen::Tab("chats", None), |
| 777 | Screen::Tab( |
| 778 | "activity", |
| 779 | Some(|visual| visual.dispatch_action(OpenActivity)), |
| 780 | ), |
| 781 | Screen::Tab( |
| 782 | "channels", |
| 783 | Some(|visual| visual.dispatch_action(OpenChannelsTab)), |
| 784 | ), |
| 785 | Screen::Tab("saved", Some(|visual| visual.dispatch_action(OpenSaved))), |
| 786 | Screen::Tab("calls", Some(|visual| visual.dispatch_action(OpenCallsTab))), |
| 787 | Screen::Tab("agents", Some(|visual| visual.dispatch_action(OpenAgents))), |
| 788 | Screen::Prejoin, |
| 789 | Screen::StatusMenu, |
| 790 | Screen::Call, |
| 791 | Screen::CallTranscript, |
| 792 | Screen::CallPeople, |
| 793 | Screen::CallShare, |
| 794 | Screen::CallConsult, |
| 795 | ]; |
| 796 | for screen in screens { |
| 797 | let (screen_name, status_menu) = match screen { |
| 798 | Screen::Tab(name, open) => { |
| 799 | if let Some(open) = open { |
| 800 | open(&mut visual); |
| 801 | } |
| 802 | (name, false) |
| 803 | } |
| 804 | Screen::Call | Screen::CallPeople => { |
| 805 | let people_open = matches!(screen, Screen::CallPeople); |
| 806 | visual.update(|_, cx| { |
| 807 | app.update(cx, |state, cx| { |
| 808 | let mut call = gallery_call(); |
| 809 | call.people_open = people_open; |
| 810 | state.call = Some(call); |
| 811 | cx.notify(); |
| 812 | }) |
| 813 | }); |
| 814 | (if people_open { "call-people" } else { "call" }, false) |
| 815 | } |
| 816 | Screen::Prejoin => { |
| 817 | let state = visual.update(|_, cx| cx.global::<AppHandle>().0.clone()); |
| 818 | visual.update(|_, cx| { |
| 819 | state.update(cx, |state, cx| state.join_meeting_link(PREJOIN_LINK, cx)) |
| 820 | }); |
| 821 | ("prejoin", false) |
| 822 | } |
| 823 | Screen::CallTranscript => { |
| 824 | visual.update(|window, cx| { |
| 825 | app.update(cx, |state, cx| { |
| 826 | let mut call = gallery_call(); |
| 827 | call.model.apply(CallUpdate::Captions(CaptionState::On)); |
| 828 | for index in 0..40 { |
| 829 | call.model.note_caption( |
| 830 | CaptionEntry { |
| 831 | id: format!("guard-{index}"), |
| 832 | user_id: "8:orgid:guard-0".into(), |
| 833 | display_name: "Guest 0".into(), |
| 834 | text: "A long guarded sentence that has to wrap inside the panel".into(), |
| 835 | is_final: true, |
| 836 | }, |
| 837 | Instant::now(), |
| 838 | ); |
| 839 | } |
| 840 | call.transcript_open = true; |
| 841 | state.call = Some(call); |
| 842 | let app_entity = cx.entity(); |
| 843 | state.transcript_panel = Some(cx.new(|cx| TranscriptPanel::new(app_entity, window, cx)).into()); |
| 844 | cx.notify(); |
| 845 | }) |
| 846 | }); |
| 847 | ("call-transcript", false) |
| 848 | } |
| 849 | Screen::CallShare => { |
| 850 | visual.update(|_, cx| { |
| 851 | app.update(cx, |state, cx| { |
| 852 | state.call = Some(sharing_call()); |
| 853 | cx.notify(); |
| 854 | }) |
| 855 | }); |
| 856 | ("call-share", false) |
| 857 | } |
| 858 | Screen::CallConsult => { |
| 859 | visual.update(|_, cx| { |
| 860 | app.update(cx, |state, cx| { |
| 861 | let (consult, held) = consult_calls(); |
| 862 | state.call = Some(consult); |
| 863 | state.held_call = Some(held); |
| 864 | cx.notify(); |
| 865 | }) |
| 866 | }); |
| 867 | ("call-consult", false) |
| 868 | } |
| 869 | Screen::StatusMenu => { |
| 870 | let tags = collect(&mut visual, None); |
| 871 | let button = tags |
| 872 | .iter() |
| 873 | .find(|(path, _)| *path == OWN_STATUS) |
| 874 | .map(|(_, bounds)| *bounds) |
| 875 | .expect("own status button rendered"); |
| 876 | visual.simulate_click(button.center(), Default::default()); |
| 877 | ("status-menu", true) |
| 878 | } |
| 879 | }; |
| 880 | let is_call = matches!( |
| 881 | screen_name, |
| 882 | "call" | "call-people" | "call-transcript" | "call-share" | "call-consult" |
| 883 | ); |
| 884 | let tags = collect(&mut visual, status_menu.then_some(MENU)); |
| 885 | let mut expected = GUARDED.to_vec(); |
| 886 | if is_call { |
| 887 | expected.extend(CALL_GUARDED); |
| 888 | let panel_open = matches!(screen_name, "call-people" | "call-transcript"); |
| 889 | if sidebar_hidden_in_call(width, panel_open) { |
| 890 | expected.retain(|path| *path != "shell/islands/sidebar"); |
| 891 | } |
| 892 | } |
| 893 | if screen_name == "call-transcript" { |
| 894 | expected.push(CALL_TRANSCRIPT); |
| 895 | } |
| 896 | if screen_name == "call-people" { |
| 897 | expected.push(PEOPLE_PANEL); |
| 898 | } |
| 899 | if screen_name == "call-share" { |
| 900 | expected.extend(SHARE_GUARDED); |
| 901 | } |
| 902 | if screen_name == "call-consult" { |
| 903 | expected.extend(CONSULT_GUARDED); |
| 904 | } |
| 905 | if status_menu { |
| 906 | expected.push(MENU); |
| 907 | } |
| 908 | match screen_name { |
| 909 | "calls" => expected.extend(CALLS_TAGS), |
| 910 | "agents" => expected.push(AGENTS_LIST), |
| 911 | "prejoin" => expected.extend(PREJOIN_TAGS), |
| 912 | _ => {} |
| 913 | } |
| 914 | let mut found = violations(&tags, window, &expected); |
| 915 | if status_menu { |
| 916 | let menu = tags |
| 917 | .iter() |
| 918 | .find(|(path, _)| *path == MENU) |
| 919 | .map(|(_, bounds)| *bounds); |
| 920 | let button = tags |
| 921 | .iter() |
| 922 | .find(|(path, _)| *path == OWN_STATUS) |
| 923 | .map(|(_, bounds)| *bounds); |
| 924 | if let (Some(menu), Some(button)) = (menu, button) { |
| 925 | if overlaps(menu, button) { |
| 926 | found.push(( |
| 927 | format!("{MENU}:covers:{OWN_STATUS}"), |
| 928 | format!( |
| 929 | "{MENU} {} covers {OWN_STATUS} {}", |
| 930 | describe(menu), |
| 931 | describe(button) |
| 932 | ), |
| 933 | )); |
| 934 | } |
| 935 | } |
| 936 | visual.simulate_keystrokes("escape"); |
| 937 | visual.run_until_parked(); |
| 938 | } |
| 939 | if is_call { |
| 940 | visual.update(|_, cx| { |
| 941 | app.update(cx, |state, cx| { |
| 942 | state.call = None; |
| 943 | state.held_call = None; |
| 944 | cx.notify(); |
| 945 | }) |
| 946 | }); |
| 947 | } |
| 948 | if screen_name == "prejoin" { |
| 949 | let state = visual.update(|_, cx| cx.global::<AppHandle>().0.clone()); |
| 950 | visual.update(|_, cx| state.update(cx, |state, cx| state.cancel_prejoin(cx))); |
| 951 | } |
| 952 | for (key, message) in found { |
| 953 | let full_key = format!("{screen_name}@{width}x{height}:{key}"); |
| 954 | if known_violations.contains(&full_key.as_str()) { |
| 955 | seen_known.insert(full_key); |
| 956 | } else { |
| 957 | unexpected.push(format!("{full_key}: {message}")); |
| 958 | } |
| 959 | } |
| 960 | } |
| 961 | } |
| 962 | let stale: Vec<String> = known_violations |
| 963 | .iter() |
| 964 | .filter(|key| !seen_known.contains(**key)) |
| 965 | .map(|key| format!("stale known violation, remove it: {key}")) |
| 966 | .collect(); |
| 967 | assert!( |
| 968 | unexpected.is_empty() && stale.is_empty(), |
| 969 | "{}", |
| 970 | unexpected |
| 971 | .into_iter() |
| 972 | .chain(stale) |
| 973 | .collect::<Vec<_>>() |
| 974 | .join("\n") |
| 975 | ); |
| 976 | } |
| 977 | #[gpui_kit::test] |
| 978 | fn own_avatar_matches_the_teams_me_control(cx: &mut TestAppContext) { |
| 979 | let (handle, _) = mount(cx); |
| 980 | let mut visual = VisualTestContext::from_window(handle, cx); |
| 981 | visual.simulate_resize(size(px(1280.), px(800.))); |
| 982 | collect(&mut visual, Some(OWN_AVATAR_DISC)); |
| 983 | let disc = visual |
| 984 | .debug_bounds(OWN_AVATAR_DISC) |
| 985 | .expect("own avatar rendered"); |
| 986 | let bar = visual |
| 987 | .debug_bounds("shell/title-bar") |
| 988 | .expect("title bar rendered"); |
| 989 | assert_eq!( |
| 990 | f32::from(disc.size.width), |
| 991 | TITLE_AVATAR_SIZE, |
| 992 | "{}", |
| 993 | describe(disc) |
| 994 | ); |
| 995 | assert_eq!( |
| 996 | f32::from(disc.size.height), |
| 997 | TITLE_AVATAR_SIZE, |
| 998 | "{}", |
| 999 | describe(disc) |
| 1000 | ); |
| 1001 | let top = f32::from(disc.top() - bar.top()); |
| 1002 | let bottom = f32::from(bar.bottom() - disc.bottom()); |
| 1003 | assert!( |
| 1004 | (top - bottom).abs() <= 1., |
| 1005 | "own avatar {} is not centered in the title bar {}", |
| 1006 | describe(disc), |
| 1007 | describe(bar) |
| 1008 | ); |
| 1009 | } |
| 1010 | |
| 1011 | const TITLE_SEARCH: &str = "shell/title-bar/search"; |
| 1012 | const TITLE_NEIGHBOURS: &[&str] = &[ |
| 1013 | "shell/title-bar/leading", |
| 1014 | "shell/title-bar/mode-pill", |
| 1015 | "shell/title-bar/trailing", |
| 1016 | "shell/title-bar/update", |
| 1017 | OWN_STATUS, |
| 1018 | ]; |
| 1019 | const TITLE_SEARCH_SIZES: [(f32, f32, bool); 3] = [ |
| 1020 | (1920., 1080., true), |
| 1021 | (1280., 800., true), |
| 1022 | (640., 480., false), |
| 1023 | ]; |
| 1024 | |
| 1025 | #[gpui_kit::test] |
| 1026 | fn title_search_is_centered_on_the_window(cx: &mut TestAppContext) { |
| 1027 | let (handle, _) = mount(cx); |
| 1028 | let mut visual = VisualTestContext::from_window(handle, cx); |
| 1029 | let shell = visual.update(|window, cx| { |
| 1030 | window |
| 1031 | .root::<gpui_kit::base::Root>() |
| 1032 | .flatten() |
| 1033 | .and_then(|root| root.read(cx).view().clone().downcast::<AppShell>().ok()) |
| 1034 | .expect("app shell is the root view") |
| 1035 | }); |
| 1036 | visual.update(|_, cx| { |
| 1037 | shell.update(cx, |shell, cx| { |
| 1038 | shell.set_update_status(crate::updater::UpdateStatus::Ready, cx) |
| 1039 | }) |
| 1040 | }); |
| 1041 | let mut found = Vec::new(); |
| 1042 | for (width, height, centered) in TITLE_SEARCH_SIZES { |
| 1043 | visual.simulate_resize(size(px(width), px(height))); |
| 1044 | collect(&mut visual, Some(TITLE_SEARCH)); |
| 1045 | let search = visual |
| 1046 | .debug_bounds(TITLE_SEARCH) |
| 1047 | .expect("title search rendered"); |
| 1048 | let search_center = f32::from(search.center().x); |
| 1049 | eprintln!( |
| 1050 | "title search @{width}x{height}: {} center {search_center:.1}, window center {:.1}", |
| 1051 | describe(search), |
| 1052 | width / 2. |
| 1053 | ); |
| 1054 | if centered && (search_center - width / 2.).abs() > 1. { |
| 1055 | found.push(format!( |
| 1056 | "@{width}x{height}: search center {search_center:.1} is not the window center {:.1}", |
| 1057 | width / 2. |
| 1058 | )); |
| 1059 | } |
| 1060 | if !contains(rectangle(0., 0., width, height), search) { |
| 1061 | found.push(format!( |
| 1062 | "@{width}x{height}: search {} leaves the window", |
| 1063 | describe(search) |
| 1064 | )); |
| 1065 | } |
| 1066 | for neighbour in TITLE_NEIGHBOURS { |
| 1067 | match visual.debug_bounds(neighbour) { |
| 1068 | None => found.push(format!("@{width}x{height}: {neighbour} was not rendered")), |
| 1069 | Some(bounds) if overlaps(search, bounds) => found.push(format!( |
| 1070 | "@{width}x{height}: search {} overlaps {neighbour} {}", |
| 1071 | describe(search), |
| 1072 | describe(bounds) |
| 1073 | )), |
| 1074 | Some(_) => {} |
| 1075 | } |
| 1076 | } |
| 1077 | } |
| 1078 | assert!(found.is_empty(), "{}", found.join("\n")); |
| 1079 | } |
| 1080 | |
| 1081 | #[gpui_kit::test] |
| 1082 | fn call_chat_bubbles_stay_inside_the_panel(cx: &mut TestAppContext) { |
| 1083 | let (handle, app) = mount(cx); |
| 1084 | let mut visual = VisualTestContext::from_window(handle, cx); |
| 1085 | for (width, height) in SIZES { |
| 1086 | visual.simulate_resize(size(px(width), px(height))); |
| 1087 | visual.update(|_, cx| { |
| 1088 | app.update(cx, |state, cx| { |
| 1089 | let mut call = gallery_call(); |
| 1090 | call.conversation_id = Some(RECAP_MEETING_CHAT.to_owned()); |
| 1091 | call.chat_open = true; |
| 1092 | state.call = Some(call); |
| 1093 | cx.notify(); |
| 1094 | }) |
| 1095 | }); |
| 1096 | collect(&mut visual, Some(CHAT_PANEL_TAG)); |
| 1097 | let panel = visual |
| 1098 | .debug_bounds(CHAT_PANEL_TAG) |
| 1099 | .expect("chat panel rendered"); |
| 1100 | let bubbles: Vec<(usize, Bounds<Pixels>)> = BUBBLE_TAGS |
| 1101 | .iter() |
| 1102 | .enumerate() |
| 1103 | .filter_map(|(index, tag)| visual.debug_bounds(tag).map(|bounds| (index, bounds))) |
| 1104 | .collect(); |
| 1105 | assert!(!bubbles.is_empty(), "no bubbles rendered in the call chat"); |
| 1106 | let found: Vec<String> = bubbles |
| 1107 | .into_iter() |
| 1108 | .filter(|(_, bounds)| { |
| 1109 | !inside_horizontally(panel, *bounds) || !overlaps(panel, *bounds) |
| 1110 | }) |
| 1111 | .map(|(index, bounds)| { |
| 1112 | format!( |
| 1113 | "call-chat@{width}x{height}: bubble {index} {} leaves the chat panel {}", |
| 1114 | describe(bounds), |
| 1115 | describe(panel) |
| 1116 | ) |
| 1117 | }) |
| 1118 | .collect(); |
| 1119 | visual.update(|_, cx| { |
| 1120 | app.update(cx, |state, cx| { |
| 1121 | state.call = None; |
| 1122 | cx.notify(); |
| 1123 | }) |
| 1124 | }); |
| 1125 | assert!(found.is_empty(), "{}", found.join("\n")); |
| 1126 | } |
| 1127 | } |
| 1128 | |
| 1129 | fn inside_horizontally(outer: Bounds<Pixels>, inner: Bounds<Pixels>) -> bool { |
| 1130 | let tolerance = px(TOLERANCE); |
| 1131 | inner.left() >= outer.left() - tolerance && inner.right() <= outer.right() + tolerance |
| 1132 | } |
| 1133 | |
| 1134 | fn scroll_sidebar(visual: &mut VisualTestContext, delta: f32) { |
| 1135 | let sidebar = visual.debug_bounds(SIDEBAR).expect("sidebar rendered"); |
| 1136 | visual.simulate_event(ScrollWheelEvent { |
| 1137 | position: sidebar.center(), |
| 1138 | delta: ScrollDelta::Pixels(point(px(0.), px(delta))), |
| 1139 | ..Default::default() |
| 1140 | }); |
| 1141 | } |
| 1142 | |
| 1143 | #[gpui_kit::test] |
| 1144 | fn chat_list_line_shows_only_while_scrolled(cx: &mut TestAppContext) { |
| 1145 | let (handle, _) = mount(cx); |
| 1146 | let mut visual = VisualTestContext::from_window(handle, cx); |
| 1147 | for (width, height) in SIZES { |
| 1148 | visual.simulate_resize(size(px(width), px(height))); |
| 1149 | let window = Bounds { |
| 1150 | origin: point(px(0.), px(0.)), |
| 1151 | size: size(px(width), px(height)), |
| 1152 | }; |
| 1153 | collect(&mut visual, None); |
| 1154 | scroll_sidebar(&mut visual, 100_000.); |
| 1155 | let tags = collect(&mut visual, None); |
| 1156 | assert!( |
| 1157 | visual.debug_bounds(SIDEBAR_SCROLL_DIVIDER).is_none(), |
| 1158 | "chat list at its top must not show the line at {width}x{height}" |
| 1159 | ); |
| 1160 | scroll_sidebar(&mut visual, -120.); |
| 1161 | let scrolled = collect(&mut visual, Some(SIDEBAR_SCROLL_DIVIDER)); |
| 1162 | let sidebar = visual.debug_bounds(SIDEBAR).expect("sidebar rendered"); |
| 1163 | let line = visual |
| 1164 | .debug_bounds(SIDEBAR_SCROLL_DIVIDER) |
| 1165 | .expect("scrolled chat list shows the line"); |
| 1166 | assert!( |
| 1167 | line.top() > sidebar.top() + px(TOLERANCE), |
| 1168 | "the line sits under the Chats header, not on the island edge: {} in {}", |
| 1169 | describe(line), |
| 1170 | describe(sidebar) |
| 1171 | ); |
| 1172 | let found: Vec<String> = violations(&scrolled, window, &[SIDEBAR_SCROLL_DIVIDER]) |
| 1173 | .into_iter() |
| 1174 | .chain(violations(&tags, window, &[])) |
| 1175 | .map(|(key, message)| format!("chat-list@{width}x{height}:{key}: {message}")) |
| 1176 | .collect(); |
| 1177 | assert!(found.is_empty(), "{}", found.join("\n")); |
| 1178 | scroll_sidebar(&mut visual, 100_000.); |
| 1179 | collect(&mut visual, None); |
| 1180 | assert!( |
| 1181 | visual.debug_bounds(SIDEBAR_SCROLL_DIVIDER).is_none(), |
| 1182 | "the line goes away once the chat list is back at its top" |
| 1183 | ); |
| 1184 | } |
| 1185 | } |
| 1186 | |
| 1187 | #[gpui_kit::test] |
| 1188 | fn header_tabs_sit_above_the_header_line(cx: &mut TestAppContext) { |
| 1189 | let (handle, state) = mount(cx); |
| 1190 | let mut visual = VisualTestContext::from_window(handle, cx); |
| 1191 | for (width, height) in SIZES { |
| 1192 | visual.simulate_resize(size(px(width), px(height))); |
| 1193 | let window = Bounds { |
| 1194 | origin: point(px(0.), px(0.)), |
| 1195 | size: size(px(width), px(height)), |
| 1196 | }; |
| 1197 | visual.update(|_, cx| { |
| 1198 | state.update(cx, |state, cx| { |
| 1199 | state.select(Selection::Chat(RECAP_MEETING_CHAT.to_owned()), cx) |
| 1200 | }) |
| 1201 | }); |
| 1202 | let tags = collect(&mut visual, Some(RECAP_TAB)); |
| 1203 | let main = visual.debug_bounds(MAIN).expect("main island rendered"); |
| 1204 | let tab = visual.debug_bounds(RECAP_TAB).expect("recap tab rendered"); |
| 1205 | let line = visual |
| 1206 | .debug_bounds(HEADER_DIVIDER) |
| 1207 | .expect("the header line is always shown"); |
| 1208 | let near = |first: Pixels, second: Pixels| f32::from(first - second).abs() <= TOLERANCE; |
| 1209 | assert!( |
| 1210 | tab.top() >= main.top() && near(tab.bottom(), line.top()), |
| 1211 | "tab {} must sit in the top bar above the line {}", |
| 1212 | describe(tab), |
| 1213 | describe(line) |
| 1214 | ); |
| 1215 | let found: Vec<String> = violations(&tags, window, &[HEADER_DIVIDER, RECAP_TAB]) |
| 1216 | .into_iter() |
| 1217 | .map(|(key, message)| format!("header@{width}x{height}:{key}: {message}")) |
| 1218 | .collect(); |
| 1219 | assert!(found.is_empty(), "{}", found.join("\n")); |
| 1220 | } |
| 1221 | } |
| 1222 | |
| 1223 | #[gpui_kit::test] |
| 1224 | fn recap_tab_stays_inside_the_main_island(cx: &mut TestAppContext) { |
| 1225 | let (handle, state) = mount(cx); |
| 1226 | let mut visual = VisualTestContext::from_window(handle, cx); |
| 1227 | for (width, height) in SIZES { |
| 1228 | visual.simulate_resize(size(px(width), px(height))); |
| 1229 | let window = Bounds { |
| 1230 | origin: point(px(0.), px(0.)), |
| 1231 | size: size(px(width), px(height)), |
| 1232 | }; |
| 1233 | visual.update(|_, cx| { |
| 1234 | state.update(cx, |state, cx| { |
| 1235 | state.select(Selection::Chat(RECAP_MEETING_CHAT.to_owned()), cx) |
| 1236 | }) |
| 1237 | }); |
| 1238 | collect(&mut visual, Some(RECAP_TAB)); |
| 1239 | let tab = visual.debug_bounds(RECAP_TAB).expect("recap tab rendered"); |
| 1240 | visual.simulate_click(tab.center(), Default::default()); |
| 1241 | let tags = collect(&mut visual, Some(RECAP_REGION)); |
| 1242 | let mut expected = GUARDED.to_vec(); |
| 1243 | expected.extend([RECAP_TAB, RECAP_REGION]); |
| 1244 | let found: Vec<String> = violations(&tags, window, &expected) |
| 1245 | .into_iter() |
| 1246 | .map(|(key, message)| format!("recap@{width}x{height}:{key}: {message}")) |
| 1247 | .collect(); |
| 1248 | assert!(found.is_empty(), "{}", found.join("\n")); |
| 1249 | } |
| 1250 | } |
| 1251 | |
| 1252 | #[gpui_kit::test] |
| 1253 | fn settings_dialog_keeps_its_inset_at_every_size(cx: &mut TestAppContext) { |
| 1254 | check_settings_dialog(cx, &SETTINGS_SECTIONS); |
| 1255 | } |
| 1256 | |
| 1257 | #[gpui_kit::test] |
| 1258 | fn settings_dialog_keeps_its_inset_in_the_teams_scheme(cx: &mut TestAppContext) { |
| 1259 | crate::theme::set_thread_scheme(Some("teams")); |
| 1260 | check_settings_dialog(cx, &[SettingsSection::Appearance, SettingsSection::General]); |
| 1261 | crate::theme::set_thread_scheme(None); |
| 1262 | } |
| 1263 | |
| 1264 | #[gpui_kit::test] |
| 1265 | fn settings_dialog_keeps_its_inset_in_the_light_theme(cx: &mut TestAppContext) { |
| 1266 | crate::theme::set_thread_shade(Some(crate::theme::Shade::Light)); |
| 1267 | check_settings_dialog(cx, &[SettingsSection::Appearance, SettingsSection::General]); |
| 1268 | crate::theme::set_thread_shade(None); |
| 1269 | } |
| 1270 | |
| 1271 | fn check_settings_dialog(cx: &mut TestAppContext, sections: &[SettingsSection]) { |
| 1272 | let (handle, state) = mount_with(cx, true); |
| 1273 | let mut visual = VisualTestContext::from_window(handle, cx); |
| 1274 | for (width, height) in SIZES.into_iter().chain([(960., 640.)]) { |
| 1275 | visual.simulate_resize(size(px(width), px(height))); |
| 1276 | let window = Bounds { |
| 1277 | origin: point(px(0.), px(0.)), |
| 1278 | size: size(px(width), px(height)), |
| 1279 | }; |
| 1280 | let inset = metrics(width, height).inset; |
| 1281 | let label_sizes = NAV_LABEL_SIZES.contains(&(width, height)); |
| 1282 | for §ion in sections { |
| 1283 | visual |
| 1284 | .update(|_, cx| state.update(cx, |state, cx| state.open_settings(section, cx))); |
| 1285 | let tags = collect(&mut visual, Some(SETTINGS_TAGS[0])); |
| 1286 | let mut found: Vec<(String, String)> = violations(&tags, window, SETTINGS_TAGS) |
| 1287 | .into_iter() |
| 1288 | .filter(|(key, _)| key.starts_with("settings")) |
| 1289 | .chain(inset_violations(&tags, INSET_RULES)) |
| 1290 | .collect(); |
| 1291 | if label_sizes { |
| 1292 | found.extend(nav_label_violations(&mut visual)); |
| 1293 | } |
| 1294 | let scrolled_exempt = SCROLLED_INSET_ALLOWLIST |
| 1295 | .iter() |
| 1296 | .any(|(exempt, _)| *exempt == section); |
| 1297 | if !scrolled_exempt { |
| 1298 | found.extend(scrolled_inset_violation(&mut visual)); |
| 1299 | } |
| 1300 | let found: Vec<String> = found |
| 1301 | .into_iter() |
| 1302 | .map(|(key, message)| format!("{section:?}@{width}x{height}:{key}: {message}")) |
| 1303 | .collect(); |
| 1304 | assert!(found.is_empty(), "{}", found.join("\n")); |
| 1305 | let dialog = visual |
| 1306 | .debug_bounds(SETTINGS_TAGS[0]) |
| 1307 | .expect("settings dialog"); |
| 1308 | let edges = [ |
| 1309 | f32::from(dialog.left()), |
| 1310 | f32::from(dialog.top()), |
| 1311 | width - f32::from(dialog.right()), |
| 1312 | height - f32::from(dialog.bottom()), |
| 1313 | ]; |
| 1314 | assert!( |
| 1315 | edges.iter().all(|edge| (edge - inset).abs() <= TOLERANCE), |
| 1316 | "{section:?}@{width}x{height}: dialog edges {edges:?}, expected {inset}" |
| 1317 | ); |
| 1318 | } |
| 1319 | } |
| 1320 | } |
| 1321 | |
| 1322 | const NAV_LABEL_SIZES: [(f32, f32); 3] = [(960., 640.), (1280., 800.), (1920., 1080.)]; |
| 1323 | |
| 1324 | fn nav_label_violations(visual: &mut VisualTestContext) -> Vec<(String, String)> { |
| 1325 | let Some(nav) = visual.debug_bounds(SETTINGS_NAV) else { |
| 1326 | return vec![(SETTINGS_NAV.to_owned(), "not rendered".to_owned())]; |
| 1327 | }; |
| 1328 | let mut found = Vec::new(); |
| 1329 | for section in SETTINGS_SECTIONS { |
| 1330 | let label_path: &'static str = Box::leak(label_selector(section).into_boxed_str()); |
| 1331 | let item_path: &'static str = if section == SettingsSection::About { |
| 1332 | SETTINGS_NAV_ABOUT |
| 1333 | } else { |
| 1334 | Box::leak(item_selector(section).into_boxed_str()) |
| 1335 | }; |
| 1336 | let (Some(label_bounds), Some(item)) = ( |
| 1337 | visual.debug_bounds(label_path), |
| 1338 | visual.debug_bounds(item_path), |
| 1339 | ) else { |
| 1340 | if section != SettingsSection::Agents { |
| 1341 | found.push((label_path.to_owned(), "not rendered".to_owned())); |
| 1342 | } |
| 1343 | continue; |
| 1344 | }; |
| 1345 | let text = label(section); |
| 1346 | let text_width = visual.update(|window, _| { |
| 1347 | let run = window.text_style().to_run(text.len()); |
| 1348 | f32::from( |
| 1349 | window |
| 1350 | .text_system() |
| 1351 | .shape_line(text.into(), px(NAV_LABEL_SIZE), &[run], None) |
| 1352 | .width, |
| 1353 | ) |
| 1354 | }); |
| 1355 | let label_width = f32::from(label_bounds.size.width); |
| 1356 | let label_height = f32::from(label_bounds.size.height); |
| 1357 | if text_width > label_width + TOLERANCE |
| 1358 | && label_height < 2. * NAV_LINE_HEIGHT - TOLERANCE |
| 1359 | { |
| 1360 | found.push(( |
| 1361 | label_path.to_owned(), |
| 1362 | format!("{text:?} needs {text_width} px, has {label_width} px on one line: truncated"), |
| 1363 | )); |
| 1364 | } |
| 1365 | let outside = |inner: Bounds<Pixels>, outer: Bounds<Pixels>| { |
| 1366 | inner.left() < outer.left() - px(TOLERANCE) |
| 1367 | || inner.right() > outer.right() + px(TOLERANCE) |
| 1368 | || inner.top() < outer.top() - px(TOLERANCE) |
| 1369 | || inner.bottom() > outer.bottom() + px(TOLERANCE) |
| 1370 | }; |
| 1371 | if outside(label_bounds, item) { |
| 1372 | found.push(( |
| 1373 | label_path.to_owned(), |
| 1374 | format!("label {label_bounds:?} outside item {item:?}"), |
| 1375 | )); |
| 1376 | } |
| 1377 | if item.left() < nav.left() || item.right() > nav.right() { |
| 1378 | found.push(( |
| 1379 | item_path.to_owned(), |
| 1380 | format!("item {item:?} outside nav {nav:?}"), |
| 1381 | )); |
| 1382 | } |
| 1383 | } |
| 1384 | found |
| 1385 | } |
| 1386 | |
| 1387 | fn scrolled_inset_violation(visual: &mut VisualTestContext) -> Option<(String, String)> { |
| 1388 | let body = visual.debug_bounds(SETTINGS_BODY)?; |
| 1389 | let content = visual.debug_bounds(SETTINGS_CONTENT)?; |
| 1390 | if content.bottom() <= body.bottom() { |
| 1391 | return None; |
| 1392 | } |
| 1393 | visual.simulate_event(ScrollWheelEvent { |
| 1394 | position: body.center(), |
| 1395 | delta: ScrollDelta::Pixels(point(px(0.), px(-100_000.))), |
| 1396 | ..Default::default() |
| 1397 | }); |
| 1398 | collect(visual, None); |
| 1399 | let body = visual.debug_bounds(SETTINGS_BODY)?; |
| 1400 | let content = visual.debug_bounds(SETTINGS_CONTENT)?; |
| 1401 | inset_violation(&SCROLLED_INSET_RULE, body, content) |
| 1402 | } |
| 1403 | |
| 1404 | #[gpui_kit::test] |
| 1405 | fn members_popup_lines_reach_the_edges(cx: &mut TestAppContext) { |
| 1406 | let (handle, state) = mount(cx); |
| 1407 | let mut visual = VisualTestContext::from_window(handle, cx); |
| 1408 | for (width, height) in SIZES { |
| 1409 | visual.simulate_resize(size(px(width), px(height))); |
| 1410 | let window = Bounds { |
| 1411 | origin: point(px(0.), px(0.)), |
| 1412 | size: size(px(width), px(height)), |
| 1413 | }; |
| 1414 | visual.update(|_, cx| { |
| 1415 | state.update(cx, |state, cx| { |
| 1416 | state.select(Selection::Chat(MEMBERS_CHAT.to_owned()), cx) |
| 1417 | }) |
| 1418 | }); |
| 1419 | collect(&mut visual, Some(MEMBERS_BUTTON)); |
| 1420 | let button = visual |
| 1421 | .debug_bounds(MEMBERS_BUTTON) |
| 1422 | .expect("members button rendered"); |
| 1423 | visual.simulate_click(button.center(), Default::default()); |
| 1424 | let tags = collect(&mut visual, Some(MEMBERS_TAGS[0])); |
| 1425 | let found: Vec<String> = violations(&tags, window, MEMBERS_TAGS) |
| 1426 | .into_iter() |
| 1427 | .filter(|(key, _)| key.starts_with("chat-members")) |
| 1428 | .map(|(key, message)| format!("members@{width}x{height}:{key}: {message}")) |
| 1429 | .collect(); |
| 1430 | visual.simulate_keystrokes("escape"); |
| 1431 | visual.run_until_parked(); |
| 1432 | assert!(found.is_empty(), "{}", found.join("\n")); |
| 1433 | } |
| 1434 | } |
| 1435 | |
| 1436 | const PINNED_CHAT: &str = "demo-chat-release"; |
| 1437 | const PINNED_CHANNEL: &str = "demo-channel-1-1"; |
| 1438 | |
| 1439 | #[gpui_kit::test] |
| 1440 | fn the_pin_banner_top_gap_matches_its_side_gaps(cx: &mut TestAppContext) { |
| 1441 | let (handle, state) = mount(cx); |
| 1442 | let mut visual = VisualTestContext::from_window(handle, cx); |
| 1443 | let mut found = Vec::new(); |
| 1444 | for (width, height) in SIZES { |
| 1445 | visual.simulate_resize(size(px(width), px(height))); |
| 1446 | visual.update(|_, cx| { |
| 1447 | state.update(cx, |state, cx| { |
| 1448 | state.select(Selection::Chat(PINNED_CHAT.to_owned()), cx) |
| 1449 | }) |
| 1450 | }); |
| 1451 | collect(&mut visual, Some(PIN_BANNER_TAG)); |
| 1452 | let banner = visual.debug_bounds(PIN_BANNER_TAG).expect("pin banner"); |
| 1453 | let timeline = visual.debug_bounds(TIMELINE_TAG).expect("timeline"); |
| 1454 | let divider = visual.debug_bounds(HEADER_DIVIDER).expect("header divider"); |
| 1455 | let gaps = [ |
| 1456 | ("top", f32::from(banner.top() - timeline.top())), |
| 1457 | ("left", f32::from(banner.left() - timeline.left())), |
| 1458 | ("right", f32::from(timeline.right() - banner.right())), |
| 1459 | ("below divider", f32::from(banner.top() - divider.bottom())), |
| 1460 | ]; |
| 1461 | let side = gaps[1].1; |
| 1462 | if gaps |
| 1463 | .iter() |
| 1464 | .any(|(_, gap)| (gap - side).abs() > INSET_TOLERANCE) |
| 1465 | { |
| 1466 | found.push(format!( |
| 1467 | "pin banner@{width}x{height}: gaps {gaps:?} must match" |
| 1468 | )); |
| 1469 | } |
| 1470 | } |
| 1471 | assert!(found.is_empty(), "{}", found.join("\n")); |
| 1472 | } |
| 1473 | |
| 1474 | #[gpui_kit::test] |
| 1475 | fn channels_show_no_pin_banner_like_teams(cx: &mut TestAppContext) { |
| 1476 | let (handle, state) = mount(cx); |
| 1477 | let mut visual = VisualTestContext::from_window(handle, cx); |
| 1478 | visual.simulate_resize(size(px(1280.), px(800.))); |
| 1479 | visual.update(|_, cx| { |
| 1480 | state.update(cx, |state, cx| { |
| 1481 | state.select(Selection::Channel(PINNED_CHANNEL.to_owned()), cx) |
| 1482 | }) |
| 1483 | }); |
| 1484 | collect(&mut visual, Some(TIMELINE_TAG)); |
| 1485 | assert!(visual.debug_bounds(PIN_BANNER_TAG).is_none()); |
| 1486 | } |
| 1487 | |
| 1488 | #[gpui_kit::test] |
| 1489 | fn the_channel_details_pane_lists_pinned_posts_inside_the_main_island(cx: &mut TestAppContext) { |
| 1490 | let (handle, state) = mount(cx); |
| 1491 | let mut visual = VisualTestContext::from_window(handle, cx); |
| 1492 | visual.simulate_resize(size(px(1280.), px(800.))); |
| 1493 | visual.update(|_, cx| { |
| 1494 | state.update(cx, |state, cx| { |
| 1495 | state.select(Selection::Channel(PINNED_CHANNEL.to_owned()), cx) |
| 1496 | }) |
| 1497 | }); |
| 1498 | collect(&mut visual, Some(CHANNEL_DETAILS_BUTTON)); |
| 1499 | let button = visual |
| 1500 | .debug_bounds(CHANNEL_DETAILS_BUTTON) |
| 1501 | .expect("channel details button"); |
| 1502 | visual.simulate_click(button.center(), Default::default()); |
| 1503 | collect(&mut visual, Some(CHANNEL_DETAILS_TAG)); |
| 1504 | let pane = visual |
| 1505 | .debug_bounds(CHANNEL_DETAILS_TAG) |
| 1506 | .expect("details pane"); |
| 1507 | let main = visual.debug_bounds(MAIN).expect("main island"); |
| 1508 | let divider = visual.debug_bounds(HEADER_DIVIDER).expect("header divider"); |
| 1509 | assert!( |
| 1510 | (f32::from(main.right() - pane.right())).abs() < INSET_TOLERANCE, |
| 1511 | "pane {pane:?} must end at the main island edge {main:?}" |
| 1512 | ); |
| 1513 | assert!( |
| 1514 | (f32::from(pane.top() - divider.bottom())).abs() < INSET_TOLERANCE, |
| 1515 | "pane {pane:?} must start under the header divider {divider:?}" |
| 1516 | ); |
| 1517 | } |
| 1518 | |
| 1519 | fn composer_button_insets(visual: &mut VisualTestContext) -> [f32; 6] { |
| 1520 | collect(visual, Some(COMPOSER_SEND_TAG)); |
| 1521 | let mut bounds_of = |path: &'static str| { |
| 1522 | visual |
| 1523 | .debug_bounds(path) |
| 1524 | .unwrap_or_else(|| panic!("{path} rendered")) |
| 1525 | }; |
| 1526 | let composer = bounds_of(COMPOSER_BOX_TAG); |
| 1527 | let leading = bounds_of(COMPOSER_LEADING_TAG); |
| 1528 | let send = bounds_of(COMPOSER_SEND_TAG); |
| 1529 | [ |
| 1530 | f32::from(leading.left() - composer.left()), |
| 1531 | f32::from(leading.top() - composer.top()), |
| 1532 | f32::from(composer.bottom() - leading.bottom()), |
| 1533 | f32::from(composer.right() - send.right()), |
| 1534 | f32::from(send.top() - composer.top()), |
| 1535 | f32::from(composer.bottom() - send.bottom()), |
| 1536 | ] |
| 1537 | } |
| 1538 | |
| 1539 | #[gpui_kit::test] |
| 1540 | fn composer_buttons_keep_equal_insets(cx: &mut TestAppContext) { |
| 1541 | let (handle, _) = mount(cx); |
| 1542 | let mut visual = VisualTestContext::from_window(handle, cx); |
| 1543 | visual.simulate_resize(size(px(1280.), px(800.))); |
| 1544 | let single_line = composer_button_insets(&mut visual); |
| 1545 | let left = single_line[0]; |
| 1546 | assert!( |
| 1547 | single_line |
| 1548 | .iter() |
| 1549 | .all(|inset| (inset - left).abs() < TOLERANCE), |
| 1550 | "single line: insets left, top, bottom of the first button and right, top, bottom of send {single_line:?}" |
| 1551 | ); |
| 1552 | let leading = visual |
| 1553 | .debug_bounds(COMPOSER_LEADING_TAG) |
| 1554 | .expect("leading buttons"); |
| 1555 | let send = visual.debug_bounds(COMPOSER_SEND_TAG).expect("send button"); |
| 1556 | visual.simulate_click( |
| 1557 | point((leading.right() + send.left()) / 2., send.center().y), |
| 1558 | Default::default(), |
| 1559 | ); |
| 1560 | visual.simulate_keystrokes("o n e shift-enter t w o shift-enter t h r e e"); |
| 1561 | let three_lines = composer_button_insets(&mut visual); |
| 1562 | assert!( |
| 1563 | three_lines[1] > single_line[1] + 30., |
| 1564 | "three lines did not grow the composer: {three_lines:?}" |
| 1565 | ); |
| 1566 | for inset in [ |
| 1567 | three_lines[0], |
| 1568 | three_lines[2], |
| 1569 | three_lines[3], |
| 1570 | three_lines[5], |
| 1571 | ] { |
| 1572 | assert!( |
| 1573 | (inset - left).abs() < TOLERANCE, |
| 1574 | "three lines: insets left, top, bottom of the first button and right, top, bottom of send {three_lines:?}, expected {left}" |
| 1575 | ); |
| 1576 | } |
| 1577 | } |
| 1578 | |
| 1579 | #[gpui_kit::test] |
| 1580 | fn the_format_bar_divider_runs_edge_to_edge_inside_the_composer(cx: &mut TestAppContext) { |
| 1581 | const FORMAT_TAGS: [&str; 6] = [ |
| 1582 | COMPOSER_BOX_TAG, |
| 1583 | COMPOSER_FORMAT_BAR_TAG, |
| 1584 | COMPOSER_FORMAT_DIVIDER_TAG, |
| 1585 | COMPOSER_LEADING_TAG, |
| 1586 | COMPOSER_FORMAT_TAG, |
| 1587 | COMPOSER_SEND_TAG, |
| 1588 | ]; |
| 1589 | let (handle, _) = mount(cx); |
| 1590 | let mut visual = VisualTestContext::from_window(handle, cx); |
| 1591 | for (width, height) in SIZES { |
| 1592 | visual.simulate_resize(size(px(width), px(height))); |
| 1593 | let window = Bounds { |
| 1594 | origin: point(px(0.), px(0.)), |
| 1595 | size: size(px(width), px(height)), |
| 1596 | }; |
| 1597 | collect(&mut visual, Some(COMPOSER_FORMAT_TAG)); |
| 1598 | let toggle = visual |
| 1599 | .debug_bounds(COMPOSER_FORMAT_TAG) |
| 1600 | .expect("format button"); |
| 1601 | visual.simulate_click(toggle.center(), Default::default()); |
| 1602 | collect(&mut visual, Some(COMPOSER_FORMAT_DIVIDER_TAG)); |
| 1603 | let tags: Vec<Tag> = FORMAT_TAGS |
| 1604 | .iter() |
| 1605 | .filter_map(|path| visual.debug_bounds(path).map(|bounds| (*path, bounds))) |
| 1606 | .collect(); |
| 1607 | let found: Vec<String> = violations(&tags, window, &FORMAT_TAGS) |
| 1608 | .into_iter() |
| 1609 | .map(|(key, message)| format!("format@{width}x{height}:{key}: {message}")) |
| 1610 | .collect(); |
| 1611 | let bar = visual.debug_bounds(COMPOSER_FORMAT_BAR_TAG).expect("bar"); |
| 1612 | let leading = visual.debug_bounds(COMPOSER_LEADING_TAG).expect("leading"); |
| 1613 | let toggle = visual |
| 1614 | .debug_bounds(COMPOSER_FORMAT_TAG) |
| 1615 | .expect("format button"); |
| 1616 | visual.simulate_click(toggle.center(), Default::default()); |
| 1617 | collect(&mut visual, None); |
| 1618 | assert!(found.is_empty(), "{}", found.join("\n")); |
| 1619 | assert!( |
| 1620 | bar.bottom() <= leading.top(), |
| 1621 | "bar {bar:?} over the buttons {leading:?}" |
| 1622 | ); |
| 1623 | assert!(visual.debug_bounds(COMPOSER_FORMAT_BAR_TAG).is_none()); |
| 1624 | } |
| 1625 | } |
| 1626 | |
| 1627 | #[gpui_kit::test] |
| 1628 | fn delivery_options_label_and_banner_line_up_with_the_composer(cx: &mut TestAppContext) { |
| 1629 | const DELIVERY_TAGS: [&str; 5] = [ |
| 1630 | COMPOSER_BOX_TAG, |
| 1631 | COMPOSER_LEADING_TAG, |
| 1632 | COMPOSER_DELIVERY_TAG, |
| 1633 | COMPOSER_IMPORTANCE_LABEL_TAG, |
| 1634 | COMPOSER_URGENT_BANNER_TAG, |
| 1635 | ]; |
| 1636 | const URGENT_OPTION: &str = "urgent-delivery-option"; |
| 1637 | const BUTTON_ICON_INSET: f32 = 6.; |
| 1638 | let (handle, _) = mount(cx); |
| 1639 | let mut visual = VisualTestContext::from_window(handle, cx); |
| 1640 | for (width, height) in SIZES { |
| 1641 | visual.simulate_resize(size(px(width), px(height))); |
| 1642 | let window = Bounds { |
| 1643 | origin: point(px(0.), px(0.)), |
| 1644 | size: size(px(width), px(height)), |
| 1645 | }; |
| 1646 | collect(&mut visual, Some(COMPOSER_DELIVERY_TAG)); |
| 1647 | let button = visual |
| 1648 | .debug_bounds(COMPOSER_DELIVERY_TAG) |
| 1649 | .expect("delivery button"); |
| 1650 | visual.simulate_click(button.center(), Default::default()); |
| 1651 | collect(&mut visual, Some(URGENT_OPTION)); |
| 1652 | let menu = visual.debug_bounds(DELIVERY_MENU_TAG).expect("menu"); |
| 1653 | assert!( |
| 1654 | contains(window, menu), |
| 1655 | "menu {} leaves the window", |
| 1656 | describe(menu) |
| 1657 | ); |
| 1658 | let urgent = visual.debug_bounds(URGENT_OPTION).expect("urgent option"); |
| 1659 | visual.simulate_click(urgent.center(), Default::default()); |
| 1660 | collect(&mut visual, Some(COMPOSER_URGENT_BANNER_TAG)); |
| 1661 | assert!(visual.debug_bounds(DELIVERY_MENU_TAG).is_none()); |
| 1662 | let tags: Vec<Tag> = DELIVERY_TAGS |
| 1663 | .iter() |
| 1664 | .filter_map(|path| visual.debug_bounds(path).map(|bounds| (*path, bounds))) |
| 1665 | .collect(); |
| 1666 | let found: Vec<String> = violations(&tags, window, &DELIVERY_TAGS) |
| 1667 | .into_iter() |
| 1668 | .map(|(key, message)| format!("delivery@{width}x{height}:{key}: {message}")) |
| 1669 | .collect(); |
| 1670 | assert!(found.is_empty(), "{}", found.join("\n")); |
| 1671 | let composer = visual.debug_bounds(COMPOSER_BOX_TAG).expect("composer"); |
| 1672 | let banner = visual |
| 1673 | .debug_bounds(COMPOSER_URGENT_BANNER_TAG) |
| 1674 | .expect("banner"); |
| 1675 | let label = visual |
| 1676 | .debug_bounds(COMPOSER_IMPORTANCE_LABEL_TAG) |
| 1677 | .expect("label"); |
| 1678 | let leading = visual.debug_bounds(COMPOSER_LEADING_TAG).expect("leading"); |
| 1679 | assert!( |
| 1680 | (f32::from(banner.left() - composer.left())).abs() <= TOLERANCE |
| 1681 | && (f32::from(banner.right() - composer.right())).abs() <= TOLERANCE, |
| 1682 | "banner {} not on the composer edges {}", |
| 1683 | describe(banner), |
| 1684 | describe(composer) |
| 1685 | ); |
| 1686 | assert!( |
| 1687 | banner.bottom() <= composer.top(), |
| 1688 | "banner {} over the composer {}", |
| 1689 | describe(banner), |
| 1690 | describe(composer) |
| 1691 | ); |
| 1692 | assert!( |
| 1693 | (inset(composer, label, Edge::Left) |
| 1694 | - inset(composer, leading, Edge::Left) |
| 1695 | - BUTTON_ICON_INSET) |
| 1696 | .abs() |
| 1697 | <= TOLERANCE, |
| 1698 | "label {} not on the icon edge of {}", |
| 1699 | describe(label), |
| 1700 | describe(leading) |
| 1701 | ); |
| 1702 | let close = visual |
| 1703 | .debug_bounds(COMPOSER_URGENT_BANNER_TAG) |
| 1704 | .map(|banner| point(banner.right() - px(18.), banner.center().y)) |
| 1705 | .expect("banner"); |
| 1706 | visual.simulate_click(close, Default::default()); |
| 1707 | collect(&mut visual, None); |
| 1708 | assert!(visual.debug_bounds(COMPOSER_URGENT_BANNER_TAG).is_none()); |
| 1709 | assert!(visual.debug_bounds(COMPOSER_IMPORTANCE_LABEL_TAG).is_none()); |
| 1710 | } |
| 1711 | } |
| 1712 | |
| 1713 | #[gpui_kit::test] |
| 1714 | fn importance_label_sits_inside_its_bubble_on_the_text_edge(cx: &mut TestAppContext) { |
| 1715 | let (handle, state) = mount_with(cx, true); |
| 1716 | let mut visual = VisualTestContext::from_window(handle, cx); |
| 1717 | for (width, height) in SIZES { |
| 1718 | visual.simulate_resize(size(px(width), px(height))); |
| 1719 | for chat in IMPORTANCE_CHATS { |
| 1720 | visual.update(|_, cx| { |
| 1721 | state.update(cx, |state, cx| { |
| 1722 | state.select(Selection::Chat(chat.to_owned()), cx) |
| 1723 | }) |
| 1724 | }); |
| 1725 | collect(&mut visual, Some(TIMELINE_TAG)); |
| 1726 | let timeline = visual.debug_bounds(TIMELINE_TAG).expect("timeline"); |
| 1727 | visual.simulate_event(ScrollWheelEvent { |
| 1728 | position: timeline.center(), |
| 1729 | delta: ScrollDelta::Pixels(point(px(0.), px(-100_000.))), |
| 1730 | ..Default::default() |
| 1731 | }); |
| 1732 | collect(&mut visual, Some(IMPORTANCE_LABEL_TAG)); |
| 1733 | let label = visual.debug_bounds(IMPORTANCE_LABEL_TAG).expect("label"); |
| 1734 | let bubble = BUBBLE_TAGS |
| 1735 | .iter() |
| 1736 | .filter_map(|tag| visual.debug_bounds(tag)) |
| 1737 | .find(|bubble| contains(*bubble, label)) |
| 1738 | .unwrap_or_else(|| { |
| 1739 | panic!("{chat}@{width}x{height}: label {label:?} outside every bubble") |
| 1740 | }); |
| 1741 | let left = inset(bubble, label, Edge::Left); |
| 1742 | let top = inset(bubble, label, Edge::Top); |
| 1743 | assert!( |
| 1744 | (left - IMPORTANCE_LABEL_INSET_X).abs() <= TOLERANCE |
| 1745 | && (top - IMPORTANCE_LABEL_INSET_TOP).abs() <= TOLERANCE, |
| 1746 | "{chat}@{width}x{height}: label inset left {left} top {top} in {}", |
| 1747 | describe(bubble) |
| 1748 | ); |
| 1749 | } |
| 1750 | } |
| 1751 | } |
| 1752 | |
| 1753 | #[gpui_kit::test] |
| 1754 | fn agent_proposals_stay_inside_the_main_island(cx: &mut TestAppContext) { |
| 1755 | let (handle, state) = mount_with(cx, true); |
| 1756 | let mut visual = VisualTestContext::from_window(handle, cx); |
| 1757 | for (width, height) in SIZES { |
| 1758 | visual.simulate_resize(size(px(width), px(height))); |
| 1759 | let window = Bounds { |
| 1760 | origin: point(px(0.), px(0.)), |
| 1761 | size: size(px(width), px(height)), |
| 1762 | }; |
| 1763 | for (chat, expected_tags) in [ |
| 1764 | (PROPOSAL_CHAT, &AGENT_TAGS[..3]), |
| 1765 | (REACTION_CHAT, &AGENT_TAGS[3..]), |
| 1766 | ] { |
| 1767 | visual.update(|_, cx| { |
| 1768 | state.update(cx, |state, cx| { |
| 1769 | state.select(Selection::Chat(chat.to_owned()), cx) |
| 1770 | }) |
| 1771 | }); |
| 1772 | let tags = collect(&mut visual, Some(expected_tags[0])); |
| 1773 | let mut expected = GUARDED.to_vec(); |
| 1774 | expected.extend(expected_tags); |
| 1775 | let found: Vec<String> = violations(&tags, window, &expected) |
| 1776 | .into_iter() |
| 1777 | .map(|(key, message)| format!("{chat}@{width}x{height}:{key}: {message}")) |
| 1778 | .collect(); |
| 1779 | assert!(found.is_empty(), "{}", found.join("\n")); |
| 1780 | } |
| 1781 | } |
| 1782 | } |
| 1783 | } |
| 1784 | |
| 1785 | #[test] |
| 1786 | fn lines_are_solid() { |
| 1787 | let mut files = Vec::new(); |
| 1788 | for root in source_roots() { |
| 1789 | source_files(&root, &mut files); |
| 1790 | } |
| 1791 | let violations: Vec<String> = files |
| 1792 | .iter() |
| 1793 | .filter(|file| !file.ends_with("layout_guard.rs")) |
| 1794 | .flat_map(|file| { |
| 1795 | let source = fs::read_to_string(file).expect("read source file"); |
| 1796 | source |
| 1797 | .lines() |
| 1798 | .enumerate() |
| 1799 | .filter(|(_, line)| { |
| 1800 | line.contains(".border_dashed(") || line.contains(".border_dotted(") |
| 1801 | }) |
| 1802 | .map(|(index, line)| { |
| 1803 | format!( |
| 1804 | "{}:{}: lines are always solid: {}", |
| 1805 | file.display(), |
| 1806 | index + 1, |
| 1807 | line.trim() |
| 1808 | ) |
| 1809 | }) |
| 1810 | .collect::<Vec<_>>() |
| 1811 | }) |
| 1812 | .collect(); |
| 1813 | assert!(violations.is_empty(), "{}", violations.join("\n")); |
| 1814 | } |
| 1815 | |
| 1816 | const STATE_RING_COLORS: &[&str] = &[ |
| 1817 | "if focused", |
| 1818 | "if selected", |
| 1819 | "if speaking", |
| 1820 | "if current", |
| 1821 | "if open", |
| 1822 | "if state.failed", |
| 1823 | ]; |
| 1824 | const OUTLINE_ALLOWLIST: &[(&str, &str, &str)] = &[ |
| 1825 | ( |
| 1826 | "notify/ring_view.rs", |
| 1827 | "theme::green()", |
| 1828 | "state signal (pulsing ring around the caller while it rings), not a resting outline", |
| 1829 | ), |
| 1830 | ( |
| 1831 | "notify/meeting_view.rs", |
| 1832 | "theme::border_strong()", |
| 1833 | "outlined button, not a surface", |
| 1834 | ), |
| 1835 | ( |
| 1836 | "settings/mcp.rs", |
| 1837 | "theme::border_strong()", |
| 1838 | "outlined button, not a surface", |
| 1839 | ), |
| 1840 | ( |
| 1841 | "settings/rows.rs", |
| 1842 | "theme::border_strong()", |
| 1843 | "outlined button, not a surface", |
| 1844 | ), |
| 1845 | ( |
| 1846 | "views/card_widgets.rs", |
| 1847 | "border)", |
| 1848 | "message content drawn as Teams renders it", |
| 1849 | ), |
| 1850 | ( |
| 1851 | "views/card_widgets.rs", |
| 1852 | "theme::border_strong()", |
| 1853 | "message content drawn as Teams renders it", |
| 1854 | ), |
| 1855 | ("views/shared_tab.rs", "theme::accent()", "input field"), |
| 1856 | ("views/card_input.rs", "if error.is_some()", "input field"), |
| 1857 | ("views/format_toolbar.rs", "if refused", "input field"), |
| 1858 | ( |
| 1859 | "views/card_code_block.rs", |
| 1860 | "theme::code_header_border()", |
| 1861 | "message content drawn as Teams renders it", |
| 1862 | ), |
| 1863 | ("views/call_transcript.rs", "theme::border()", "input field"), |
| 1864 | ( |
| 1865 | "views/conversation.rs", |
| 1866 | "theme::accent()", |
| 1867 | "state signal (speaking, sharing, drag, drop, failed), not a resting outline", |
| 1868 | ), |
| 1869 | ( |
| 1870 | "views/attachment_tray.rs", |
| 1871 | "theme::accent()", |
| 1872 | "state signal (speaking, sharing, drag, drop, failed), not a resting outline", |
| 1873 | ), |
| 1874 | ( |
| 1875 | "views/card_carousel.rs", |
| 1876 | "theme::border_strong()", |
| 1877 | "message content drawn as Teams renders it", |
| 1878 | ), |
| 1879 | ( |
| 1880 | "views/card_table.rs", |
| 1881 | "grid_color", |
| 1882 | "message content drawn as Teams renders it", |
| 1883 | ), |
| 1884 | ( |
| 1885 | "views/activity_panel.rs", |
| 1886 | "theme::surface()", |
| 1887 | "cutout ring in the background color, invisible against its surface", |
| 1888 | ), |
| 1889 | ( |
| 1890 | "views/avatar.rs", |
| 1891 | "theme::surface()", |
| 1892 | "cutout ring in the background color, invisible against its surface", |
| 1893 | ), |
| 1894 | ( |
| 1895 | "views/avatar.rs", |
| 1896 | "theme::text_muted()", |
| 1897 | "offline presence glyph, Teams icon", |
| 1898 | ), |
| 1899 | ( |
| 1900 | "views/message_row.rs", |
| 1901 | "theme::border_strong()", |
| 1902 | "deleted message placeholder, Teams look", |
| 1903 | ), |
| 1904 | ( |
| 1905 | "views/message_row.rs", |
| 1906 | "theme::red()", |
| 1907 | "state signal (speaking, sharing, drag, drop, failed), not a resting outline", |
| 1908 | ), |
| 1909 | ( |
| 1910 | "views/message_row.rs", |
| 1911 | "if row.delivery", |
| 1912 | "state signal (speaking, sharing, drag, drop, failed), not a resting outline", |
| 1913 | ), |
| 1914 | ( |
| 1915 | "views/new_meeting_dialog.rs", |
| 1916 | "theme::border()", |
| 1917 | "input field", |
| 1918 | ), |
| 1919 | ( |
| 1920 | "views/call_mini_window.rs", |
| 1921 | "theme::border_strong()", |
| 1922 | "self view over live video, needs an edge on any picture", |
| 1923 | ), |
| 1924 | ("views/transfer_picker.rs", "theme::border()", "input field"), |
| 1925 | ( |
| 1926 | "views/call_notice.rs", |
| 1927 | "theme::border_strong()", |
| 1928 | "outlined button, not a surface", |
| 1929 | ), |
| 1930 | ( |
| 1931 | "views/composer/schedule.rs", |
| 1932 | "theme::accent()", |
| 1933 | "input field", |
| 1934 | ), |
| 1935 | ( |
| 1936 | "views/call_mini.rs", |
| 1937 | "border)", |
| 1938 | "state signal (speaking, sharing, drag, drop, failed), not a resting outline", |
| 1939 | ), |
| 1940 | ( |
| 1941 | "views/sidebar.rs", |
| 1942 | "theme::accent()", |
| 1943 | "state signal (speaking, sharing, drag, drop, failed), not a resting outline", |
| 1944 | ), |
| 1945 | ( |
| 1946 | "views/adaptive_card.rs", |
| 1947 | "theme::border()", |
| 1948 | "message content drawn as Teams renders it", |
| 1949 | ), |
| 1950 | ( |
| 1951 | "views/adaptive_card.rs", |
| 1952 | "border)", |
| 1953 | "message content drawn as Teams renders it", |
| 1954 | ), |
| 1955 | ( |
| 1956 | "views/share_bar.rs", |
| 1957 | "theme::red()", |
| 1958 | "state signal (speaking, sharing, drag, drop, failed), not a resting outline", |
| 1959 | ), |
| 1960 | ("settings_controls.rs", "if invalid", "input field"), |
| 1961 | ]; |
| 1962 | |
| 1963 | fn outlines(source: &str) -> Vec<(usize, String)> { |
| 1964 | let pattern = Regex::new(r"\.border(_1\(\)|_2\(\)|\(px)").expect("pattern"); |
| 1965 | pattern |
| 1966 | .find_iter(source) |
| 1967 | .map(|found| { |
| 1968 | let line_number = source[..found.start()].matches('\n').count() + 1; |
| 1969 | let after = &source[found.end()..]; |
| 1970 | let color = after |
| 1971 | .find(".border_color(") |
| 1972 | .filter(|position| *position < 120) |
| 1973 | .map(|position| { |
| 1974 | let start = &after[position + ".border_color(".len()..]; |
| 1975 | start |
| 1976 | .lines() |
| 1977 | .map(str::trim) |
| 1978 | .find(|line| !line.is_empty()) |
| 1979 | .unwrap_or_default() |
| 1980 | .to_owned() |
| 1981 | }) |
| 1982 | .unwrap_or_default(); |
| 1983 | (line_number, color) |
| 1984 | }) |
| 1985 | .collect() |
| 1986 | } |
| 1987 | |
| 1988 | #[test] |
| 1989 | fn outline_matcher_reads_the_border_color() { |
| 1990 | let source = "div()\n .border_1()\n .border_color(theme::amber())\n .border_t_1()\n"; |
| 1991 | assert_eq!(outlines(source), [(2, "theme::amber())".to_owned())]); |
| 1992 | } |
| 1993 | |
| 1994 | #[test] |
| 1995 | fn surfaces_are_soft() { |
| 1996 | let mut matched_allowlist = HashSet::new(); |
| 1997 | let mut violations = Vec::new(); |
| 1998 | for root in source_roots() { |
| 1999 | let mut files = Vec::new(); |
| 2000 | source_files(&root, &mut files); |
| 2001 | for file in files { |
| 2002 | let relative = file |
| 2003 | .strip_prefix(&root) |
| 2004 | .expect("inside src") |
| 2005 | .to_string_lossy() |
| 2006 | .replace('\\', "/"); |
| 2007 | if relative == "layout_guard.rs" { |
| 2008 | continue; |
| 2009 | } |
| 2010 | let source = fs::read_to_string(&file).expect("read source file"); |
| 2011 | for (line_number, color) in outlines(&source) { |
| 2012 | if STATE_RING_COLORS |
| 2013 | .iter() |
| 2014 | .any(|prefix| color.starts_with(prefix)) |
| 2015 | { |
| 2016 | continue; |
| 2017 | } |
| 2018 | match OUTLINE_ALLOWLIST.iter().position(|(allowed_file, prefix, _)| { |
| 2019 | *allowed_file == relative && color.starts_with(prefix) |
| 2020 | }) { |
| 2021 | Some(position) => { |
| 2022 | matched_allowlist.insert(position); |
| 2023 | } |
| 2024 | None => violations.push(format!( |
| 2025 | "{relative}:{line_number}: hard outline on a surface, use a soft fill or shadow (border_color {color})" |
| 2026 | )), |
| 2027 | } |
| 2028 | } |
| 2029 | } |
| 2030 | } |
| 2031 | let stale = OUTLINE_ALLOWLIST |
| 2032 | .iter() |
| 2033 | .enumerate() |
| 2034 | .filter(|(position, _)| !matched_allowlist.contains(position)) |
| 2035 | .map(|(_, (file, prefix, _))| { |
| 2036 | format!("stale outline allowlist entry, remove it: {file} {prefix}") |
| 2037 | }); |
| 2038 | let report: Vec<String> = violations.into_iter().chain(stale).collect(); |
| 2039 | assert!(report.is_empty(), "{}", report.join("\n")); |
| 2040 | } |
| 2041 | |
| 2042 | fn indentation(line: &str) -> usize { |
| 2043 | line.len() - line.trim_start().len() |
| 2044 | } |
| 2045 | |
| 2046 | fn chain_lines<'a>(lines: &[&'a str], index: usize) -> Vec<&'a str> { |
| 2047 | let indent = indentation(lines[index]); |
| 2048 | let belongs = |line: &&str| line.trim().is_empty() || indentation(line) >= indent; |
| 2049 | let start = lines[..index] |
| 2050 | .iter() |
| 2051 | .rposition(|line| !belongs(line)) |
| 2052 | .map_or(0, |position| position + 1); |
| 2053 | let end = lines[index..] |
| 2054 | .iter() |
| 2055 | .position(|line| !belongs(line)) |
| 2056 | .map_or(lines.len(), |position| index + position); |
| 2057 | lines[start..end] |
| 2058 | .iter() |
| 2059 | .filter(|line| indentation(line) == indent) |
| 2060 | .copied() |
| 2061 | .collect() |
| 2062 | } |
| 2063 | |
| 2064 | fn shrinking_rows(row_height: &Regex, source: &str) -> Vec<usize> { |
| 2065 | let lines: Vec<&str> = source.lines().collect(); |
| 2066 | (0..lines.len()) |
| 2067 | .filter(|&index| row_height.is_match(lines[index])) |
| 2068 | .filter(|&index| { |
| 2069 | !chain_lines(&lines, index).iter().any(|line| { |
| 2070 | let line = line.trim(); |
| 2071 | line.starts_with(".flex_none()") || line.starts_with(".flex_shrink_0()") |
| 2072 | }) |
| 2073 | }) |
| 2074 | .map(|index| index + 1) |
| 2075 | .collect() |
| 2076 | } |
| 2077 | |
| 2078 | fn row_height_pattern() -> Regex { |
| 2079 | Regex::new(r"^\s*\.h\(px\(((\w+_)?(ROW|ENTRY)_HEIGHT|row_height)\)\)").expect("pattern") |
| 2080 | } |
| 2081 | |
| 2082 | #[test] |
| 2083 | fn matcher_finds_rows_without_flex_none() { |
| 2084 | 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"; |
| 2085 | assert_eq!(shrinking_rows(&row_height_pattern(), source), [3]); |
| 2086 | } |
| 2087 | |
| 2088 | #[test] |
| 2089 | fn fixed_height_rows_never_shrink() { |
| 2090 | let row_height = row_height_pattern(); |
| 2091 | let mut files = Vec::new(); |
| 2092 | for root in source_roots() { |
| 2093 | source_files(&root, &mut files); |
| 2094 | } |
| 2095 | let violations: Vec<String> = files |
| 2096 | .iter() |
| 2097 | .filter(|file| !file.ends_with("layout_guard.rs")) |
| 2098 | .flat_map(|file| { |
| 2099 | let source = fs::read_to_string(file).expect("read source file"); |
| 2100 | shrinking_rows(&row_height, &source) |
| 2101 | .into_iter() |
| 2102 | .map(|line| format!("{}:{line}", file.display())) |
| 2103 | .collect::<Vec<_>>() |
| 2104 | }) |
| 2105 | .collect(); |
| 2106 | assert!( |
| 2107 | violations.is_empty(), |
| 2108 | "a fixed-height row needs .flex_none(), or a max_h scroll list shrinks it to its content: {violations:?}" |
| 2109 | ); |
| 2110 | } |