From a0a6516e74f3b400058af01babfba61bdb1516eb Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sun, 5 Apr 2015 19:19:01 -0500 Subject: add EDS tests to confirm valarm attachments are loaded properly and trigger in the AlarmQueue --- tests/test-eds-valarms.cpp | 113 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 tests/test-eds-valarms.cpp (limited to 'tests/test-eds-valarms.cpp') diff --git a/tests/test-eds-valarms.cpp b/tests/test-eds-valarms.cpp new file mode 100644 index 0000000..f30539a --- /dev/null +++ b/tests/test-eds-valarms.cpp @@ -0,0 +1,113 @@ +/* + * Copyright 2015 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 . + * + * Authors: + * Charles Kerr + */ + +#include + +#include + +#include +#include +#include +#include + +#include + +#include "glib-fixture.h" +#include "timezone-mock.h" +#include "wakeup-timer-mock.h" + + +using namespace unity::indicator::datetime; + +class VAlarmFixture: public GlibFixture +{ +private: + + typedef GlibFixture super; + +protected: + + void SetUp() + { + super::SetUp(); + } + + void TearDown() + { + super::TearDown(); + } +}; + +/*** +**** +***/ + +TEST_F(VAlarmFixture, MultipleAppointments) +{ + // start the EDS engine + auto engine = std::make_shared(); + + // make a planner that looks at the first half of 2015 in EDS + auto tz = std::make_shared("America/Chicago"); + auto planner = std::make_shared(engine, tz); + const auto range_begin = DateTime::Local(2015,1, 1, 0, 0, 0.0); + const auto range_end = DateTime::Local(2015,6,31,23,59,59.5); + planner->range().set(std::make_pair(range_begin, range_end)); + + // give EDS a moment to load + if (planner->appointments().get().empty()) { + g_debug("waiting a moment for EDS to load..."); + auto on_appointments_changed = [this](const std::vector& appointments){ + g_debug("ah, they loaded"); + if (!appointments.empty()) + g_main_loop_quit(loop); + }; + core::ScopedConnection conn(planner->appointments().changed().connect(on_appointments_changed)); + constexpr int max_wait_sec = 10; + wait_msec(max_wait_sec * G_TIME_SPAN_MILLISECOND); + } + + const auto appts = planner->appointments().get(); + ASSERT_EQ(1, appts.size()); + const auto& appt = appts.front(); + ASSERT_EQ(8, appt.alarms.size()); + EXPECT_EQ(Alarm({"Time to pack!", "", DateTime::Local(2015,4,23,13,35,0)}), appt.alarms[0]); + EXPECT_EQ(Alarm({"Time to pack!", "", DateTime::Local(2015,4,23,13,37,0)}), appt.alarms[1]); + EXPECT_EQ(Alarm({"Time to pack!", "", DateTime::Local(2015,4,23,13,39,0)}), appt.alarms[2]); + EXPECT_EQ(Alarm({"Time to pack!", "", DateTime::Local(2015,4,23,13,41,0)}), appt.alarms[3]); + EXPECT_EQ(Alarm({"Go to the airport!", "", DateTime::Local(2015,4,24,10,35,0)}), appt.alarms[4]); + EXPECT_EQ(Alarm({"Go to the airport!", "", DateTime::Local(2015,4,24,10,37,0)}), appt.alarms[5]); + EXPECT_EQ(Alarm({"Go to the airport!", "", DateTime::Local(2015,4,24,10,39,0)}), appt.alarms[6]); + EXPECT_EQ(Alarm({"Go to the airport!", "", DateTime::Local(2015,4,24,10,41,0)}), appt.alarms[7]); + + // now let's try this out with AlarmQueue... + // hook the planner up to a SimpleAlarmQueue and confirm that it triggers for each of the reminders + auto mock_clock = std::make_shared(range_begin); + std::shared_ptr clock = mock_clock; + std::shared_ptr wakeup_timer = std::make_shared(clock); + auto alarm_queue = std::make_shared(clock, planner, wakeup_timer); + int triggered_count = 0; + alarm_queue->alarm_reached().connect([&triggered_count, appt](const Appointment&, const Alarm& active_alarm) { + EXPECT_TRUE(std::find(appt.alarms.begin(), appt.alarms.end(), active_alarm) != appt.alarms.end()); + ++triggered_count; + }); + for (auto now=range_begin; nowset_localtime(now); + EXPECT_EQ(appt.alarms.size(), triggered_count); +} -- cgit v1.2.3 From 38a39c03d5b44c825afe0d10f67cc0802dcf2573 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sun, 5 Apr 2015 21:12:01 -0500 Subject: remove some new bits that turned out to be unneeded after all --- include/datetime/appointment.h | 2 -- tests/test-eds-valarms.cpp | 24 ++---------------------- 2 files changed, 2 insertions(+), 24 deletions(-) (limited to 'tests/test-eds-valarms.cpp') diff --git a/include/datetime/appointment.h b/include/datetime/appointment.h index e9c1bc2..e323c9c 100644 --- a/include/datetime/appointment.h +++ b/include/datetime/appointment.h @@ -22,9 +22,7 @@ #include -#include #include -#include #include namespace unity { diff --git a/tests/test-eds-valarms.cpp b/tests/test-eds-valarms.cpp index f30539a..e9e07d8 100644 --- a/tests/test-eds-valarms.cpp +++ b/tests/test-eds-valarms.cpp @@ -19,8 +19,6 @@ #include -#include - #include #include #include @@ -32,27 +30,8 @@ #include "timezone-mock.h" #include "wakeup-timer-mock.h" - using namespace unity::indicator::datetime; - -class VAlarmFixture: public GlibFixture -{ -private: - - typedef GlibFixture super; - -protected: - - void SetUp() - { - super::SetUp(); - } - - void TearDown() - { - super::TearDown(); - } -}; +using VAlarmFixture = GlibFixture; /*** **** @@ -83,6 +62,7 @@ TEST_F(VAlarmFixture, MultipleAppointments) wait_msec(max_wait_sec * G_TIME_SPAN_MILLISECOND); } + // the planner should match what we've got in the calendar.ics file const auto appts = planner->appointments().get(); ASSERT_EQ(1, appts.size()); const auto& appt = appts.front(); -- cgit v1.2.3 From af81477b2f5d4ffc76c51c72bd3ac89a4c4cac64 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Mon, 6 Apr 2015 10:20:49 -0500 Subject: in the unit tests, add a PrintTo function for Alarms so that GTest can represent it as a string --- tests/print-to.h | 45 +++++++++++++++++++++++++++++++++++++++++++++ tests/test-eds-valarms.cpp | 1 + 2 files changed, 46 insertions(+) create mode 100644 tests/print-to.h (limited to 'tests/test-eds-valarms.cpp') diff --git a/tests/print-to.h b/tests/print-to.h new file mode 100644 index 0000000..78cf574 --- /dev/null +++ b/tests/print-to.h @@ -0,0 +1,45 @@ +/* + * Copyright 2015 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 . + * + * Authors: + * Charles Kerr + */ + +#ifndef INDICATOR_DATETIME_TESTS_PRINT_TO +#define INDICATOR_DATETIME_TESTS_PRINT_TO + +#include + +#include + +namespace unity { +namespace indicator { +namespace datetime { + +/*** +**** PrintTo() functions for GTest to represent objects as strings +***/ + +void +PrintTo(const Alarm& alarm, std::ostream* os) +{ + *os << "{text:'" << alarm.text << "', audio_url:'" << alarm.audio_url << "', time:'"< #include "glib-fixture.h" +#include "print-to.h" #include "timezone-mock.h" #include "wakeup-timer-mock.h" -- cgit v1.2.3 From a3c896c8dd8c82b1d622a3a1220df0870f84d573 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Mon, 6 Apr 2015 13:16:42 -0500 Subject: in new EDS tests, use timezones consistently --- tests/test-eds-valarms.cpp | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) (limited to 'tests/test-eds-valarms.cpp') diff --git a/tests/test-eds-valarms.cpp b/tests/test-eds-valarms.cpp index b92d190..47f29e7 100644 --- a/tests/test-eds-valarms.cpp +++ b/tests/test-eds-valarms.cpp @@ -43,18 +43,22 @@ TEST_F(VAlarmFixture, MultipleAppointments) // start the EDS engine auto engine = std::make_shared(); + // we need a consistent timezone for the planner and our local DateTimes + constexpr char const * zone_str {"America/Chicago"}; + auto tz = std::make_shared(zone_str); + auto gtz = g_time_zone_new(zone_str); + // make a planner that looks at the first half of 2015 in EDS - auto tz = std::make_shared("America/Chicago"); auto planner = std::make_shared(engine, tz); - const auto range_begin = DateTime::Local(2015,1, 1, 0, 0, 0.0); - const auto range_end = DateTime::Local(2015,6,31,23,59,59.5); + const DateTime range_begin {gtz, 2015,1, 1, 0, 0, 0.0}; + const DateTime range_end {gtz, 2015,6,31,23,59,59.5}; planner->range().set(std::make_pair(range_begin, range_end)); // give EDS a moment to load if (planner->appointments().get().empty()) { - g_debug("waiting a moment for EDS to load..."); + g_message("waiting a moment for EDS to load..."); auto on_appointments_changed = [this](const std::vector& appointments){ - g_debug("ah, they loaded"); + g_message("ah, they loaded"); if (!appointments.empty()) g_main_loop_quit(loop); }; @@ -68,14 +72,14 @@ TEST_F(VAlarmFixture, MultipleAppointments) ASSERT_EQ(1, appts.size()); const auto& appt = appts.front(); ASSERT_EQ(8, appt.alarms.size()); - EXPECT_EQ(Alarm({"Time to pack!", "", DateTime::Local(2015,4,23,13,35,0)}), appt.alarms[0]); - EXPECT_EQ(Alarm({"Time to pack!", "", DateTime::Local(2015,4,23,13,37,0)}), appt.alarms[1]); - EXPECT_EQ(Alarm({"Time to pack!", "", DateTime::Local(2015,4,23,13,39,0)}), appt.alarms[2]); - EXPECT_EQ(Alarm({"Time to pack!", "", DateTime::Local(2015,4,23,13,41,0)}), appt.alarms[3]); - EXPECT_EQ(Alarm({"Go to the airport!", "", DateTime::Local(2015,4,24,10,35,0)}), appt.alarms[4]); - EXPECT_EQ(Alarm({"Go to the airport!", "", DateTime::Local(2015,4,24,10,37,0)}), appt.alarms[5]); - EXPECT_EQ(Alarm({"Go to the airport!", "", DateTime::Local(2015,4,24,10,39,0)}), appt.alarms[6]); - EXPECT_EQ(Alarm({"Go to the airport!", "", DateTime::Local(2015,4,24,10,41,0)}), appt.alarms[7]); + EXPECT_EQ(Alarm({"Time to pack!", "", DateTime(gtz,2015,4,23,13,35,0)}), appt.alarms[0]); + EXPECT_EQ(Alarm({"Time to pack!", "", DateTime(gtz,2015,4,23,13,37,0)}), appt.alarms[1]); + EXPECT_EQ(Alarm({"Time to pack!", "", DateTime(gtz,2015,4,23,13,39,0)}), appt.alarms[2]); + EXPECT_EQ(Alarm({"Time to pack!", "", DateTime(gtz,2015,4,23,13,41,0)}), appt.alarms[3]); + EXPECT_EQ(Alarm({"Go to the airport!", "", DateTime(gtz,2015,4,24,10,35,0)}), appt.alarms[4]); + EXPECT_EQ(Alarm({"Go to the airport!", "", DateTime(gtz,2015,4,24,10,37,0)}), appt.alarms[5]); + EXPECT_EQ(Alarm({"Go to the airport!", "", DateTime(gtz,2015,4,24,10,39,0)}), appt.alarms[6]); + EXPECT_EQ(Alarm({"Go to the airport!", "", DateTime(gtz,2015,4,24,10,41,0)}), appt.alarms[7]); // now let's try this out with AlarmQueue... // hook the planner up to a SimpleAlarmQueue and confirm that it triggers for each of the reminders @@ -91,4 +95,7 @@ TEST_F(VAlarmFixture, MultipleAppointments) for (auto now=range_begin; nowset_localtime(now); EXPECT_EQ(appt.alarms.size(), triggered_count); + + // cleanup + g_time_zone_unref(gtz); } -- cgit v1.2.3