on xenial help sign in
The fake plugin provides better scopes overview scope search results
commit770ffe6b77bd8742ef32aa681108e9f88ded7bd8 authorAlbert Astals <albert.astals@canonical.com> dateThu 17 Jul 2014, 12:36 (12 years ago) parenta747edd
4 files changed+122−31
tests/mocks/Unity/fake_scopes.cpp+4−15 tests/mocks/Unity/fake_scopes.h+1−2 tests/mocks/Unity/fake_scopesoverview.cpp+95−11 tests/mocks/Unity/fake_scopesoverview.h+22−3
tests/mocks/Unity/fake_scopes.cpp+4−15
@@ -140,26 +140,15 @@ bool Scopes::loaded() const
140140 return m_loaded;
141141}
142142
143−int Scopes::count(bool onlyVisible) const
143+QList<unity::shell::scopes::ScopeInterface *> Scopes::scopes(bool onlyVisible) const
144144{
145− int count = 0;
146− for (Scope *scope : m_scopes) {
147− if (!onlyVisible || scope->visible())
148− count++;
149− }
150− return count;
151−}
152−
153−Scope *Scopes::scopeAt(int index, bool onlyVisible) const
154−{
155− int count = 0;
145+ QList<unity::shell::scopes::ScopeInterface *> res;
156146 for (Scope *scope : m_scopes) {
157147 if (!onlyVisible || scope->visible()) {
158− if (index == count) return scope;
159− count++;
148+ res << scope;
160149 }
161150 }
162− return nullptr;
151+ return res;
163152}
164153
165154void Scopes::addScope(Scope* scope)
tests/mocks/Unity/fake_scopes.h+1−2
@@ -51,8 +51,7 @@ public:
5151
5252 bool loaded() const override;
5353
54− int count(bool onlyVisible) const;
55− Scope *scopeAt(int index, bool onlyVisible) const;
54+ QList<unity::shell::scopes::ScopeInterface *> scopes(bool onlyVisible) const;
5655
5756private Q_SLOTS:
5857 void updateScopes();
tests/mocks/Unity/fake_scopesoverview.cpp+95−11
@@ -23,8 +23,9 @@
2323ScopesOverview::ScopesOverview(Scopes* parent)
2424 : Scope("scopesOverview", "Scopes Overview", false, parent)
2525{
26− m_searchCategories = m_categories; // save the usual categories as search result
26+ delete m_categories; // delete the usual categories, we're not going to use it
2727 m_scopesOverviewCategories = new ScopesOverviewCategories(parent, this);
28+ m_searchCategories = new ScopesOverviewSearchCategories(parent, this);
2829 m_categories = m_scopesOverviewCategories;
2930}
3031
@@ -69,15 +70,17 @@ ScopesOverviewCategories::data(const QModelIndex& index, int role) const
6970 return QVariant();
7071 }
7172
73+ const QString categoryId = index.row() == 0 ? "favorites" : "all";
74+
7275 unity::shell::scopes::ResultsModelInterface *resultsModel = m_resultsModels[index.row()];
7376 if (!resultsModel) {
7477 QObject *that = const_cast<ScopesOverviewCategories*>(this);
75− resultsModel = new ScopesOverviewResultsModel(m_scopes, index.row() == 0, that);
78+ resultsModel = new ScopesOverviewResultsModel(m_scopes->scopes(index.row() == 0), categoryId, that);
7679 m_resultsModels[index.row()] = resultsModel;
7780 }
7881 switch (role) {
7982 case RoleCategoryId:
80− return index.row() == 0 ? "favorites" : "all";
83+ return categoryId;
8184 case RoleName:
8285 return index.row() == 0 ? "Favorites" : "All";
8386 case RoleIcon:
@@ -114,16 +117,96 @@ ScopesOverviewCategories::data(const QModelIndex& index, int role) const
114117
115118
116119
117−ScopesOverviewResultsModel::ScopesOverviewResultsModel(Scopes *scopes, bool isFavoriteCategory, QObject* parent)
120+ScopesOverviewSearchCategories::ScopesOverviewSearchCategories(Scopes *scopes, QObject* parent)
121+ : unity::shell::scopes::CategoriesInterface(parent)
122+ , m_scopes(scopes)
123+{
124+}
125+
126+int ScopesOverviewSearchCategories::rowCount(const QModelIndex& /*parent*/) const
127+{
128+ return 2;
129+}
130+
131+void ScopesOverviewSearchCategories::addSpecialCategory(QString const&, QString const&, QString const&, QString const&, QObject*)
132+{
133+ qFatal("Using un-implemented ScopesOverviewSearchCategories::addSpecialCategory");
134+}
135+
136+bool ScopesOverviewSearchCategories::overrideCategoryJson(QString const& /* categoryId */, QString const& /* json */)
137+{
138+ qFatal("Using un-implemented ScopesOverviewSearchCategories::overrideCategoryJson");
139+}
140+
141+QVariant
142+ScopesOverviewSearchCategories::data(const QModelIndex& index, int role) const
143+{
144+ if (!index.isValid()) {
145+ return QVariant();
146+ }
147+
148+ const QString categoryId = index.row() == 0 ? "searchA" : "searchB";
149+
150+ unity::shell::scopes::ResultsModelInterface *resultsModel = m_resultsModels[index.row()];
151+ if (!resultsModel) {
152+ QObject *that = const_cast<ScopesOverviewSearchCategories*>(this);
153+ QList<unity::shell::scopes::ScopeInterface *> scopes;
154+ if (index.row() == 0) {
155+ scopes << m_scopes->getScope("clickscope") << nullptr << m_scopes->getScope("MockScope2");
156+ } else {
157+ scopes << nullptr << m_scopes->getScope("MockScope7") << nullptr << m_scopes->getScope("MockScope1");
158+ }
159+ resultsModel = new ScopesOverviewResultsModel(scopes, categoryId, that);
160+ m_resultsModels[index.row()] = resultsModel;
161+ }
162+ switch (role) {
163+ case RoleCategoryId:
164+ return categoryId;
165+ case RoleName:
166+ return index.row() == 0 ? "SearchA" : "SearchB";
167+ case RoleIcon:
168+ return QVariant();
169+ case RoleRawRendererTemplate:
170+ qFatal("Using un-implemented RoleRawRendererTemplate Categories role");
171+ return QVariant();
172+ case RoleRenderer:
173+ {
174+ QVariantMap map;
175+ map["category-layout"] = "grid";
176+ map["card-size"] = "small";
177+ map["overlay"] = true;
178+ return map;
179+ }
180+ case RoleComponents:
181+ {
182+ QVariantMap map, artMap;
183+ artMap["aspect-ratio"] = "1";
184+ artMap["field"] = "art";
185+ map["art"] = artMap;
186+ map["title"] = "HOLA";
187+ return map;
188+ }
189+ case RoleResults:
190+ return QVariant::fromValue(resultsModel);
191+ case RoleCount:
192+ return resultsModel->rowCount();
193+ default:
194+ qFatal("Using un-implemented Categories role");
195+ return QVariant();
196+ }
197+}
198+
199+
200+ScopesOverviewResultsModel::ScopesOverviewResultsModel(const QList<unity::shell::scopes::ScopeInterface *> &scopes, const QString &categoryId, QObject* parent)
118201 : unity::shell::scopes::ResultsModelInterface(parent)
119202 , m_scopes(scopes)
120− , m_isFavoriteCategory(isFavoriteCategory)
203+ , m_categoryId(categoryId)
121204{
122205}
123206
124207QString ScopesOverviewResultsModel::categoryId() const
125208{
126− return m_isFavoriteCategory ? "favorites" : "all";
209+ return m_categoryId;
127210}
128211
129212void ScopesOverviewResultsModel::setCategoryId(QString const& /*id*/)
@@ -135,7 +218,7 @@ int ScopesOverviewResultsModel::scopeIndex(QString const& id) const
135218{
136219 const int scopeCount = count();
137220 for (int i = 0; i < scopeCount; ++i) {
138− if (m_scopes->scopeAt(i, m_isFavoriteCategory)->id() == id)
221+ if (m_scopes[i]->id() == id)
139222 return i;
140223 }
141224 return -1;
@@ -152,7 +235,7 @@ int ScopesOverviewResultsModel::rowCount(const QModelIndex& parent) const
152235{
153236 Q_UNUSED(parent);
154237
155− return m_scopes->count(m_isFavoriteCategory);
238+ return m_scopes.count();
156239}
157240
158241int ScopesOverviewResultsModel::count() const
@@ -163,22 +246,23 @@ int ScopesOverviewResultsModel::count() const
163246QVariant
164247ScopesOverviewResultsModel::data(const QModelIndex& index, int role) const
165248{
249+ unity::shell::scopes::ScopeInterface *scope = m_scopes[index.row()];
166250 switch (role) {
167251 case RoleUri:
168252 case RoleCategoryId:
169253 case RoleDndUri:
170254 return QString();
171255 case RoleResult:
172− return m_scopes->scopeAt(index.row(), m_isFavoriteCategory)->id();
256+ return scope ? scope->id() : QString("Result.%1.%2").arg(categoryId()).arg(index.row());
173257 case RoleTitle:
174− return m_scopes->scopeAt(index.row(), m_isFavoriteCategory)->name();
258+ return scope ? scope->name() : QString("Title.%1.%2").arg(categoryId()).arg(index.row());
175259 case RoleArt:
176260 return qmlDirectory() + "graphics/applicationIcons/dash.png";
177261 case RoleMascot:
178262 case RoleEmblem:
179263 case RoleSummary:
180264 case RoleBackground + 1: // scopeId
181− return m_scopes->scopeAt(index.row(), m_isFavoriteCategory)->id();
265+ return scope ? scope->id() : nullptr;
182266 break;
183267 default:
184268 return QVariant();
tests/mocks/Unity/fake_scopesoverview.h+22−3
@@ -55,12 +55,31 @@ private:
5555 Scopes *m_scopes;
5656};
5757
58+class ScopesOverviewSearchCategories : public unity::shell::scopes::CategoriesInterface
59+{
60+ Q_OBJECT
61+
62+public:
63+ ScopesOverviewSearchCategories(Scopes *scopes, QObject* parent = 0);
64+
65+ QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
66+ int rowCount(const QModelIndex& parent = QModelIndex()) const override;
67+
68+ Q_INVOKABLE void addSpecialCategory(QString const& categoryId, QString const& name, QString const& icon, QString const& rawTemplate, QObject* countObject) override;
69+ Q_INVOKABLE bool overrideCategoryJson(QString const& categoryId, QString const& json) override;
70+
71+private:
72+ mutable QHash<int, unity::shell::scopes::ResultsModelInterface*> m_resultsModels;
73+
74+ Scopes *m_scopes;
75+};
76+
5877class ScopesOverviewResultsModel : public unity::shell::scopes::ResultsModelInterface
5978{
6079 Q_OBJECT
6180
6281public:
63− explicit ScopesOverviewResultsModel(Scopes *scopes, bool isFavoriteCategory, QObject* parent = 0);
82+ explicit ScopesOverviewResultsModel(const QList<unity::shell::scopes::ScopeInterface *> &scopes, const QString &categoryId, QObject* parent = 0);
6483
6584 int rowCount(const QModelIndex& parent = QModelIndex()) const override;
6685 QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
@@ -77,8 +96,8 @@ public:
7796 QHash<int, QByteArray> roleNames() const override;
7897
7998private:
80− Scopes *m_scopes;
81− bool m_isFavoriteCategory;
99+ QList<unity::shell::scopes::ScopeInterface *> m_scopes;
100+ QString m_categoryId;
82101};
83102
84103#endif // FAKE_SCOPESOVERVIEW_H