Skip to content

Commit 4b08e39

Browse files
author
Deren Vural
committed
Added initial testing support
- Moved setup functions from main.rs to lib.rs to allow testing - Moved main.rs to new bin directory in src directory - Created tests directory with integration_tests_1.rs - Currently only tests if application exits successfully, need to expand - Added gtk4-macros crate as this is needed for gtk initialisation in tests - Added shortcuts to application (Ctrl+q: close, Ctrl+w: toggle maximized, Ctrl+m: minimize) Signed-off-by: Deren Vural <[email protected]>
1 parent 79b8f84 commit 4b08e39

File tree

4 files changed

+120
-38
lines changed

4 files changed

+120
-38
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ edition = "2021"
1010
adwaita = { version = "^0.1.1", package = "libadwaita" }
1111
gtk = { version = "^0.4.8", package = "gtk4" }
1212
gdk = { version = "^0.4.8", package = "gdk4" }
13+
gtk4_macros = { version = "^0.5.2", package = "gtk4-macros" }
1314

1415
#serde = { version = "1.0", features = ["derive"] }
1516
#serde_json = "1.0"

src/bin/main.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// SPDX-FileCopyrightText: 2022 Deren Vural
2+
// SPDX-License-Identifier: GPL-3.0-or-later
3+
4+
/**
5+
* Name:
6+
* gtk4-nvidia-monitor-rust
7+
*
8+
* Description:
9+
* GTK-rs app for monitoring Nvidia GPU statistics
10+
*
11+
* Made:
12+
* 12/09/2022
13+
*
14+
* Made by:
15+
* Deren Vural
16+
*
17+
* Notes:
18+
*
19+
*/
20+
21+
// Declare module
22+
extern crate gtk4_nvidia_monitor_rust;
23+
24+
// Imports
25+
use adwaita::{prelude::ApplicationExtManual, Application};
26+
27+
/**
28+
* Name:
29+
* main
30+
*
31+
* Description:
32+
* Create application using lib.rs functions and run
33+
*
34+
* Made:
35+
* 13/09/2022
36+
*
37+
* Made by:
38+
* Deren Vural
39+
*
40+
* Notes:
41+
*
42+
*/
43+
fn main() {
44+
// Intialise GTK & Create a new application
45+
let app: Application = gtk4_nvidia_monitor_rust::create_app();
46+
47+
// Run the application
48+
println!("{}", app.run());
49+
}

src/main.rs renamed to src/lib.rs

Lines changed: 25 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
* Notes:
1818
*
1919
*/
20+
2021
// Modules
2122
mod formatter;
2223
mod gpu_page;
@@ -30,7 +31,7 @@ mod custom_button;
3031
mod settingswindow;
3132

3233
// Imports
33-
use adwaita::{gio, prelude::*, Application};
34+
use adwaita::{gio, glib, prelude::*, Application};
3435
use gdk::Display;
3536
use gio::resources_register_include;
3637
use gtk::{CssProvider, StyleContext};
@@ -54,7 +55,7 @@ const APP_ID: &str = "com.gtk_d.NvidiaMonitorRust";
5455
* Notes:
5556
*
5657
*/
57-
fn main() {
58+
pub fn create_app() -> Application {
5859
// Resources
5960
resources_register_include!("nvidiamonitorrust.gresource")
6061
.expect("Failed to register resources.");
@@ -66,12 +67,12 @@ fn main() {
6667
let app: Application = Application::builder().application_id(APP_ID).build();
6768

6869
// Connect to signals of `app`
69-
//app.connect_startup(setup_shortcuts);
70+
app.connect_startup(setup_shortcuts);
7071
app.connect_startup(|_| load_css());
7172
app.connect_activate(build_ui);
7273

73-
// Run the application
74-
println!("{}", app.run());
74+
// Return the application
75+
app
7576
}
7677

7778
/**
@@ -91,14 +92,18 @@ fn main() {
9192
* <https://github.com/gtk-rs/gtk4-rs/blob/master/book/listings/todo/5/main.rs>
9293
* <https://gtk-rs.org/gtk4-rs/stable/latest/book/todo_3.html>
9394
*
95+
* https://gtk-rs.org/gtk4-rs/git/book/actions.html
96+
* https://gtk-rs.org/gtk4-rs/git/docs/gtk4/struct.Window.html#actions
9497
*/
95-
/*
9698
fn setup_shortcuts(app: &Application) {
97-
app.set_accels_for_action("win.filter('All')", &["<Ctrl>a"]);
98-
app.set_accels_for_action("win.filter('Open')", &["<Ctrl>o"]);
99-
app.set_accels_for_action("win.filter('Done')", &["<Ctrl>d"]);
99+
//app.set_accels_for_action("win.filter('All')", &["<Ctrl>a"]);
100+
//app.set_accels_for_action("win.filter('Open')", &["<Ctrl>o"]);
101+
//app.set_accels_for_action("win.filter('Done')", &["<Ctrl>d"]);
102+
//app.set_accels_for_action("win.filter('Close')", &["<Ctrl>q"])
103+
app.set_accels_for_action("window.close", &["<Ctrl>q"]);
104+
app.set_accels_for_action("window.toggle-maximized", &["<Ctrl>w"]);
105+
app.set_accels_for_action("window.minimize", &["<Ctrl>m"]);
100106
}
101-
*/
102107

103108
/**
104109
* Name:
@@ -149,44 +154,26 @@ fn build_ui(app: &Application) {
149154
// Create a new custom window and show it
150155
let window: MainWindow = MainWindow::new(app);
151156

157+
// Create custom action to close window (for shortcuts)
158+
// Was a "close" replica
159+
/*
160+
let action_close = SimpleAction::new("something", None);
161+
action_close.connect_activate(clone!(@weak window => move |_, _| {
162+
window.close();
163+
}));
164+
window.add_action(&action_close);
165+
*/
166+
152167
// Present window
153168
window.show();
154169

155170
/*
156-
// Menu Child
157-
let menu: Menu = Menu::new();
158-
let item: Menu = Menu::new();
159-
item.append(Some("Utilisation"), Some("app.util"));
160-
item.append(Some("Temperature"), Some("app.temp"));
161-
item.append(Some("Memory Usage"), Some("app.memo"));
162-
item.append(Some("Fan Speed"), Some("app.fans"));
163-
menu.append_submenu(Some("Item 1"), &item);
164-
menu.append(Some("SMI"), Some("app.smi"));
165-
menu.append(Some("Settings"), Some("app.settings"));
166-
app.set_menubar(Some(&menu));
167-
168171
// App Indicator
169172
//let mut indicator = AppIndicator::new("Nvidia App", "");
170173
//indicator.set_status(AppIndicatorStatus::Active);
171174
//let icon_path = Path::new(env!("CARGO_MANIFEST_DIR")).join("resources");
172175
//indicator.set_icon_theme_path(icon_path.to_str().unwrap());
173176
//indicator.set_icon_full("rust-logo", "icon");
174177
//indicator.set_menu(&mut menu);
175-
176-
// Create Parent window
177-
let window: ApplicationWindow = ApplicationWindow::new(app);
178-
window.set_title(Some("Nvidia App"));
179-
window.set_default_size(400, 400);
180-
window.set_show_menubar(true);
181-
182-
// Add children to window
183-
//window.set_child(Some(&button1));
184-
//window.set_child(Some(&button2));
185-
//window.set_child(Some(&button3));
186-
//window.set_child(Some(&button4));
187-
//window.set_child(Some(&button5));
188-
189-
// Present window
190-
window.show();
191178
*/
192179
}

tests/integration_tests_1.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// SPDX-FileCopyrightText: 2022 Deren Vural
2+
// SPDX-License-Identifier: GPL-3.0-or-later
3+
4+
/* *
5+
* Name:
6+
* integration_tests_1.rs
7+
*
8+
* Description:
9+
* Integration tests for Nvidia Gnome Extension (Rust)
10+
*
11+
* Made:
12+
* 26/11/2022
13+
*
14+
* Made by:
15+
* Deren Vural
16+
*
17+
* Notes:
18+
*
19+
*/
20+
21+
// Imports
22+
extern crate gtk4_macros;
23+
24+
// Declare module
25+
extern crate gtk4_nvidia_monitor_rust;
26+
27+
// Imports
28+
use adwaita::{prelude::ApplicationExtManual, Application};
29+
30+
pub fn init_ui() -> Application {
31+
// Intialise GTK & Create a new application
32+
gtk4_nvidia_monitor_rust::create_app()
33+
}
34+
35+
/*
36+
* Integration tests
37+
*/
38+
#[gtk::test]
39+
fn test_test() {
40+
// Intialise GTK & Create a new application
41+
let app: Application = init_ui();
42+
43+
// Run the application
44+
assert_eq!(app.run(), 0);
45+
}

0 commit comments

Comments
 (0)