aboutsummaryrefslogtreecommitdiff
path: root/tests/gtest-dialog.cpp
blob: c35a6cc853bcc6295e445b8c12007e9ace9e2d6e (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152

#include <gtk/gtk.h>
#include <gtest/gtest.h>
#include "idomessagedialog.h"

class TestDialog : public ::testing::Test
{
  private:

    guint log_handler_id;

    int log_count_actual;

    static void log_count_func (const gchar *log_domain,
                                GLogLevelFlags log_level,
                                const gchar *message,
                                gpointer user_data)
    {
      reinterpret_cast<TestDialog*>(user_data)->log_count_actual++;
    }

  protected:

    int log_count_expected;

    GMainLoop * main_loop;

  protected:

    virtual void SetUp()
    {
      const GLogLevelFlags flags = GLogLevelFlags(G_LOG_LEVEL_CRITICAL|G_LOG_LEVEL_WARNING);
      log_handler_id = g_log_set_handler ("Gdk", flags, log_count_func, this);
      log_count_expected = 0;
      log_count_actual = 0;

      main_loop = NULL;

      static bool initialized = false;
      if (G_UNLIKELY (!initialized))
        {
          g_type_init();
          initialized = true;
        }

      main_loop = g_main_loop_new (NULL, FALSE);
    }

    virtual void TearDown()
    {
      ASSERT_EQ (log_count_expected, log_count_actual);

      g_clear_pointer (&main_loop, g_main_loop_unref);
    }

  public:

    TestDialog()
    {
      gint argc = 0;
      gchar * argv[] = {NULL};
      gtk_init (&argc, (gchar ***)&argv);
    }

  protected:

    static gboolean
    on_wait_timeout (gpointer main_loop)
    {
      g_main_loop_quit (static_cast<GMainLoop*>(main_loop));
      return G_SOURCE_REMOVE;
    }

    void
    WaitForSignal (gpointer instance, const gchar * detailed_signal)
    {
      guint timeout_id;
      gulong handler_id;
      const int timeout_seconds = 5;

      ASSERT_TRUE (instance != NULL);
      ASSERT_TRUE (main_loop != NULL);


      handler_id = g_signal_connect_swapped (instance,
                                             detailed_signal,
                                             G_CALLBACK(g_main_loop_quit),
                                             main_loop);

      timeout_id = g_timeout_add_seconds (timeout_seconds,
                                          on_wait_timeout,
                                          main_loop);

      // wait for the signal or for timeout, whichever comes first
      g_main_loop_run (main_loop);
      ASSERT_TRUE (g_main_context_find_source_by_id(NULL,timeout_id) != NULL);
      g_signal_handler_disconnect (instance, handler_id);
      g_source_remove (timeout_id);
    }

  protected:

    void ShowDialog (GtkWidget * dialog)
    {
      EXPECT_TRUE (dialog != NULL);
      EXPECT_TRUE (IDO_IS_MESSAGE_DIALOG (dialog));

      gtk_message_dialog_format_secondary_markup (GTK_MESSAGE_DIALOG (dialog),
                                                  "Some Secondary Text");

      GtkWidget * action_area;
      action_area = gtk_dialog_get_action_area (GTK_DIALOG (dialog));
      EXPECT_TRUE (GTK_IS_WIDGET (action_area));

      gtk_widget_show (dialog);

      // synthesize a focus-in event to activate the idotimeline
      GdkEventFocus focus;
      focus.type = GDK_FOCUS_CHANGE;
      focus.window = gtk_widget_get_window (dialog);
      focus.send_event = FALSE;
      focus.in = TRUE;
      gtk_main_do_event ((GdkEvent*)&focus);
      ++log_count_expected; // this will throw up a synthesized event warning

      g_timeout_add_seconds (1, on_wait_timeout, main_loop);
      g_main_loop_run (main_loop);
    }
};

TEST_F (TestDialog, BuildMessageDialog)
{
  GtkWidget * dialog = ido_message_dialog_new (NULL,
                                               GtkDialogFlags(0),
                                               GTK_MESSAGE_INFO,
                                               GTK_BUTTONS_CLOSE,
                                               "%s",
                                               "Hello World");
  ShowDialog (dialog);
}

TEST_F (TestDialog, BuildMessageDialogWithMarkup)
{
  GtkWidget * dialog = ido_message_dialog_new_with_markup (NULL,
                                                           GtkDialogFlags(0),
                                                           GTK_MESSAGE_INFO,
                                                           GTK_BUTTONS_CLOSE,
                                                           "%s",
                                                           "<small>Hello World</small>");
  ShowDialog (dialog);
}