This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
collection.js
146 lines (121 loc) · 3.85 KB
/
collection.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
'use strict';
/* global module */
var Actions = require('marionette-client').Actions;
function Collection(client, server) {
this.client = client;
this.server = server;
this.actions = new Actions(client);
}
/**
* @type String Origin of Collection app
*/
Collection.URL = 'app://collection.gaiamobile.org';
Collection.Selectors = {
cloudMenu: '#cloud-menu',
cloudMenuPin: '#pin-cloudapp',
contextMenuTarget: '#icons',
menuAddButton: '#create-smart-collection',
collectionsSelect: '#collections-select',
// The first pinned result
firstPinnedResult: 'gaia-grid .icon',
// The first web result when NO pinned items exist
firstWebResultNoPinned: 'gaia-grid .icon',
// The first web result when pinned items exist
firstWebResultPinned: 'gaia-grid .divider + .icon',
allDividers: 'gaia-grid .divider',
allIcons: 'gaia-grid .icon',
offlineMessage: '#offline-message'
};
Collection.prototype = {
/**
* Enters the create collection screen from the homescreen.
*/
enterCreateScreen: function() {
var selectors = Collection.Selectors;
var container = this.client.helper.waitForElement(
selectors.contextMenuTarget);
this.actions.longPress(container, 1).perform();
this.client.helper.waitForElement(selectors.menuAddButton).click();
},
/**
* Selects a collection by positions in the list.
* @return {Array} An array of item names.
*/
selectNew: function(names) {
if (!Array.isArray(names)) {
names = [names];
}
this.client.switchToFrame();
this.client.apps.switchToApp(Collection.URL);
var selectors = Collection.Selectors;
var select = this.client.helper.waitForElement(
selectors.collectionsSelect);
// XXX: System dialog does not appear for select boxes for some reason in
// activites. For now we simply manually select everything and fire a blur
// event which the code looks for to submit the form. There is no UI in the
// test because of this, but everything seems to work.
for (var i = 0, iLen = names.length; i < iLen; i++) {
var name = names[i];
this.client.helper.tapSelectOption(select, name);
}
select.scriptWith(function(el) {
el.dispatchEvent(new CustomEvent('blur'));
});
this.client.switchToFrame();
},
/**
* Verifies that a collection exists by name.
* @return {Element} The collection icon.
*/
getCollectionByName: function(name) {
var icon;
this.client.waitFor(function() {
try {
var icons = this.client.findElements('body .icon');
for (var i = 0, iLen = icons.length; i < iLen; i++) {
if (icons[i].text() === name) {
icon = icons[i];
return true;
}
}
} catch(e) {
return false;
}
}.bind(this));
return icon;
},
/**
* Gets an icon by identifier.
*/
getIconByIdentifier: function(identifier) {
return this.client.helper.waitForElement(
'[data-identifier="' + identifier + '"]'
);
},
/**
* Taps on a collection and waits for it to load.
* @param {Object} icon The collection icon on the homescreen.
*/
enterCollection: function(icon) {
// Scroll the icon into view
icon.scriptWith(function(el) {
el.scrollIntoView(false);
});
icon.tap();
this.client.switchToFrame();
this.client.apps.switchToApp(Collection.URL);
this.client.helper.waitForElement(
Collection.Selectors.firstWebResultNoPinned);
},
/**
* Pins a result to the top of the collection.
* @param {String} selector The selector to find the icon in web results.
*/
pin: function(selector) {
var selectors = Collection.Selectors;
var firstIcon = this.client.helper.waitForElement(selector);
this.actions.longPress(firstIcon, 1).perform();
this.client.helper.waitForElement(selectors.cloudMenuPin).click();
}
};
module.exports = Collection;