Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / platform_apps / app_view / shim / main.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 var util = {};
6 var embedder = {};
7 embedder.baseGuestURL = '';
8 embedder.emptyGuestURL = '';
9 embedder.windowOpenGuestURL = '';
10 embedder.noReferrerGuestURL = '';
11 embedder.redirectGuestURL = '';
12 embedder.redirectGuestURLDest = '';
13 embedder.closeSocketURL = '';
14 embedder.tests = {};
15
16 embedder.setUp_ = function(config) {
17   if (!config || !config.testServer) {
18     return;
19   }
20   embedder.baseGuestURL = 'http://localhost:' + config.testServer.port;
21   embedder.emptyGuestURL = embedder.baseGuestURL +
22       '/extensions/platform_apps/web_view/shim/empty_guest.html';
23 };
24
25 window.runTest = function(testName, appToEmbed) {
26   if (!embedder.test.testList[testName]) {
27     window.console.log('Incorrect testName: ' + testName);
28     embedder.test.fail();
29     return;
30   }
31
32   // Run the test.
33   embedder.test.testList[testName](appToEmbed);
34 };
35
36 var LOG = function(msg) {
37   window.console.log(msg);
38 };
39
40 embedder.test = {};
41 embedder.test.succeed = function() {
42   chrome.test.sendMessage('TEST_PASSED');
43 };
44
45 embedder.test.fail = function() {
46   chrome.test.sendMessage('TEST_FAILED');
47 };
48
49 embedder.test.assertEq = function(a, b) {
50   if (a != b) {
51     console.log('assertion failed: ' + a + ' != ' + b);
52     embedder.test.fail();
53   }
54 };
55
56 embedder.test.assertTrue = function(condition) {
57   if (!condition) {
58     console.log('assertion failed: true != ' + condition);
59     embedder.test.fail();
60   }
61 };
62
63 embedder.test.assertFalse = function(condition) {
64   if (condition) {
65     console.log('assertion failed: false != ' + condition);
66     embedder.test.fail();
67   }
68 };
69
70 // Tests begin.
71 function testAppViewBasic(appToEmbed) {
72   var appview = new AppView();
73   LOG('appToEmbed  ' + appToEmbed);
74   // Step 1: Attempt to connect to a non-existant app.
75   LOG('attempting to connect to non-existant app.');
76   appview.connect('abc123', {}, function(success) {
77     // Make sure we fail.
78     if (success) {
79       embedder.test.fail();
80       return;
81     }
82     LOG('failed to connect to non-existant app.');
83     LOG('attempting to connect to known app.');
84     // Step 2: Attempt to connect to an app we know exists.
85     appview.connect(appToEmbed, {}, function(success) {
86       // Make sure we don't fail.
87       if (!success) {
88         embedder.test.fail();
89         return;
90       }
91       embedder.test.succeed();
92     });
93   });
94   document.body.appendChild(appview);
95 };
96
97 function testAppViewRefusedDataShouldFail(appToEmbed) {
98   var appview = new AppView();
99   LOG('appToEmbed  ' + appToEmbed);
100   LOG('Attempting to connect to app with refused params.');
101   appview.connect(appToEmbed, { 'foo': 'bar' }, function(success) {
102     // Make sure we fail.
103     if (success) {
104       embedder.test.fail();
105       return;
106     }
107     embedder.test.succeed();
108   });
109   document.body.appendChild(appview);
110 };
111
112 function testAppViewGoodDataShouldSucceed(appToEmbed) {
113   var appview = new AppView();
114   LOG('appToEmbed  ' + appToEmbed);
115   LOG('Attempting to connect to app with good params.');
116   // Step 2: Attempt to connect to an app with good params.
117   appview.connect(appToEmbed, { 'foo': 'bleep' }, function(success) {
118     // Make sure we don't fail.
119     if (!success) {
120       embedder.test.fail();
121       return;
122     }
123     embedder.test.succeed();
124   });
125   document.body.appendChild(appview);
126 };
127
128 embedder.test.testList = {
129   'testAppViewBasic': testAppViewBasic,
130   'testAppViewRefusedDataShouldFail': testAppViewRefusedDataShouldFail,
131   'testAppViewGoodDataShouldSucceed': testAppViewGoodDataShouldSucceed
132 };
133
134 onload = function() {
135   chrome.test.getConfig(function(config) {
136     embedder.setUp_(config);
137     chrome.test.sendMessage('Launched');
138   });
139 };