aboutsummaryrefslogtreecommitdiff
path: root/tests/test-formatter.cc
blob: 488d9e14f94cf253af7bae052c706912bf3c4ec3 (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

/*
 * Copyright 2013 Canonical Ltd.
 *
 * Authors:
 *   Charles Kerr <charles.kerr@canonical.com>
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 3, as published
 * by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <string>
#include <vector>

#include <langinfo.h>
#include <locale.h>

#include <glib/gi18n.h>

#include "utils.h"
#include "settings-shared.h"

#include "glib-fixture.h"

/***
****
***/

class FormatterFixture: public GlibFixture
{
  private:

    typedef GlibFixture super;
    gchar * original_locale = nullptr;

  protected:

    GSettings * settings = nullptr;

    virtual void SetUp ()
    {
      super::SetUp ();
  
      settings = g_settings_new (SETTINGS_INTERFACE);

      original_locale = g_strdup (setlocale (LC_TIME, NULL));
    }

    virtual void TearDown ()
    {
      g_clear_object (&settings);

      setlocale (LC_TIME, original_locale);
      g_clear_pointer (&original_locale, g_free);

      super::TearDown ();
    }

    bool SetLocale (const char * expected_locale, const char * name)
    {
      setlocale (LC_TIME, expected_locale);
      const char * actual_locale = setlocale (LC_TIME, NULL);
      if (!g_strcmp0 (expected_locale, actual_locale))
        {
          return true;
        }
      else
        {
          g_warning ("Unable to set locale to %s; skipping %s locale tests.", expected_locale, name);
          return false;
        }
    }

    inline bool Set24hLocale () { return SetLocale ("C",          "24h"); }
    inline bool Set12hLocale () { return SetLocale ("en_US.utf8", "12h"); }
};


/**
 * Test the phone header format
 */
TEST_F (FormatterFixture, TestPhoneHeader)
{
  // test the default value in a 24h locale
  if (Set24hLocale ())
    {
      const gchar * format = get_terse_header_time_format_string ();
      ASSERT_NE (nullptr, format);
      ASSERT_STREQ ("%H:%M", format);
    }

  // test the default value in a 12h locale
  if (Set12hLocale ())
    {
      const gchar * format = get_terse_header_time_format_string ();
      ASSERT_NE (nullptr, format);
      ASSERT_STREQ ("%l:%M %p", format);
    }
}