[libgweather/wip/hadess/fix-iwin-queries: 3/3] tests: Add unfinished weather test



commit 90b5dac56fae215326b0ec2f5874df4a6de7b9a8
Author: Bastien Nocera <hadess hadess net>
Date:   Mon Aug 26 15:01:24 2019 +0300

    tests: Add unfinished weather test
    
    Could do with options being implemented

 libgweather/meson.build    |   5 ++
 libgweather/test_weather.c | 130 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 135 insertions(+)
---
diff --git a/libgweather/meson.build b/libgweather/meson.build
index 6688807..7ca614e 100644
--- a/libgweather/meson.build
+++ b/libgweather/meson.build
@@ -131,3 +131,8 @@ executable('test_sun_moon',
   c_args: test_cargs,
   dependencies: libgweather_dep,
   install: false)
+executable('test_weather',
+  ['test_weather.c'],
+  c_args: test_cargs,
+  dependencies: libgweather_dep,
+  install: false)
diff --git a/libgweather/test_weather.c b/libgweather/test_weather.c
new file mode 100644
index 0000000..fdce073
--- /dev/null
+++ b/libgweather/test_weather.c
@@ -0,0 +1,130 @@
+
+#include <gweather-version.h>
+#include "gweather-weather.h"
+
+#include <locale.h>
+
+static char *search_str;
+// gnome-weather uses:
+static GWeatherProvider providers = GWEATHER_PROVIDER_METAR | GWEATHER_PROVIDER_YR_NO | 
GWEATHER_PROVIDER_OWM;
+
+// default:
+//static GWeatherProvider providers = GWEATHER_PROVIDER_METAR | GWEATHER_PROVIDER_IWIN;
+
+static void
+find_loc_children (GWeatherLocation  *location,
+                  const char        *search_str,
+                  GWeatherLocation **ret)
+{
+    GWeatherLocation **children;
+    guint i;
+
+    children = gweather_location_get_children (location);
+    for (i = 0; children[i] != NULL; i++) {
+        if (gweather_location_get_level (children[i]) == GWEATHER_LOCATION_WEATHER_STATION) {
+            const char *code;
+
+            code = gweather_location_get_code (children[i]);
+            if (g_strcmp0 (search_str, code) == 0) {
+                *ret = gweather_location_ref (children[i]);
+                return;
+            }
+        } else {
+            find_loc_children (children[i], search_str, ret);
+        }
+    }
+}
+
+static GWeatherLocation *
+find_loc (GWeatherLocation *world,
+         const char       *search_str)
+{
+    GWeatherLocation *loc = NULL;
+
+    find_loc_children (world, search_str, &loc);
+    return loc;
+}
+
+static void
+weather_updated (GWeatherInfo *info,
+                 GMainLoop    *loop)
+{
+    GSList *forecasts, *l;
+    time_t val;
+    static gboolean weather_printed = FALSE;
+    static gboolean forecast_printed = FALSE;
+
+    if (gweather_info_get_value_update (info, &val)) {
+        g_message ("Weather now: %s", gweather_info_get_temp_summary (info));
+        weather_printed = TRUE;
+    }
+
+    forecasts = gweather_info_get_forecast_list (info);
+    if (!forecasts)
+        return;
+
+    for (l = forecasts; l != NULL; l = l->next) {
+        GWeatherInfo *i = l->data;
+
+        if (gweather_info_get_value_update (i, &val)) {
+            GDateTime *d;
+            char *date_str;
+
+            d = g_date_time_new_from_unix_utc (val);
+            date_str = g_date_time_format (d, "%c");
+            g_message ("Weather for %s: %s", date_str, gweather_info_get_temp_summary (i));
+            g_free (date_str);
+
+            /* One will be enough... */
+            forecast_printed = TRUE;
+        }
+    }
+
+    if (weather_printed &&
+        forecast_printed)
+        g_main_loop_quit (loop);
+}
+
+static void
+set_providers (GWeatherInfo *info)
+{
+    //FIXME print the current providers
+    gweather_info_set_enabled_providers (info, providers);
+}
+
+int
+main (int argc, char **argv)
+{
+    GWeatherLocation *world, *loc;
+    GWeatherInfo *info;
+    GMainLoop *loop;
+
+    setlocale (LC_ALL, "");
+
+    // FIXME add options
+    if (argc != 2)
+        return 1;
+    search_str = argv[1];
+
+    world = gweather_location_get_world ();
+    loc = find_loc (world, search_str);
+
+    if (!loc) {
+        g_message ("Could not find station for %s", search_str);
+        return 1;
+    }
+
+    g_message ("Found station %s for '%s'", gweather_location_get_name (loc), search_str);
+
+    loop = g_main_loop_new (NULL, TRUE);
+    info = gweather_info_new (loc);
+    set_providers (info);
+    g_signal_connect (G_OBJECT (info), "updated",
+                      G_CALLBACK (weather_updated), loop);
+    gweather_info_update (info);
+
+    g_main_loop_run (loop);
+    g_main_loop_unref (loop);
+
+    return 0;
+}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]