xref: /haiku/src/tests/kits/locale/catalogTest.cpp (revision b6f76ebe7153b94820cf35f8db4facc158841abb)
1c3ac87e8SOliver Tappe /*
2c3ac87e8SOliver Tappe ** Copyright 2003, Oliver Tappe, zooey@hirschkaefer.de. All rights reserved.
3*b6f76ebeSAugustin Cavalier ** Distributed under the terms of the MIT License.
4c3ac87e8SOliver Tappe */
5c3ac87e8SOliver Tappe 
6c3ac87e8SOliver Tappe #include <assert.h>
7c3ac87e8SOliver Tappe #include <stdio.h>
875f15221SOliver Tappe #include <stdlib.h>
9c3ac87e8SOliver Tappe #include <unistd.h>
10c3ac87e8SOliver Tappe 
11c3ac87e8SOliver Tappe #include <Application.h>
12c3ac87e8SOliver Tappe #include <Catalog.h>
13c3ac87e8SOliver Tappe #include <DefaultCatalog.h>
14c3ac87e8SOliver Tappe #include <Entry.h>
15c3ac87e8SOliver Tappe #include <Locale.h>
16c3ac87e8SOliver Tappe #include <Path.h>
17c3ac87e8SOliver Tappe #include <Roster.h>
18c3ac87e8SOliver Tappe 
19c3ac87e8SOliver Tappe class CatalogTest {
20c3ac87e8SOliver Tappe 	public:
21c3ac87e8SOliver Tappe 		void Run();
22c3ac87e8SOliver Tappe 		void Check();
23c3ac87e8SOliver Tappe };
24c3ac87e8SOliver Tappe 
25546208a5SOliver Tappe #define B_TRANSLATION_CONTEXT "CatalogTest"
26c3ac87e8SOliver Tappe 
27c3ac87e8SOliver Tappe 
28c3ac87e8SOliver Tappe #define catSig "x-vnd.Be.locale.catalogTest"
29c3ac87e8SOliver Tappe #define catName catSig".catalog"
30c3ac87e8SOliver Tappe 
31c3ac87e8SOliver Tappe void
Run()32c3ac87e8SOliver Tappe CatalogTest::Run()
33c3ac87e8SOliver Tappe {
34c3ac87e8SOliver Tappe 	printf("app...");
35c3ac87e8SOliver Tappe 	status_t res;
36c3ac87e8SOliver Tappe 	BString s;
37546208a5SOliver Tappe 	s << "string" << "\x01" << B_TRANSLATION_CONTEXT << "\x01";
38c3ac87e8SOliver Tappe 	size_t hashVal = CatKey::HashFun(s.String());
39c3ac87e8SOliver Tappe 	assert(be_locale != NULL);
40c3ac87e8SOliver Tappe 	system("mkdir -p ./locale/catalogs/"catSig);
41c3ac87e8SOliver Tappe 
42c3ac87e8SOliver Tappe 	// create an empty catalog of default type...
43c3ac87e8SOliver Tappe 	BPrivate::EditableCatalog cata("Default", catSig, "German");
44c3ac87e8SOliver Tappe 	assert(cata.InitCheck() == B_OK);
45c3ac87e8SOliver Tappe 
46c3ac87e8SOliver Tappe 	// ...and populate the catalog with some data:
47546208a5SOliver Tappe 	res = cata.SetString("string", "Schnur", B_TRANSLATION_CONTEXT);
48c3ac87e8SOliver Tappe 	assert(res == B_OK);
49c3ac87e8SOliver Tappe 	res = cata.SetString(hashVal, "Schnur_id");
50c3ac87e8SOliver Tappe 		// add a second entry for the same hash-value, but with different
51c3ac87e8SOliver Tappe 		// translation
52c3ac87e8SOliver Tappe 	assert(res == B_OK);
53c3ac87e8SOliver Tappe 	res = cata.SetString("string", "String", "programming");
54c3ac87e8SOliver Tappe 	assert(res == B_OK);
55c3ac87e8SOliver Tappe 	res = cata.SetString("string", "Textpuffer", "programming",
56c3ac87e8SOliver Tappe 		"Deutsches Fachbuch");
57c3ac87e8SOliver Tappe 	assert(res == B_OK);
58546208a5SOliver Tappe 	res = cata.SetString("string", "Leine", B_TRANSLATION_CONTEXT,
59546208a5SOliver Tappe 		"Deutsches Fachbuch");
60c3ac87e8SOliver Tappe 	assert(res == B_OK);
61c3ac87e8SOliver Tappe 	res = cata.WriteToFile("./locale/catalogs/"catSig"/german.catalog");
62c3ac87e8SOliver Tappe 	assert(res == B_OK);
63c3ac87e8SOliver Tappe 
64c3ac87e8SOliver Tappe 	// check if we are getting back the correct strings:
65546208a5SOliver Tappe 	s = cata.GetString(("string"), B_TRANSLATION_CONTEXT);
66c3ac87e8SOliver Tappe 	assert(s == "Schnur");
67c3ac87e8SOliver Tappe 	s = cata.GetString(hashVal);
68c3ac87e8SOliver Tappe 	assert(s == "Schnur_id");
69c3ac87e8SOliver Tappe 	s = cata.GetString("string", "programming");
70c3ac87e8SOliver Tappe 	assert(s == "String");
71c3ac87e8SOliver Tappe 	s = cata.GetString("string", "programming", "Deutsches Fachbuch");
72c3ac87e8SOliver Tappe 	assert(s == "Textpuffer");
73546208a5SOliver Tappe 	s = cata.GetString("string", B_TRANSLATION_CONTEXT, "Deutsches Fachbuch");
74c3ac87e8SOliver Tappe 	assert(s == "Leine");
75c3ac87e8SOliver Tappe 
76c3ac87e8SOliver Tappe 	// now we create a new (base) catalog and embed this one into the app-file:
77c3ac87e8SOliver Tappe 	BPrivate::EditableCatalog catb("Default", catSig, "English");
78c3ac87e8SOliver Tappe 	assert(catb.InitCheck() == B_OK);
79c3ac87e8SOliver Tappe 	// the following string is unique to the embedded catalog:
80c3ac87e8SOliver Tappe 	res = catb.SetString("string", "string", "base");
81c3ac87e8SOliver Tappe 	assert(res == B_OK);
82c3ac87e8SOliver Tappe 	// the following id is unique to the embedded catalog:
83c3ac87e8SOliver Tappe 	res = catb.SetString(32, "hashed string");
84c3ac87e8SOliver Tappe 	assert(res == B_OK);
85c3ac87e8SOliver Tappe 	// the following string will be hidden by the definition inside the
86c3ac87e8SOliver Tappe 	// german catalog:
87546208a5SOliver Tappe 	res = catb.SetString("string", "hidden", B_TRANSLATION_CONTEXT);
88c3ac87e8SOliver Tappe 	assert(res == B_OK);
89c3ac87e8SOliver Tappe 	app_info appInfo;
90c3ac87e8SOliver Tappe 	res = be_app->GetAppInfo(&appInfo);
91c3ac87e8SOliver Tappe 	assert(res == B_OK);
92c3ac87e8SOliver Tappe 	// embed created catalog into application file (catalogTest):
93c3ac87e8SOliver Tappe 	res = catb.WriteToResource(&appInfo.ref);
94c3ac87e8SOliver Tappe 	assert(res == B_OK);
95c3ac87e8SOliver Tappe 
96c3ac87e8SOliver Tappe 	printf("ok.\n");
97c3ac87e8SOliver Tappe 	Check();
98c3ac87e8SOliver Tappe }
99c3ac87e8SOliver Tappe 
100c3ac87e8SOliver Tappe 
101c3ac87e8SOliver Tappe void
Check()102c3ac87e8SOliver Tappe CatalogTest::Check()
103c3ac87e8SOliver Tappe {
104c3ac87e8SOliver Tappe 	status_t res;
105c3ac87e8SOliver Tappe 	printf("app-check...");
106c3ac87e8SOliver Tappe 	BString s;
107546208a5SOliver Tappe 	s << "string" << "\x01" << B_TRANSLATION_CONTEXT << "\x01";
108c3ac87e8SOliver Tappe 	size_t hashVal = CatKey::HashFun(s.String());
109c3ac87e8SOliver Tappe 	// ok, we now try to re-load the catalog that has just been written:
110c3ac87e8SOliver Tappe 	//
111c3ac87e8SOliver Tappe 	// actually, the following code can be seen as an example of what an
112c3ac87e8SOliver Tappe 	// app needs in order to translate strings:
113c3ac87e8SOliver Tappe 	BCatalog cat;
114c3ac87e8SOliver Tappe 	res = be_locale->GetAppCatalog(&cat);
115c3ac87e8SOliver Tappe 	assert(res == B_OK);
116c3ac87e8SOliver Tappe 	// fetch basic data:
11775f15221SOliver Tappe 	uint32 fingerprint = 0;
118c3ac87e8SOliver Tappe 	res = cat.GetFingerprint(&fingerprint);
119c3ac87e8SOliver Tappe 	assert(res == B_OK);
120c3ac87e8SOliver Tappe 	BString lang;
121c3ac87e8SOliver Tappe 	res = cat.GetLanguage(&lang);
122c3ac87e8SOliver Tappe 	assert(res == B_OK);
123c3ac87e8SOliver Tappe 	BString sig;
124c3ac87e8SOliver Tappe 	res = cat.GetSignature(&sig);
125c3ac87e8SOliver Tappe 	assert(res == B_OK);
126c3ac87e8SOliver Tappe 
127c3ac87e8SOliver Tappe 	// now check strings:
128483e1262SMatt Madia 	s = B_TRANSLATE_ID(hashVal);
129c3ac87e8SOliver Tappe 	assert(s == "Schnur_id");
130483e1262SMatt Madia 	s = B_TRANSLATE_ALL("string", "programming", "");
131c3ac87e8SOliver Tappe 	assert(s == "String");
132483e1262SMatt Madia 	s = B_TRANSLATE_ALL("string", "programming", "Deutsches Fachbuch");
133c3ac87e8SOliver Tappe 	assert(s == "Textpuffer");
134483e1262SMatt Madia 	s = B_TRANSLATE_COMMENT("string", "Deutsches Fachbuch");
135c3ac87e8SOliver Tappe 	assert(s == "Leine");
136c3ac87e8SOliver Tappe 	// the following string should be found in the embedded catalog only:
137483e1262SMatt Madia 	s = B_TRANSLATE_ALL("string", "base", NULL);
138c3ac87e8SOliver Tappe 	assert(s == "string");
139c3ac87e8SOliver Tappe 	// the following id should be found in the embedded catalog only:
140483e1262SMatt Madia 	s = B_TRANSLATE_ID(32);
141c3ac87e8SOliver Tappe 	assert(s == "hashed string");
142c3ac87e8SOliver Tappe 	// the following id doesn't exist anywhere (hopefully):
143483e1262SMatt Madia 	s = B_TRANSLATE_ID(-1);
144c3ac87e8SOliver Tappe 	assert(s == "");
145c3ac87e8SOliver Tappe 	// the following string exists twice, in the embedded as well as in the
146c3ac87e8SOliver Tappe 	// external catalog. So we should get the external translation (as it should
147c3ac87e8SOliver Tappe 	// override the embedded one):
148483e1262SMatt Madia 	s = B_TRANSLATE("string");
149c3ac87e8SOliver Tappe 	assert(s == "Schnur");
150c3ac87e8SOliver Tappe 
151c3ac87e8SOliver Tappe 	// now check if trying to access same catalog by specifying its data works:
152c3ac87e8SOliver Tappe 	BCatalog cat2(sig.String(), lang.String(), fingerprint);
153c3ac87e8SOliver Tappe 	assert(cat2.InitCheck() == B_OK);
154c3ac87e8SOliver Tappe 	// now check if trying to access same catalog with wrong fingerprint fails:
155c3ac87e8SOliver Tappe 	BCatalog cat3(sig.String(), lang.String(), fingerprint*-1);
156c3ac87e8SOliver Tappe 	assert(cat3.InitCheck() == B_NO_INIT);
157c3ac87e8SOliver Tappe 	// translating through an invalid catalog should yield the native string:
158c3ac87e8SOliver Tappe 	s = cat3.GetString("string");
159c3ac87e8SOliver Tappe 	assert(s == "string");
160c3ac87e8SOliver Tappe 
161c3ac87e8SOliver Tappe 	printf("ok.\n");
162c3ac87e8SOliver Tappe }
163c3ac87e8SOliver Tappe 
164c3ac87e8SOliver Tappe 
165c3ac87e8SOliver Tappe int
main(int argc,char ** argv)166c3ac87e8SOliver Tappe main(int argc, char **argv)
167c3ac87e8SOliver Tappe {
168c3ac87e8SOliver Tappe 	BApplication* testApp
169c3ac87e8SOliver Tappe 		= new BApplication("application/"catSig);
170c3ac87e8SOliver Tappe 
171c3ac87e8SOliver Tappe 	// change to app-folder:
172c3ac87e8SOliver Tappe 	app_info appInfo;
173c3ac87e8SOliver Tappe 	be_app->GetAppInfo(&appInfo);
174c3ac87e8SOliver Tappe 	BEntry appEntry(&appInfo.ref);
175c3ac87e8SOliver Tappe 	BEntry appFolder;
176c3ac87e8SOliver Tappe 	appEntry.GetParent( &appFolder);
177c3ac87e8SOliver Tappe 	BPath appPath;
178c3ac87e8SOliver Tappe 	appFolder.GetPath( &appPath);
179c3ac87e8SOliver Tappe 	chdir( appPath.Path());
180c3ac87e8SOliver Tappe 
181c3ac87e8SOliver Tappe 	CatalogTest catTest;
182c3ac87e8SOliver Tappe 	catTest.Run();
183c3ac87e8SOliver Tappe 
184c3ac87e8SOliver Tappe 	char cwd[B_FILE_NAME_LENGTH];
185c3ac87e8SOliver Tappe 	getcwd(cwd, B_FILE_NAME_LENGTH);
186c3ac87e8SOliver Tappe 	BString addonName(cwd);
187c3ac87e8SOliver Tappe 	addonName << "/" "catalogTestAddOn";
188c3ac87e8SOliver Tappe 	image_id image = load_add_on(addonName.String());
189c3ac87e8SOliver Tappe 	assert(image >= B_OK);
190c3ac87e8SOliver Tappe 	void (*runAddonFunc)() = 0;
191c3ac87e8SOliver Tappe 	get_image_symbol(image, "run_test_add_on",
192c3ac87e8SOliver Tappe 		B_SYMBOL_TYPE_TEXT, (void **)&runAddonFunc);
193c3ac87e8SOliver Tappe 	assert(runAddonFunc);
194c3ac87e8SOliver Tappe 	runAddonFunc();
195c3ac87e8SOliver Tappe 
196c3ac87e8SOliver Tappe 	catTest.Check();
197c3ac87e8SOliver Tappe 
198c3ac87e8SOliver Tappe 	delete testApp;
199c3ac87e8SOliver Tappe }
200