summaryrefslogtreecommitdiff
path: root/test/test_load.cpp
blob: 1728263ace60bcfd9e3f6a4727c3b3105273cecd (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
186
187
188
189
190
#include <gtest/gtest.h>

#include <Imlib2.h>
#include <fcntl.h>

#include "config.h"

int                 debug = 0;

#define D(...)  if (debug) printf(__VA_ARGS__)
#define D2(...) if (debug > 1) printf(__VA_ARGS__)

#define EXPECT_OK(x)  EXPECT_FALSE(x)
#define EXPECT_ERR(x) EXPECT_TRUE(x)

#define TOPDIR  	SRC_DIR
#define IMGDIR		TOPDIR "/test/images"
#define FILE_REF	"icon-64.png"

static const char  *const pfxs[] = {
   "argb",
   "bmp",
   "ff",
   "gif",
   "ico",
   "jpg.mp3",                   // id3
   "jpeg",
   "ilbm",                      // lbm
   "png",
   "pbm",                       // pnm
   "ppm",                       // pnm
   "tga",
#ifdef BUILD_SVG_LOADER
   "svg",
#endif
   "tiff",
   "webp",
   "xbm",
   "xpm",
#ifdef BUILD_BZ2_LOADER
   "ff.bz2",                    // bz2
#endif
#ifdef BUILD_ZLIB_LOADER
   "ff.gz",                     // zlib
#endif
#ifdef BUILD_LZMA_LOADER
   "ff.xz",                     // lzma
#endif
};
#define N_PFX (sizeof(pfxs) / sizeof(char*))

static int
progress(Imlib_Image im, char percent, int update_x, int update_y,
         int update_w, int update_h)
{
   D2("%s: %3d%% %4d,%4d %4dx%4d\n",
      __func__, percent, update_x, update_y, update_w, update_h);

   return 1;                    /* Continue */
}

static void
image_free(Imlib_Image im)
{
   imlib_context_set_image(im);
   imlib_free_image_and_decache();
}

static void
test_load(void)
{
   char                filei[256];
   char                fileo[256];
   unsigned int        i, j;
   Imlib_Image         im;
   Imlib_Load_Error    lerr;
   FILE               *fp;
   int                 fd;
   int                 err;

   snprintf(filei, sizeof(filei), "%s/%s", IMGDIR, FILE_REF);
   D("Load '%s'\n", filei);
   im = imlib_load_image(filei);

   ASSERT_TRUE(im);

   image_free(im);

   for (i = 0; i < N_PFX; i++)
     {
        // Load files of all types
        snprintf(fileo, sizeof(fileo), "%s/%s.%s", IMGDIR, "icon-64", pfxs[i]);
        D("Load '%s'\n", fileo);
        im = imlib_load_image_with_error_return(fileo, &lerr);
        EXPECT_TRUE(im);
        EXPECT_EQ(lerr, 0);
        if (!im || lerr)
           D("Error %d im=%p loading '%s'\n", lerr, im, fileo);
        if (im)
           image_free(im);

        imlib_flush_loaders();

        if (strchr(pfxs[i], '.') == 0)
          {
             snprintf(filei, sizeof(filei), "%s.%s", "icon-64", pfxs[i]);
             for (j = 0; j < N_PFX; j++)
               {
                  // Load certain types pretending they are something else
                  snprintf(fileo, sizeof(fileo), "%s/%s.%s.%s", IMGDIR,
                           "icon-64", pfxs[i], pfxs[j]);
                  unlink(fileo);
                  symlink(filei, fileo);
                  D("Load incorrect suffix '%s'\n", fileo);
                  im = imlib_load_image_with_error_return(fileo, &lerr);
                  EXPECT_TRUE(im);
                  EXPECT_EQ(lerr, 0);
                  if (!im || lerr)
                     D("Error %d im=%p loading '%s'\n", lerr, im, fileo);
                  if (im)
                     image_free(im);
               }
          }

        // Empty files of all types
        snprintf(fileo, sizeof(fileo), "%s/%s.%s", IMGDIR, "empty", pfxs[i]);
        unlink(fileo);
        fp = fopen(fileo, "wb");
        fclose(fp);
        D("Load empty '%s'\n", fileo);
        im = imlib_load_image_with_error_return(fileo, &lerr);
        D("  err = %d\n", lerr);
        EXPECT_TRUE(lerr == IMLIB_LOAD_ERROR_UNKNOWN);

        // Non-existing files of all types
        snprintf(fileo, sizeof(fileo), "%s/%s.%s", IMGDIR, "nonex", pfxs[i]);
        unlink(fileo);
        symlink("non-existing", fileo);
        D("Load non-existing '%s'\n", fileo);
        im = imlib_load_image_with_error_return(fileo, &lerr);
        EXPECT_EQ(lerr, IMLIB_LOAD_ERROR_FILE_DOES_NOT_EXIST);

        // Load via fd
        snprintf(fileo, sizeof(fileo), "%s/%s.%s", IMGDIR, "icon-64", pfxs[i]);
        fd = open(fileo, O_RDONLY);
        D("Load fd %d '%s'\n", fd, fileo);
        snprintf(fileo, sizeof(fileo), ".%s", pfxs[i]);
        im = imlib_load_image_fd(fd, pfxs[i]);
        EXPECT_TRUE(im);
        if (im)
           image_free(im);
        err = close(fd);
        EXPECT_TRUE(err != 0);
     }
}

TEST(LOAD, load_1)
{
   test_load();
}

TEST(LOAD, load_2)
{
   imlib_context_set_progress_function(progress);
   imlib_context_set_progress_granularity(10);
   test_load();
}

int
main(int argc, char **argv)
{
   const char         *s;

   ::testing::InitGoogleTest(&argc, argv);

   for (argc--, argv++; argc > 0; argc--, argv++)
     {
        s = argv[0];
        if (*s++ != '-')
           break;
        switch (*s)
          {
          case 'd':
             debug++;
             break;
          }
     }

   return RUN_ALL_TESTS();
}