aboutsummaryrefslogtreecommitdiff
path: root/tests/test-formatter.cc
blob: 42c828c308808a0daf7fe5c2db5bc7e4550cbe76 (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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273

/*
 * 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 "glib-fixture.h"

#include <datetime/clock-mock.h>
#include <datetime/formatter.h>
#include <datetime/settings-shared.h>

#include <glib/gi18n.h>

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

using unity::indicator::datetime::Clock;
using unity::indicator::datetime::DateTime;
using unity::indicator::datetime::DesktopFormatter;
using unity::indicator::datetime::MockClock;
using unity::indicator::datetime::PhoneFormatter;

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

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)
{
    GDateTime * now = g_date_time_new_local (2020, 10, 31, 18, 30, 59);
    std::shared_ptr<MockClock> mock (new MockClock(DateTime(now)));
    g_date_time_unref(now);
    std::shared_ptr<Clock> clock = std::dynamic_pointer_cast<Clock>(mock);

    // test the default value in a 24h locale
    if (Set24hLocale ())
    {
        PhoneFormatter formatter (clock);
        EXPECT_EQ (std::string("%H:%M"), formatter.headerFormat.get());
        EXPECT_EQ (std::string("18:30"), formatter.header.get());
    }

    // test the default value in a 12h locale
    if (Set12hLocale ())
    {
        PhoneFormatter formatter (clock);
        EXPECT_EQ (std::string("%l:%M %p"), formatter.headerFormat.get());
        EXPECT_EQ (std::string(" 6:30 PM"), formatter.header.get());
    }
}

#define EM_SPACE "\u2003"

/**
 * Test the default values of the desktop header format
 */
TEST_F (FormatterFixture, TestDesktopHeader)
{
  struct {
    bool is_12h;
    bool show_day;
    bool show_date;
    bool show_year;
    const char * expected_format_string;
  } test_cases[] = {
    { false, false, false, false, "%H:%M" },
    { false, false, false, true,  "%H:%M" }, // show_year is ignored iff show_date is false
    { false, false, true,  false, "%b %e" EM_SPACE "%H:%M" },
    { false, false, true,  true,  "%b %e %Y" EM_SPACE "%H:%M" },
    { false, true,  false, false, "%a" EM_SPACE "%H:%M" },
    { false, true,  false, true,  "%a" EM_SPACE "%H:%M" }, // show_year is ignored iff show_date is false
    { false, true,  true,  false, "%a %b %e" EM_SPACE "%H:%M" },
    { false, true,  true,  true,  "%a %b %e %Y" EM_SPACE "%H:%M" },
    { true,  false, false, false, "%l:%M %p" },
    { true,  false, false, true,  "%l:%M %p" }, // show_year is ignored iff show_date is false
    { true,  false, true,  false, "%b %e" EM_SPACE "%l:%M %p" },
    { true,  false, true,  true,  "%b %e %Y" EM_SPACE "%l:%M %p" },
    { true,  true,  false, false, "%a" EM_SPACE "%l:%M %p" },
    { true,  true,  false, true,  "%a" EM_SPACE "%l:%M %p" }, // show_year is ignored iff show_date is false
    { true,  true,  true,  false, "%a %b %e" EM_SPACE "%l:%M %p" },
    { true,  true,  true,  true,  "%a %b %e %Y" EM_SPACE "%l:%M %p" }
  };

  GDateTime * now = g_date_time_new_local(2020, 10, 31, 18, 30, 59);
  std::shared_ptr<MockClock> mock(new MockClock(DateTime(now)));
  g_date_time_unref(now);
  std::shared_ptr<Clock> clock = std::dynamic_pointer_cast<Clock>(mock);

  for (int i=0, n=G_N_ELEMENTS(test_cases); i<n; i++)
    {
      if (test_cases[i].is_12h ? Set12hLocale() : Set24hLocale())
        {
          DesktopFormatter f (clock);

          g_settings_set_boolean (settings, SETTINGS_SHOW_DAY_S, test_cases[i].show_day);
          g_settings_set_boolean (settings, SETTINGS_SHOW_DATE_S, test_cases[i].show_date);
          g_settings_set_boolean (settings, SETTINGS_SHOW_YEAR_S, test_cases[i].show_year);

          ASSERT_STREQ (test_cases[i].expected_format_string, f.headerFormat.get().c_str());

          g_settings_reset (settings, SETTINGS_SHOW_DAY_S);
          g_settings_reset (settings, SETTINGS_SHOW_DATE_S);
          g_settings_reset (settings, SETTINGS_SHOW_YEAR_S);
        }
    }
}

/**
 * Test the default values of the desktop header format
 */
TEST_F (FormatterFixture, TestUpcomingTimes)
{
    auto a = g_date_time_new_local (2020, 10, 31, 18, 30, 59);

    struct {
        gboolean is_12h;
        GDateTime * now;
        GDateTime * then;
        const char * expected_format_string;
    } test_cases[] = {
        { true, g_date_time_ref(a), g_date_time_ref(a), "%l:%M %p" }, // identical time
        { true, g_date_time_ref(a), g_date_time_add_hours(a,1), "%l:%M %p" }, // later today
        { true, g_date_time_ref(a), g_date_time_add_days(a,1), "Tomorrow" EM_SPACE "%l:%M %p" }, // tomorrow
        { true, g_date_time_ref(a), g_date_time_add_days(a,2), "%a" EM_SPACE "%l:%M %p" },
        { true, g_date_time_ref(a), g_date_time_add_days(a,6), "%a" EM_SPACE "%l:%M %p" },
        { true, g_date_time_ref(a), g_date_time_add_days(a,7), "%a %d %b" EM_SPACE "%l:%M %p" }, // over one week away

        { false, g_date_time_ref(a), g_date_time_ref(a), "%H:%M" }, // identical time
        { false, g_date_time_ref(a), g_date_time_add_hours(a,1), "%H:%M" }, // later today
        { false, g_date_time_ref(a), g_date_time_add_days(a,1), "Tomorrow" EM_SPACE "%H:%M" }, // tomorrow
        { false, g_date_time_ref(a), g_date_time_add_days(a,2), "%a" EM_SPACE "%H:%M" },
        { false, g_date_time_ref(a), g_date_time_add_days(a,6), "%a" EM_SPACE "%H:%M" },
        { false, g_date_time_ref(a), g_date_time_add_days(a,7), "%a %d %b" EM_SPACE "%H:%M" } // over one week away
    };

    for (int i=0, n=G_N_ELEMENTS(test_cases); i<n; i++)
    {
        if (test_cases[i].is_12h ? Set12hLocale() : Set24hLocale())
        {
            DateTime tmp(test_cases[i].now);
            tmp.get();
            std::shared_ptr<MockClock> mock (new MockClock(tmp));//DateTime(test_cases[i].now)));
            std::shared_ptr<Clock> clock = std::dynamic_pointer_cast<Clock>(mock);
            DesktopFormatter f (clock);
        
            std::string fmt = f.getRelativeFormat (test_cases[i].then);
            ASSERT_STREQ (test_cases[i].expected_format_string, fmt.c_str());

            g_clear_pointer (&test_cases[i].now, g_date_time_unref);
            g_clear_pointer (&test_cases[i].then, g_date_time_unref);
        }
    }

    g_date_time_unref (a);
}


/**
 * Test the default values of the desktop header format
 */
TEST_F (FormatterFixture, TestEventTimes)
{
    auto day            = g_date_time_new_local (2013, 1, 1, 13, 0, 0);
    auto day_begin      = g_date_time_new_local (2013, 1, 1, 13, 0, 0);
    auto day_end        = g_date_time_add_days (day_begin, 1);
    auto tomorrow_begin = g_date_time_add_days (day_begin, 1);
    auto tomorrow_end   = g_date_time_add_days (tomorrow_begin, 1);

    struct {
        bool is_12h;
        GDateTime * now;
        GDateTime * then;
        GDateTime * then_end;
        const char * expected_format_string;
    } test_cases[] = {
        { false, g_date_time_ref(day), g_date_time_ref(day_begin), g_date_time_ref(day_end), _("Today") },
        { true, g_date_time_ref(day), g_date_time_ref(day_begin), g_date_time_ref(day_end), _("Today") },
        { false, g_date_time_ref(day), g_date_time_ref(tomorrow_begin), g_date_time_ref(tomorrow_end), _("Tomorrow") },
        { true, g_date_time_ref(day), g_date_time_ref(tomorrow_begin), g_date_time_ref(tomorrow_end), _("Tomorrow") }
    };

    for (int i=0, n=G_N_ELEMENTS(test_cases); i<n; i++)
    {
        if (test_cases[i].is_12h ? Set12hLocale() : Set24hLocale())
        {
            std::shared_ptr<MockClock> mock (new MockClock(DateTime(test_cases[i].now)));
            std::shared_ptr<Clock> clock = std::dynamic_pointer_cast<Clock>(mock);
            DesktopFormatter f (clock);
          
            std::string fmt = f.getRelativeFormat (test_cases[i].then, test_cases[i].then_end);
            ASSERT_STREQ (test_cases[i].expected_format_string, fmt.c_str());

            g_clear_pointer (&test_cases[i].now, g_date_time_unref);
            g_clear_pointer (&test_cases[i].then, g_date_time_unref);
            g_clear_pointer (&test_cases[i].then_end, g_date_time_unref);
        }
    }

    g_date_time_unref (tomorrow_end);
    g_date_time_unref (tomorrow_begin);
    g_date_time_unref (day_end);
    g_date_time_unref (day_begin);
    g_date_time_unref (day);
}