Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / api_test / webstore_private / common.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 // The id of an extension we're using for install tests.
6 var extensionId = "enfkhcelefdadlmkffamgdlgplcionje";
7
8 // The id of an app we're using for install tests.
9 var appId = "iladmdjkfniedhfhcfoefgojhgaiaccc";
10
11 var assertEq = chrome.test.assertEq;
12 var assertFalse = chrome.test.assertFalse;
13 var assertNoLastError = chrome.test.assertNoLastError;
14 var assertTrue = chrome.test.assertTrue;
15 var callbackFail = chrome.test.callbackFail;
16 var callbackPass = chrome.test.callbackPass;
17 var listenOnce = chrome.test.listenOnce;
18 var runTests = chrome.test.runTests;
19 var succeed = chrome.test.succeed;
20
21 // Calls |callback| with true/false indicating whether an item with the |id|
22 // is installed.
23 function checkItemInstalled(id, callback) {
24   chrome.management.getAll(function(extensions) {
25     callback(extensions.some(function(ext) {
26       return ext.id == id;
27     }));
28   });
29 }
30
31 // Calls |callback| with true/false indicating whether an item with an id of
32 // extensionId is installed.
33 function checkInstalled(callback) {
34   checkItemInstalled(extensionId, callback);
35 }
36
37 var cachedIcon = null;
38 var img = null;
39
40 // This returns the base64-encoded content of the extension's image.
41 function getIconData(callback) {
42   if (cachedIcon) {
43     callback(cachedIcon);
44     return;
45   }
46   var canvas = document.createElement("canvas");
47   canvas.style.display = "none";
48   canvas.width = 128;
49   canvas.height = 128;
50   img = new Image();
51   img.onload = function() {
52     console.log('img.onload called');
53     var ctx = canvas.getContext("2d");
54     ctx.drawImage(img, 0, 0);
55     var tmp = canvas.toDataURL();
56     // Strip the data url prefix to just get the base64-encoded bytes.
57     cachedIcon = tmp.slice(tmp.search(",")+1);
58     callback(cachedIcon);
59   };
60   img.src = "extension/icon.png";
61 }
62
63 // This returns the string contents of the extension's manifest file.
64 function getManifest(alternativePath) {
65   // Do a synchronous XHR to get the manifest.
66   var xhr = new XMLHttpRequest();
67   xhr.open("GET",
68            alternativePath ? alternativePath : "extension/manifest.json",
69            false);
70   xhr.send(null);
71   return xhr.responseText;
72 }
73
74 // Installs the extension with the given |installOptions|, calls
75 // |whileInstalled| and then uninstalls the extension.
76 function installAndCleanUp(installOptions, whileInstalled) {
77   // Begin installing.
78   chrome.webstorePrivate.beginInstallWithManifest3(
79       installOptions,
80       callbackPass(function(result) {
81         assertNoLastError();
82         assertEq("", result);
83
84         // Now complete the installation.
85         chrome.webstorePrivate.completeInstall(
86             extensionId,
87             callbackPass(function(result) {
88               assertNoLastError();
89               assertEq(undefined, result);
90
91               whileInstalled();
92
93               chrome.test.runWithUserGesture(callbackPass(function() {
94                 chrome.management.uninstall(extensionId, {}, callbackPass());
95               }));
96             }));
97       }));
98 }