2
* Copyright (C) 2013 Canonical Ltd
4
* This program is free software: you can redistribute it and/or modify
5
* it under the terms of the GNU Lesser General Public License version 3 as
6
* published by the Free Software Foundation.
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 Lesser General Public License for more details.
13
* You should have received a copy of the GNU Lesser General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16
* Authored by: Pawel Stolowski <pawel.stolowski@canonical.com>
19
#include <unity/scopes/internal/RuntimeImpl.h>
20
#include <unity/scopes/internal/ScopeImpl.h>
21
#include <unity/scopes/FilterOption.h>
22
#include <unity/scopes/SearchMetadata.h>
23
#include <unity/UnityExceptions.h>
24
#include <gtest/gtest.h>
25
#include <TestScope.h>
27
using namespace unity::scopes;
28
using namespace unity::scopes::internal;
30
class WaitUntilFinished
33
void wait_until_finished()
35
std::unique_lock<std::mutex> lock(mutex_);
36
cond_.wait(lock, [this] { return this->query_complete_; });
42
// Signal that the query has completed.
43
std::unique_lock<std::mutex> lock(mutex_);
44
query_complete_ = true;
51
std::condition_variable cond_;
54
class SearchReceiver : public SearchListener, public WaitUntilFinished
57
virtual void push(CategorisedResult /* result */) override {}
59
virtual void push(Filters const& filters, FilterState const& filter_state) override
61
this->filters = filters;
62
this->filter_state = filter_state;
65
virtual void finished(ListenerBase::Reason /* reason */, std::string const& /* error_message */) override
71
FilterState filter_state;
76
auto rt = Runtime::create_scope_runtime("TestScope", "Runtime.ini");
78
rt->run_scope(&scope);
83
// parent: connect to scope and run a query
84
auto rt = internal::RuntimeImpl::create("", "Runtime.ini");
85
auto mw = rt->factory()->create("TestScope", "Zmq", "Zmq.ini");
87
auto proxy = mw->create_scope_proxy("TestScope");
88
auto scope = internal::ScopeImpl::create(proxy, rt.get(), "TestScope");
90
SearchMetadata hints("pl", "phone");
91
auto receiver = std::make_shared<SearchReceiver>();
92
auto ctrl = scope->create_query("test", hints, receiver);
93
receiver->wait_until_finished();
95
auto filter_state = receiver->filter_state; // copy filter state, it will be sent with 2nd query
97
auto filters = receiver->filters;
98
EXPECT_EQ(1, filters.size());
99
EXPECT_EQ("f1", filters.front()->id());
100
auto filter_type = filters.front()->filter_type();
101
EXPECT_EQ("option_selector", filter_type);
102
auto selector = std::dynamic_pointer_cast<const OptionSelectorFilter>(filters.front());
103
EXPECT_EQ(2, selector->options().size());
104
EXPECT_EQ(0, selector->active_options(filter_state).size());
106
auto option1 = selector->options().front();
107
selector->update_state(filter_state, option1, true); // activate filter option
110
// send 2nd query, this time with filter state (one active option)
111
receiver = std::make_shared<SearchReceiver>();
112
ctrl = scope->create_query("test2", filter_state, hints, receiver);
113
receiver->wait_until_finished();
115
auto filters = receiver->filters;
116
auto filter_state2 = receiver->filter_state;
117
auto selector = std::dynamic_pointer_cast<const OptionSelectorFilter>(filters.front());
118
EXPECT_EQ(1, selector->active_options(filter_state2).size());
119
auto option1 = *(selector->active_options(filter_state2).begin());
120
EXPECT_EQ("o1", option1->id());
124
int main(int argc, char **argv)
126
::testing::InitGoogleTest(&argc, argv);
127
std::thread scope_t(scope_thread);
129
return RUN_ALL_TESTS();