1
/* ATK -  Accessibility Toolkit
2
 * Copyright 2001 Sun Microsystems Inc.
3
 * Copyright 2013 Igalia S.L.
4
 *
5
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Library General Public
7
 * License as published by the Free Software Foundation; either
8
 * version 2 of the License, or (at your option) any later version.
9
 *
10
 * This library is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Library General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Library General Public
16
 * License along with this library; if not, write to the
17
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18
 * Boston, MA 02111-1307, USA.
19
 */
20

            
21
#include <atk/atk.h>
22

            
23
#define EXPECTED_NUMBER 5
24

            
25
#define TEST_TYPE_DOCUMENT (test_document_get_type ())
26
#define TEST_DOCUMENT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TEST_TYPE_DOCUMENT, TestDocument))
27
#define TEST_DOCUMENT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TEST_TYPE_DOCUMENT, TestDocumentClass))
28
#define TEST_IS_DOCUMENT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TEST_TYPE_DOCUMENT))
29
#define TEST_IS_DOCUMENT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TEST_TYPE_DOCUMENT))
30
#define TEST_DOCUMENT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TEST_TYPE_DOCUMENT, TestDocumentClass))
31

            
32
typedef struct _TestDocument TestDocument;
33
typedef struct _TestDocumentClass TestDocumentClass;
34

            
35
struct _TestDocument
36
{
37
  AtkObject parent;
38
  GMainLoop *loop;
39
  gint number_emissions;
40
};
41

            
42
struct _TestDocumentClass
43
{
44
  AtkObjectClass parent_class;
45
};
46

            
47
GType test_document_get_type (void) G_GNUC_CONST;
48
static void test_document_interface_init (AtkDocumentIface *iface);
49

            
50
1
G_DEFINE_TYPE_WITH_CODE (TestDocument,
51
                         test_document,
52
                         ATK_TYPE_OBJECT,
53
                         G_IMPLEMENT_INTERFACE (ATK_TYPE_DOCUMENT,
54
                                                test_document_interface_init));
55

            
56
static void
57
1
test_document_class_init (TestDocumentClass *klass)
58
{
59
1
}
60

            
61
static void
62
1
test_document_init (TestDocument *document)
63
{
64
1
}
65

            
66
static void
67
1
test_document_interface_init (AtkDocumentIface *iface)
68
{
69
1
}
70

            
71
static void
72
5
document_page_changed_cb (AtkDocument *document,
73
                          gint page_number,
74
                          gpointer data)
75
{
76
5
  TestDocument *test_document = TEST_DOCUMENT (document);
77

            
78
5
  g_print ("Page-changed callback, page_number = %i\n", page_number);
79
5
  test_document->number_emissions++;
80
5
}
81

            
82
static gboolean
83
5
document_emit_page_changed (gpointer data)
84
{
85
5
  TestDocument *test_document = TEST_DOCUMENT (data);
86
  static gint next_page = 1;
87

            
88
5
  g_print ("Moving to next page. Emitting page-change, page_number = %i\n",
89
           next_page);
90
5
  g_signal_emit_by_name (test_document, "page-changed", next_page++, NULL);
91

            
92
5
  if (next_page > EXPECTED_NUMBER)
93
    {
94
1
      g_main_loop_quit (test_document->loop);
95
1
      return G_SOURCE_REMOVE;
96
    }
97
  else
98
4
    return G_SOURCE_CONTINUE;
99
}
100

            
101
static void
102
1
test_page_changed (void)
103
{
104
  TestDocument *my_document;
105

            
106
1
  my_document = TEST_DOCUMENT (g_object_new (TEST_TYPE_DOCUMENT, NULL));
107

            
108
1
  g_signal_connect (my_document, "page-changed",
109
                    G_CALLBACK (document_page_changed_cb),
110
                    NULL);
111

            
112
1
  g_idle_add (document_emit_page_changed, my_document);
113

            
114
1
  my_document->loop = g_main_loop_new (NULL, FALSE);
115

            
116
1
  g_main_loop_run (my_document->loop);
117

            
118
1
  g_assert_cmpint (my_document->number_emissions, ==, EXPECTED_NUMBER);
119
1
  g_main_loop_unref (my_document->loop);
120
1
  g_object_unref (my_document);
121
1
}
122

            
123
int
124
1
main (gint argc,
125
      char *argv[])
126
{
127
1
  g_test_init (&argc, &argv, NULL);
128
1
  g_test_add_func ("/atk/document/page_changed", test_page_changed);
129

            
130
1
  return g_test_run ();
131
}