Back to index

unity  6.0.0
TestStaticCairoText.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2010 Canonical Ltd.
00003  *
00004  * This program is free software: you can redistribute it and/or modify it
00005  * under the terms of the GNU General Public License version 3, as published
00006  * by the  Free Software Foundation.
00007  *
00008  * This program is distributed in the hope that it will be useful, but
00009  * WITHOUT ANY WARRANTY; without even the implied warranties of
00010  * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
00011  * PURPOSE.  See the GNU General Public License for more details.
00012  *
00013  * You should have received a copy of the GNU General Public License
00014  * version 3 along with this program.  If not, see
00015  * <http://www.gnu.org/licenses/>
00016  *
00017  * Authored by: Christopher James Halse Rogers <christopher.halse.rogers@canonical.com>
00018  *
00019  */
00020 
00021 #include "config.h"
00022 
00023 #include <glib.h>
00024 #include <pango/pango.h>
00025 
00026 #include "Nux/Nux.h"
00027 #include "StaticCairoText.h"
00028 
00029 static void
00030 switch_language (const char *new_locale)
00031 {
00032   setenv ("LANGUAGE", new_locale, 1);
00033 
00034   /*
00035    * The GNU gettext manual suggests this is necessary to ensure the gettext
00036    * cache doesn't interfere with the locale change
00037    */
00038   {
00039     extern int _nl_msg_cat_cntr;
00040     ++_nl_msg_cat_cntr;
00041   }
00042 }
00043 
00044 static void
00045 assert_width_increases_with_substring_length (const gchar *test_string)
00046 {
00047   nux::StaticCairoText text("");
00048   gchar *substring = new gchar[strlen(test_string)];
00049 
00050   int width, oldwidth;
00051   int height;
00052 
00053   text.GetTextExtents(width, height);
00054   // The empty string should have no width...
00055   g_assert_cmpint (width, ==, 0);
00056   oldwidth = width;
00057 
00058   for (int substr_len = 1; substr_len <= g_utf8_strlen(test_string, -1); ++substr_len) {
00059     text.SetText (g_utf8_strncpy (substring, test_string, substr_len));
00060     text.GetTextExtents(width, height);
00061     g_assert_cmpint(width, >, oldwidth);
00062     oldwidth = width;
00063   }
00064   
00065   delete substring;
00066 }
00067 
00068 static void
00069 TestLeftToRightExtentIncreasesWithLength ()
00070 {
00071   const gchar *test_string = "Just a string of text";
00072   
00073   switch_language("en");
00074 
00075   g_assert(g_utf8_validate(test_string, -1, NULL));
00076   g_assert(pango_find_base_dir(test_string, -1) == PANGO_DIRECTION_LTR);
00077 
00078   assert_width_increases_with_substring_length (test_string);
00079 }
00080 
00081 static void
00082 TestRightToLeftExtentIncreasesWithLength ()
00083 {
00084   // This is the UTF-8 encoding of מחרוזת אקראית עברית, a hebrew test string.
00085   const gchar *test_string = "\xd7\x9e\xd7\x97\xd7\xa8\xd7\x95\xd7\x96\xd7\xaa"
00086                              " \xd7\x90\xd7\xa7\xd7\xa8\xd7\x90\xd7\x99\xd7\xaa"
00087                              "\xd7\xa2\xd7\x91\xd7\xa8\xd7\x99\xd7\xaa";
00088 
00089   // Hebrew is an RTL language
00090   switch_language("he");
00091 
00092   g_assert(g_utf8_validate(test_string, -1, NULL));
00093   g_assert(pango_find_base_dir(test_string, -1) == PANGO_DIRECTION_RTL);
00094   
00095   assert_width_increases_with_substring_length (test_string);
00096 }
00097 
00098 void
00099 TestStaticCairoTextCreateSuite()
00100 {
00101 #define _DOMAIN "/Unit/StaticCairoText"
00102 
00103   g_test_add_func(_DOMAIN"/TextLeftToRightExtentIncreasesWithLength", TestLeftToRightExtentIncreasesWithLength);
00104   g_test_add_func(_DOMAIN"/TestRightToLeftExtentIncreasesWithLength", TestRightToLeftExtentIncreasesWithLength);
00105 }
00106