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