summaryrefslogtreecommitdiff
path: root/testsuite/gtk/adjustment.c
blob: 598408a58eb680580aa80bf871fa172237fad14d (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
153
154
/* GTK - The GIMP Toolkit
 * Copyright (C) 2014 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 */

#include <gtk/gtk.h>

static void
test_basic (void)
{
  GtkAdjustment *a;

  a = gtk_adjustment_new (2.0, 0.0, 100.0, 1.0, 5.0, 10.0);
  
  g_assert_cmpfloat (gtk_adjustment_get_value (a), ==, 2.0);
  g_assert_cmpfloat (gtk_adjustment_get_lower (a), ==, 0.0);
  g_assert_cmpfloat (gtk_adjustment_get_upper (a), ==, 100.0);
  g_assert_cmpfloat (gtk_adjustment_get_step_increment (a), ==, 1.0);
  g_assert_cmpfloat (gtk_adjustment_get_page_increment (a), ==, 5.0);
  g_assert_cmpfloat (gtk_adjustment_get_page_size (a), ==, 10.0);
  g_assert_cmpfloat (gtk_adjustment_get_minimum_increment (a), ==, 1.0);

  gtk_adjustment_set_value (a, 50.0);
  gtk_adjustment_set_lower (a, 20.0);
  gtk_adjustment_set_upper (a, 75.5);
  gtk_adjustment_set_step_increment (a, 2.2);
  gtk_adjustment_set_page_increment (a, 1.5);
  gtk_adjustment_set_page_size (a, 10.0);

  g_assert_cmpfloat (gtk_adjustment_get_value (a), ==, 50.0);
  g_assert_cmpfloat (gtk_adjustment_get_lower (a), ==, 20.0);
  g_assert_cmpfloat (gtk_adjustment_get_upper (a), ==, 75.5);
  g_assert_cmpfloat (gtk_adjustment_get_step_increment (a), ==, 2.2);
  g_assert_cmpfloat (gtk_adjustment_get_page_increment (a), ==, 1.5);
  g_assert_cmpfloat (gtk_adjustment_get_page_size (a), ==, 10.0);
  g_assert_cmpfloat (gtk_adjustment_get_minimum_increment (a), ==, 1.5);

  g_object_unref (a);
}

static gint changed_count;
static gint value_changed_count;

static void
changed_cb (GtkAdjustment *a)
{
  changed_count++;
}

static void
value_changed_cb (GtkAdjustment *a)
{
  value_changed_count++;
}

static void
test_signals (void)
{
  GtkAdjustment *a;

  a = gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);

  g_signal_connect (a, "changed", G_CALLBACK (changed_cb), NULL);
  g_signal_connect (a, "value-changed", G_CALLBACK (value_changed_cb), NULL);

  changed_count = value_changed_count = 0;
  gtk_adjustment_configure (a, 0.0, 0.0, 100.0, 1.0, 5.0, 0.0);
  g_assert_cmpint (changed_count, ==, 1);  
  g_assert_cmpint (value_changed_count, ==, 0);  

  changed_count = value_changed_count = 0;
  gtk_adjustment_set_value (a, 50.0);
  gtk_adjustment_set_lower (a, 20.0);
  gtk_adjustment_set_upper (a, 75.5);
  gtk_adjustment_set_step_increment (a, 2.2);
  gtk_adjustment_set_page_increment (a, 1.5);
  gtk_adjustment_set_page_size (a, 10.0);
  g_assert_cmpint (changed_count, ==, 5);  
  g_assert_cmpint (value_changed_count, ==, 1);  

  g_object_unref (a); 
}

static void
test_clamp (void)
{
  GtkAdjustment *a;

  a = gtk_adjustment_new (2.0, 0.0, 100.0, 1.0, 5.0, 10.0);

  gtk_adjustment_set_value (a, -10.0);
  g_assert_cmpfloat (gtk_adjustment_get_value (a), ==, 0.0);

  gtk_adjustment_set_value (a, 200.0);
  g_assert_cmpfloat (gtk_adjustment_get_value (a), ==, 90.0);

  gtk_adjustment_set_value (a, 99.0);
  g_assert_cmpfloat (gtk_adjustment_get_value (a), ==, 90.0);

  gtk_adjustment_configure (a, 0.0, 0.0, 10.0, 1.0, 5.0, 20.0);
  
  gtk_adjustment_set_value (a, 5.0);
  g_assert_cmpfloat (gtk_adjustment_get_value (a), ==, 0.0);

  g_object_unref (a);
}

static void
test_clamp_page (void)
{
  GtkAdjustment *a;

  a = gtk_adjustment_new (20.0, 0.0, 100.0, 1.0, 5.0, 10.0);

  gtk_adjustment_clamp_page (a, 50.0, 55.0);
  g_assert_cmpfloat (gtk_adjustment_get_value (a), ==, 45.0);

  gtk_adjustment_clamp_page (a, 52.0, 58.0);
  g_assert_cmpfloat (gtk_adjustment_get_value (a), ==, 48.0);

  gtk_adjustment_clamp_page (a, 48.0, 50.0);
  g_assert_cmpfloat (gtk_adjustment_get_value (a), ==, 48.0);

  gtk_adjustment_clamp_page (a, 30.0, 50.0);
  g_assert_cmpfloat (gtk_adjustment_get_value (a), ==, 30.0);

  g_object_unref (a);
}

int
main (int   argc,
      char *argv[])
{
  gtk_test_init (&argc, &argv);

  g_test_add_func ("/adjustment/basic", test_basic);
  g_test_add_func ("/adjustment/signals", test_signals);
  g_test_add_func ("/adjustment/clamp", test_clamp);
  g_test_add_func ("/adjustment/clamp_page", test_clamp_page);

  return g_test_run();
}