~unity-team/unity8/ota9.5

« back to all changes in this revision

Viewing changes to tests/qmltests/plugins/HudClient/fake_libhud_client.cpp

  • Committer: Michał Sawicz
  • Date: 2013-06-05 22:03:08 UTC
  • Revision ID: michal.sawicz@canonical.com-20130605220308-yny8fv3futtr04fg
Inital unity8 commit.

Previous history can be found at https://code.launchpad.net/~unity-team/unity/phablet

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2012, 2013 Canonical, Ltd.
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License as published by
 
6
 * the Free Software Foundation; version 3.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 */
 
16
 
 
17
#include <hud-client.h>
 
18
 
 
19
#include "libhud_client_stub.h"
 
20
 
 
21
/* Schema that is used in the DeeModel representing
 
22
   the results */
 
23
static const gchar * results_model_schema[] = {
 
24
    "v", /* Command ID */
 
25
    "s", /* Command Name */
 
26
    "a(ii)", /* Highlights in command name */
 
27
    "s", /* Description */
 
28
    "a(ii)", /* Highlights in description */
 
29
    "s", /* Shortcut */
 
30
    "u", /* Distance */
 
31
    "b", /* Parameterized */
 
32
};
 
33
 
 
34
DeeModel *resultsModel = 0;
 
35
GMenuModel *parametrizedActionModel = 0;
 
36
GActionGroup *parametrizedActionGroup = 0;
 
37
 
 
38
static void hud_client_query_class_init(HudClientQueryClass *klass);
 
39
static void hud_client_query_init(HudClientQuery *self);
 
40
G_DEFINE_TYPE(HudClientQuery, hud_client_query, G_TYPE_OBJECT)
 
41
 
 
42
static void hud_client_query_init(HudClientQuery * /*self*/)
 
43
{
 
44
}
 
45
static void hud_client_query_class_init(HudClientQueryClass * /*klass*/)
 
46
{
 
47
    HudClientStub::m_querySignalToolbarUpdated = g_signal_new (HUD_CLIENT_QUERY_SIGNAL_TOOLBAR_UPDATED,
 
48
                                           HUD_CLIENT_TYPE_QUERY,
 
49
                                           G_SIGNAL_RUN_LAST,
 
50
                                           0, /* offset */
 
51
                                           NULL, NULL, /* Accumulator */
 
52
                                           g_cclosure_marshal_VOID__VOID,
 
53
                                           G_TYPE_NONE, 0, G_TYPE_NONE);
 
54
}
 
55
 
 
56
HudClientQuery *hud_client_query_new(const gchar *query)
 
57
{
 
58
    HudClientStub::m_lastSetQuery = QString::fromUtf8(query);
 
59
    HudClientStub::m_query = HUD_CLIENT_QUERY(g_object_new(HUD_CLIENT_TYPE_QUERY, NULL));
 
60
    return HudClientStub::m_query;
 
61
}
 
62
 
 
63
DeeModel *hud_client_query_get_results_model(HudClientQuery *cquery)
 
64
{
 
65
    Q_ASSERT(cquery == HudClientStub::m_query);
 
66
    if (!resultsModel) {
 
67
        resultsModel = dee_sequence_model_new();
 
68
        dee_model_set_schema_full(resultsModel, results_model_schema, G_N_ELEMENTS(results_model_schema));
 
69
 
 
70
        GVariant * columns[G_N_ELEMENTS(results_model_schema) + 1];
 
71
        columns[0] = g_variant_new_variant(g_variant_new_uint64(0));
 
72
        columns[1] = g_variant_new_string("Help");
 
73
        columns[2] = g_variant_new_array(G_VARIANT_TYPE("(ii)"), NULL, 0);
 
74
        columns[3] = g_variant_new_string("Get Help");
 
75
        columns[4] = g_variant_new_array(G_VARIANT_TYPE("(ii)"), NULL, 0);
 
76
        columns[5] = g_variant_new_string("");
 
77
        columns[6] = g_variant_new_uint32(3);
 
78
        columns[7] = g_variant_new_boolean(false);
 
79
        columns[8] = NULL;
 
80
        dee_model_append_row(resultsModel, columns);
 
81
 
 
82
        columns[0] = g_variant_new_variant(g_variant_new_uint64(1));
 
83
        columns[1] = g_variant_new_string("About");
 
84
        columns[3] = g_variant_new_string("Show About");
 
85
        dee_model_append_row(resultsModel, columns);
 
86
 
 
87
        columns[0] = g_variant_new_variant(g_variant_new_uint64(2));
 
88
        columns[1] = g_variant_new_string("Foo");
 
89
        columns[3] = g_variant_new_string("Show Foo");
 
90
        columns[7] = g_variant_new_boolean(true);
 
91
        dee_model_append_row(resultsModel, columns);
 
92
 
 
93
        columns[0] = g_variant_new_variant(g_variant_new_uint64(3));
 
94
        columns[1] = g_variant_new_string("Bar");
 
95
        columns[3] = g_variant_new_string("Show Bar");
 
96
        columns[7] = g_variant_new_boolean(false);
 
97
        dee_model_append_row(resultsModel, columns);
 
98
 
 
99
        columns[0] = g_variant_new_variant(g_variant_new_uint64(4));
 
100
        columns[1] = g_variant_new_string("FooBar");
 
101
        columns[3] = g_variant_new_string("Show FooBar");
 
102
        dee_model_append_row(resultsModel, columns);
 
103
 
 
104
        columns[0] = g_variant_new_variant(g_variant_new_uint64(5));
 
105
        columns[1] = g_variant_new_string("UltraFooBar");
 
106
        columns[3] = g_variant_new_string("Show UltraFooBar");
 
107
        dee_model_append_row(resultsModel, columns);
 
108
    }
 
109
    return resultsModel;
 
110
}
 
111
 
 
112
void hud_client_query_execute_toolbar_item(HudClientQuery *cquery, HudClientQueryToolbarItems item, guint timestamp)
 
113
{
 
114
    Q_ASSERT(cquery == HudClientStub::m_query);
 
115
    Q_UNUSED(timestamp);
 
116
    HudClientStub::m_lastExecutedToolbarItem = item;
 
117
}
 
118
 
 
119
void hud_client_query_set_query(HudClientQuery *cquery, const char *query)
 
120
{
 
121
    Q_ASSERT(cquery == HudClientStub::m_query);
 
122
    HudClientStub::m_lastSetQuery = QString::fromUtf8(query);
 
123
}
 
124
 
 
125
void hud_client_query_execute_command(HudClientQuery *cquery, GVariant *command_key, guint timestamp)
 
126
{
 
127
    Q_ASSERT(cquery == HudClientStub::m_query);
 
128
    Q_UNUSED(timestamp);
 
129
    for (uint i = 0; i < dee_model_get_n_rows(resultsModel); ++i) {
 
130
        DeeModelIter *iter = dee_model_get_iter_at_row(resultsModel, i);
 
131
        if (!dee_model_get_bool(resultsModel, iter, 7)) {
 
132
            GVariant *it_command = dee_model_get_value(resultsModel, iter, 0);
 
133
            if (g_variant_equal (command_key, it_command)) {
 
134
                HudClientStub::m_lastExecutedCommandRow = i;
 
135
                g_variant_unref(it_command);
 
136
                return;
 
137
            }
 
138
            g_variant_unref(it_command);
 
139
        }
 
140
    }
 
141
    HudClientStub::m_lastExecutedCommandRow = -1;
 
142
}
 
143
 
 
144
HudClientParam *hud_client_query_execute_param_command(HudClientQuery *cquery, GVariant *command_key, guint timestamp)
 
145
{
 
146
    Q_ASSERT(cquery == HudClientStub::m_query);
 
147
    Q_UNUSED(timestamp);
 
148
    for (uint i = 0; i < dee_model_get_n_rows(resultsModel); ++i) {
 
149
        DeeModelIter *iter = dee_model_get_iter_at_row(resultsModel, i);
 
150
        if (dee_model_get_bool(resultsModel, iter, 7)) {
 
151
            GVariant *it_command = dee_model_get_value(resultsModel, iter, 0);
 
152
            if (g_variant_equal (command_key, it_command)) {
 
153
                HudClientStub::m_lastExecutedParametrizedCommandRow = i;
 
154
                g_variant_unref(it_command);
 
155
                // No need to create a real HudClientParam since it's always passed down to us
 
156
                return (HudClientParam *)g_object_new(G_TYPE_OBJECT, NULL);
 
157
            }
 
158
            g_variant_unref(it_command);
 
159
        }
 
160
    }
 
161
    HudClientStub::m_lastExecutedParametrizedCommandRow = -1;
 
162
    return NULL;
 
163
}
 
164
 
 
165
void hud_client_query_voice_query(HudClientQuery *cquery)
 
166
{
 
167
    Q_ASSERT(cquery == HudClientStub::m_query);
 
168
    // TODO We are not testing voice queries yet
 
169
}
 
170
 
 
171
gboolean hud_client_query_toolbar_item_active(HudClientQuery *cquery, HudClientQueryToolbarItems item)
 
172
{
 
173
    Q_ASSERT(cquery == HudClientStub::m_query);
 
174
    if (item == HUD_CLIENT_QUERY_TOOLBAR_HELP)
 
175
        return HudClientStub::m_helpToolbarItemEnabled;
 
176
 
 
177
    return true;
 
178
}
 
179
 
 
180
GMenuModel *hud_client_param_get_model(HudClientParam *param)
 
181
{
 
182
    Q_UNUSED(param);
 
183
 
 
184
    if (!parametrizedActionModel) {
 
185
        GMenu *menu = g_menu_new();
 
186
        GMenuItem *item = g_menu_item_new("Item1Label", NULL);
 
187
        g_menu_item_set_attribute_value(item, "parameter-type", g_variant_new_string("slider"));
 
188
        g_menu_item_set_attribute_value(item, "min", g_variant_new_double(10));
 
189
        g_menu_item_set_attribute_value(item, "max", g_variant_new_double(80));
 
190
        g_menu_item_set_attribute_value(item, "live", g_variant_new_boolean(true));
 
191
        g_menu_item_set_attribute_value(item, "value", g_variant_new_double(75));
 
192
        g_menu_item_set_attribute_value(item, "action", g_variant_new_string("costAction"));
 
193
        g_menu_append_item (menu, item);
 
194
 
 
195
        parametrizedActionModel = G_MENU_MODEL(menu);
 
196
    }
 
197
 
 
198
    return parametrizedActionModel;
 
199
}
 
200
 
 
201
void hud_client_param_send_cancel(HudClientParam *param)
 
202
{
 
203
    Q_UNUSED(param);
 
204
    g_clear_object(&parametrizedActionModel);
 
205
    parametrizedActionModel = 0;
 
206
    HudClientStub::m_lastParametrizedCommandCommited = false;
 
207
}
 
208
 
 
209
static void on_signal_activated (GSimpleAction *action, GVariant *parameter,  gpointer /*user_data*/)
 
210
{
 
211
    // Only double for the moment
 
212
    HudClientStub::m_activatedActions.insert(g_action_get_name(G_ACTION(action)), QVariant(g_variant_get_double(parameter)));
 
213
}
 
214
 
 
215
GActionGroup *hud_client_param_get_actions(HudClientParam *param)
 
216
{
 
217
    Q_UNUSED(param);
 
218
    if (!parametrizedActionGroup) {
 
219
        GSimpleActionGroup *actionGroup = g_simple_action_group_new();
 
220
        GSimpleAction *action = g_simple_action_new("costAction", G_VARIANT_TYPE_DOUBLE);
 
221
        g_simple_action_group_insert(actionGroup, G_ACTION(action));
 
222
 
 
223
        g_signal_connect (action, "activate", G_CALLBACK (on_signal_activated), NULL);
 
224
 
 
225
 
 
226
        parametrizedActionGroup = G_ACTION_GROUP(actionGroup);
 
227
    }
 
228
 
 
229
    return parametrizedActionGroup;
 
230
}
 
231
 
 
232
void hud_client_param_send_commit(HudClientParam *param)
 
233
{
 
234
    Q_UNUSED(param);
 
235
    g_object_unref(parametrizedActionGroup);
 
236
    parametrizedActionGroup = 0;
 
237
    g_clear_object(&parametrizedActionModel);
 
238
    parametrizedActionModel = 0;
 
239
    HudClientStub::m_lastParametrizedCommandCommited = true;
 
240
}