2
* Copyright 2013 Canonical Ltd.
4
* This program is free software: you can redistribute it and/or modify it
5
* under the terms of the GNU General Public License version 3, as published
6
* by the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful, but
9
* WITHOUT ANY WARRANTY; without even the implied warranties of
10
* MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11
* PURPOSE. See the GNU General Public License for more details.
13
* You should have received a copy of the GNU General Public License along
14
* with this program. If not, see <http://www.gnu.org/licenses/>.
17
* Charles Kerr <charles.kerr@canonical.com>
20
#include "geoclue-fixture.h"
22
#include <datetime/settings.h>
23
#include <datetime/timezones-live.h>
25
#include <memory> // std::shared_ptr
27
#include <cstdio> // fopen()
28
#include <unistd.h> // sync()
30
using namespace unity::indicator::datetime;
32
typedef GeoclueFixture TimezonesFixture;
34
#define TIMEZONE_FILE (SANDBOX "/timezone")
38
/* convenience func to set the timezone file */
39
void set_file(const std::string& text)
41
auto fp = fopen(TIMEZONE_FILE, "w+");
42
fprintf(fp, "%s\n", text.c_str());
49
TEST_F(TimezonesFixture, ManagerTest)
51
std::string timezone_file = "America/New_York";
52
std::string timezone_geo = "America/Denver";
54
set_file(timezone_file);
55
std::shared_ptr<Settings> settings(new Settings);
56
LiveTimezones z(settings, TIMEZONE_FILE);
57
wait_msec(500); // wait for the bus to get set up
58
EXPECT_EQ(timezone_file, z.timezone.get());
59
auto zones = z.timezones.get();
60
//std::set<std::string> zones = z.timezones.get();
61
EXPECT_EQ(1, zones.size());
62
EXPECT_EQ(1, zones.count(timezone_file));
64
bool zone_changed = false;
65
auto zone_connection = z.timezone.changed().connect([&zone_changed, this](const std::string&) {
67
g_main_loop_quit(loop);
70
// start listening for a timezone change, then change the timezone
71
bool zones_changed = false;
72
auto zones_connection = z.timezones.changed().connect([&zones_changed, &zones, this](const std::set<std::string>& timezones) {
75
g_main_loop_quit(loop);
78
g_idle_add([](gpointer s_in) {
79
auto s = static_cast<Settings*>(s_in);
80
g_message("geolocation was %d", (int)s->show_detected_location.get());
81
g_message("turning geolocation on");
82
s->show_detected_location.set(true);
83
return G_SOURCE_REMOVE;
86
// turn on geoclue during the idle... this should add timezone_1 to the 'timezones' property
87
g_main_loop_run(loop);
88
EXPECT_TRUE(zones_changed);
89
EXPECT_EQ(timezone_file, z.timezone.get());
90
EXPECT_EQ(2, zones.size());
91
EXPECT_EQ(1, zones.count(timezone_file));
92
EXPECT_EQ(1, zones.count(timezone_geo));
93
zones_changed = false;
95
// now tweak the geoclue value... the geoclue-detected timezone should change,
96
// causing the 'timezones' property to change
98
zones_changed = false;
99
timezone_geo = "America/Chicago";
100
setGeoclueTimezoneOnIdle(timezone_geo);
101
g_main_loop_run(loop);
102
EXPECT_FALSE(zone_changed);
103
EXPECT_TRUE(zones_changed);
104
EXPECT_EQ(timezone_file, z.timezone.get());
105
EXPECT_EQ(2, zones.size());
106
EXPECT_EQ(1, zones.count(timezone_file));
107
EXPECT_EQ(1, zones.count(timezone_geo));
109
// now set the file value... this should change both the primary property and set property
110
zone_changed = false;
111
zones_changed = false;
112
timezone_file = "America/Los_Angeles";
113
EXPECT_EQ(0, zones.count(timezone_file));
114
g_idle_add([](gpointer str) {set_file(static_cast<const char*>(str)); return G_SOURCE_REMOVE;}, const_cast<char*>(timezone_file.c_str()));
115
g_main_loop_run(loop);
116
EXPECT_TRUE(zone_changed);
117
EXPECT_TRUE(zones_changed);
118
EXPECT_EQ(timezone_file, z.timezone.get());
119
EXPECT_EQ(2, zones.size());
120
EXPECT_EQ(1, zones.count(timezone_file));
121
EXPECT_EQ(1, zones.count(timezone_geo));