~charlesk/indicator-datetime/lp-1001595

« back to all changes in this revision

Viewing changes to tests/test-timezones.cpp

  • Committer: CI bot
  • Author(s): Charles Kerr
  • Date: 2014-01-31 11:46:08 UTC
  • mfrom: (290.2.86 cpp)
  • Revision ID: ps-jenkins@lists.canonical.com-20140131114608-8sfzhimcfdywmk46
Finally land this. Other, still open bugs will be fixed in subsequent commits. Fixes: 793450, 1271484, 1274046

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2013 Canonical Ltd.
 
3
 *
 
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.
 
7
 *
 
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.
 
12
 *
 
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/>.
 
15
 *
 
16
 * Authors:
 
17
 *   Charles Kerr <charles.kerr@canonical.com>
 
18
 */
 
19
 
 
20
#include "geoclue-fixture.h"
 
21
 
 
22
#include <datetime/settings.h>
 
23
#include <datetime/timezones-live.h>
 
24
 
 
25
#include <memory> // std::shared_ptr
 
26
 
 
27
#include <cstdio> // fopen()
 
28
#include <unistd.h> // sync()
 
29
 
 
30
using namespace unity::indicator::datetime;
 
31
 
 
32
typedef GeoclueFixture TimezonesFixture;
 
33
 
 
34
#define TIMEZONE_FILE (SANDBOX "/timezone")
 
35
 
 
36
namespace
 
37
{
 
38
    /* convenience func to set the timezone file */
 
39
    void set_file(const std::string& text)
 
40
    {
 
41
        auto fp = fopen(TIMEZONE_FILE, "w+");
 
42
        fprintf(fp, "%s\n", text.c_str());
 
43
        fclose(fp);
 
44
        sync();
 
45
    }
 
46
}
 
47
 
 
48
 
 
49
TEST_F(TimezonesFixture, ManagerTest)
 
50
{
 
51
  std::string timezone_file = "America/New_York";
 
52
  std::string timezone_geo = "America/Denver";
 
53
 
 
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));
 
63
 
 
64
  bool zone_changed = false;
 
65
  auto zone_connection = z.timezone.changed().connect([&zone_changed, this](const std::string&) {
 
66
          zone_changed = true;
 
67
          g_main_loop_quit(loop);
 
68
        });
 
69
 
 
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) {
 
73
          zones_changed = true;
 
74
          zones = timezones;
 
75
          g_main_loop_quit(loop);
 
76
        });
 
77
 
 
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;
 
84
        }, settings.get());
 
85
 
 
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;
 
94
 
 
95
  // now tweak the geoclue value... the geoclue-detected timezone should change,
 
96
  // causing the 'timezones' property to change
 
97
  zone_changed = false;
 
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));
 
108
 
 
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));
 
122
}
 
123
 
 
124