~midori/midori/gtk3WebKit2only

5354 by Christian Dywan
Add dialog API with unit test hooks
1
/*
6159.1.1 by Cody Garver
Update dates to 2013.
2
 Copyright (C) 2011-2013 Christian Dywan <christian@twotoats.de>
5354 by Christian Dywan
Add dialog API with unit test hooks
3
4
 This library is free software; you can redistribute it and/or
5
 modify it under the terms of the GNU Lesser General Public
6
 License as published by the Free Software Foundation; either
7
 version 2.1 of the License, or (at your option) any later version.
8
9
 See the file COPYING for the full license text.
10
*/
11
12
namespace Midori {
5577 by Christian Dywan
Fix wrong unref in session saving, add timeout testing
13
    namespace Timeout {
14
        public uint add_seconds (uint interval, owned SourceFunc function) {
15
            if (Test.test_idle_timeouts)
16
                return GLib.Idle.add (function);
17
            return GLib.Timeout.add_seconds (interval, function);
18
        }
5583 by Christian Dywan
Support Midori.Test.idle_timeouts for non-second timeouts
19
        public uint add (uint interval, owned SourceFunc function) {
20
            if (Test.test_idle_timeouts)
21
                return GLib.Idle.add (function);
22
            return GLib.Timeout.add (interval, function);
23
        }
5577 by Christian Dywan
Fix wrong unref in session saving, add timeout testing
24
    }
25
5354 by Christian Dywan
Add dialog API with unit test hooks
26
    namespace Test {
6411.5.9 by Christian Dywan
Introduce Midori.Test.init with a custom log handler
27
        public void init ([CCode (array_length_pos = 0.9)] ref unowned string[] args) {
28
            GLib.Test.init (ref args);
29
30
            /* Always log to stderr */
31
            Log.set_handler (null,
32
                LogLevelFlags.LEVEL_MASK | LogLevelFlags.FLAG_FATAL | LogLevelFlags.FLAG_RECURSION,
33
                (domain, log_levels, message) => {
34
                stderr.printf ("** %s\n", message);
35
            });
36
        }
37
5591 by Christian Dywan
Refactor test timeout as grab_max_timeout, try wakeup first
38
        internal static uint test_max_timeout = 0;
39
        internal static string? test_first_try = null;
40
        public void grab_max_timeout () {
5985 by Christian Dywan
Allow overriding test timeout with MIDORI_TIMEOUT
41
            int seconds = (Environment.get_variable ("MIDORI_TIMEOUT") ?? "42").to_int ();
5591 by Christian Dywan
Refactor test timeout as grab_max_timeout, try wakeup first
42
            test_first_try = "once";
5985 by Christian Dywan
Allow overriding test timeout with MIDORI_TIMEOUT
43
            test_max_timeout = GLib.Timeout.add_seconds (seconds > 0 ? seconds / 2 : 0, ()=>{
6411.5.9 by Christian Dywan
Introduce Midori.Test.init with a custom log handler
44
                stderr.printf ("Timed out %s%s\n", test_first_try,
5591 by Christian Dywan
Refactor test timeout as grab_max_timeout, try wakeup first
45
                    MainContext.default ().pending () ? " (loop)" : "");
46
                if (test_first_try == "twice")
47
                    Process.exit (0);
48
                test_first_try = "twice";
49
                MainContext.default ().wakeup ();
50
                return true;
51
                });
52
        }
53
        public void release_max_timeout () {
54
            assert (test_max_timeout > 0);
55
            GLib.Source.remove (test_max_timeout);
56
            test_max_timeout = 0;
57
        }
58
5577 by Christian Dywan
Fix wrong unref in session saving, add timeout testing
59
        internal static bool test_idle_timeouts = false;
60
        public void idle_timeouts () {
61
            test_idle_timeouts = true;
62
        }
63
6411.5.8 by Christian Dywan
Introduce Test.Job class for async test functions
64
        public abstract class Job : GLib.Object {
65
            bool done;
66
            public abstract async void run (Cancellable cancellable) throws GLib.Error;
67
            async void run_wrapped (Cancellable cancellable) {
68
                try {
69
                    yield run (cancellable);
70
                } catch (Error error) {
71
                    GLib.error (error.message);
72
                }
73
                done = true;
74
            }
75
            public void run_sync () {
76
                var loop = MainContext.default ();
77
                var cancellable = new Cancellable ();
78
                done = false;
79
                run_wrapped.begin (cancellable);
80
                do { loop.iteration (true); } while (!done);
81
            }
82
        }
83
5567 by Christian Dywan
Add generic log_set_fatal_handler_for_icons
84
        public void log_set_fatal_handler_for_icons () {
85
            GLib.Test.log_set_fatal_handler ((domain, log_levels, message)=> {
86
                return !message.contains ("Error loading theme icon")
87
                    && !message.contains ("Could not find the icon")
5617 by Christian Dywan
Ignore GTK+3 CSS parsing errors in unit tests
88
                    && !message.contains ("Junk at end of value")
5775 by Christian Dywan
Ignore gtk_notebook_get_tab_label caused by Granite
89
                    && !message.contains ("gtk_notebook_get_tab_label: assertion `GTK_IS_WIDGET (child)' failed")
5567 by Christian Dywan
Add generic log_set_fatal_handler_for_icons
90
                    && !message.contains ("get_column_number: assertion `i < gtk_tree_view_get_n_columns (treeview)' failed");
91
            });
92
93
        }
94
5354 by Christian Dywan
Add dialog API with unit test hooks
95
        internal static Gtk.ResponseType test_response = Gtk.ResponseType.NONE;
96
        public void set_dialog_response (Gtk.ResponseType response) {
97
            test_response = response;
98
        }
5375 by Christian Dywan
Check resources to see if a file has subresources
99
100
        internal static string? test_filename = null;
101
        public void set_file_chooser_filename (string filename) {
102
            test_filename = filename;
103
        }
5354 by Christian Dywan
Add dialog API with unit test hooks
104
    }
105
6558.3.4 by Christian Dywan
Move Open Image in Viewer into Open With extension
106
    public static void show_message_dialog (Gtk.MessageType type, string short, string detailed, bool modal) {
107
        var dialog = new Gtk.MessageDialog (null, 0, type, Gtk.ButtonsType.OK, "%s", short);
108
        dialog.format_secondary_text ("%s", detailed);
109
        if (modal) {
110
            dialog.run ();
111
            dialog.destroy ();
112
        } else {
113
            dialog.response.connect ((response) => {
114
                dialog.destroy ();
115
            });
116
            dialog.show ();
117
        }
118
    }
119
5354 by Christian Dywan
Add dialog API with unit test hooks
120
    public class FileChooserDialog : Gtk.FileChooserDialog {
6448.1.33 by Paweł Forysiuk
Don't shadow certificate export dialog
121
        public FileChooserDialog (string title, Gtk.Window? window, Gtk.FileChooserAction action) {
5354 by Christian Dywan
Add dialog API with unit test hooks
122
            /* Creates a new file chooser dialog to Open or Save and Cancel.
123
               The positive response is %Gtk.ResponseType.OK. */
124
            unowned string stock_id = Gtk.Stock.OPEN;
125
            if (action == Gtk.FileChooserAction.SAVE)
126
                stock_id = Gtk.Stock.SAVE;
127
            this.title = title;
128
            transient_for = window;
129
            this.action = action;
130
            add_buttons (Gtk.Stock.CANCEL, Gtk.ResponseType.CANCEL,
131
                         stock_id, Gtk.ResponseType.OK);
132
            icon_name = stock_id;
133
        }
134
    }
135
136
    namespace Dialog {
137
        public static new int run (Gtk.Dialog dialog) {
5375 by Christian Dywan
Check resources to see if a file has subresources
138
            if (Test.test_response != Gtk.ResponseType.NONE) {
139
                if (Test.test_filename != null && dialog is Gtk.FileChooser)
140
                    (dialog as Gtk.FileChooser).set_filename (Test.test_filename);
5354 by Christian Dywan
Add dialog API with unit test hooks
141
                return Test.test_response;
5375 by Christian Dywan
Check resources to see if a file has subresources
142
            }
5354 by Christian Dywan
Add dialog API with unit test hooks
143
            return dialog.run ();
144
        }
145
    }
146
}
147