1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 function waitForElements(selector
, populateFunctionName
, callback
) {
6 var elements
= document
.querySelectorAll(selector
);
11 var originalFunction
= window
[populateFunctionName
];
12 expectNotEquals(undefined, originalFunction
);
13 expectEquals(undefined, originalFunction
.__isSniffer
);
14 var interceptFunction = function() {
15 originalFunction
.apply(window
, arguments
);
16 var elements
= document
.querySelectorAll(selector
);
17 if (elements
.length
) {
18 window
[populateFunctionName
] = originalFunction
;
22 interceptFunction
.__isSniffer
= true;
23 window
[populateFunctionName
] = interceptFunction
;
26 function findByContentSubstring(elements
, content
, childSelector
) {
27 return Array
.prototype.filter
.call(elements
, function(element
) {
29 element
= element
.querySelector(childSelector
);
30 return element
&& element
.textContent
.indexOf(content
) >= 0;
34 function testTargetListed(sectionSelector
, populateFunctionName
, url
) {
36 sectionSelector
+ ' .row',
39 var urlElement
= findByContentSubstring(elements
, url
, '.url');
40 expectNotEquals(undefined, urlElement
);
45 function testAdbTargetsListed() {
46 waitForElements('.device', 'populateRemoteTargets', function(devices
) {
47 expectEquals(2, devices
.length
);
49 var offlineDevice
= findByContentSubstring(
50 devices
, 'Offline', '.device-name');
51 expectNotEquals(undefined, offlineDevice
);
53 var onlineDevice
= findByContentSubstring(
54 devices
, 'Nexus 6', '.device-name');
55 expectNotEquals(undefined, onlineDevice
);
57 var browsers
= onlineDevice
.querySelectorAll('.browser');
58 expectEquals(4, browsers
.length
);
60 var chromeBrowser
= findByContentSubstring(
61 browsers
, 'Chrome (32.0.1679.0)', '.browser-name');
62 expectNotEquals(undefined, chromeBrowser
);
64 var chromePages
= chromeBrowser
.querySelectorAll('.pages');
65 var chromiumPage
= findByContentSubstring(
66 chromePages
, 'http://www.chromium.org/', '.url');
67 expectNotEquals(undefined, chromiumPage
);
70 Array
.prototype.forEach
.call(devices
, function (device
) {
71 var pages
= device
.querySelectorAll('.row');
72 Array
.prototype.forEach
.call(pages
, function (page
) {
73 expectEquals(undefined, pageById
[page
.targetId
]);
74 pageById
[page
.targetId
] = page
;
78 var webView
= findByContentSubstring(
79 browsers
, 'WebView in com.sample.feed (4.0)', '.browser-name');
80 expectNotEquals(undefined, webView
);