aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/device.c474
-rw-r--r--src/device.h45
-rw-r--r--src/service.c83
-rw-r--r--tests/Makefile.am2
-rw-r--r--tests/test-device.cc152
5 files changed, 499 insertions, 257 deletions
diff --git a/src/device.c b/src/device.c
index 7f1b14f..c941454 100644
--- a/src/device.c
+++ b/src/device.c
@@ -37,6 +37,11 @@ struct _IndicatorPowerDevicePrivate
gchar * object_path;
gdouble percentage;
time_t time;
+
+ /* Timestamp of when when we first noticed that upower couldn't estimate
+ the time-remaining field for this device, or 0 if not applicable.
+ This is used when generating the time-remaining string. */
+ GTimer * inestimable;
};
#define INDICATOR_POWER_DEVICE_GET_PRIVATE(o) (INDICATOR_POWER_DEVICE(o)->priv)
@@ -136,6 +141,11 @@ indicator_power_device_init (IndicatorPowerDevice *self)
static void
indicator_power_device_dispose (GObject *object)
{
+ IndicatorPowerDevice * self = INDICATOR_POWER_DEVICE(object);
+ IndicatorPowerDevicePrivate * p = self->priv;
+
+ g_clear_pointer (&p->inestimable, g_timer_destroy);
+
G_OBJECT_CLASS (indicator_power_device_parent_class)->dispose (object);
}
@@ -192,35 +202,50 @@ static void
set_property (GObject * o, guint prop_id, const GValue * value, GParamSpec * pspec)
{
IndicatorPowerDevice * self = INDICATOR_POWER_DEVICE(o);
- IndicatorPowerDevicePrivate * priv = self->priv;
+ IndicatorPowerDevicePrivate * p = self->priv;
switch (prop_id)
{
case PROP_KIND:
- priv->kind = g_value_get_int (value);
+ p->kind = g_value_get_int (value);
break;
case PROP_STATE:
- priv->state = g_value_get_int (value);
+ p->state = g_value_get_int (value);
break;
case PROP_OBJECT_PATH:
- g_free (priv->object_path);
- priv->object_path = g_value_dup_string (value);
+ g_free (p->object_path);
+ p->object_path = g_value_dup_string (value);
break;
case PROP_PERCENTAGE:
- priv->percentage = g_value_get_double (value);
+ p->percentage = g_value_get_double (value);
break;
case PROP_TIME:
- priv->time = g_value_get_uint64(value);
+ p->time = g_value_get_uint64(value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(o, prop_id, pspec);
break;
}
+
+ /* update inestimable_at */
+
+ const gboolean is_inestimable = (p->time == 0)
+ && (p->state != UP_DEVICE_STATE_FULLY_CHARGED)
+ && (p->percentage > 0);
+
+ if (!is_inestimable)
+ {
+ g_clear_pointer (&p->inestimable, g_timer_destroy);
+ }
+ else if (p->inestimable == NULL)
+ {
+ p->inestimable = g_timer_new ();
+ }
}
/***
@@ -442,57 +467,6 @@ indicator_power_device_get_gicon (const IndicatorPowerDevice * device)
****
***/
-/* Format time remaining for reading ("H:MM") and speech ("H hours, MM minutes") */
-static void
-get_timestring (guint64 time_secs,
- gchar **readable_timestring,
- gchar **accessible_timestring)
-{
- gint hours;
- gint minutes;
-
- /* Add 0.5 to do rounding */
- minutes = (int) ( ( time_secs / 60.0 ) + 0.5 );
-
- if (minutes == 0)
- {
- *readable_timestring = g_strdup (_("Unknown time"));
- *accessible_timestring = g_strdup (_("Unknown time"));
-
- return;
- }
-
- if (minutes < 60)
- {
- *readable_timestring = g_strdup_printf ("0:%.2i", minutes);
- *accessible_timestring = g_strdup_printf (g_dngettext (GETTEXT_PACKAGE, "%i minute",
- "%i minutes",
- minutes), minutes);
- return;
- }
-
- hours = minutes / 60;
- minutes = minutes % 60;
-
- *readable_timestring = g_strdup_printf ("%i:%.2i", hours, minutes);
-
- if (minutes == 0)
- {
- *accessible_timestring = g_strdup_printf (g_dngettext (GETTEXT_PACKAGE,
- "%i hour",
- "%i hours",
- hours), hours);
- }
- else
- {
- /* TRANSLATOR: "%i %s %i %s" are "%i hours %i minutes"
- * Swap order with "%2$s %2$i %1$s %1$i if needed */
- *accessible_timestring = g_strdup_printf (_("%i %s %i %s"),
- hours, g_dngettext (GETTEXT_PACKAGE, "hour", "hours", hours),
- minutes, g_dngettext (GETTEXT_PACKAGE, "minute", "minutes", minutes));
- }
-}
-
static const gchar *
device_kind_to_localised_string (UpDeviceKind kind)
{
@@ -555,149 +529,325 @@ device_kind_to_localised_string (UpDeviceKind kind)
return text;
}
-static char *
-join_strings (const char * name, const char * time, const char * percent)
+/**
+ * The '''brief time-remaining string''' for a component should be:
+ * * the time remaining for it to empty or fully charge,
+ * if estimable, in H:MM format; otherwise
+ * * “estimating…” if the time remaining has been inestimable for
+ * less than 30 seconds; otherwise
+ * * “unknown” if the time remaining has been inestimable for
+ * between 30 seconds and one minute; otherwise
+ * * the empty string.
+ */
+static void
+get_brief_time_remaining (const IndicatorPowerDevice * device,
+ char * str,
+ gulong size)
{
- char * str;
- const gboolean have_name = name && *name;
- const gboolean have_time = time && *time;
- const gboolean have_percent = percent && *percent;
-
- if (have_name && have_time && have_percent)
- str = g_strdup_printf (_("%s (%s, %s)"), name, time, percent);
- else if (have_name && have_time)
- str = g_strdup_printf (_("%s (%s)"), name, time);
- else if (have_name && have_percent)
- str = g_strdup_printf (_("%s (%s)"), name, percent);
- else if (have_name)
- str = g_strdup (name);
- else if (have_time && have_percent)
- str = g_strdup_printf (_("(%s, %s)"), time, percent);
- else if (have_time)
- str = g_strdup_printf (_("(%s)"), time);
- else if (have_percent)
- str = g_strdup_printf (_("(%s)"), percent);
- else
- str = g_strdup ("");
+ const IndicatorPowerDevicePrivate * p = device->priv;
+
+ if (p->time > 0)
+ {
+ int minutes = p->time / 60;
+ int hours = minutes / 60;
+ minutes %= 60;
+
+ g_snprintf (str, size, "%0d:%02d", hours, minutes);
+ }
+ else if (p->inestimable != NULL)
+ {
+ const double elapsed = g_timer_elapsed (p->inestimable, NULL);
- return str;
+ if (elapsed < 30)
+ {
+ g_snprintf (str, size, _("estimating…"));
+ }
+ else if (elapsed < 60)
+ {
+ g_snprintf (str, size, _("unknown"));
+ }
+ else
+ {
+ *str = '\0';
+ }
+ }
+ else
+ {
+ *str = '\0';
+ }
}
+/**
+ * The '''expanded time-remaining string''' for a component should
+ * be the same as the brief time-remaining string, except that if
+ * the time is estimable:
+ * * if the component is charging, it should be “H:MM to charge”
+ * * if the component is discharging, it should be “H:MM left”.
+ */
static void
-indicator_power_device_get_text (const IndicatorPowerDevice * device,
- gboolean show_time_in_header,
- gboolean show_percentage_in_header,
- gchar ** header,
- gchar ** label,
- gchar ** a11y)
+get_expanded_time_remaining (const IndicatorPowerDevice * device,
+ char * str,
+ gulong size)
{
- if (!INDICATOR_IS_POWER_DEVICE(device))
- {
- if (a11y != NULL) *a11y = NULL;
- if (label != NULL) *label = NULL;
- if (header != NULL) *header = NULL;
- g_warning ("%s: %p is not an IndicatorPowerDevice", G_STRFUNC, device);
- return;
- }
+ const IndicatorPowerDevicePrivate * p;
- const time_t time = indicator_power_device_get_time (device);
- const UpDeviceState state = indicator_power_device_get_state (device);
- const UpDeviceKind kind = indicator_power_device_get_kind (device);
- const gchar * device_name = device_kind_to_localised_string (kind);
- const gdouble percentage = indicator_power_device_get_percentage (device);
- char pctstr[32] = { '\0' };
- g_snprintf (pctstr, sizeof(pctstr), "%.0lf%%", percentage);
+ g_return_if_fail (str != NULL);
+ g_return_if_fail (size > 0);
+ *str = '\0';
+ g_return_if_fail (INDICATOR_IS_POWER_DEVICE(device));
- GString * terse_time = g_string_new (NULL);
- GString * verbose_time = g_string_new (NULL);
- GString * accessible_time = g_string_new (NULL);
+ p = device->priv;
- if (time > 0)
+ if (p->time && ((p->state == UP_DEVICE_STATE_CHARGING) || (p->state == UP_DEVICE_STATE_DISCHARGING)))
{
- char * readable_timestr = NULL;
- char * accessible_timestr = NULL;
- get_timestring (time, &readable_timestr, &accessible_timestr);
+ int minutes = p->time / 60;
+ int hours = minutes / 60;
+ minutes %= 60;
- if (state == UP_DEVICE_STATE_CHARGING)
+ if (p->state == UP_DEVICE_STATE_CHARGING)
{
- g_string_assign (terse_time, readable_timestr);
- g_string_printf (verbose_time, _("%s to charge"), readable_timestr);
- g_string_printf (accessible_time, _("%s to charge"), accessible_timestr);
+ g_snprintf (str, size, _("%0d:%02d to charge"), hours, minutes);
}
- else if ((state == UP_DEVICE_STATE_DISCHARGING) && (time <= (60*60*24)))
+ else // discharging
{
- g_string_assign (terse_time, readable_timestr);
- g_string_printf (verbose_time, _("%s left"), readable_timestr);
- g_string_printf (accessible_time, _("%s left"), accessible_timestr);
+ g_snprintf (str, size, _("%0d:%02d left"), hours, minutes);
}
- else
+ }
+ else
+ {
+ get_brief_time_remaining (device, str, size);
+ }
+}
+
+/**
+ * The '''accessible time-remaining string''' for a component
+ * should be the same as the expanded time-remaining string,
+ * except the H:MM time should be rendered as “''H'' hours ''M'' minutes”,
+ * or just as “''M'' * minutes” if the time is less than one hour.
+ */
+static void
+get_accessible_time_remaining (const IndicatorPowerDevice * device,
+ char * str,
+ gulong size)
+{
+ const IndicatorPowerDevicePrivate * p;
+
+ g_return_if_fail (str != NULL);
+ g_return_if_fail (size > 0);
+ *str = '\0';
+ g_return_if_fail (INDICATOR_IS_POWER_DEVICE(device));
+
+ p = device->priv;
+
+ if (p->time && ((p->state == UP_DEVICE_STATE_CHARGING) || (p->state == UP_DEVICE_STATE_DISCHARGING)))
+ {
+ int minutes = p->time / 60;
+ int hours = minutes / 60;
+ minutes %= 60;
+
+ if (p->state == UP_DEVICE_STATE_CHARGING)
{
- /* if there's more than 24 hours remaining, we don't show it */
+ if (hours)
+ g_snprintf (str, size, _("%d %s %d %s to charge"),
+ hours, g_dngettext (NULL, "hour", "hours", hours),
+ minutes, g_dngettext (NULL, "minute", "minutes", minutes));
+ else
+ g_snprintf (str, size, _("%d %s to charge"),
+ minutes, g_dngettext (NULL, "minute", "minutes", minutes));
+ }
+ else // discharging
+ {
+ if (hours)
+ g_snprintf (str, size, _("%d %s %d %s left"),
+ hours, g_dngettext (NULL, "hour", "hours", hours),
+ minutes, g_dngettext (NULL, "minute", "minutes", minutes));
+ else
+ g_snprintf (str, size, _("%d %s left"),
+ minutes, g_dngettext (NULL, "minute", "minutes", minutes));
}
-
- g_free (readable_timestr);
- g_free (accessible_timestr);
}
- else if (state == UP_DEVICE_STATE_FULLY_CHARGED)
+ else
{
- g_string_assign (verbose_time, _("charged"));
- g_string_assign (accessible_time, _("charged"));
+ get_brief_time_remaining (device, str, size);
}
- else if (percentage > 0)
+}
+
+/**
+ * The time is relevant for a device if either (a) the component is charging,
+ * or (b) the component is discharging and the estimated time is less than
+ * 24 hours. (A time greater than 24 hours is probably a mistaken calculation.)
+ */
+static gboolean
+time_is_relevant (const IndicatorPowerDevice * device)
+{
+ const IndicatorPowerDevicePrivate * p = device->priv;
+
+ if (p->state == UP_DEVICE_STATE_CHARGING)
+ return TRUE;
+
+ if ((p->state == UP_DEVICE_STATE_DISCHARGING) && (p->time<(24*60*60)))
+ return TRUE;
+
+ return FALSE;
+}
+
+/**
+ * The menu item for each chargeable component should consist of ...
+ * Text representing the name of the component (“Battery”, “Mouse”,
+ * “UPS”, “Alejandra’s iPod”, etc) and the charge status in brackets:
+ *
+ * * “X (charged)” if it is fully charged and not discharging;
+ * * “X (expanded time-remaining string)” if it is charging,
+ * or discharging with less than 24 hours left;
+ * * “X” if it is discharging with 24 hours or more left.
+ *
+ * The accessible label for the menu item should be the same as the
+ * visible label, except with the accessible time-remaining string
+ * instead of the expanded time-remaining string.
+ */
+static void
+get_menuitem_text (const IndicatorPowerDevice * device,
+ gchar * str,
+ gulong size,
+ gboolean accessible)
+{
+ const IndicatorPowerDevicePrivate * p = device->priv;
+ const char * kind_str = device_kind_to_localised_string (p->kind);
+
+ if (p->state == UP_DEVICE_STATE_FULLY_CHARGED)
{
- g_string_assign (terse_time, _("estimating…"));
- g_string_assign (verbose_time, _("estimating…"));
- g_string_assign (accessible_time, _("estimating…"));
+ g_snprintf (str, size, _("%s (charged)"), kind_str);
}
else
{
- *pctstr = '\0';
+ char buf[64];
- if (kind != UP_DEVICE_KIND_LINE_POWER)
+ if (time_is_relevant (device))
{
- g_string_assign (verbose_time, _("not present"));
- g_string_assign (accessible_time, _("not present"));
+ if (accessible)
+ get_accessible_time_remaining (device, buf, sizeof(buf));
+ else
+ get_expanded_time_remaining (device, buf, sizeof(buf));
}
+ else
+ {
+ *buf = '\0';
+ }
+
+ if (*buf)
+ g_snprintf (str, size, _("%s (%s)"), kind_str, buf);
+ else
+ g_strlcpy (str, kind_str, size);
}
+}
- if (header != NULL)
- *header = join_strings (NULL,
- show_time_in_header ? terse_time->str : "",
- show_percentage_in_header ? pctstr : "");
+void
+indicator_power_device_get_readable_text (const IndicatorPowerDevice * device,
+ gchar * str,
+ gulong size)
+{
+ g_return_if_fail (str != NULL);
+ g_return_if_fail (size > 0);
+ *str = '\0';
+ g_return_if_fail (INDICATOR_IS_POWER_DEVICE(device));
- if (label != NULL)
- *label = join_strings (device_name,
- verbose_time->str,
- NULL);
+ get_menuitem_text (device, str, size, FALSE);
+}
- if (a11y != NULL)
- *a11y = join_strings (device_name,
- accessible_time->str,
- pctstr);
+void
+indicator_power_device_get_accessible_text (const IndicatorPowerDevice * device,
+ gchar * str,
+ gulong size)
+{
+ g_return_if_fail (str != NULL);
+ g_return_if_fail (size > 0);
+ *str = '\0';
+ g_return_if_fail (INDICATOR_IS_POWER_DEVICE(device));
- g_string_free (terse_time, TRUE);
- g_string_free (verbose_time, TRUE);
- g_string_free (accessible_time, TRUE);
+ get_menuitem_text (device, str, size, TRUE);
}
-gchar *
-indicator_power_device_get_label (const IndicatorPowerDevice * device)
+#if 0
+- If the time is relevant, the brackets should contain the time-remaining string for that component.
++ If the time is relevant, the brackets should contain the brief time-remaining string for that component.
+
+- Regardless, the accessible name for the whole menu title should be the same as the accessible name for that thing’s component inside the menu itself.
++ The accessible name for the whole menu title should be the same as the except using the accessible time-remaining string instead of the brief time-remaining string.
+#endif
+
+/**
+ * If the time is relevant and/or “Show Percentage in Menu Bar” is checked,
+ * the icon should be followed by brackets.
+ *
+ * If the time is relevant, the brackets should contain the time-remaining
+ * string for that component.
+ *
+ * If “Show Percentage in Menu Bar” is checked (as it should not be by default),
+ * the brackets should contain the percentage charge for that device.
+ *
+ * If both conditions are true, the time and percentage should be separated by a space.
+ */
+void
+indicator_power_device_get_readable_title (const IndicatorPowerDevice * device,
+ gchar * str,
+ gulong size,
+ gboolean want_time,
+ gboolean want_percent)
{
- gchar * label = NULL;
- indicator_power_device_get_text (device, FALSE, FALSE,
- NULL, &label, NULL);
- return label;
+ char tr[64];
+ const IndicatorPowerDevicePrivate * p;
+
+ g_return_if_fail (str != NULL);
+ g_return_if_fail (size > 0);
+ *str = '\0';
+ g_return_if_fail (INDICATOR_IS_POWER_DEVICE(device));
+
+ p = device->priv;
+
+ if (want_time && !time_is_relevant (device))
+ want_time = FALSE;
+
+ if (p->percentage < 0.01)
+ want_percent = FALSE;
+
+ if (want_time)
+ {
+ get_brief_time_remaining (device, tr, sizeof(tr));
+
+ if (!*tr)
+ want_time = FALSE;
+ }
+
+ if (want_time && want_percent)
+ {
+ g_snprintf (str, size, _("(%s, %.0lf%%)"), tr, p->percentage);
+ }
+ else if (want_time)
+ {
+ g_snprintf (str, size, _("(%s)"), tr);
+ }
+ else if (want_percent)
+ {
+ g_snprintf (str, size, _("(%.0lf%%)"), p->percentage);
+ }
+ else
+ {
+ *str = '\0';
+ }
}
+/**
+ * Regardless, the accessible name for the whole menu title should be the same
+ * as the accessible name for that thing’s component inside the menu itself.
+ */
void
-indicator_power_device_get_header (const IndicatorPowerDevice * device,
- gboolean show_time,
- gboolean show_percentage,
- gchar ** header,
- gchar ** a11y)
+indicator_power_device_get_accessible_title (const IndicatorPowerDevice * device,
+ gchar * str,
+ gulong size,
+ gboolean want_time G_GNUC_UNUSED,
+ gboolean want_percent G_GNUC_UNUSED)
{
- indicator_power_device_get_text (device, show_time, show_percentage,
- header, NULL, a11y);
+ indicator_power_device_get_accessible_text (device, str, size);
}
/***
diff --git a/src/device.h b/src/device.h
index 1f395a1..65c6767 100644
--- a/src/device.h
+++ b/src/device.h
@@ -116,22 +116,35 @@ IndicatorPowerDevice* indicator_power_device_new (const gchar * object_path,
IndicatorPowerDevice* indicator_power_device_new_from_variant (GVariant * variant);
-UpDeviceKind indicator_power_device_get_kind (const IndicatorPowerDevice * device);
-UpDeviceState indicator_power_device_get_state (const IndicatorPowerDevice * device);
-const gchar * indicator_power_device_get_object_path (const IndicatorPowerDevice * device);
-gdouble indicator_power_device_get_percentage (const IndicatorPowerDevice * device);
-time_t indicator_power_device_get_time (const IndicatorPowerDevice * device);
-
-GStrv indicator_power_device_get_icon_names (const IndicatorPowerDevice * device);
-GIcon * indicator_power_device_get_gicon (const IndicatorPowerDevice * device);
-
-gchar * indicator_power_device_get_label (const IndicatorPowerDevice * device);
-
-void indicator_power_device_get_header (const IndicatorPowerDevice * device,
- gboolean show_time,
- gboolean show_percentage,
- gchar ** header,
- gchar ** a11y);
+UpDeviceKind indicator_power_device_get_kind (const IndicatorPowerDevice * device);
+UpDeviceState indicator_power_device_get_state (const IndicatorPowerDevice * device);
+const gchar * indicator_power_device_get_object_path (const IndicatorPowerDevice * device);
+gdouble indicator_power_device_get_percentage (const IndicatorPowerDevice * device);
+time_t indicator_power_device_get_time (const IndicatorPowerDevice * device);
+
+GStrv indicator_power_device_get_icon_names (const IndicatorPowerDevice * device);
+GIcon * indicator_power_device_get_gicon (const IndicatorPowerDevice * device);
+
+
+void indicator_power_device_get_readable_text (const IndicatorPowerDevice * device,
+ gchar * str,
+ gulong size);
+
+void indicator_power_device_get_accessible_text (const IndicatorPowerDevice * device,
+ gchar * str,
+ gulong size);
+
+void indicator_power_device_get_readable_title (const IndicatorPowerDevice * device,
+ gchar * str,
+ gulong size,
+ gboolean want_time,
+ gboolean want_percent);
+
+void indicator_power_device_get_accessible_title (const IndicatorPowerDevice * device,
+ gchar * str,
+ gulong size,
+ gboolean want_time,
+ gboolean want_percent);
G_END_DECLS
diff --git a/src/service.c b/src/service.c
index 982a24e..248f953 100644
--- a/src/service.c
+++ b/src/service.c
@@ -312,21 +312,7 @@ static GVariant *
create_header_state (IndicatorPowerService * self)
{
GVariantBuilder b;
- gchar * label = NULL;
- gchar * a11y = NULL;
- GIcon * icon = NULL;
- priv_t * p = self->priv;
-
- if (p->primary_device != NULL)
- {
- indicator_power_device_get_header (p->primary_device,
- g_settings_get_boolean (p->settings, SETTINGS_SHOW_TIME_S),
- g_settings_get_boolean (p->settings, SETTINGS_SHOW_PERCENTAGE_S),
- &label,
- &a11y);
-
- icon = indicator_power_device_get_gicon (p->primary_device);
- }
+ const priv_t * const p = self->priv;
g_variant_builder_init (&b, G_VARIANT_TYPE("a{sv}"));
@@ -335,24 +321,42 @@ create_header_state (IndicatorPowerService * self)
g_variant_builder_add (&b, "{sv}", "visible",
g_variant_new_boolean (should_be_visible (self)));
- if (label != NULL)
- g_variant_builder_add (&b, "{sv}", "label", g_variant_new_take_string (label));
-
- if (icon != NULL)
+ if (p->primary_device != NULL)
{
- GVariant * v;
+ char buf[128];
+ GIcon * icon;
+ const gboolean want_time = g_settings_get_boolean (p->settings, SETTINGS_SHOW_TIME_S);
+ const gboolean want_percent = g_settings_get_boolean (p->settings, SETTINGS_SHOW_PERCENTAGE_S);
+
+ indicator_power_device_get_readable_title (p->primary_device,
+ buf, sizeof(buf),
+ want_time,
+ want_percent);
+ if (*buf)
+ g_variant_builder_add (&b, "{sv}", "label", g_variant_new_string (buf));
+
+
+ indicator_power_device_get_accessible_title (p->primary_device,
+ buf, sizeof(buf),
+ want_time,
+ want_percent);
+ if (*buf)
+ g_variant_builder_add (&b, "{sv}", "accessible-desc", g_variant_new_string (buf));
- if ((v = g_icon_serialize (icon)))
+
+ if ((icon = indicator_power_device_get_gicon (p->primary_device)))
{
- g_variant_builder_add (&b, "{sv}", "icon", v);
- g_variant_unref (v);
- }
+ GVariant * serialized_icon = g_icon_serialize (icon);
- g_object_unref (icon);
- }
+ if (serialized_icon != NULL)
+ {
+ g_variant_builder_add (&b, "{sv}", "icon", serialized_icon);
+ g_variant_unref (serialized_icon);
+ }
- if (a11y != NULL)
- g_variant_builder_add (&b, "{sv}", "accessible-desc", g_variant_new_take_string (a11y));
+ g_object_unref (icon);
+ }
+ }
return g_variant_builder_end (&b);
}
@@ -371,28 +375,31 @@ append_device_to_menu (GMenu * menu, const IndicatorPowerDevice * device)
if (kind != UP_DEVICE_KIND_LINE_POWER)
{
- char * label;
+ char buf[128];
GMenuItem * item;
GIcon * icon;
- label = indicator_power_device_get_label (device);
- item = g_menu_item_new (label, "indicator.activate-statistics");
- g_free (label);
- g_menu_item_set_action_and_target(item, "indicator.activate-statistics", "s",
- indicator_power_device_get_object_path (device));
+ indicator_power_device_get_readable_text (device, buf, sizeof(buf));
+ item = g_menu_item_new (buf, "indicator.activate-statistics");
if ((icon = indicator_power_device_get_gicon (device)))
{
- GVariant * v;
- if ((v = g_icon_serialize (icon)))
+ GVariant * serialized_icon = g_icon_serialize (icon);
+
+ if (serialized_icon != NULL)
{
- g_menu_item_set_attribute_value (item, G_MENU_ATTRIBUTE_ICON, v);
- g_variant_unref (v);
+ g_menu_item_set_attribute_value (item,
+ G_MENU_ATTRIBUTE_ICON,
+ serialized_icon);
+ g_variant_unref (serialized_icon);
}
g_object_unref (icon);
}
+ g_menu_item_set_action_and_target(item, "indicator.activate-statistics", "s",
+ indicator_power_device_get_object_path (device));
+
g_menu_append_item (menu, item);
g_object_unref (item);
}
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 5c7d802..47b3e84 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -18,7 +18,7 @@ nodist_libgtest_a_SOURCES = \
$(GTEST_SOURCE)/src/gtest-all.cc \
$(GTEST_SOURCE)/src/gtest_main.cc
-AM_CPPFLAGS = $(GTEST_CPPFLAGS) -I${top_srcdir}/src -Wall -Werror
+AM_CPPFLAGS = $(GTEST_CPPFLAGS) -I${top_srcdir}/src -Wall -Werror -std=c++11
AM_CXXFLAGS = $(GTEST_CXXFLAGS)
###
diff --git a/tests/test-device.cc b/tests/test-device.cc
index 2762d4a..dfbba59 100644
--- a/tests/test-device.cc
+++ b/tests/test-device.cc
@@ -72,12 +72,11 @@ class DeviceTest : public ::testing::Test
void check_label (const IndicatorPowerDevice * device,
const char * expected_label)
{
- char * label;
-
- label = indicator_power_device_get_label (device);
+ char label[128];
+ indicator_power_device_get_readable_text (device, label, sizeof(label));
+ if (expected_label == nullptr)
+ expected_label = "";
EXPECT_STREQ (expected_label, label);
-
- g_free (label);
}
void check_header (const IndicatorPowerDevice * device,
@@ -86,32 +85,23 @@ class DeviceTest : public ::testing::Test
const char * expected_percent,
const char * expected_a11y)
{
- char * label;
- char * a11y;
-
- indicator_power_device_get_header (device, true, true, &label, &a11y);
- EXPECT_STREQ (expected_time_and_percent, label);
- EXPECT_STREQ (expected_a11y, a11y);
- g_free (label);
- g_free (a11y);
-
- indicator_power_device_get_header (device, true, false, &label, &a11y);
- EXPECT_STREQ (expected_time, label);
- EXPECT_STREQ (expected_a11y, a11y);
- g_free (label);
- g_free (a11y);
-
- indicator_power_device_get_header (device, false, true, &label, &a11y);
- EXPECT_STREQ (expected_percent, label);
- EXPECT_STREQ (expected_a11y, a11y);
- g_free (label);
- g_free (a11y);
-
- indicator_power_device_get_header (device, false, false, &label, &a11y);
- ASSERT_TRUE (!label || !*label);
- EXPECT_STREQ (expected_a11y, a11y);
- g_free (label);
- g_free (a11y);
+ char a11y[128];
+ char title[128];
+
+ indicator_power_device_get_readable_title (device, title, sizeof(title), true, true);
+ EXPECT_STREQ (expected_time_and_percent ? expected_time_and_percent : "", title);
+
+ indicator_power_device_get_readable_title (device, title, sizeof(title), true, false);
+ EXPECT_STREQ (expected_time ? expected_time : "", title);
+
+ indicator_power_device_get_readable_title (device, title, sizeof(title), false, true);
+ EXPECT_STREQ (expected_percent ? expected_percent : "", title);
+
+ indicator_power_device_get_readable_title (device, title, sizeof(title), false, false);
+ EXPECT_STREQ ("", title);
+
+ indicator_power_device_get_accessible_title (device, a11y, sizeof(a11y), false, false);
+ EXPECT_STREQ (expected_a11y ? expected_a11y : "", a11y);
}
};
@@ -481,14 +471,16 @@ TEST_F(DeviceTest, Labels)
g_setenv ("LANG", "en_US.UTF-8", TRUE);
// bad args: NULL device
- log_count_ipower_expected += 5;
+ log_count_ipower_expected++;
check_label (NULL, NULL);
+ log_count_ipower_expected += 5;
check_header (NULL, NULL, NULL, NULL, NULL);
// bad args: a GObject that isn't a device
- log_count_ipower_expected += 5;
GObject * o = G_OBJECT(g_cancellable_new());
+ log_count_ipower_expected++;
check_label ((IndicatorPowerDevice*)o, NULL);
+ log_count_ipower_expected += 5;
check_header (NULL, NULL, NULL, NULL, NULL);
g_object_unref (o);
@@ -509,7 +501,7 @@ TEST_F(DeviceTest, Labels)
check_header (device, "(1:01, 50%)",
"(1:01)",
"(50%)",
- "Battery (1 hour 1 minute to charge, 50%)");
+ "Battery (1 hour 1 minute to charge)");
// discharging, < 12 hours left
g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY,
@@ -521,7 +513,7 @@ TEST_F(DeviceTest, Labels)
check_header (device, "(1:01, 50%)",
"(1:01)",
"(50%)",
- "Battery (1 hour 1 minute left, 50%)");
+ "Battery (1 hour 1 minute left)");
// discharging, > 24 hours left
// we don't show the clock time when > 24 hours discharging
@@ -534,7 +526,7 @@ TEST_F(DeviceTest, Labels)
check_header (device, "(50%)",
"",
"(50%)",
- "Battery (50%)");
+ "Battery");
// fully charged
g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY,
@@ -546,7 +538,7 @@ TEST_F(DeviceTest, Labels)
check_header (device, "(100%)",
"",
"(100%)",
- "Battery (charged, 100%)");
+ "Battery (charged)");
// percentage but no time estimate
g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY,
@@ -558,7 +550,7 @@ TEST_F(DeviceTest, Labels)
check_header (device, "(estimating…, 50%)",
"(estimating…)",
"(50%)",
- "Battery (estimating…, 50%)");
+ "Battery (estimating…)");
// no percentage, no time estimate
g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY,
@@ -566,8 +558,8 @@ TEST_F(DeviceTest, Labels)
INDICATOR_POWER_DEVICE_PERCENTAGE, 0.0,
INDICATOR_POWER_DEVICE_TIME, guint64(0),
NULL);
- check_label (device, "Battery (not present)");
- check_header (device, "", "", "", "Battery (not present)");
+ check_label (device, "Battery");
+ check_header (device, "", "", "", "Battery");
// power line
g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_LINE_POWER,
@@ -584,6 +576,84 @@ TEST_F(DeviceTest, Labels)
g_free (real_lang);
}
+
+TEST_F(DeviceTest, Inestimable___this_takes_80_seconds)
+{
+ // set our language so that i18n won't break these tests
+ auto real_lang = g_strdup(g_getenv ("LANG"));
+ g_setenv ("LANG", "en_US.UTF-8", true);
+
+ // set up the main loop
+ auto loop = g_main_loop_new (nullptr, false);
+ auto loop_quit_sourcefunc = [](gpointer l){
+ g_main_loop_quit(static_cast<GMainLoop*>(l));
+ return G_SOURCE_REMOVE;
+ };
+
+ auto device = INDICATOR_POWER_DEVICE (g_object_new (INDICATOR_POWER_DEVICE_TYPE, nullptr));
+ auto o = G_OBJECT(device);
+
+ // percentage but no time estimate
+ auto timer = g_timer_new ();
+ g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY,
+ INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING,
+ INDICATOR_POWER_DEVICE_PERCENTAGE, 50.0,
+ INDICATOR_POWER_DEVICE_TIME, guint64(0),
+ nullptr);
+
+ /*
+ * “estimating…” if the time remaining has been inestimable for
+ * less than 30 seconds; otherwise “unknown” if the time remaining
+ * has been inestimable for between 30 seconds and one minute;
+ * otherwise the empty string.
+ */
+ for (;;)
+ {
+ g_timeout_add_seconds (1, loop_quit_sourcefunc, loop);
+ g_main_loop_run (loop);
+
+ const auto elapsed = g_timer_elapsed (timer, nullptr);
+
+ if (elapsed < 30)
+ {
+ check_label (device, "Battery (estimating…)");
+ check_header (device, "(estimating…, 50%)",
+ "(estimating…)",
+ "(50%)",
+ "Battery (estimating…)");
+ }
+ else if (elapsed < 60)
+ {
+ check_label (device, "Battery (unknown)");
+ check_header (device, "(unknown, 50%)",
+ "(unknown)",
+ "(50%)",
+ "Battery (unknown)");
+ }
+ else if (elapsed < 80)
+ {
+ check_label (device, "Battery");
+ check_header (device, "(50%)",
+ "",
+ "(50%)",
+ "Battery");
+ }
+ else
+ {
+ break;
+ }
+ }
+
+ g_main_loop_unref (loop);
+
+ // cleanup
+ g_timer_destroy (timer);
+ g_object_unref (o);
+ g_setenv ("LANG", real_lang, TRUE);
+ g_free (real_lang);
+}
+
+
/* The menu title should tell you at a glance what you need to know most: what
device will lose power soonest (and optionally when), or otherwise which
device will take longest to charge (and optionally how long it will take). */
@@ -645,10 +715,12 @@ TEST_F(DeviceTest, ChoosePrimary)
INDICATOR_POWER_DEVICE_PERCENTAGE, tests[j].percentage,
NULL);
ASSERT_EQ (a, indicator_power_service_choose_primary_device(device_list));
+ g_object_unref (G_OBJECT(a));
/* reverse the list to check that list order doesn't matter */
device_list = g_list_reverse (device_list);
ASSERT_EQ (a, indicator_power_service_choose_primary_device(device_list));
+ g_object_unref (G_OBJECT(a));
}
}