~ubuntu-branches/ubuntu/trusty/unity-scopes-api/trusty-proposed

« back to all changes in this revision

Viewing changes to test/gtest/scopes/Filters/Filters_test.cpp

  • Committer: Package Import Robot
  • Author(s): Ubuntu daily release, Pawel Stolowski
  • Date: 2014-02-11 17:55:05 UTC
  • mfrom: (1.1.8)
  • Revision ID: package-import@ubuntu.com-20140211175505-av3z0612p7o8w54b
Tags: 0.3.1+14.04.20140211.2-0ubuntu1
[ Pawel Stolowski ]
* [ Pawel Stolowski ]
* Added preliminary API for filters and departments.
* Changes to preview action activation API: support for widget id.
* Changes to Annotation API.
* Return ScopeProxy from Result::target_scope_proxy (replaces activation_scope_name).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 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 Lesser General Public License version 3 as
 
6
 * published by the Free Software Foundation.
 
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 Lesser General Public License for more details.
 
12
 *
 
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/>.
 
15
 *
 
16
 * Authored by: Pawel Stolowski <pawel.stolowski@canonical.com>
 
17
 */
 
18
 
 
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>
 
26
 
 
27
using namespace unity::scopes;
 
28
using namespace unity::scopes::internal;
 
29
 
 
30
class WaitUntilFinished
 
31
{
 
32
public:
 
33
    void wait_until_finished()
 
34
    {
 
35
        std::unique_lock<std::mutex> lock(mutex_);
 
36
        cond_.wait(lock, [this] { return this->query_complete_; });
 
37
    }
 
38
 
 
39
protected:
 
40
    void notify()
 
41
    {
 
42
        // Signal that the query has completed.
 
43
        std::unique_lock<std::mutex> lock(mutex_);
 
44
        query_complete_ = true;
 
45
        cond_.notify_one();
 
46
    }
 
47
 
 
48
private:
 
49
    bool query_complete_;
 
50
    std::mutex mutex_;
 
51
    std::condition_variable cond_;
 
52
};
 
53
 
 
54
class SearchReceiver : public SearchListener, public WaitUntilFinished
 
55
{
 
56
public:
 
57
    virtual void push(CategorisedResult /* result */) override {}
 
58
 
 
59
    virtual void push(Filters const& filters, FilterState const& filter_state) override
 
60
    {
 
61
        this->filters = filters;
 
62
        this->filter_state = filter_state;
 
63
    }
 
64
 
 
65
    virtual void finished(ListenerBase::Reason /* reason */, std::string const& /* error_message */) override
 
66
    {
 
67
        notify();
 
68
    }
 
69
 
 
70
    Filters filters;
 
71
    FilterState filter_state;
 
72
};
 
73
 
 
74
void scope_thread()
 
75
{
 
76
    auto rt = Runtime::create_scope_runtime("TestScope", "Runtime.ini");
 
77
    TestScope scope;
 
78
    rt->run_scope(&scope);
 
79
}
 
80
 
 
81
TEST(Filters, scope)
 
82
{
 
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");
 
86
    mw->start();
 
87
    auto proxy = mw->create_scope_proxy("TestScope");
 
88
    auto scope = internal::ScopeImpl::create(proxy, rt.get(), "TestScope");
 
89
 
 
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();
 
94
 
 
95
    auto filter_state = receiver->filter_state; // copy filter state, it will be sent with 2nd query
 
96
    {
 
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());
 
105
 
 
106
        auto option1 = selector->options().front();
 
107
        selector->update_state(filter_state, option1, true); // activate filter option
 
108
    }
 
109
 
 
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();
 
114
    {
 
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());
 
121
    }
 
122
}
 
123
 
 
124
int main(int argc, char **argv)
 
125
{
 
126
    ::testing::InitGoogleTest(&argc, argv);
 
127
    std::thread scope_t(scope_thread);
 
128
    scope_t.detach();
 
129
    return RUN_ALL_TESTS();
 
130
}