aboutsummaryrefslogtreecommitdiffstats
path: root/test/render_test.cpp
blob: 4c5c7aadc0aa8a4091362aba65b52944b81eeaa4 (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
#define _CRT_SECURE_NO_WARNINGS
#include <gtest/gtest.h>
#include <fstream>
#ifdef _WIN32
	#include "dirent.h"
#else
	#include <dirent.h>
#endif
#include "../containers/test/test_container.h"
#include "../containers/test/Bitmap.h"
using namespace std;

vector<string> find_htm_files();
void test(string filename);

const char* test_dir = "../test/render"; // ctest is run from litehtml/build

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
using render_test = testing::TestWithParam<string>;

TEST_P(render_test, _)
{
	test(string(test_dir) + "/" + GetParam());
}

INSTANTIATE_TEST_SUITE_P(, render_test, testing::ValuesIn(find_htm_files()));
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

void error(const char* msg) { puts(msg); exit(1); }

void read_dir(const string& subdir, vector<string>& files)
{
	string full_path = string(test_dir) + "/" + subdir;
	DIR* dir = opendir(full_path.c_str());
	if (!dir) error(full_path.c_str());
	while (dirent* ent = readdir(dir))
	{
		string name = ent->d_name;
		if (ent->d_type == DT_DIR)
		{
			if(name != "." && name != ".." && name[0] != '-')
			{
				read_dir(subdir + "/" + name, files);
			}
		} else if (ent->d_type == DT_REG)
		{
			if (name[0] != '-' && name.size() > 4 &&
				(name.substr(name.size() - 4) == ".htm" || name.substr(name.size() - 5) == ".html"))
				files.push_back(subdir + "/" + name);
		}
	}
	closedir(dir);
}

vector<string> find_htm_files()
{
	vector<string> ret;
	read_dir("", ret);
	sort(ret.begin(), ret.end());
	return ret;
}

string readfile(string filename)
{
	stringstream ss;
	ifstream(filename) >> ss.rdbuf();
	return ss.str();
}

Bitmap draw(document::ptr doc, int width, int height)
{
	Bitmap bmp(width, height);
	position clip(0, 0, width, height);

	doc->draw((uint_ptr)&bmp, 0, 0, &clip);

	bmp.resize(width, height);

	return bmp;
}

void test(string filename)
{
	string html = readfile(filename);

	int width = 800, height = 1600; // image will be cropped to content_width/content_height
	auto last_slash_pos = filename.find_last_of('/');
	string base_path;
	if(last_slash_pos != string::npos)
	{
		base_path = filename.substr(0, last_slash_pos);
	} else
	{
		base_path = test_dir;
	}
	test_container container(width, height, base_path);

	auto doc = document::createFromString(html.c_str(), &container);
	doc->render(width);
	Bitmap bmp = draw(doc, doc->content_width(), doc->content_height());

	Bitmap good(filename + ".png");
	if (bmp != good)
	{
		bmp.save(filename + "-FAILED.png");
		ASSERT_TRUE(false);
	}
}