Repository: tauri-apps/tauri
File: core/tauri/src/window.rs (Rust)

Code before:
  }
}

/// Webview APIs.
impl<R: Runtime> Window<R> {
  /// Returns the current url of the webview.
  // TODO: in v2, change this type to Result
  #[cfg(not(test))]
  pub fn url(&self) -> Url {
    self.window.dispatcher.url().unwrap()
  }

  #[cfg(test)]
  pub fn url(&self) -> Url {
    self.current_url.clone()
  }

  /// Navigates the webview to the defined url.
  #[cfg(not(test))]
  pub fn navigate(&mut self, url: Url) {
    self.window.dispatcher.navigate(url).unwrap();
  }

  #[cfg(test)]
  pub fn navigate(&mut self, url: Url) {
    self.current_url = url;
  }

  fn is_local_url(&self, current_url: &Url) -> bool {
    self.manager.get_url().make_relative(current_url).is_some() || {
      let protocol_url = self.manager.protocol_url();
      current_url.scheme() == protocol_url.scheme() && current_url.domain() == protocol_url.domain()
    }
  }

  /// Handles this window receiving an [`InvokeMessage`].
  pub fn on_message(self, payload: InvokePayload) -> crate::Result<()> {
    let manager = self.manager.clone();
    let current_url = self.url();
    let is_local = self.is_local_url(&current_url);

    let mut scope_not_found_error_message =
      ipc_scope_not_found_error_message(&self.window.label, current_url.as_str());
    let scope = if is_local {
      None
    } else {
      match self.ipc_scope().remote_access_for(&self, &current_url) {
        Ok(scope) => Some(scope),
        Err(e) => {
          if e.matches_window {
            scope_not_found_error_message = ipc_scope_domain_error_message(current_url.as_str());

Review comment (suggestion):
Now that we have an actual `navigate` function, we can remove the [current_url field](https://github.com/tauri-apps/tauri/blob/1542ad17d647460ec7e2750182a502debc110e28/core/tauri/src/window.rs#L782) and implement `fn navigate` under the [mock runtime](https://github.com/tauri-apps/tauri/blob/1542ad17d647460ec7e2750182a502debc110e28/core/tauri/src/test/mock_runtime.rs#L553). Basically you'll need to change the [Dispatcher#url](https://github.com/tauri-apps/tauri/blob/1542ad17d647460ec7e2750182a502debc110e28/core/tauri/src/test/mock_runtime.rs#L193) to a `Mutex<String>` and mutate it on the `navigate` impl.

With this change we have less `#[cfg(test)] #[cfg(not(test))]` to deal with.

Code after:
  pub fn url(&self) -> Url {
    self.window.dispatcher.url().unwrap()
  }

  /// Navigates the webview to the defined url.
  pub fn navigate(&mut self, url: Url) {
    self.window.dispatcher.navigate(url).unwrap();
  }

  fn is_local_url(&self, current_url: &Url) -> bool {
    self.manager.get_url().make_relative(current_url).is_some() || {
      let protocol_url = self.manager.protocol_url();
      current_url.scheme() == protocol_url.scheme() && current_url.domain() == protocol_url.domain()
    }
  }

  /// Handles this window receiving an [`InvokeMessage`].
  pub fn on_message(self, payload: InvokePayload) -> crate::Result<()> {
    let manager = self.manager.clone();
    let current_url = self.url();
    let is_local = self.is_local_url(&current_url);

    let mut scope_not_found_error_message =
      ipc_scope_not_found_error_message(&self.window.label, current_url.as_str());
    let scope = if is_local {
      None
    } else {
      match self.ipc_scope().remote_access_for(&self, &current_url) {
        Ok(scope) => Some(scope),
        Err(e) => {
          if e.matches_window {
            scope_not_found_error_message = ipc_scope_domain_error_message(current_url.as_str());
          } else if e.matches_domain {
            scope_not_found_error_message = ipc_scope_window_error_message(&self.window.label);
          }
          None
        }
      }
    };
    match payload.cmd.as_str() {
      "__initialized" => {
        let payload: PageLoadPayload = serde_json::from_value(payload.inner)?;
        manager.run_on_page_load(self, payload);
      }
      _ => {
        let message = InvokeMessage::new(
          self.clone(),
          manager.state(),
          payload.cmd.to_string(),
          payload.inner,
        );