aboutsummaryrefslogtreecommitdiff
path: root/tests/test-timezones.cpp
blob: 7144aaf422fcbc03962de2de7fce71e3d56b5c27 (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
/*
 * Copyright 2013 Canonical Ltd.
 *
 * 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/>.
 *
 * Authors:
 *   Charles Kerr <charles.kerr@canonical.com>
 */

#include "geoclue-fixture.h"
#include "timezone-mock.h"
#include <datetime/settings.h>
#include <datetime/timezones-live.h>
#include <datetime/timezone-timedated.h>
#include <memory> // std::shared_ptr

#include <cstdio> // fopen()
#include <unistd.h> // sync()

using namespace ayatana::indicator::datetime;

typedef GeoclueFixture TimezonesFixture;

#define TIMEZONE_FILE (SANDBOX "/timezone")

namespace
{
    /* convenience func to set the timezone file */
    void set_file(const std::string& text)
    {
        auto fp = fopen(TIMEZONE_FILE, "w+");
        fprintf(fp, "%s\n", text.c_str());
        fclose(fp);
        sync();
    }
}


TEST_F(TimezonesFixture, ManagerTest)
{
  std::string timezone_file = "America/New_York";
  std::string timezone_geo = "America/Denver";

  set_file(timezone_file);
  auto settings = std::make_shared<Settings>();
  auto timezone = std::make_shared<MockTimezone>(timezone_file);
  LiveTimezones z(settings, timezone);
  wait_msec(500); // wait for the bus to get set up
  EXPECT_EQ(timezone_file, z.timezone.get());
  auto zones = z.timezones.get();
  //std::set<std::string> zones = z.timezones.get();
  EXPECT_EQ(1, zones.size());
  EXPECT_EQ(1, zones.count(timezone_file));

  bool zone_changed = false;
  auto zone_connection = z.timezone.changed().connect([&zone_changed, this](const std::string&) {
          zone_changed = true;
          g_main_loop_quit(loop);
        });

  // start listening for a timezone change, then change the timezone
  bool zones_changed = false;
  auto zones_connection = z.timezones.changed().connect([&zones_changed, &zones, this](const std::set<std::string>& timezones) {
          zones_changed = true;
          zones = timezones;
          g_main_loop_quit(loop);
        });

  g_idle_add([](gpointer s_in) {
          auto s = static_cast<Settings*>(s_in);
          g_message("geolocation was %d", (int)s->show_detected_location.get());
          g_message("turning geolocation on");
          s->show_detected_location.set(true);
          return G_SOURCE_REMOVE;
        }, settings.get());

  // turn on geoclue during the idle... this should add timezone_1 to the 'timezones' property
  g_main_loop_run(loop);
  EXPECT_TRUE(zones_changed);
  EXPECT_EQ(timezone_file, z.timezone.get());
  EXPECT_EQ(2, zones.size());
  EXPECT_EQ(1, zones.count(timezone_file));
  EXPECT_EQ(1, zones.count(timezone_geo));
  zones_changed = false;

  // now tweak the geoclue value... the geoclue-detected timezone should change,
  // causing the 'timezones' property to change
  zone_changed = false;
  zones_changed = false;
  timezone_geo = "America/Chicago";
  setGeoclueTimezoneOnIdle(timezone_geo);
  g_main_loop_run(loop);
  EXPECT_FALSE(zone_changed);
  EXPECT_TRUE(zones_changed);
  EXPECT_EQ(timezone_file, z.timezone.get());
  EXPECT_EQ(2, zones.size());
  EXPECT_EQ(1, zones.count(timezone_file));
  EXPECT_EQ(1, zones.count(timezone_geo));
}