- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / platform_apps / web_view / src_attribute / main.js
1 // Copyright (c) 2012 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 var checkSrc = function(element, expectedValue) {
6   // Note that element.getAttribute('src') should not be used, it can be out of
7   // sync with element.src.
8   chrome.test.assertEq(expectedValue, element.src);
9 };
10
11 onload = function() {
12   chrome.test.runTests([
13     function webView() {
14       var expectedSrcOne = 'data:text/html,<body>One</body>';
15       var expectedSrcTwo = 'data:text/html,<body>Two</body>';
16       var expectedSrcThree = 'data:text/html,<body>Three</body>';
17
18       var step = 1;
19       // For setting src, we check if both webview.setAttribute('src', ?);
20       // and webview.src = ?; works properly.
21       var webview = document.querySelector('webview');
22
23       var runStep2 = function() {
24         step = 2;
25         chrome.test.log('run step: ' + step);
26         // Check if initial src is set correctly.
27         checkSrc(webview, expectedSrcOne);
28         webview.setAttribute('src', expectedSrcTwo);
29       };
30
31       var runStep3 = function() {
32         step = 3;
33         chrome.test.log('run step: ' + step);
34         // Expect the src change to be reflected.
35         checkSrc(webview, expectedSrcTwo);
36         // Set src attribute directly on the element.
37         webview.src = expectedSrcThree;
38       };
39
40       var runStep4 = function() {
41         step = 4;
42         chrome.test.log('run step: ' + step);
43         // Expect the src change to be reflected.
44         checkSrc(webview, expectedSrcThree);
45         // Set empty src, this will be ignored.
46         webview.setAttribute('src', '');
47
48         setTimeout(function() {
49           // Expect empty src to be ignored.
50           checkSrc(webview, expectedSrcThree);
51           // Set empty src again, directly changing the src attribute.
52           webview.src = '';
53
54           setTimeout(function() {
55             // Expect empty src to be ignored.
56             checkSrc(webview, expectedSrcThree);
57             chrome.test.succeed();
58           }, 0);
59         }, 0);
60       };
61
62       // Wait for navigation to complete before checking src attribute.
63       webview.addEventListener('loadcommit', function(e) {
64         switch (step) {
65           case 1:
66             runStep2();
67             break;
68           case 2:
69             runStep3();
70             break;
71           case 3:
72             runStep4();
73             break;
74           default:
75             // Unchecked.
76             chrome.test.fail('Unexpected step: ' + step + ' with url: ' +
77                              e.url);
78         }
79       });
80     }
81   ]);
82 };