summaryrefslogtreecommitdiff
path: root/tests/test-console.sh
blob: 374afa8e6fb1815b8bcef09d428b04981e18f748 (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/bin/sh
# vim:set noet ts=4:
#
# ibus-anthy - The Anthy engine for IBus
#
# Copyright (c) 2018 Takao Fujiwara <takao.fujiwara1@gmail.com>
# Copyright (c) 2018 Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

# This test runs /usr/share/ibus-anthy/engine/engine.py after install anthy

PROGNAME=`basename $0`;
VERSION=0.1;
DISPLAY=:99.0;
BUILDDIR=".";
SRCDIR=".";
HAVE_GRAPHICS=1;
DESKTOP_COMMAND="gnome-session";
PYTHON="python3";
PID_XORG=0;
PID_GNOME_SESSION=0;
FORCE_TEST="";
RUN_ARGS="--exit";

usage()
{
    echo -e \
"This test runs /usr/share/ibus-anthy/engine/engine.py after install anthy\n"  \
"$PROGNAME [OPTIONS…]\n"                                                       \
"\n"                                                                           \
"OPTIONS:\n"                                                                   \
"-h, --help                       This help\n"                                 \
"-v, --version                    Show version\n"                              \
"-b, --builddir=BUILDDIR          Set the BUILDDIR\n"                          \
"-s, --srcdir=SOURCEDIR           Set the SOURCEDIR\n"                         \
"-c, --no-graphics                Use Xvfb instead of Xorg\n"                  \
"-p, --python=PATH                Use the PATH of python2 or python3\n"        \
"-d, --desktop=DESKTOP            Run DESTKTOP. The default is gnome-session\n"\
"-f, --force                      Run test suite forcibly\n"                   \
""
}

parse_args()
{
    # This is GNU getopt. "sudo port getopt" in BSD?
    ARGS=`getopt -o hvb:s:cp:d:f --long help,version,builddir:,srcdir:,no-graphics,python:,desktop:,force \
        -- "$@"`;
    eval set -- "$ARGS"
    while [ 1 ] ; do
        case "$1" in
        -h | --help )        usage; exit 0;;
        -v | --version )     echo -e "$VERSION"; exit 0;;
        -b | --builddir )    BUILDDIR="$2"; shift 2;;
        -s | --srcdir )      SRCDIR="$2"; shift 2;;
        -c | --no-graphics ) HAVE_GRAPHICS=0; shift;;
        -p | --python )      PYTHON="$2"; shift 2;;
        -d | --desktop )     DESKTOP_COMMAND="$2"; shift 2;;
        -f | --force )       FORCE_TEST="1"; shift;;
        -- )                 shift; break;;
        * )                  usage; exit 1;;
        esac;
    done;
}

init_desktop()
{
    if test x$FORCE_TEST != x ; then
        RUN_ARGS="$RUN_ARGS --force";
    fi;

    if test ! -f $HOME/.config/gnome-initial-setup-done ; then
        if test ! -f /var/lib/AccountsService/users/$USER ; then
            mkdir -p /var/lib/AccountsService/users
            cat >> /var/lib/AccountsService/users/$USER << _EOF
[User]
Language=ja_JP.UTF-8
XSession=gnome
SystemAccount=false
_EOF
        fi
        mkdir -p $HOME/.config
        touch $HOME/.config/gnome-initial-setup-done
    fi

    # Prevent from launching a XDG dialog
    XDG_LOCALE_FILE="$HOME/.config/user-dirs.locale"
    if test -f $XDG_LOCALE_FILE ; then
        XDG_LANG_ORIG=`cat $XDG_LOCALE_FILE`
        XDG_LANG_NEW=`echo $LANG | sed -e 's/\(.*\)\..*/\1/'`
        if [ "$XDG_LANG_ORIG" != "$XDG_LANG_NEW" ] ; then
            echo "Overriding XDG locale $XDG_LANG_ORIG with $XDG_LANG_NEW"
            echo "$XDG_LANG_NEW" > $XDG_LOCALE_FILE
        fi
    fi
}

run_dbus_daemon()
{
    a=`ps -ef | grep dbus-daemon | grep "\-\-system" | grep -v session | grep -v grep`
    if test x"$a" = x ; then
        eval `dbus-launch --sh-syntax`
    fi
    SUSER=`echo "$USER" | cut -c 1-7`;
    a=`ps -ef | grep dbus-daemon | grep "$SUSER" | grep -v gdm | grep session | grep -v grep`
    if test x"$a" = x ; then
        systemctl --user start dbus
        export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$UID/bus
    fi
    systemctl --user status dbus | col -b
    ps -ef | grep dbus-daemon | grep "$SUSER" | grep -v gdm | egrep 'session|system' | grep -v grep
    systemctl --user show-environment | col -b
}

run_desktop()
{
    if test $HAVE_GRAPHICS -eq 1 ; then
        /usr/libexec/Xorg.wrap -noreset +extension GLX +extension RANDR +extension RENDER -logfile ./xorg.log -config ./xorg.conf -configdir . $DISPLAY &
    else
        /usr/bin/Xvfb $DISPLAY -noreset +extension GLX +extension RANDR +extension RENDER -screen 0 1280x1024x24 &
    fi;
    PID_XORG=$!;
    sleep 1;
    export DISPLAY=$DISPLAY;
    $DESKTOP_COMMAND &
    PID_GNOME_SESSION=$!;
    sleep 30;
    if test "$DESKTOP_COMMAND" != "gnome-session" ; then
        ibus-daemon --daemonize --verbose;
        sleep 1;
    fi
}

run_test_suite()
{
    rm -rf $HOME/.anthy;
    rm -rf $HOME/.config/anthy;
    cd `dirname $0`;

    echo "#### Starting $PYTHON API test $RUN_ARGS";
    export GTK_IM_MODULE=ibus
    $PYTHON -u $SRCDIR/anthytest.py $RUN_ARGS;
    if test $? -ne 0 ; then
        exit -1;
    fi;
    if test x$FORCE_TEST = x ; then
        for ANTHY_CONFIG in ".anthy" ".config/anthy" ; do
            if test -d $HOME/$ANTHY_CONFIG ; then
                rm -r $HOME/$ANTHY_CONFIG;
            fi;
        done;
    fi;
}

finit()
{
    if "test $DESKTOP_COMMAND" != "gnome-session" ; then
        ibus exit;
    fi;
    kill $PID_GNOME_SESSION $PID_XORG;
}

main()
{
    parse_args $@;
    init_desktop;
    run_dbus_daemon;
    run_desktop;
    run_test_suite;
    finit;
}

main $@;