summaryrefslogtreecommitdiff
path: root/src/vala/unit-tests-report.vala
blob: 6442e48746b8adc1f990d9eef2ae5e736c5c0ffe (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
[CCode (cheader_filename = "config.h")]
extern const string UNIT_TESTS_REPORT_VALA;

namespace Disfluid {

	class UnitTestsReport: GLib.Object {
		public string output;
		public size_t n_tests;
		public size_t n_errors;

		public UnitTestsReport (string output, size_t n_tests, size_t n_errors) {
			this.output = output;
			this.n_tests = n_tests;
			this.n_errors = n_errors;
		}
	}

	async UnitTestsReport do_run_tests_in_bg () throws GLib.ThreadError {
		GLib.SourceFunc callback = do_run_tests_in_bg.callback;
		UnitTestsReport[] output = new UnitTestsReport[1];
		ThreadFunc<bool> run = () => {
			size_t n_tests, n_errors;
			var o = Disfluid.run_tests (out n_tests, out n_errors);
			if (o == null) {
				o = _ ("The tests did not produce any output.");
			}
			output[0] = new UnitTestsReport (o, n_tests, n_errors);
			Idle.add ((owned) callback);
			return true;
		};
		new Thread<bool> ("unit-tests-thread", run);
		yield;
		return output[0];
	}

	[GtkTemplate (ui = "/eu/planete_kraus/Disfluid/src/unit_tests_report.ui")]
	public class UnitTestsReportDialog: Adw.ApplicationWindow {

		[GtkChild]
		private unowned Gtk.Stack stack;

		[GtkChild]
		private unowned Adw.StatusPage page_wait;

		[GtkChild]
		private unowned Adw.StatusPage page_success;

		[GtkChild]
		private unowned Adw.StatusPage page_failure;

		[GtkChild]
		private unowned Gtk.TextView debug_info;

		[GtkChild]
		private unowned Gtk.LinkButton report_button;

		public signal void new_debug_info (string debug_info);

		public void start () {
			this.stack.visible_child = this.page_wait;
			do_run_tests_in_bg.begin ((obj, res) => {
					try {
						var results = do_run_tests_in_bg.end (res);
						if (results.n_errors == 0) {
							this.stack.visible_child = this.page_success;
						}
						else {
							this.stack.visible_child = this.page_failure;
							this.debug_info.buffer.text = results.output;
							// Do not translate the subject
							var subject = "[Disfluid unit tests failure] " + _ ("*** Your subject line here, in English ***") ;
							var body = _ ("*** Please give as much information as possible, in English, as best as you can. Following your message is the debug information, check if there is no sensitive personal information. ***") + "\n\n" + results.output;
							var query = "subject=" + GLib.Uri.escape_string (subject) + "&body=" + GLib.Uri.escape_string (body);
							this.report_button.uri = "mailto:vivien@planete-kraus.eu?" + query;
							this.new_debug_info (results.output);
						}
					}
					catch (ThreadError e) {
						this.stack.visible_child = this.page_failure;
					}
				});
		}

		public UnitTestsReportDialog (Gtk.Application app) {
			this.application = app;
			var s = new Gtk.Spinner ();
			s.spinning = true;
			var spinner = new Gtk.WidgetPaintable (s);
			this.page_wait.paintable = spinner;
			this.start ();
			GLib.SimpleAction rerun = new GLib.SimpleAction ("rerun-unit-tests", null);
			rerun.activate.connect (() => {
					this.start ();
				});
			this.add_action (rerun);
		}
	}
}