2
* Copyright (C) 2014 Canonical, Ltd.
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.
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
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
* Author: Pete Woods <pete.woods@canonical.com>
19
#include <youtube/scope/scope.h>
21
#include <core/posix/exec.h>
22
#include <gtest/gtest.h>
23
#include <gmock/gmock.h>
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>
33
using namespace testing;
34
using namespace youtube::scope;
36
namespace posix = core::posix;
37
namespace sc = unity::scopes;
38
namespace sct = unity::scopes::testing;
42
MATCHER_P2(ResultProp, prop, value, "") {
43
if (arg.contains(prop)) {
44
*result_listener << "result[" << prop << "] is " << arg[prop].serialize_json();
46
*result_listener << "result[" << prop << "] is not set";
48
return arg.contains(prop) && arg[prop] == sc::Variant(value);
51
MATCHER_P(IsDepartment, department, "") {
52
return arg->serialize() == department->serialize();
55
typedef sct::TypedScopeFixture<Scope> TypedScopeFixtureScope;
57
class TestYoutubeScope: public TypedScopeFixtureScope {
61
fake_youtube_server_ = posix::exec(FAKE_YOUTUBE_SERVER, { }, { },
62
posix::StandardStream::stdout);
64
ASSERT_GT(fake_youtube_server_.pid(), 0);
66
fake_youtube_server_.cout() >> port;
68
string apiroot = "http://127.0.0.1:" + port;
69
setenv("YOUTUBE_SCOPE_APIROOT", apiroot.c_str(), true);
71
setenv("YOUTUBE_SCOPE_IGNORE_ACCOUNTS", "true", true);
73
// Do the parent SetUp
74
TypedScopeFixtureScope::SetUp();
77
posix::ChildProcess fake_youtube_server_ = posix::ChildProcess::invalid();
80
TEST_F(TestYoutubeScope, non_empty_query) {
81
const sc::CategoryRenderer renderer;
82
NiceMock<sct::MockSearchReply> reply;
84
sc::CannedQuery query("unity-scope-youtube", "banana", ""); // searching with query text
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)));
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(
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(
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(
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(
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(
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);