tests/plover-gtk/test-transactionhelper.c
author J. Ali Harlow <ali@juiblex.co.uk>
Tue Jun 28 19:05:38 2016 +0100 (2016-06-28)
changeset 51 ea1c42c3a780
parent 38 a29623b68ca2
child 61 31fb35727621
permissions -rw-r--r--
Fix mingw64 build
ali@38
     1
/*
ali@38
     2
 * Copyright (C) 2016  J. Ali Harlow <ali@juiblex.co.uk>
ali@38
     3
 *
ali@38
     4
 * This program is free software; you can redistribute it and/or modify
ali@38
     5
 * it under the terms of the GNU General Public License as published by
ali@38
     6
 * the Free Software Foundation; either version 2 of the License, or
ali@38
     7
 * (at your option) any later version.
ali@38
     8
 *
ali@38
     9
 * This program is distributed in the hope that it will be useful,
ali@38
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ali@38
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
ali@38
    12
 * GNU General Public License for more details.
ali@38
    13
 *
ali@38
    14
 * You should have received a copy of the GNU General Public License along
ali@38
    15
 * with this program; if not, write to the Free Software Foundation, Inc.,
ali@38
    16
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
ali@38
    17
 */
ali@38
    18
ali@38
    19
#include <stdlib.h>
ali@38
    20
#include <string.h>
ali@38
    21
#include <locale.h>
ali@38
    22
#include <sys/stat.h>
ali@38
    23
#include <glib.h>
ali@38
    24
#include <gdk/gdk.h>
ali@38
    25
#include <gtk/gtk.h>
ali@38
    26
#include <plover/plover.h>
ali@38
    27
#include <plover-gtk/transactionhelper.h>
ali@38
    28
ali@38
    29
GtkBuilder *ui;
ali@38
    30
gboolean manual_mode=FALSE;
ali@38
    31
ali@38
    32
PloverTransactionHelper *get_transaction_helper(void)
ali@38
    33
{
ali@38
    34
    const char *dir;
ali@38
    35
    gchar *s;
ali@38
    36
    GError *err=NULL;
ali@38
    37
    PloverTransactionHelper *helper;
ali@38
    38
    dir=g_getenv("gtk_srcdir");
ali@38
    39
    s=g_build_filename(dir,"software-installation.ui",NULL);
ali@38
    40
    ui=gtk_builder_new();
ali@38
    41
    if (!gtk_builder_add_from_file(ui,s,&err))
ali@38
    42
	g_error("%s: %s",s,err->message);
ali@38
    43
    g_free(s);
ali@38
    44
    helper=plover_transaction_helper_new(ui);
ali@38
    45
    g_object_unref(ui);
ali@38
    46
    return helper;
ali@38
    47
}
ali@38
    48
ali@38
    49
static void test_init(void)
ali@38
    50
{
ali@38
    51
    PloverTransactionHelper *helper;
ali@38
    52
    helper=get_transaction_helper();
ali@38
    53
    g_object_unref(helper);
ali@38
    54
}
ali@38
    55
ali@38
    56
static void test_basic_properties(void)
ali@38
    57
{
ali@38
    58
    const char *prefix;
ali@38
    59
    GError *err=NULL;
ali@38
    60
    struct comps *comps;
ali@38
    61
    PloverTransactionHelper *helper;
ali@38
    62
    PloverPackageSet *installed;
ali@38
    63
    PloverRepository *upstream;
ali@38
    64
    upstream=plover_repository_new_from_yum("../yum-repo-test-dir",&err);
ali@38
    65
    if (!upstream)
ali@38
    66
	g_error("../yum-repo-test-dir: %s",err->message);
ali@38
    67
    installed=plover_package_set_new_from_installed("../razor-test-dir",&err);
ali@38
    68
    if (!installed)
ali@38
    69
	g_error("../razor-test-dir: %s",err->message);
ali@38
    70
    helper=get_transaction_helper();
ali@38
    71
    g_assert(!plover_transaction_helper_get_visible(helper));
ali@38
    72
    plover_transaction_helper_set_installed(helper,installed);
ali@38
    73
    g_assert(plover_transaction_helper_get_installed(helper)==installed);
ali@38
    74
    plover_transaction_helper_set_upstream(helper,upstream);
ali@38
    75
    g_assert(plover_transaction_helper_get_upstream(helper,&err)==upstream);
ali@38
    76
    g_assert(!err);
ali@38
    77
    plover_transaction_helper_set_base(helper,"../yum-repo-test-dir");
ali@38
    78
    g_assert_cmpstr(plover_transaction_helper_get_base(helper),==,"../yum-repo-test-dir");
ali@38
    79
    comps=plover_transaction_helper_get_comps(helper,&err);
ali@38
    80
    g_assert(!err);
ali@38
    81
    g_assert(plover_comps_lookup_group(comps,"base"));
ali@38
    82
    prefix=plover_transaction_helper_get_prefix(helper,&err);
ali@38
    83
    g_assert(!err);
ali@38
    84
    g_assert_cmpstr(prefix,==,plover_default_prefix_for_vendor("Acme Corporation"));
ali@38
    85
    g_assert(!plover_transaction_helper_get_visible(helper));
ali@38
    86
    g_assert(!plover_transaction_helper_get_error(helper,NULL));
ali@38
    87
    g_object_unref(upstream);
ali@38
    88
    g_object_unref(installed);
ali@38
    89
    g_object_unref(helper);
ali@38
    90
}
ali@38
    91
ali@38
    92
static void test_install_group(void)
ali@38
    93
{
ali@42
    94
    gchar *root,*root_uri;
ali@38
    95
    GError *err=NULL;
ali@42
    96
    GFile *file;
ali@38
    97
    PloverPackageSet *installed;
ali@38
    98
    PloverTransactionHelper *helper;
ali@38
    99
    root=g_strdup("razor-test-dir-XXXXXX");
ali@38
   100
    g_assert(mkdtemp(root));
ali@42
   101
    file=g_file_new_for_path(root);
ali@38
   102
    g_free(root);
ali@42
   103
    root_uri=g_file_get_uri(file);
ali@42
   104
    g_object_unref(file);
ali@42
   105
    g_setenv("RAZOR_ROOT",root_uri,TRUE);
ali@42
   106
    g_free(root_uri);
ali@38
   107
    helper=get_transaction_helper();
ali@38
   108
    installed=plover_package_set_new_from_installed("../razor-test-dir",&err);
ali@38
   109
    if (!installed)
ali@38
   110
	g_error("../razor-test-dir: %s",err->message);
ali@38
   111
    plover_transaction_helper_set_installed(helper,installed);
ali@38
   112
    g_object_unref(installed);
ali@38
   113
    plover_transaction_helper_set_base(helper,"../yum-repo-test-dir");
ali@38
   114
    if (!plover_transaction_helper_install_group(helper,"base",&err))
ali@38
   115
	g_error("base: %s",err->message);
ali@38
   116
    g_assert(!err);
ali@38
   117
    g_object_unref(helper);
ali@38
   118
    g_unsetenv("RAZOR_ROOT");
ali@38
   119
}
ali@38
   120
ali@38
   121
static void test_remove_group(void)
ali@38
   122
{
ali@42
   123
    gchar *root,*root_uri;
ali@38
   124
    GError *err=NULL;
ali@42
   125
    GFile *file;
ali@38
   126
    PloverPackageSet *installed;
ali@38
   127
    PloverTransactionHelper *helper;
ali@38
   128
    struct plover_vector *packages;
ali@38
   129
    char *pkgs[]={"zip",NULL};
ali@38
   130
    root=g_strdup("razor-test-dir-XXXXXX");
ali@38
   131
    g_assert(mkdtemp(root));
ali@42
   132
    file=g_file_new_for_path(root);
ali@42
   133
    root_uri=g_file_get_uri(file);
ali@42
   134
    g_object_unref(file);
ali@42
   135
    g_setenv("RAZOR_ROOT",root_uri,TRUE);
ali@42
   136
    g_free(root_uri);
ali@38
   137
    helper=get_transaction_helper();
ali@38
   138
    plover_transaction_helper_set_base(helper,"../yum-repo-test-dir");
ali@38
   139
    packages=plover_transaction_helper_group_get_default_packages(helper,
ali@38
   140
      "zappy",&err);
ali@38
   141
    if (!packages)
ali@38
   142
	g_error("zappy: %s",err->message);
ali@38
   143
    if (!plover_install("../yum-repo-test-dir",NULL,packages->strings,&err))
ali@38
   144
	g_error("plover_install: %s",err->message);
ali@38
   145
    plover_vector_free(packages);
ali@38
   146
    installed=plover_package_set_new_from_installed(root,&err);
ali@38
   147
    if (!installed)
ali@38
   148
	g_error("%s: %s",root,err->message);
ali@38
   149
    plover_transaction_helper_set_installed(helper,installed);
ali@38
   150
    g_object_unref(installed);
ali@38
   151
    if (!plover_transaction_helper_remove_group(helper,"zappy",&err))
ali@38
   152
	g_error("zappy: %s",err->message);
ali@38
   153
    g_assert(!err);
ali@38
   154
    g_object_unref(helper);
ali@38
   155
    g_unsetenv("RAZOR_ROOT");
ali@38
   156
    g_free(root);
ali@38
   157
}
ali@38
   158
ali@38
   159
static void test_update(void)
ali@38
   160
{
ali@42
   161
    gchar *root,*root_uri;
ali@38
   162
    GError *err=NULL;
ali@42
   163
    GFile *file;
ali@38
   164
    PloverPackageSet *installed;
ali@38
   165
    PloverTransactionHelper *helper;
ali@38
   166
    root=g_strdup("razor-test-dir-XXXXXX");
ali@38
   167
    g_assert(mkdtemp(root));
ali@42
   168
    file=g_file_new_for_path(root);
ali@38
   169
    g_free(root);
ali@42
   170
    root_uri=g_file_get_uri(file);
ali@42
   171
    g_object_unref(file);
ali@42
   172
    g_setenv("RAZOR_ROOT",root_uri,TRUE);
ali@42
   173
    g_free(root_uri);
ali@38
   174
    helper=get_transaction_helper();
ali@38
   175
    installed=plover_package_set_new_from_installed("../razor-test-dir",&err);
ali@38
   176
    if (!installed)
ali@38
   177
	g_error("../razor-test-dir: %s",err->message);
ali@38
   178
    plover_transaction_helper_set_installed(helper,installed);
ali@38
   179
    g_object_unref(installed);
ali@38
   180
    plover_transaction_helper_set_base(helper,"../yum-repo-test-dir");
ali@38
   181
    if (plover_transaction_helper_update(helper,&err))
ali@38
   182
	g_error("plover_transaction_helper_update reports work to be done");
ali@38
   183
    g_assert_error(err,PLOVER_GENERAL_ERROR,PLOVER_GENERAL_ERROR_NO_WORK);
ali@38
   184
    g_object_unref(helper);
ali@38
   185
    g_unsetenv("RAZOR_ROOT");
ali@38
   186
}
ali@38
   187
ali@38
   188
struct run_install_baton {
ali@38
   189
    enum {
ali@38
   190
	RI_STATE_INIT = 0,
ali@38
   191
	RI_STATE_SUMMARY,
ali@38
   192
	RI_STATE_PROGRESS,
ali@38
   193
	RI_STATE_PROGRESS_DELAY,
ali@38
   194
	RI_STATE_DONE,
ali@38
   195
	RI_STATE_FINISH
ali@38
   196
    } state;
ali@38
   197
    guint eid;		/* event ID (or 0) */
ali@38
   198
    PloverTransactionHelper *helper;
ali@38
   199
};
ali@38
   200
ali@38
   201
gboolean run_install_tick(gpointer data)
ali@38
   202
{
ali@38
   203
    gboolean retval=TRUE;
ali@38
   204
    struct run_install_baton *baton=data;
ali@38
   205
    GtkWidget *page;
ali@38
   206
    GtkAssistant *assistant=baton->helper->assistant;
ali@38
   207
    switch(baton->state)
ali@38
   208
    {
ali@38
   209
	case RI_STATE_INIT:
ali@38
   210
	    if (!assistant || !gtk_widget_get_visible(GTK_WIDGET(assistant)))
ali@38
   211
		return TRUE;
ali@38
   212
	    if (!manual_mode)
ali@38
   213
		gtk_button_clicked(GTK_BUTTON(assistant->forward));
ali@38
   214
	    break;
ali@38
   215
	case RI_STATE_SUMMARY:
ali@38
   216
	    if (gtk_assistant_get_current_page(assistant)<1)
ali@38
   217
		return TRUE;
ali@38
   218
	    if (!manual_mode)
ali@38
   219
		gtk_button_clicked(GTK_BUTTON(assistant->apply));
ali@38
   220
	    break;
ali@38
   221
	case RI_STATE_PROGRESS:
ali@38
   222
	    if (gtk_assistant_get_current_page(assistant)<2)
ali@38
   223
		return TRUE;
ali@38
   224
	    page=gtk_assistant_get_nth_page(assistant,2);
ali@38
   225
	    baton->eid=g_timeout_add_seconds(1,run_install_tick,baton);
ali@38
   226
	    if (!gtk_assistant_get_page_complete(assistant,page))
ali@38
   227
		return FALSE;
ali@38
   228
	    else
ali@38
   229
		retval=FALSE;
ali@38
   230
	    break;
ali@38
   231
	case RI_STATE_PROGRESS_DELAY:
ali@38
   232
	    retval=FALSE;
ali@38
   233
	    baton->eid=g_idle_add_full(G_PRIORITY_LOW,run_install_tick,baton,
ali@38
   234
	      NULL);
ali@38
   235
	    if (!manual_mode)
ali@38
   236
		gtk_button_clicked(GTK_BUTTON(assistant->forward));
ali@38
   237
	    break;
ali@38
   238
	case RI_STATE_DONE:
ali@38
   239
	    if (gtk_assistant_get_current_page(assistant)<3)
ali@38
   240
		return TRUE;
ali@38
   241
	    if (!manual_mode)
ali@38
   242
		gtk_button_clicked(GTK_BUTTON(assistant->close));
ali@38
   243
	    break;
ali@38
   244
	case RI_STATE_FINISH:
ali@38
   245
	    if (assistant && gtk_widget_get_visible(GTK_WIDGET(assistant)))
ali@38
   246
		return TRUE;
ali@38
   247
	    gtk_main_quit();
ali@38
   248
	    baton->eid=0;
ali@38
   249
	    return FALSE;
ali@38
   250
    }
ali@38
   251
    baton->state++;
ali@38
   252
    return retval;
ali@38
   253
}
ali@38
   254
ali@38
   255
static void test_run_install(void)
ali@38
   256
{
ali@42
   257
    gchar *root,*root_uri;
ali@38
   258
    GError *err=NULL;
ali@42
   259
    GFile *file;
ali@38
   260
    struct plover_vector *packages;
ali@38
   261
    PloverPackageSet *installed;
ali@38
   262
    PloverTransactionHelper *helper;
ali@38
   263
    struct run_install_baton baton={0,};
ali@38
   264
    root=g_strdup("razor-test-dir-XXXXXX");
ali@38
   265
    g_assert(mkdtemp(root));
ali@42
   266
    file=g_file_new_for_path(root);
ali@42
   267
    root_uri=g_file_get_uri(file);
ali@42
   268
    g_object_unref(file);
ali@42
   269
    g_setenv("RAZOR_ROOT",root_uri,TRUE);
ali@42
   270
    g_free(root_uri);
ali@38
   271
    helper=get_transaction_helper();
ali@38
   272
    installed=plover_package_set_new();
ali@38
   273
    if (!plover_package_set_open(installed,root,TRUE,&err))
ali@38
   274
	g_error("%s: %s",root,err->message);
ali@38
   275
    plover_transaction_helper_set_installed(helper,installed);
ali@38
   276
    g_object_unref(installed);
ali@38
   277
    plover_transaction_helper_set_base(helper,"../yum-repo-test-dir");
ali@38
   278
    packages=plover_vector_new();
ali@38
   279
    plover_vector_append(packages,"zappy-tools");
ali@38
   280
    if (!plover_transaction_helper_install_packages(helper,packages,&err))
ali@38
   281
	g_error("zappy-tools: %s",err->message);
ali@38
   282
    g_assert(!err);
ali@38
   283
    plover_vector_free(packages);
ali@38
   284
    plover_transaction_helper_present(helper);
ali@38
   285
    baton.helper=helper;
ali@38
   286
    baton.eid=g_idle_add_full(G_PRIORITY_LOW,run_install_tick,&baton,NULL);
ali@38
   287
    gtk_main();
ali@38
   288
    g_object_unref(helper);
ali@38
   289
    g_unsetenv("RAZOR_ROOT");
ali@38
   290
    g_free(root);
ali@38
   291
}
ali@38
   292
ali@38
   293
struct run_remove_baton {
ali@38
   294
    enum {
ali@38
   295
	RR_STATE_INIT = 0,
ali@38
   296
	RR_STATE_SUMMARY,
ali@38
   297
	RR_STATE_PROGRESS,
ali@38
   298
	RR_STATE_PROGRESS_DELAY,
ali@38
   299
	RR_STATE_DONE,
ali@38
   300
	RR_STATE_FINISH
ali@38
   301
    } state;
ali@38
   302
    guint eid;		/* event ID (or 0) */
ali@38
   303
    PloverTransactionHelper *helper;
ali@38
   304
};
ali@38
   305
ali@38
   306
gboolean run_remove_tick(gpointer data)
ali@38
   307
{
ali@38
   308
    gboolean retval=TRUE;
ali@38
   309
    struct run_remove_baton *baton=data;
ali@38
   310
    GtkWidget *page;
ali@38
   311
    GtkAssistant *assistant=baton->helper->assistant;
ali@38
   312
    switch(baton->state)
ali@38
   313
    {
ali@38
   314
	case RR_STATE_INIT:
ali@38
   315
	    if (!assistant || !gtk_widget_get_visible(GTK_WIDGET(assistant)))
ali@38
   316
		return TRUE;
ali@38
   317
	    if (!manual_mode)
ali@38
   318
		gtk_button_clicked(GTK_BUTTON(assistant->forward));
ali@38
   319
	    break;
ali@38
   320
	case RR_STATE_SUMMARY:
ali@38
   321
	    if (gtk_assistant_get_current_page(assistant)<1)
ali@38
   322
		return TRUE;
ali@38
   323
	    if (!manual_mode)
ali@38
   324
		gtk_button_clicked(GTK_BUTTON(assistant->apply));
ali@38
   325
	    break;
ali@38
   326
	case RR_STATE_PROGRESS:
ali@38
   327
	    if (gtk_assistant_get_current_page(assistant)<2)
ali@38
   328
		return TRUE;
ali@38
   329
	    page=gtk_assistant_get_nth_page(assistant,2);
ali@38
   330
	    baton->eid=g_timeout_add_seconds(1,run_remove_tick,baton);
ali@38
   331
	    if (!gtk_assistant_get_page_complete(assistant,page))
ali@38
   332
		return FALSE;
ali@38
   333
	    else
ali@38
   334
		retval=FALSE;
ali@38
   335
	    break;
ali@38
   336
	case RR_STATE_PROGRESS_DELAY:
ali@38
   337
	    retval=FALSE;
ali@38
   338
	    baton->eid=g_idle_add_full(G_PRIORITY_LOW,run_remove_tick,baton,
ali@38
   339
	      NULL);
ali@38
   340
	    if (!manual_mode)
ali@38
   341
		gtk_button_clicked(GTK_BUTTON(assistant->forward));
ali@38
   342
	    break;
ali@38
   343
	case RR_STATE_DONE:
ali@38
   344
	    if (gtk_assistant_get_current_page(assistant)<3)
ali@38
   345
		return TRUE;
ali@38
   346
	    if (!manual_mode)
ali@38
   347
		gtk_button_clicked(GTK_BUTTON(assistant->close));
ali@38
   348
	    break;
ali@38
   349
	case RR_STATE_FINISH:
ali@38
   350
	    if (assistant && gtk_widget_get_visible(GTK_WIDGET(assistant)))
ali@38
   351
		return TRUE;
ali@38
   352
	    gtk_main_quit();
ali@38
   353
	    baton->eid=0;
ali@38
   354
	    return FALSE;
ali@38
   355
    }
ali@38
   356
    baton->state++;
ali@38
   357
    return retval;
ali@38
   358
}
ali@38
   359
ali@38
   360
static void test_run_remove(void)
ali@38
   361
{
ali@42
   362
    gchar *root,*root_uri;
ali@38
   363
    GError *err=NULL;
ali@42
   364
    GFile *file;
ali@38
   365
    struct plover_vector *packages;
ali@38
   366
    PloverPackageSet *installed;
ali@38
   367
    PloverTransactionHelper *helper;
ali@38
   368
    struct run_remove_baton baton={0,};
ali@38
   369
    root=g_strdup("razor-test-dir-XXXXXX");
ali@38
   370
    g_assert(mkdtemp(root));
ali@42
   371
    file=g_file_new_for_path(root);
ali@42
   372
    root_uri=g_file_get_uri(file);
ali@42
   373
    g_object_unref(file);
ali@42
   374
    g_setenv("RAZOR_ROOT",root_uri,TRUE);
ali@42
   375
    g_free(root_uri);
ali@38
   376
    helper=get_transaction_helper();
ali@38
   377
    plover_transaction_helper_set_base(helper,"../yum-repo-test-dir");
ali@38
   378
    packages=
ali@38
   379
      plover_transaction_helper_group_get_default_packages(helper,"zappy",&err);
ali@38
   380
    if (!packages)
ali@38
   381
	g_error("zappy: %s",err->message);
ali@38
   382
    if (!plover_install("../yum-repo-test-dir",NULL,packages->strings,&err))
ali@38
   383
	g_error("plover_install: %s",err->message);
ali@38
   384
    plover_vector_free(packages);
ali@38
   385
    installed=plover_package_set_new();
ali@38
   386
    if (!plover_package_set_open(installed,root,TRUE,&err))
ali@38
   387
	g_error("%s: %s",root,err->message);
ali@38
   388
    plover_transaction_helper_set_installed(helper,installed);
ali@38
   389
    g_object_unref(installed);
ali@38
   390
    if (!plover_transaction_helper_remove_group(helper,"zappy",&err))
ali@38
   391
	g_error("zappy: %s",err->message);
ali@38
   392
    g_assert(!err);
ali@38
   393
    plover_transaction_helper_present(helper);
ali@38
   394
    baton.helper=helper;
ali@38
   395
    baton.eid=g_idle_add_full(G_PRIORITY_LOW,run_remove_tick,&baton,NULL);
ali@38
   396
    gtk_main();
ali@38
   397
    g_object_unref(helper);
ali@38
   398
    g_unsetenv("RAZOR_ROOT");
ali@38
   399
    g_free(root);
ali@38
   400
}
ali@38
   401
ali@38
   402
struct run_update_baton {
ali@38
   403
    enum {
ali@38
   404
	RU_STATE_INIT = 0,
ali@38
   405
	RU_STATE_SUMMARY,
ali@38
   406
	RU_STATE_PROGRESS,
ali@38
   407
	RU_STATE_PROGRESS_DELAY,
ali@38
   408
	RU_STATE_DONE,
ali@38
   409
	RU_STATE_FINISH
ali@38
   410
    } state;
ali@38
   411
    guint eid;		/* event ID (or 0) */
ali@38
   412
    PloverTransactionHelper *helper;
ali@38
   413
};
ali@38
   414
ali@38
   415
gboolean run_update_tick(gpointer data)
ali@38
   416
{
ali@38
   417
    gboolean retval=TRUE;
ali@38
   418
    struct run_update_baton *baton=data;
ali@38
   419
    GtkWidget *page;
ali@38
   420
    GtkAssistant *assistant=baton->helper->assistant;
ali@38
   421
    switch(baton->state)
ali@38
   422
    {
ali@38
   423
	case RU_STATE_INIT:
ali@38
   424
	    if (!assistant || !gtk_widget_get_visible(GTK_WIDGET(assistant)))
ali@38
   425
		return TRUE;
ali@38
   426
	    if (!manual_mode)
ali@38
   427
		gtk_button_clicked(GTK_BUTTON(assistant->forward));
ali@38
   428
	    break;
ali@38
   429
	case RU_STATE_SUMMARY:
ali@38
   430
	    if (gtk_assistant_get_current_page(assistant)<1)
ali@38
   431
		return TRUE;
ali@38
   432
	    if (!manual_mode)
ali@38
   433
		gtk_button_clicked(GTK_BUTTON(assistant->apply));
ali@38
   434
	    break;
ali@38
   435
	case RU_STATE_PROGRESS:
ali@38
   436
	    if (gtk_assistant_get_current_page(assistant)<2)
ali@38
   437
		return TRUE;
ali@38
   438
	    page=gtk_assistant_get_nth_page(assistant,2);
ali@38
   439
	    baton->eid=g_timeout_add_seconds(1,run_update_tick,baton);
ali@38
   440
	    if (!gtk_assistant_get_page_complete(assistant,page))
ali@38
   441
		return FALSE;
ali@38
   442
	    else
ali@38
   443
		retval=FALSE;
ali@38
   444
	    break;
ali@38
   445
	case RU_STATE_PROGRESS_DELAY:
ali@38
   446
	    retval=FALSE;
ali@38
   447
	    baton->eid=g_idle_add_full(G_PRIORITY_LOW,run_update_tick,baton,
ali@38
   448
	      NULL);
ali@38
   449
	    if (!manual_mode)
ali@38
   450
		gtk_button_clicked(GTK_BUTTON(assistant->forward));
ali@38
   451
	    break;
ali@38
   452
	case RU_STATE_DONE:
ali@38
   453
	    if (gtk_assistant_get_current_page(assistant)<3)
ali@38
   454
		return TRUE;
ali@38
   455
	    if (!manual_mode)
ali@38
   456
		gtk_button_clicked(GTK_BUTTON(assistant->close));
ali@38
   457
	    break;
ali@38
   458
	case RU_STATE_FINISH:
ali@38
   459
	    if (assistant && gtk_widget_get_visible(GTK_WIDGET(assistant)))
ali@38
   460
		return TRUE;
ali@38
   461
	    gtk_main_quit();
ali@38
   462
	    baton->eid=0;
ali@38
   463
	    return FALSE;
ali@38
   464
    }
ali@38
   465
    baton->state++;
ali@38
   466
    return retval;
ali@38
   467
}
ali@38
   468
ali@38
   469
static void test_run_update(void)
ali@38
   470
{
ali@42
   471
    gchar *root,*root_uri;
ali@38
   472
    GError *err=NULL;
ali@42
   473
    GFile *file;
ali@38
   474
    struct razor_importer *importer;
ali@38
   475
    struct razor_set *downgraded;
ali@38
   476
    struct razor_atomic *atomic;
ali@38
   477
    struct plover_vector *packages;
ali@38
   478
    PloverPackageSet *installed;
ali@38
   479
    PloverTransactionHelper *helper;
ali@38
   480
    struct run_update_baton baton={0,};
ali@38
   481
    root=g_strdup("razor-test-dir-XXXXXX");
ali@38
   482
    g_assert(mkdtemp(root));
ali@42
   483
    file=g_file_new_for_path(root);
ali@42
   484
    root_uri=g_file_get_uri(file);
ali@42
   485
    g_object_unref(file);
ali@42
   486
    g_setenv("RAZOR_ROOT",root_uri,TRUE);
ali@42
   487
    g_free(root_uri);
ali@38
   488
    helper=get_transaction_helper();
ali@38
   489
    plover_transaction_helper_set_base(helper,"../yum-repo-test-dir");
ali@38
   490
    installed=plover_package_set_new();
ali@38
   491
    if (!plover_package_set_open(installed,root,TRUE,&err))
ali@38
   492
	g_error("%s: %s",root,err->message);
ali@38
   493
    importer=razor_importer_create();
ali@38
   494
    razor_importer_begin_package(importer,"zappy","0-1","noarch");
ali@38
   495
    razor_importer_add_details(importer,"","","","");
ali@38
   496
    razor_importer_add_property(importer,"zappy",RAZOR_PROPERTY_PROVIDES,"0-1");
ali@38
   497
    razor_importer_finish_package(importer);
ali@38
   498
    downgraded=razor_importer_finish(importer);
ali@38
   499
    atomic=razor_atomic_open("Add downgraded packages");
ali@38
   500
    if (!plover_package_set_update(installed,downgraded,atomic) ||
ali@38
   501
      razor_atomic_commit(atomic))
ali@38
   502
	g_error("%s: %s",root,razor_atomic_get_error_msg(atomic));
ali@38
   503
    razor_atomic_destroy(atomic);
ali@38
   504
    razor_set_unref(downgraded);
ali@38
   505
    plover_transaction_helper_set_installed(helper,installed);
ali@38
   506
    g_object_unref(installed);
ali@38
   507
    if (!plover_transaction_helper_update(helper,&err))
ali@38
   508
	g_error("update: %s",err->message);
ali@38
   509
    g_assert(!err);
ali@38
   510
    plover_transaction_helper_present(helper);
ali@38
   511
    baton.helper=helper;
ali@38
   512
    baton.eid=g_idle_add_full(G_PRIORITY_LOW,run_update_tick,&baton,NULL);
ali@38
   513
    gtk_main();
ali@38
   514
    g_object_unref(helper);
ali@38
   515
    g_unsetenv("RAZOR_ROOT");
ali@38
   516
    g_free(root);
ali@38
   517
}
ali@38
   518
ali@38
   519
struct check_vendor_baton {
ali@38
   520
    enum {
ali@38
   521
	CV_STATE_INIT = 0,
ali@38
   522
	CV_STATE_SUMMARY,
ali@38
   523
	CV_STATE_PROGRESS,
ali@38
   524
	CV_STATE_PROGRESS_DELAY,
ali@38
   525
	CV_STATE_DONE,
ali@38
   526
	CV_STATE_FINISH
ali@38
   527
    } state;
ali@38
   528
    guint eid;		/* event ID (or 0) */
ali@38
   529
    PloverTransactionHelper *helper;
ali@38
   530
};
ali@38
   531
ali@38
   532
gboolean check_vendor_tick(gpointer data)
ali@38
   533
{
ali@38
   534
    gboolean retval=TRUE;
ali@38
   535
    struct check_vendor_baton *baton=data;
ali@38
   536
    GtkWidget *page,*w;
ali@38
   537
    GtkAssistant *assistant=baton->helper->assistant;
ali@38
   538
    switch(baton->state)
ali@38
   539
    {
ali@38
   540
	case CV_STATE_INIT:
ali@38
   541
	    if (!assistant || !gtk_widget_get_visible(GTK_WIDGET(assistant)))
ali@38
   542
		return TRUE;
ali@38
   543
	    if (!manual_mode)
ali@38
   544
		gtk_button_clicked(GTK_BUTTON(assistant->forward));
ali@38
   545
	    break;
ali@38
   546
	case CV_STATE_SUMMARY:
ali@38
   547
	    if (gtk_assistant_get_current_page(assistant)<1)
ali@38
   548
		return TRUE;
ali@38
   549
	    g_assert(!gtk_widget_is_sensitive(assistant->apply));
ali@38
   550
	    w=GTK_WIDGET(gtk_builder_get_object(ui,"SIRemoveExisting"));
ali@38
   551
	    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w),TRUE);
ali@38
   552
	    g_assert(gtk_widget_is_sensitive(assistant->apply));
ali@38
   553
	    if (!manual_mode)
ali@38
   554
		gtk_button_clicked(GTK_BUTTON(assistant->apply));
ali@38
   555
	    break;
ali@38
   556
	case CV_STATE_PROGRESS:
ali@38
   557
	    if (gtk_assistant_get_current_page(assistant)<2)
ali@38
   558
		return TRUE;
ali@38
   559
	    page=gtk_assistant_get_nth_page(assistant,2);
ali@38
   560
	    baton->eid=g_timeout_add_seconds(1,check_vendor_tick,baton);
ali@38
   561
	    if (!gtk_assistant_get_page_complete(assistant,page))
ali@38
   562
		return FALSE;
ali@38
   563
	    else
ali@38
   564
		retval=FALSE;
ali@38
   565
	    break;
ali@38
   566
	case CV_STATE_PROGRESS_DELAY:
ali@38
   567
	    retval=FALSE;
ali@38
   568
	    baton->eid=g_idle_add_full(G_PRIORITY_LOW,check_vendor_tick,baton,
ali@38
   569
	      NULL);
ali@38
   570
	    if (!manual_mode)
ali@38
   571
		gtk_button_clicked(GTK_BUTTON(assistant->forward));
ali@38
   572
	    break;
ali@38
   573
	case CV_STATE_DONE:
ali@38
   574
	    if (gtk_assistant_get_current_page(assistant)<3)
ali@38
   575
		return TRUE;
ali@38
   576
	    if (!manual_mode)
ali@38
   577
		gtk_button_clicked(GTK_BUTTON(assistant->close));
ali@38
   578
	    break;
ali@38
   579
	case CV_STATE_FINISH:
ali@38
   580
	    if (assistant && gtk_widget_get_visible(GTK_WIDGET(assistant)))
ali@38
   581
		return TRUE;
ali@38
   582
	    gtk_main_quit();
ali@38
   583
	    baton->eid=0;
ali@38
   584
	    return FALSE;
ali@38
   585
    }
ali@38
   586
    baton->state++;
ali@38
   587
    return retval;
ali@38
   588
}
ali@38
   589
ali@38
   590
static void test_check_vendor(void)
ali@38
   591
{
ali@38
   592
    int fh;
ali@42
   593
    gchar *root,*root_uri,*s;
ali@42
   594
    GFile *file;
ali@38
   595
    GError *err=NULL;
ali@38
   596
    struct razor_importer *importer;
ali@38
   597
    struct razor_set *downgraded;
ali@38
   598
    struct razor_atomic *atomic;
ali@38
   599
    struct plover_vector *packages;
ali@38
   600
    PloverPackageSet *installed;
ali@38
   601
    PloverTransactionHelper *helper;
ali@38
   602
    struct run_update_baton baton={0,};
ali@38
   603
    g_setenv("PLOVER_VENDOR_PREFIX","/srv",TRUE);
ali@38
   604
    root=g_strdup("razor-test-dir-XXXXXX");
ali@38
   605
    g_assert(mkdtemp(root));
ali@42
   606
    file=g_file_new_for_path(root);
ali@42
   607
    root_uri=g_file_get_uri(file);
ali@42
   608
    g_object_unref(file);
ali@42
   609
    g_setenv("RAZOR_ROOT",root_uri,TRUE);
ali@38
   610
    helper=get_transaction_helper();
ali@38
   611
    plover_transaction_helper_set_check_vendor(helper,TRUE);
ali@38
   612
    plover_transaction_helper_set_base(helper,"../yum-repo-test-dir");
ali@38
   613
    installed=plover_package_set_new();
ali@38
   614
    if (!plover_package_set_open(installed,root,TRUE,&err))
ali@38
   615
	g_error("%s: %s",root,err->message);
ali@38
   616
    importer=razor_importer_create();
ali@38
   617
    razor_importer_begin_package(importer,"zappy","0-1","noarch");
ali@38
   618
    razor_importer_add_details(importer,"","","","");
ali@38
   619
    razor_importer_add_property(importer,"zappy",RAZOR_PROPERTY_PROVIDES,"0-1");
ali@38
   620
    razor_importer_add_install_prefix(importer,"/test");
ali@38
   621
    razor_importer_add_file(importer,"/test/bin/zappy");
ali@38
   622
    razor_importer_finish_package(importer);
ali@38
   623
    downgraded=razor_importer_finish(importer);
ali@38
   624
    atomic=razor_atomic_open("Add downgraded packages");
ali@42
   625
    razor_atomic_make_dirs(atomic,root_uri,"/test/bin/zappy");
ali@42
   626
    s=g_build_filename(root_uri,"test/bin/zappy",NULL);
ali@38
   627
    fh=razor_atomic_create_file(atomic,s,S_IRWXU|S_IRWXG|S_IRWXO);
ali@38
   628
    g_free(s);
ali@38
   629
    razor_atomic_close(atomic,fh);
ali@38
   630
    if (!plover_package_set_update(installed,downgraded,atomic) ||
ali@38
   631
      razor_atomic_commit(atomic))
ali@38
   632
	g_error("%s: %s",root,razor_atomic_get_error_msg(atomic));
ali@38
   633
    razor_atomic_destroy(atomic);
ali@38
   634
    razor_set_unref(downgraded);
ali@38
   635
    plover_transaction_helper_set_installed(helper,installed);
ali@38
   636
    g_object_unref(installed);
ali@38
   637
    if (!plover_transaction_helper_update(helper,&err))
ali@38
   638
	g_error("update: %s",err->message);
ali@38
   639
    g_assert(!err);
ali@38
   640
    plover_transaction_helper_present(helper);
ali@38
   641
    baton.helper=helper;
ali@38
   642
    baton.eid=g_idle_add_full(G_PRIORITY_LOW,check_vendor_tick,&baton,NULL);
ali@38
   643
    gtk_main();
ali@38
   644
    g_object_unref(helper);
ali@38
   645
    g_unsetenv("RAZOR_ROOT");
ali@38
   646
    g_free(root);
ali@42
   647
    g_free(root_uri);
ali@38
   648
    g_unsetenv("PLOVER_VENDOR_PREFIX");
ali@38
   649
}
ali@38
   650
ali@38
   651
struct set_error_baton {
ali@38
   652
    enum {
ali@38
   653
	SE_STATE_INIT = 0,
ali@38
   654
	SE_STATE_FINISH
ali@38
   655
    } state;
ali@38
   656
    PloverTransactionHelper *helper;
ali@38
   657
};
ali@38
   658
ali@38
   659
/*
ali@38
   660
 * This handler may be called as either an event (ie., idle or timeout)
ali@38
   661
 * or as a (swapped) signal. In the latter case, the return is ignored.
ali@38
   662
 */
ali@38
   663
gboolean set_error_tick(gpointer data)
ali@38
   664
{
ali@38
   665
    struct set_error_baton *baton=data;
ali@38
   666
    GtkDialog *dlg;
ali@38
   667
    GtkWidget *button;
ali@38
   668
    dlg=GTK_DIALOG(baton->helper->error_dialog);
ali@38
   669
    switch(baton->state)
ali@38
   670
    {
ali@38
   671
	case SE_STATE_INIT:
ali@38
   672
	    if (!dlg || !gtk_widget_get_visible(GTK_WIDGET(dlg)))
ali@38
   673
		return TRUE;
ali@38
   674
	    button=gtk_dialog_get_widget_for_response(dlg,GTK_RESPONSE_CLOSE);
ali@38
   675
	    if (!manual_mode)
ali@38
   676
		gtk_button_clicked(GTK_BUTTON(button));
ali@38
   677
	    break;
ali@38
   678
	case SE_STATE_FINISH:
ali@38
   679
	    if (dlg && gtk_widget_get_visible(GTK_WIDGET(dlg)))
ali@38
   680
		return TRUE;
ali@38
   681
	    gtk_main_quit();
ali@38
   682
	    return FALSE;
ali@38
   683
    }
ali@38
   684
    baton->state++;
ali@38
   685
    return TRUE;
ali@38
   686
}
ali@38
   687
ali@38
   688
static void test_set_error(void)
ali@38
   689
{
ali@42
   690
    gchar *root,*root_uri;
ali@38
   691
    const char *errmsg;
ali@38
   692
    GError *err=NULL;
ali@38
   693
    const GError *err2=NULL;
ali@42
   694
    GFile *file;
ali@38
   695
    PloverPackageSet *installed;
ali@38
   696
    PloverTransactionHelper *helper;
ali@38
   697
    struct set_error_baton baton={0,};
ali@38
   698
    root=g_strdup("razor-test-dir-XXXXXX");
ali@38
   699
    g_assert(mkdtemp(root));
ali@42
   700
    file=g_file_new_for_path(root);
ali@38
   701
    g_free(root);
ali@42
   702
    root_uri=g_file_get_uri(file);
ali@42
   703
    g_object_unref(file);
ali@42
   704
    g_setenv("RAZOR_ROOT",root_uri,TRUE);
ali@42
   705
    g_free(root_uri);
ali@38
   706
    helper=get_transaction_helper();
ali@38
   707
    installed=plover_package_set_new_from_installed("../razor-test-dir",&err);
ali@38
   708
    if (!installed)
ali@38
   709
	g_error("../razor-test-dir: %s",err->message);
ali@38
   710
    plover_transaction_helper_set_installed(helper,installed);
ali@38
   711
    g_object_unref(installed);
ali@38
   712
    plover_transaction_helper_set_base(helper,"../yum-repo-test-dir");
ali@38
   713
    if (!plover_transaction_helper_update(helper,&err))
ali@38
   714
	plover_transaction_helper_set_error(helper,err,"Expected error");
ali@38
   715
    g_assert(plover_transaction_helper_get_visible(helper));
ali@38
   716
    errmsg=plover_transaction_helper_get_error(helper,&err2);
ali@38
   717
    g_assert_cmpstr(errmsg,==,"Expected error");
ali@38
   718
    g_assert_error(err2,err->domain,err->code);
ali@38
   719
    plover_transaction_helper_present(helper);
ali@38
   720
    baton.helper=helper;
ali@38
   721
    g_idle_add_full(G_PRIORITY_LOW,set_error_tick,&baton,NULL);
ali@38
   722
    g_error_free(err);
ali@38
   723
    gtk_main();
ali@38
   724
    g_object_unref(helper);
ali@38
   725
    g_unsetenv("RAZOR_ROOT");
ali@38
   726
}
ali@38
   727
ali@38
   728
int main(int argc,char **argv)
ali@38
   729
{
ali@38
   730
    int retval;
ali@38
   731
    GError *err=NULL;
ali@38
   732
    /*
ali@38
   733
     * Note that because g_test_init() handles --help,
ali@38
   734
     * these options will not appear in the output.
ali@38
   735
     */
ali@38
   736
    GOptionEntry options[]={
ali@38
   737
	{"manual",0,0,G_OPTION_ARG_NONE,&manual_mode,
ali@38
   738
	  "Disable automatic mode",NULL},
ali@38
   739
	{NULL}
ali@38
   740
    };
ali@38
   741
    g_test_init(&argc,&argv,NULL);
ali@38
   742
    g_setenv("GTK_MODULES","",TRUE);
ali@38
   743
    g_setenv("GTK2_RC_FILES","/dev/null",TRUE);
ali@38
   744
    gtk_disable_setlocale();
ali@38
   745
    setlocale(LC_ALL,"C");
ali@38
   746
    g_test_bug_base("mailto:ali@juiblex.co.uk");
ali@38
   747
    if (!gtk_init_with_args(&argc,&argv,NULL,options,NULL,&err))
ali@38
   748
    {
ali@38
   749
	g_printf("%s\n",err->message);
ali@38
   750
	exit(0);
ali@38
   751
    }
ali@38
   752
    g_test_add_func("/transactionhelper/init",test_init);
ali@38
   753
    g_test_add_func("/transactionhelper/basic-properties",
ali@38
   754
      test_basic_properties);
ali@38
   755
    g_test_add_func("/transactionhelper/install-group",test_install_group);
ali@38
   756
    g_test_add_func("/transactionhelper/remove-group",test_remove_group);
ali@38
   757
    g_test_add_func("/transactionhelper/update",test_update);
ali@38
   758
    g_test_add_func("/transactionhelper/run-install",test_run_install);
ali@38
   759
    g_test_add_func("/transactionhelper/run-remove",test_run_remove);
ali@38
   760
    g_test_add_func("/transactionhelper/run-update",test_run_update);
ali@38
   761
    g_test_add_func("/transactionhelper/check-vendor",test_check_vendor);
ali@38
   762
    g_test_add_func("/transactionhelper/set-error",test_set_error);
ali@38
   763
    retval=g_test_run();
ali@38
   764
    return retval;
ali@38
   765
}