Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / chrome / test / data / webui / inspect_ui_test.js
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.
4
5 function waitForElements(selector, populateFunctionName, callback) {
6   var elements = document.querySelectorAll(selector);
7   if (elements.length) {
8     callback(elements);
9     return;
10   }
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;
19       callback(elements);
20     }
21   };
22   interceptFunction.__isSniffer = true;
23   window[populateFunctionName] = interceptFunction;
24 }
25
26 function findByContentSubstring(elements, content, childSelector) {
27   return Array.prototype.filter.call(elements, function(element) {
28     if (childSelector)
29       element = element.querySelector(childSelector);
30     return element && element.textContent.indexOf(content) >= 0;
31   })[0];
32 }
33
34 function testTargetListed(sectionSelector, populateFunctionName, url) {
35   waitForElements(
36       sectionSelector + ' .row',
37       populateFunctionName,
38       function(elements) {
39         var urlElement = findByContentSubstring(elements, url, '.url');
40         expectNotEquals(undefined, urlElement);
41         testDone();
42       });
43 }
44
45 function testAdbTargetsListed() {
46   waitForElements('.device', 'populateRemoteTargets', function(devices) {
47     expectEquals(2, devices.length);
48
49     var offlineDevice = findByContentSubstring(
50         devices, 'Offline', '.device-name');
51     expectNotEquals(undefined, offlineDevice);
52
53     var onlineDevice = findByContentSubstring(
54         devices, 'Nexus 6', '.device-name');
55     expectNotEquals(undefined, onlineDevice);
56
57     var browsers = onlineDevice.querySelectorAll('.browser');
58     expectEquals(4, browsers.length);
59
60     var chromeBrowser = findByContentSubstring(
61        browsers, 'Chrome (32.0.1679.0)', '.browser-name');
62     expectNotEquals(undefined, chromeBrowser);
63
64     var chromePages = chromeBrowser.querySelectorAll('.pages');
65     var chromiumPage = findByContentSubstring(
66        chromePages, 'http://www.chromium.org/', '.url');
67     expectNotEquals(undefined, chromiumPage);
68
69     var pageById = {};
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;
75       });
76     });
77
78     var webView = findByContentSubstring(
79        browsers, 'WebView in com.sample.feed (4.0)', '.browser-name');
80     expectNotEquals(undefined, webView);
81
82     testDone();
83   });
84 }