Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add taskbar progress tests #1

Open
wants to merge 2 commits into
base: taskbar_progress
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion czkawka_gui/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Remove console window in Windows OS
#![windows_subsystem = "windows"]
#![cfg_attr(not(test), windows_subsystem = "windows")]

mod connect_about_buttons;
mod connect_button_delete;
Expand Down
111 changes: 111 additions & 0 deletions czkawka_gui/src/taskbar_progress_win.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ impl TaskbarProgress {
let result = unsafe {
if let Some(list) = self.taskbar_list.as_ref() {
list.SetProgressState(self.hwnd, tbp_flags)
} else if cfg!(test) {
S_OK
} else {
E_POINTER
}
Expand All @@ -47,6 +49,7 @@ impl TaskbarProgress {
}

pub fn set_progress_value(&self, completed: u64, total: u64) {
debug_assert!(completed <= total, "Task progress is over 100% - completed {} out of {}", completed, total);
// Don't change the value if the is_active flag is false or the value has not changed.
// If is_active is true and the value has not changed, but the progress indicator was in NOPROGRESS or INDETERMINATE state, set the value (and NORMAL state).
if ((completed, total) == *self.current_progress.borrow() && *self.current_state.borrow() != tbp_flags::TBPF_NOPROGRESS && *self.current_state.borrow() != tbp_flags::TBPF_INDETERMINATE) || !*self.is_active.borrow() {
Expand All @@ -55,6 +58,8 @@ impl TaskbarProgress {
let result = unsafe {
if let Some(list) = self.taskbar_list.as_ref() {
list.SetProgressValue(self.hwnd, completed, total)
} else if cfg!(test) {
S_OK
} else {
E_POINTER
}
Expand Down Expand Up @@ -93,6 +98,21 @@ impl TaskbarProgress {
}
}
}

#[cfg(test)]
fn get_state(&self) -> TBPFLAG {
*self.current_state.borrow()
}

#[cfg(test)]
fn get_value(&self) -> (u64, u64) {
*self.current_progress.borrow()
}

#[cfg(test)]
fn get_is_active(&self) -> bool {
*self.is_active.borrow()
}
}

impl From<HWND> for TaskbarProgress {
Expand Down Expand Up @@ -152,3 +172,94 @@ impl Drop for TaskbarProgress {
}
}
}

#[cfg(test)]
mod tests {
use super::tbp_flags::{TBPF_INDETERMINATE, TBPF_NOPROGRESS, TBPF_NORMAL, TBPF_PAUSED};
use super::TaskbarProgress;

#[test]
fn set_val_new() {
let tbp = TaskbarProgress::new();
assert_eq!(tbp.get_state(), TBPF_NOPROGRESS, "The initial state should be NOPROGRESS");
tbp.show();
let a = 13;
let b = 12345;
tbp.set_progress_value(a, b);
assert_eq!(tbp.get_value(), (a, b), "Testing set_progress_value with {}/{}", a, b);
assert_eq!(tbp.get_state(), TBPF_NORMAL, "Setting value should change the state from NOPROGRESS to NORMAL");
}

#[test]
fn set_val_indeterminate() {
let tbp = TaskbarProgress::new();
assert_eq!(tbp.get_state(), TBPF_NOPROGRESS, "The initial state should be NOPROGRESS");
tbp.show();
tbp.set_progress_state(TBPF_INDETERMINATE);
assert_eq!(tbp.get_state(), TBPF_INDETERMINATE, "Testing state change");
let a = 13;
let b = 12345;
tbp.set_progress_value(a, b);
assert_eq!(tbp.get_value(), (a, b), "Testing set_progress_value with {}/{}", a, b);
assert_eq!(tbp.get_state(), TBPF_NORMAL, "Setting value should change the state from INDETERMINATE to NORMAL");
}

#[test]
fn set_val_pause() {
let tbp = TaskbarProgress::new();
assert_eq!(tbp.get_state(), TBPF_NOPROGRESS, "The initial state should be NOPROGRESS");
tbp.show();
tbp.set_progress_state(TBPF_PAUSED);
assert_eq!(tbp.get_state(), TBPF_PAUSED, "Testing state change");
let a = 13;
let b = 12345;
tbp.set_progress_value(a, b);
assert_eq!(tbp.get_value(), (a, b), "Testing set_progress_value with {}/{}", a, b);
assert_eq!(tbp.get_state(), TBPF_PAUSED, "Setting value should not change the state from PAUSED (or ERROR)");
}

#[test]
#[should_panic(expected = "Task progress is over 100% - completed 15 out of 7")]
fn set_over_100() {
let tbp = TaskbarProgress::new();
assert_eq!(tbp.get_state(), TBPF_NOPROGRESS, "The initial state should be NOPROGRESS");
tbp.show();
tbp.set_progress_value(15, 7);
}

#[test]
fn hide_sets_noprogress() {
let tbp = TaskbarProgress::new();
assert_eq!(tbp.get_state(), TBPF_NOPROGRESS, "The initial state should be NOPROGRESS");
tbp.show();
tbp.set_progress_state(TBPF_INDETERMINATE);
assert_eq!(tbp.get_state(), TBPF_INDETERMINATE, "Testing state change");
tbp.hide();
assert_eq!(tbp.get_state(), TBPF_NOPROGRESS, "Hiding should set NOPROGRESS");
}

#[test]
fn default_hidden() {
let tbp = TaskbarProgress::new();
assert_eq!(tbp.get_state(), TBPF_NOPROGRESS, "The initial state should be NOPROGRESS");
assert_eq!(tbp.get_is_active(), false, "TaskbarProgress should be initialised as inactive");
}

#[test]
fn hide_disallows_state_changes() {
let tbp = TaskbarProgress::new();
assert_eq!(tbp.get_state(), TBPF_NOPROGRESS, "The initial state should be NOPROGRESS");
tbp.set_progress_state(TBPF_INDETERMINATE);
assert_eq!(tbp.get_state(), TBPF_NOPROGRESS, "Changing state should not be posible when hidden/inactive");
}

#[test]
fn hide_disallows_value_changes() {
let tbp = TaskbarProgress::new();
assert_eq!(tbp.get_state(), TBPF_NOPROGRESS, "The initial state should be NOPROGRESS");
let init_val = tbp.get_value();
tbp.set_progress_value(5, 15);
assert_eq!(tbp.get_value(), init_val, "Changing value should not be posible when hidden/inactive");
assert_eq!(tbp.get_state(), TBPF_NOPROGRESS, "Changing value when hidden/inactive should not change the state");
}
}