| 1 | use gpui::{ |
| 2 | Axis, Div, InteractiveElement as _, Length, ParentElement as _, Pixels, Stateful, |
| 3 | StatefulInteractiveElement as _, Styled as _, blue, green, prelude::FluentBuilder as _, px, |
| 4 | red, |
| 5 | }; |
| 6 | use gpui_component::{ |
| 7 | AxisExt as _, FocusTrapElement, InteractiveElementExt, LengthExt as _, Placement, Side, |
| 8 | animation, |
| 9 | }; |
| 10 | |
| 11 | #[test] |
| 12 | fn legacy_foundation_exports_remain_available() { |
| 13 | assert!(Axis::Horizontal.is_horizontal()); |
| 14 | assert!(Axis::Vertical.is_vertical()); |
| 15 | assert_eq!(Placement::Top.axis(), Axis::Vertical); |
| 16 | assert!(Side::Left.is_left()); |
| 17 | |
| 18 | let length = Length::Definite(px(12.).into()); |
| 19 | let pixels: Option<Pixels> = length.to_pixels(px(16.).into(), px(16.)); |
| 20 | assert_eq!(pixels, Some(px(12.))); |
| 21 | |
| 22 | assert_eq!(animation::ease_in_cubic(0.5), 0.125); |
| 23 | |
| 24 | fn requires_interaction_extensions<T: FocusTrapElement + InteractiveElementExt>() {} |
| 25 | requires_interaction_extensions::<Stateful<Div>>(); |
| 26 | } |
| 27 | |
| 28 | #[test] |
| 29 | fn focus_ring_api_exposes_component_state_without_visual_options() { |
| 30 | use gpui_component::FocusableExt as _; |
| 31 | |
| 32 | let button = gpui_component::button::Button::new("focus-ring-state").focus_ring(false); |
| 33 | assert!(!button.is_focus_ring_enabled()); |
| 34 | } |
| 35 | |
| 36 | #[test] |
| 37 | fn base_crate_exports_the_same_foundation_types() { |
| 38 | let legacy = gpui_component::Edges::all(1_u8); |
| 39 | let base: gpui_base::Edges<u8> = legacy; |
| 40 | |
| 41 | assert_eq!(base.top, 1); |
| 42 | assert_eq!(base.right, 1); |
| 43 | assert_eq!(base.bottom, 1); |
| 44 | assert_eq!(base.left, 1); |
| 45 | } |
| 46 | |
| 47 | #[test] |
| 48 | fn motion_core_types_are_available_from_the_base_facade() { |
| 49 | let timing = gpui_base::Timing::new(std::time::Duration::from_millis(100)) |
| 50 | .ease(gpui_base::Easing::Linear); |
| 51 | assert_eq!( |
| 52 | timing |
| 53 | .sample(std::time::Duration::from_millis(50)) |
| 54 | .directed_progress, |
| 55 | 0.5 |
| 56 | ); |
| 57 | } |
| 58 | |
| 59 | #[test] |
| 60 | fn base_avatar_uses_application_owned_image_and_fallback_slots() { |
| 61 | let _ = gpui_base::Avatar::new() |
| 62 | .image(gpui_base::AvatarImage::new(gpui::ImageSource::from( |
| 63 | "avatar.png", |
| 64 | ))) |
| 65 | .fallback(gpui_base::AvatarFallback::new().child("JL")); |
| 66 | } |
| 67 | |
| 68 | #[test] |
| 69 | fn base_sheet_accepts_application_owned_overlay_and_surface() { |
| 70 | fn build(cx: &mut gpui::App) { |
| 71 | let _ = gpui_base::Sheet::new(cx) |
| 72 | .overlay(gpui::div()) |
| 73 | .surface(gpui::div()) |
| 74 | .overlay_closable(false) |
| 75 | .on_close(|_, _, _| {}); |
| 76 | } |
| 77 | |
| 78 | let _ = build; |
| 79 | } |
| 80 | |
| 81 | #[test] |
| 82 | fn base_dialog_owns_modal_actions_and_alert_defaults() { |
| 83 | fn dialog(cx: &mut gpui::App) { |
| 84 | let handle = gpui_base::DialogHandle::new(true); |
| 85 | let _ = gpui_base::Dialog::new(cx) |
| 86 | .handle(handle.clone()) |
| 87 | .on_open_change(|_, _, _, _| {}) |
| 88 | .backdrop(gpui_base::DialogBackdrop::new()) |
| 89 | .popup(gpui_base::DialogPopup::new()) |
| 90 | .on_ok(|_, _, _| true) |
| 91 | .on_cancel(|_, _, _| true) |
| 92 | .on_close(|_, _, _| {}); |
| 93 | let _ = gpui_base::AlertDialog::new(cx) |
| 94 | .handle(handle) |
| 95 | .backdrop(gpui_base::AlertDialogBackdrop::new()) |
| 96 | .popup(gpui_base::AlertDialogPopup::new()); |
| 97 | } |
| 98 | |
| 99 | let _ = dialog; |
| 100 | let _ = gpui_base::DialogTitle::new().child("Title"); |
| 101 | let _ = gpui_base::DialogDescription::new().child("Description"); |
| 102 | let _ = gpui_base::DialogClose::new().child(gpui::div()); |
| 103 | let _ = gpui_base::AlertDialogTrigger::new(gpui::div()); |
| 104 | let _ = gpui_base::AlertDialogTitle::new().child("Title"); |
| 105 | let _ = gpui_base::AlertDialogDescription::new().child("Description"); |
| 106 | let _ = gpui_base::AlertDialogAction::new().child(gpui::div()); |
| 107 | let _ = gpui_base::AlertDialogCancel::new().child(gpui::div()); |
| 108 | let _: gpui_base::actions::Cancel = gpui_component::dialog::Cancel; |
| 109 | let _: gpui_base::actions::Confirm = gpui_component::dialog::Confirm { secondary: false }; |
| 110 | } |
| 111 | |
| 112 | #[test] |
| 113 | fn base_button_accepts_application_owned_state_styles() { |
| 114 | let _button = gpui_base::Button::new("save") |
| 115 | .accessibility_label("Save") |
| 116 | .disabled(false) |
| 117 | .on_click(|_, _, _| {}) |
| 118 | .child("Save") |
| 119 | .bg(red()) |
| 120 | .hover(|style| style.bg(green())) |
| 121 | .active(|style| style.bg(blue())) |
| 122 | .focus_visible(|style| style.border_1()); |
| 123 | } |
| 124 | |
| 125 | #[test] |
| 126 | fn base_controls_expose_typed_semantic_style_contexts() { |
| 127 | let _ = gpui_base::Button::new("button") |
| 128 | .styles(|styles| styles.disabled(|style| style.opacity(0.5))); |
| 129 | let _ = gpui_base::Checkbox::new("checkbox").styles(|styles| { |
| 130 | styles |
| 131 | .checked(|style| style.bg(green())) |
| 132 | .indeterminate(|style| style.bg(blue())) |
| 133 | .disabled(|style| style.when(true, |style| style.opacity(0.5))) |
| 134 | }); |
| 135 | let _ = |
| 136 | gpui_base::Radio::new("radio").styles(|styles| styles.checked(|style| style.bg(green()))); |
| 137 | let _ = |
| 138 | gpui_base::Switch::new("switch").styles(|styles| styles.checked(|style| style.bg(green()))); |
| 139 | let _ = |
| 140 | gpui_base::Toggle::new("toggle").styles(|styles| styles.pressed(|style| style.bg(green()))); |
| 141 | let _ = |
| 142 | gpui_base::Link::new("link").styles(|styles| styles.disabled(|style| style.opacity(0.5))); |
| 143 | let _ = gpui_base::Tab::new("tab").styles(|styles| { |
| 144 | styles |
| 145 | .selected(|style| style.bg(green())) |
| 146 | .disabled(|style| style.opacity(0.5)) |
| 147 | }); |
| 148 | let _ = gpui_base::Tabs::new("tabs").child( |
| 149 | gpui_base::Tab::new("first") |
| 150 | .selected(true) |
| 151 | .on_click(|_, _, _| {}), |
| 152 | ); |
| 153 | } |
| 154 | |
| 155 | #[test] |
| 156 | fn base_collapsible_uses_normal_child_and_content_slots() { |
| 157 | let _ = gpui_base::Collapsible::new() |
| 158 | .open(true) |
| 159 | .child("Trigger") |
| 160 | .content("Content"); |
| 161 | let _ = gpui_component::collapsible::Collapsible::new() |
| 162 | .open(true) |
| 163 | .child("Trigger") |
| 164 | .content("Content"); |
| 165 | } |
| 166 | |
| 167 | #[test] |
| 168 | fn base_accordion_uses_normal_parts_and_children() { |
| 169 | let trigger = gpui_base::AccordionTrigger::new("trigger") |
| 170 | .open(true) |
| 171 | .disabled(false) |
| 172 | .child("Title"); |
| 173 | let header = gpui_base::AccordionHeader::new(trigger).level(3); |
| 174 | let panel = gpui_base::AccordionPanel::new().open(true).child("Content"); |
| 175 | let item = gpui_base::AccordionItem::new() |
| 176 | .open(true) |
| 177 | .header(header) |
| 178 | .panel(panel); |
| 179 | let _ = gpui_base::Accordion::new("accordion").child(item); |
| 180 | } |
| 181 | |
| 182 | #[test] |
| 183 | fn legacy_behavior_traits_are_the_base_traits() { |
| 184 | fn selectable<T: gpui_base::Selectable>(value: T) -> T { |
| 185 | value |
| 186 | } |
| 187 | fn disableable<T: gpui_base::Disableable>(value: T) -> T { |
| 188 | value |
| 189 | } |
| 190 | |
| 191 | let _ = selectable(gpui_component::button::Button::new("selectable")); |
| 192 | let _ = disableable(gpui_component::button::Button::new("disableable")); |
| 193 | } |
| 194 | |
| 195 | #[test] |
| 196 | fn legacy_measure_type_is_the_base_type() { |
| 197 | fn accepts_base(_: gpui_base::Measure) {} |
| 198 | accepts_base(gpui_component::Measure::new("compat")); |
| 199 | } |
| 200 | |
| 201 | #[test] |
| 202 | fn base_progress_accepts_application_owned_indicator_content() { |
| 203 | let _ = gpui_base::Progress::new("progress") |
| 204 | .value(42.) |
| 205 | .child(gpui_base::ProgressTrack::new()) |
| 206 | .child(gpui_base::ProgressIndicator::new().child(gpui::div())); |
| 207 | } |
| 208 | |
| 209 | #[test] |
| 210 | fn base_table_and_toast_accept_application_owned_composition() { |
| 211 | let row = gpui_base::TableRow::new("row", 1) |
| 212 | .child(gpui_base::TableHead::new("head", 1).child("Name")) |
| 213 | .child(gpui_base::TableCell::new("cell", 1).child("Ada")); |
| 214 | let _ = gpui_base::Table::new("table") |
| 215 | .child(gpui_base::TableHeader::new("header").child(row)) |
| 216 | .child(gpui_base::TableBody::new("body")) |
| 217 | .child(gpui_base::TableCaption::new("caption").child("People")); |
| 218 | |
| 219 | let _ = gpui_base::ToastStack::new("toasts", gpui_base::ToastStackState::default()) |
| 220 | .child(gpui_base::Toast::new("toast").child("Saved")); |
| 221 | } |
| 222 | |
| 223 | #[test] |
| 224 | fn legacy_tree_models_are_base_types() { |
| 225 | fn accepts_base(_: gpui_base::TreeItem) {} |
| 226 | accepts_base(gpui_component::tree::TreeItem::new("id", "Label")); |
| 227 | } |
| 228 | |
| 229 | #[test] |
| 230 | fn base_combobox_accepts_application_owned_content() { |
| 231 | fn build(trigger: gpui::FocusHandle, content: gpui::FocusHandle) { |
| 232 | let _ = gpui_base::Combobox::new("language") |
| 233 | .open(true) |
| 234 | .disabled(false) |
| 235 | .focus_handle(&trigger) |
| 236 | .content_focus_handle(&content) |
| 237 | .on_open_change(|_, _, _| {}) |
| 238 | .on_confirm(|_, _| {}) |
| 239 | .child(gpui::div()); |
| 240 | } |
| 241 | |
| 242 | let _ = build; |
| 243 | } |
| 244 | |
| 245 | #[test] |
| 246 | fn base_input_frames_accept_application_owned_content() { |
| 247 | let _ = gpui_base::InputBase::new("input").child(gpui::div()); |
| 248 | fn build_number(state: &gpui::Entity<gpui_base::input::InputState>) { |
| 249 | let _ = gpui_base::NumberInput::new(state) |
| 250 | .on_step(|_, _, _| {}) |
| 251 | .decrement_button(|button| button.child("-")) |
| 252 | .increment_button(|button| button.child("+")) |
| 253 | .input(gpui::div()) |
| 254 | .child(gpui::div()); |
| 255 | } |
| 256 | let _ = build_number; |
| 257 | let _ = gpui_base::NumberInputText::new().child(gpui::div()); |
| 258 | |
| 259 | let _: gpui_base::StepAction = gpui_component::input::StepAction::Increment; |
| 260 | } |
| 261 | |
| 262 | #[test] |
| 263 | fn base_input_accepts_application_owned_highlighters() { |
| 264 | struct CustomHighlighter; |
| 265 | |
| 266 | impl gpui_base::input::InputHighlighter for CustomHighlighter { |
| 267 | fn language(&self) -> gpui::SharedString { |
| 268 | "custom".into() |
| 269 | } |
| 270 | |
| 271 | fn update( |
| 272 | &mut self, |
| 273 | _: Option<gpui_base::input::InputEdit>, |
| 274 | _: &gpui_base::input::Rope, |
| 275 | _: bool, |
| 276 | _: &mut gpui::Window, |
| 277 | _: &mut gpui::Context<gpui_base::input::EditorState>, |
| 278 | ) { |
| 279 | } |
| 280 | |
| 281 | fn styles( |
| 282 | &self, |
| 283 | _: &std::ops::Range<usize>, |
| 284 | _: &dyn gpui_base::input::HighlightStyleResolver, |
| 285 | ) -> Vec<(std::ops::Range<usize>, gpui::HighlightStyle)> { |
| 286 | Vec::new() |
| 287 | } |
| 288 | |
| 289 | fn fold_ranges(&self, _: &gpui_base::input::Rope) -> Vec<gpui_base::input::FoldRange> { |
| 290 | Vec::new() |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | let factory: gpui_base::input::InputHighlighterFactory = |
| 295 | std::rc::Rc::new(|_| Some(Box::new(CustomHighlighter))); |
| 296 | let _ = factory; |
| 297 | } |
| 298 | |
| 299 | #[test] |
| 300 | fn legacy_list_settings_are_the_base_type() { |
| 301 | fn accepts_base(_: gpui_base::ListSettings) {} |
| 302 | accepts_base(gpui_component::list::ListSettings::default()); |
| 303 | } |
| 304 | |
| 305 | #[test] |
| 306 | fn legacy_global_state_is_the_base_type() { |
| 307 | fn accepts_base(_: &gpui_base::GlobalState) {} |
| 308 | fn through_legacy(state: &gpui_component::GlobalState) { |
| 309 | accepts_base(state); |
| 310 | } |
| 311 | |
| 312 | let _ = through_legacy; |
| 313 | } |
| 314 | |
| 315 | #[test] |
| 316 | fn legacy_popover_state_is_the_base_type() { |
| 317 | fn accepts_base(_: &gpui_base::PopoverState) {} |
| 318 | fn through_legacy(state: &gpui_component::popover::PopoverState) { |
| 319 | accepts_base(state); |
| 320 | } |
| 321 | |
| 322 | let _ = through_legacy; |
| 323 | } |
| 324 | |
| 325 | #[test] |
| 326 | fn legacy_hover_card_state_is_the_base_type() { |
| 327 | fn accepts_base(_: &gpui_base::HoverCardState) {} |
| 328 | fn through_legacy(state: &gpui_component::hover_card::HoverCardState) { |
| 329 | accepts_base(state); |
| 330 | } |
| 331 | |
| 332 | let _ = through_legacy; |
| 333 | } |
| 334 | |
| 335 | #[test] |
| 336 | fn base_tooltip_positioner_uses_normal_children() { |
| 337 | let _ = gpui_base::Tooltip::new("tooltip-popup") |
| 338 | .child("Tooltip") |
| 339 | .child("⌘K"); |
| 340 | let _ = gpui_base::TooltipPositioner::new(gpui::Bounds::default()) |
| 341 | .placement(gpui_component::Placement::Right) |
| 342 | .child("Tooltip"); |
| 343 | } |
| 344 | |
| 345 | #[test] |
| 346 | fn transition_ids_accept_strings_and_named_channels() { |
| 347 | let _: gpui_base::TransitionId = "opacity".into(); |
| 348 | let _: gpui_base::TransitionId = ("checkbox", "fill").into(); |
| 349 | |
| 350 | fn requires_interpolation<T: gpui_base::Interpolate>() {} |
| 351 | requires_interpolation::<f32>(); |
| 352 | } |
| 353 | |
| 354 | #[test] |
| 355 | fn legacy_styled_and_sizing_exports_remain_available() { |
| 356 | use gpui_component::Sizable as _; |
| 357 | |
| 358 | let _: gpui_component::Size = gpui_component::Size::Medium; |
| 359 | let _ = gpui_component::StyledExt::font_medium(gpui::div()); |
| 360 | let _ = gpui_component::h_flex(); |
| 361 | let _ = gpui_component::v_flex(); |
| 362 | let _ = gpui_component::box_shadow(0., 0., 0., 0., gpui::hsla(0., 0., 0., 0.)); |
| 363 | |
| 364 | struct SizedValue; |
| 365 | impl gpui_component::Sizable for SizedValue { |
| 366 | fn with_size(self, _: impl Into<gpui_component::Size>) -> Self { |
| 367 | self |
| 368 | } |
| 369 | } |
| 370 | let _ = SizedValue.small(); |
| 371 | } |
| 372 | |
| 373 | #[test] |
| 374 | fn element_ext_is_available_from_base_and_the_legacy_root() { |
| 375 | use gpui_component::ElementExt as _; |
| 376 | |
| 377 | fn requires_base<T: gpui_base::ElementExt>() {} |
| 378 | fn requires_legacy<T: gpui_component::ElementExt>() {} |
| 379 | requires_base::<gpui::Div>(); |
| 380 | requires_legacy::<gpui::Div>(); |
| 381 | |
| 382 | let _ = gpui::div().on_prepaint(|_, _, _| {}); |
| 383 | } |
| 384 | |
| 385 | #[test] |
| 386 | fn legacy_history_path_reexports_the_base_type() { |
| 387 | let _: gpui_component::history::History<u8> = gpui_base::History::new(); |
| 388 | let _: gpui_component::history::UndoHistory<u8> = gpui_base::UndoHistory::new(); |
| 389 | } |
| 390 | |
| 391 | #[test] |
| 392 | fn legacy_auto_scroll_path_reexports_the_base_type() { |
| 393 | fn through_legacy_path(scroll: gpui_base::AutoScroll) -> gpui_component::scroll::AutoScroll { |
| 394 | scroll |
| 395 | } |
| 396 | |
| 397 | let _ = through_legacy_path(gpui_base::AutoScroll::default()); |
| 398 | } |
| 399 | |
| 400 | #[test] |
| 401 | fn root_resizable_paths_reexport_base_types() { |
| 402 | fn state_through_facade(state: gpui_base::ResizableState) -> gpui_component::ResizableState { |
| 403 | state |
| 404 | } |
| 405 | |
| 406 | fn group_through_facade( |
| 407 | group: gpui_base::ResizablePanelGroup, |
| 408 | ) -> gpui_component::ResizablePanelGroup { |
| 409 | group |
| 410 | } |
| 411 | |
| 412 | fn panel_through_facade(panel: gpui_base::ResizablePanel) -> gpui_component::ResizablePanel { |
| 413 | panel |
| 414 | } |
| 415 | |
| 416 | let _ = state_through_facade(gpui_base::ResizableState::default()); |
| 417 | let _ = group_through_facade(gpui_base::ResizablePanelGroup::new("group")); |
| 418 | let _ = panel_through_facade(gpui_base::resizable_panel()); |
| 419 | let _ = gpui_component::h_resizable("horizontal"); |
| 420 | let _ = gpui_component::v_resizable("vertical"); |
| 421 | } |
| 422 | |
| 423 | #[test] |
| 424 | fn legacy_resizable_module_paths_remain_available() { |
| 425 | fn panel_through_legacy( |
| 426 | panel: gpui_base::ResizablePanel, |
| 427 | ) -> gpui_component::resizable::ResizablePanel { |
| 428 | panel |
| 429 | } |
| 430 | |
| 431 | let _ = panel_through_legacy(gpui_base::resizable_panel()); |
| 432 | let _ = gpui_component::resizable::h_resizable("horizontal"); |
| 433 | let _ = gpui_component::resizable::v_resizable("vertical"); |
| 434 | } |