Commit Servo loaded URLs to tabs
This commit is contained in:
@@ -23,19 +23,26 @@ impl BrowserCore {
|
||||
/// no new tab is created and the active tab id is unchanged.
|
||||
pub fn navigate_active_tab(&mut self, url: UrlText) -> Result<(), CoreError> {
|
||||
let active_id = self.active_tab_id.clone();
|
||||
let tab_index = self
|
||||
.tabs
|
||||
.iter()
|
||||
.position(|tab| tab.id() == &active_id)
|
||||
.ok_or_else(|| CoreError::TabNotFound { id: active_id.clone() })?;
|
||||
self.tabs[tab_index].navigate_to(url);
|
||||
self.tabs[tab_index].mark_ready();
|
||||
let snapshot_tab = self.tabs[tab_index].clone();
|
||||
self.record_history_entry(&snapshot_tab);
|
||||
self.record_tab_activity(&active_id, SystemTime::now());
|
||||
self.update_tab_url(&active_id, url, TabUrlUpdate::PushHistory)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn navigate_tab_to_loaded_url(
|
||||
&mut self,
|
||||
tab_id: &TabId,
|
||||
url: UrlText,
|
||||
) -> Result<bool, CoreError> {
|
||||
self.update_tab_url(tab_id, url, TabUrlUpdate::PushHistory)
|
||||
}
|
||||
|
||||
pub fn replace_tab_loaded_url(
|
||||
&mut self,
|
||||
tab_id: &TabId,
|
||||
url: UrlText,
|
||||
) -> Result<bool, CoreError> {
|
||||
self.update_tab_url(tab_id, url, TabUrlUpdate::PreserveHistory)
|
||||
}
|
||||
|
||||
pub fn navigate_active_tab_back(&mut self) -> Result<bool, CoreError> {
|
||||
self.navigate_active_tab_history(TabHistoryDirection::Back)
|
||||
}
|
||||
@@ -377,6 +384,32 @@ impl BrowserCore {
|
||||
self.tabs.get_mut(active_index).ok_or(CoreError::MissingActiveTab)
|
||||
}
|
||||
|
||||
fn update_tab_url(
|
||||
&mut self,
|
||||
tab_id: &TabId,
|
||||
url: UrlText,
|
||||
update: TabUrlUpdate,
|
||||
) -> Result<bool, CoreError> {
|
||||
let tab_index = self
|
||||
.tabs
|
||||
.iter()
|
||||
.position(|tab| tab.id() == tab_id)
|
||||
.ok_or_else(|| CoreError::TabNotFound { id: tab_id.clone() })?;
|
||||
if self.tabs[tab_index].url() == &url {
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
match update {
|
||||
TabUrlUpdate::PushHistory => self.tabs[tab_index].navigate_to(url),
|
||||
TabUrlUpdate::PreserveHistory => self.tabs[tab_index].set_url(url),
|
||||
}
|
||||
self.tabs[tab_index].mark_ready();
|
||||
let snapshot_tab = self.tabs[tab_index].clone();
|
||||
self.record_history_entry(&snapshot_tab);
|
||||
self.record_tab_activity(tab_id, SystemTime::now());
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
fn navigate_active_tab_history(
|
||||
&mut self,
|
||||
direction: TabHistoryDirection,
|
||||
@@ -459,3 +492,8 @@ enum TabHistoryDirection {
|
||||
Back,
|
||||
Forward,
|
||||
}
|
||||
|
||||
enum TabUrlUpdate {
|
||||
PushHistory,
|
||||
PreserveHistory,
|
||||
}
|
||||
|
||||
@@ -40,6 +40,34 @@ fn new_navigation_clears_forward_stack() -> Result<(), Box<dyn Error>> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn observed_loaded_url_replaces_active_url_without_back_stack() -> Result<(), Box<dyn Error>> {
|
||||
let mut core = BrowserCore::new(InitialBrowserConfig::ely_defaults()?)?;
|
||||
let tab_id = core.active_tab()?.id().clone();
|
||||
|
||||
assert!(core.replace_tab_loaded_url(&tab_id, UrlText::parse("https://example.com/")?)?);
|
||||
|
||||
assert_eq!(core.active_tab()?.url().as_str(), "https://example.com/");
|
||||
assert!(!core.active_tab()?.can_navigate_back());
|
||||
assert!(!core.active_tab()?.can_navigate_forward());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn user_loaded_url_enters_active_back_stack() -> Result<(), Box<dyn Error>> {
|
||||
let mut core = BrowserCore::new(InitialBrowserConfig::ely_defaults()?)?;
|
||||
core.navigate_active_tab(UrlText::parse("https://example.com/a")?)?;
|
||||
let tab_id = core.active_tab()?.id().clone();
|
||||
|
||||
assert!(core.navigate_tab_to_loaded_url(&tab_id, UrlText::parse("https://example.com/b")?)?);
|
||||
|
||||
assert_eq!(core.active_tab()?.url().as_str(), "https://example.com/b");
|
||||
assert!(core.active_tab()?.can_navigate_back());
|
||||
assert!(core.navigate_active_tab_back()?);
|
||||
assert_eq!(core.active_tab()?.url().as_str(), "https://example.com/a");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_history_navigation_keeps_active_url() -> Result<(), Box<dyn Error>> {
|
||||
let mut core = BrowserCore::new(InitialBrowserConfig::ely_defaults()?)?;
|
||||
|
||||
Reference in New Issue
Block a user