~marcustomlinson/unity-scope-youtube/install_account_service

« back to all changes in this revision

Viewing changes to tests/unit/youtube/scope/test-youtube-scope.cpp

  • Committer: Pete Woods
  • Date: 2014-07-03 13:40:34 UTC
  • Revision ID: pete.woods@canonical.com-20140703134034-96b21842diuxqrjq
Basic test for search mode

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2014 Canonical, Ltd.
 
3
 *
 
4
 * This library is free software; you can redistribute it and/or modify it under
 
5
 * the terms of version 3 of the GNU Lesser General Public License as published
 
6
 * by the Free Software Foundation.
 
7
 *
 
8
 * This library is distributed in the hope that it will be useful, but WITHOUT
 
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
10
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 
11
 * 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
 * Author: Pete Woods <pete.woods@canonical.com>
 
17
 */
 
18
 
 
19
#include <youtube/scope/scope.h>
 
20
 
 
21
#include <core/posix/exec.h>
 
22
#include <gtest/gtest.h>
 
23
#include <gmock/gmock.h>
 
24
#include <string>
 
25
#include <unity/scopes/SearchReply.h>
 
26
#include <unity/scopes/SearchReplyProxyFwd.h>
 
27
#include <unity/scopes/Variant.h>
 
28
#include <unity/scopes/testing/Category.h>
 
29
#include <unity/scopes/testing/MockSearchReply.h>
 
30
#include <unity/scopes/testing/TypedScopeFixture.h>
 
31
 
 
32
using namespace std;
 
33
using namespace testing;
 
34
using namespace youtube::scope;
 
35
 
 
36
namespace posix = core::posix;
 
37
namespace sc = unity::scopes;
 
38
namespace sct = unity::scopes::testing;
 
39
 
 
40
namespace {
 
41
 
 
42
MATCHER_P2(ResultProp, prop, value, "") {
 
43
    if (arg.contains(prop)) {
 
44
        *result_listener << "result[" << prop << "] is " << arg[prop].serialize_json();
 
45
    } else {
 
46
        *result_listener << "result[" << prop << "] is not set";
 
47
    }
 
48
    return arg.contains(prop) && arg[prop] == sc::Variant(value);
 
49
}
 
50
 
 
51
MATCHER_P(IsDepartment, department, "") {
 
52
    return arg->serialize() == department->serialize();
 
53
}
 
54
 
 
55
typedef sct::TypedScopeFixture<Scope> TypedScopeFixtureScope;
 
56
 
 
57
class TestYoutubeScope: public TypedScopeFixtureScope {
 
58
protected:
 
59
    void SetUp() override
 
60
    {
 
61
        fake_youtube_server_ = posix::exec(FAKE_YOUTUBE_SERVER, { }, { },
 
62
                posix::StandardStream::stdout);
 
63
 
 
64
        ASSERT_GT(fake_youtube_server_.pid(), 0);
 
65
        string port;
 
66
        fake_youtube_server_.cout() >> port;
 
67
 
 
68
        string apiroot = "http://127.0.0.1:" + port;
 
69
        setenv("YOUTUBE_SCOPE_APIROOT", apiroot.c_str(), true);
 
70
 
 
71
        setenv("YOUTUBE_SCOPE_IGNORE_ACCOUNTS", "true", true);
 
72
 
 
73
        // Do the parent SetUp
 
74
        TypedScopeFixtureScope::SetUp();
 
75
    }
 
76
 
 
77
    posix::ChildProcess fake_youtube_server_ = posix::ChildProcess::invalid();
 
78
};
 
79
 
 
80
TEST_F(TestYoutubeScope, non_empty_query) {
 
81
    const sc::CategoryRenderer renderer;
 
82
    NiceMock<sct::MockSearchReply> reply;
 
83
 
 
84
    sc::CannedQuery query("unity-scope-youtube", "banana", ""); // searching with query text
 
85
 
 
86
    EXPECT_CALL(reply, register_category("youtube", "1000000 results from Youtube", "", _)).Times(1)
 
87
                .WillOnce(Return(make_shared<sct::Category>("youtube", "1000000 results from Youtube", "", renderer)));
 
88
 
 
89
    EXPECT_CALL(reply, push(Matcher<sc::CategorisedResult const&>(AllOf(
 
90
            ResultProp("uri", "gLPKjkXsWM8"),
 
91
            ResultProp("link", "https://www.youtube.com/watch?v=gLPKjkXsWM8"),
 
92
            ResultProp("title", "Harry eating a banana on stage (Düsseldorf, Germany) HD"),
 
93
            ResultProp("art", "https://i.ytimg.com/vi/gLPKjkXsWM8/hqdefault.jpg"),
 
94
            ResultProp("description", "One Direction at the Where We Are Tour in Düsseldorf, Germany on the 2nd of July 2014. HD."),
 
95
            ResultProp("username", ""))))).WillOnce(
 
96
            Return(true));
 
97
    EXPECT_CALL(reply, push(Matcher<sc::CategorisedResult const&>(AllOf(
 
98
            ResultProp("uri", "ZYXTZh8CW4E"),
 
99
            ResultProp("link", "https://www.youtube.com/watch?v=ZYXTZh8CW4E"),
 
100
            ResultProp("title", "Minions - Banana 14:20 mins"),
 
101
            ResultProp("art", "https://i.ytimg.com/vi/ZYXTZh8CW4E/hqdefault.jpg"),
 
102
            ResultProp("description", "Despicable me 2 Minions Banana song."),
 
103
            ResultProp("username", "ohvsk8"))))).WillOnce(
 
104
            Return(true));
 
105
    EXPECT_CALL(reply, push(Matcher<sc::CategorisedResult const&>(AllOf(
 
106
            ResultProp("uri", "BYBw_o_2nG0"),
 
107
            ResultProp("link", "https://www.youtube.com/watch?v=BYBw_o_2nG0"),
 
108
            ResultProp("title", "Despicable Me - Mini-Movie 'Banana' Preview"),
 
109
            ResultProp("art", "https://i.ytimg.com/vi/BYBw_o_2nG0/hqdefault.jpg"),
 
110
            ResultProp("description", ""),
 
111
            ResultProp("username", "DespicableMeMovie"))))).WillOnce(
 
112
            Return(true));
 
113
    EXPECT_CALL(reply, push(Matcher<sc::CategorisedResult const&>(AllOf(
 
114
            ResultProp("uri", "FQymDE3FaHY"),
 
115
            ResultProp("link", "https://www.youtube.com/watch?v=FQymDE3FaHY"),
 
116
            ResultProp("title", "Tutorial: Anti-rush flashbang @ de_inferno banana"),
 
117
            ResultProp("art", "https://i.ytimg.com/vi/FQymDE3FaHY/hqdefault.jpg"),
 
118
            ResultProp("description", "Made by: https://www.youtube.com/user/ThePavle995 » Liked this video? Subscribe for more: http://youtube.com/subscription_center?add_user=piccgamer ..."),
 
119
            ResultProp("username", "piccgamer"))))).WillOnce(
 
120
            Return(true));
 
121
    EXPECT_CALL(reply, push(Matcher<sc::CategorisedResult const&>(AllOf(
 
122
            ResultProp("uri", "Z01ts2f-mHY"),
 
123
            ResultProp("link", "https://www.youtube.com/watch?v=Z01ts2f-mHY"),
 
124
            ResultProp("title", "Banana (香蕉人)"),
 
125
            ResultProp("art", "https://i.ytimg.com/vi/Z01ts2f-mHY/hqdefault.jpg"),
 
126
            ResultProp("description", "When you can't understand something, don't act like you can! :) SUBSCRIBE TO US! DanKhooProductions' FB - http://fb.com/dankhooproductions ..."),
 
127
            ResultProp("username", "DanKhooProductions"))))).WillOnce(
 
128
            Return(true));
 
129
 
 
130
    sc::SearchReplyProxy reply_proxy(&reply, [](sc::SearchReply*) {}); // note: this is a std::shared_ptr with empty deleter
 
131
    sc::SearchMetadata meta_data("en_EN", "phone");
 
132
    auto search_query = scope->search(query, meta_data);
 
133
    ASSERT_NE(nullptr, search_query);
 
134
    search_query->run(reply_proxy);
 
135
}
 
136
 
 
137
} // namespace