Skip to content

Commit

Permalink
Adding app_view tests to app_shell_browsertests.
Browse files Browse the repository at this point in the history
This CL adds the following tests to app_shell_browsertests:
AppViewTest.TestAppViewGoodDataShouldSucceed
AppViewTest.TestAppViewRefusedDataShouldFail
AppViewTest.TestAppViewWithUndefinedDataShouldSucceed

BUG=352293

Review URL: https://codereview.chromium.org/643703007

Cr-Commit-Position: refs/heads/master@{#302198}
  • Loading branch information
lucasgadani authored and Commit bot committed Oct 31, 2014
1 parent fef4b7c commit 45e87e9
Show file tree
Hide file tree
Showing 10 changed files with 302 additions and 6 deletions.
105 changes: 105 additions & 0 deletions extensions/browser/guest_view/app_view/app_view_apitest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/path_service.h"
#include "base/strings/stringprintf.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/test_utils.h"
#include "extensions/browser/app_window/app_window.h"
#include "extensions/browser/app_window/app_window_registry.h"
#include "extensions/browser/guest_view/guest_view_manager.h"
#include "extensions/browser/guest_view/guest_view_manager_factory.h"
#include "extensions/browser/guest_view/test_guest_view_manager.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_paths.h"
#include "extensions/shell/browser/shell_content_browser_client.h"
#include "extensions/shell/browser/shell_extension_system.h"
#include "extensions/shell/test/shell_test.h"
#include "extensions/test/extension_test_message_listener.h"
#include "net/base/filename_util.h"

namespace extensions {

class AppViewTest : public AppShellTest {
protected:
AppViewTest() { GuestViewManager::set_factory_for_testing(&factory_); }

TestGuestViewManager* GetGuestViewManager() {
return static_cast<TestGuestViewManager*>(
TestGuestViewManager::FromBrowserContext(
ShellContentBrowserClient::Get()->GetBrowserContext()));
}

content::WebContents* GetFirstAppWindowWebContents() {
const AppWindowRegistry::AppWindowList& app_window_list =
AppWindowRegistry::Get(browser_context_)->app_windows();
DCHECK(app_window_list.size() == 1);
return (*app_window_list.begin())->web_contents();
}

const Extension* LoadApp(const std::string& app_location) {
base::FilePath test_data_dir;
PathService::Get(DIR_TEST_DATA, &test_data_dir);
test_data_dir = test_data_dir.AppendASCII(app_location.c_str());
return extension_system_->LoadApp(test_data_dir);
}

void RunTest(const std::string& test_name,
const std::string& app_location,
const std::string& app_to_embed) {
extension_system_->Init();

const Extension* app_embedder = LoadApp(app_location);
ASSERT_TRUE(app_embedder);
const Extension* app_embedded = LoadApp(app_to_embed);
ASSERT_TRUE(app_embedded);

extension_system_->LaunchApp(app_embedder->id());

ExtensionTestMessageListener launch_listener("LAUNCHED", false);
ASSERT_TRUE(launch_listener.WaitUntilSatisfied());

embedder_web_contents_ = GetFirstAppWindowWebContents();

ExtensionTestMessageListener done_listener("TEST_PASSED", false);
done_listener.set_failure_message("TEST_FAILED");
ASSERT_TRUE(
content::ExecuteScript(embedder_web_contents_,
base::StringPrintf("runTest('%s', '%s')",
test_name.c_str(),
app_embedded->id().c_str())))
<< "Unable to start test.";
ASSERT_TRUE(done_listener.WaitUntilSatisfied());
}

protected:
content::WebContents* embedder_web_contents_;
TestGuestViewManagerFactory factory_;
};

// Tests that <appview> correctly processes parameters passed on connect.
IN_PROC_BROWSER_TEST_F(AppViewTest, TestAppViewGoodDataShouldSucceed) {
RunTest("testAppViewGoodDataShouldSucceed",
"app_view/apitest",
"app_view/apitest/skeleton");
}

// Tests that <appview> correctly processes parameters passed on connect.
// This test should fail to connect because the embedded app (skeleton) will
// refuse the data passed by the embedder app and deny the request.
IN_PROC_BROWSER_TEST_F(AppViewTest, TestAppViewRefusedDataShouldFail) {
RunTest("testAppViewRefusedDataShouldFail",
"app_view/apitest",
"app_view/apitest/skeleton");
}

// Tests that <appview> is able to navigate to another installed app.
IN_PROC_BROWSER_TEST_F(AppViewTest, TestAppViewWithUndefinedDataShouldSucceed) {
RunTest("testAppViewWithUndefinedDataShouldSucceed",
"app_view/apitest",
"app_view/apitest/skeleton");
}

} // namespace extensions
10 changes: 4 additions & 6 deletions extensions/browser/guest_view/web_view/web_view_apitest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,10 @@ void WebViewAPITest::RunTest(const std::string& test_name,

ExtensionTestMessageListener done_listener("TEST_PASSED", false);
done_listener.set_failure_message("TEST_FAILED");
if (!content::ExecuteScript(
embedder_web_contents_,
base::StringPrintf("runTest('%s')", test_name.c_str()))) {
LOG(ERROR) << "Unable to start test.";
return;
}
ASSERT_TRUE(content::ExecuteScript(
embedder_web_contents_,
base::StringPrintf("runTest('%s')", test_name.c_str())))
<< "Unable to start test.";
ASSERT_TRUE(done_listener.WaitUntilSatisfied());
}

Expand Down
1 change: 1 addition & 0 deletions extensions/shell/app_shell.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@
'../browser/api/sockets_tcp/sockets_tcp_apitest.cc',
'../browser/api/sockets_udp/sockets_udp_apitest.cc',
'../browser/api/usb/usb_apitest.cc',
'../browser/guest_view/app_view/app_view_apitest.cc',
'../browser/guest_view/web_view/web_view_apitest.h',
'../browser/guest_view/web_view/web_view_apitest.cc',
'../browser/guest_view/web_view/web_view_media_access_apitest.cc',
Expand Down
12 changes: 12 additions & 0 deletions extensions/test/data/app_view/apitest/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!--
* Copyright (c) 2012 The Chromium Authors. All rights reserved. Use of this
* source code is governed by a BSD-style license that can be found in the
* LICENSE file.
-->
<html>
<body>
<div id="object-container"></div>

<script src="main.js"></script>
</body>
</html>
127 changes: 127 additions & 0 deletions extensions/test/data/app_view/apitest/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

var util = {};
var embedder = {};

window.runTest = function(testName, appToEmbed) {
if (!embedder.test.testList[testName]) {
window.console.log('Incorrect testName: ' + testName);
embedder.test.fail();
return;
}

// Run the test.
embedder.test.testList[testName](appToEmbed);
};

var LOG = function(msg) {
window.console.log(msg);
};

embedder.test = {};
embedder.test.succeed = function() {
chrome.test.sendMessage('TEST_PASSED');
};

embedder.test.fail = function() {
chrome.test.sendMessage('TEST_FAILED');
};

embedder.test.assertEq = function(a, b) {
if (a != b) {
console.log('Assertion failed: ' + a + ' != ' + b);
embedder.test.fail();
}
};

embedder.test.assertTrue = function(condition) {
if (!condition) {
console.log('Assertion failed: true != ' + condition);
embedder.test.fail();
}
};

embedder.test.assertFalse = function(condition) {
if (condition) {
console.log('Assertion failed: false != ' + condition);
embedder.test.fail();
}
};

// Tests begin.
function testAppViewGoodDataShouldSucceed(appToEmbed) {
var appview = new AppView();
LOG('appToEmbed ' + appToEmbed);
document.body.appendChild(appview);
LOG('Attempting to connect to app with good params.');
// Step 2: Attempt to connect to an app with good params.
appview.connect(appToEmbed, {'foo': 'bleep'}, function(success) {
// Make sure we don't fail.
if (!success) {
LOG('FAILED TO CONNECT.');
embedder.test.fail();
return;
}
LOG('Connected.');
embedder.test.succeed();
});
};

function testAppViewRefusedDataShouldFail(appToEmbed) {
var appview = new AppView();
LOG('appToEmbed ' + appToEmbed);
document.body.appendChild(appview);
LOG('Attempting to connect to app with refused params.');
appview.connect(appToEmbed, {'foo': 'bar'}, function(success) {
// Make sure we fail.
if (success) {
LOG('UNEXPECTED CONNECTION.');
embedder.test.fail();
return;
}
LOG('Failed to connect.');
embedder.test.succeed();
});
};

function testAppViewWithUndefinedDataShouldSucceed(appToEmbed) {
var appview = new AppView();
LOG('appToEmbed ' + appToEmbed);
document.body.appendChild(appview);
// Step 1: Attempt to connect to a non-existant app (abc123).
LOG('Attempting to connect to non-existant app.');
appview.connect('abc123', undefined, function(success) {
// Make sure we fail.
if (success) {
LOG('UNEXPECTED CONNECTION.');
embedder.test.fail();
return;
}
LOG('failed to connect to non-existant app.');
LOG('attempting to connect to known app.');
// Step 2: Attempt to connect to an app we know exists.
appview.connect(appToEmbed, undefined, function(success) {
// Make sure we don't fail.
if (!success) {
LOG('FAILED TO CONNECT.');
embedder.test.fail();
return;
}
LOG('Connected.');
embedder.test.succeed();
});
});
};

embedder.test.testList = {
'testAppViewGoodDataShouldSucceed': testAppViewGoodDataShouldSucceed,
'testAppViewRefusedDataShouldFail': testAppViewRefusedDataShouldFail,
'testAppViewWithUndefinedDataShouldSucceed':
testAppViewWithUndefinedDataShouldSucceed
};

onload = function() {
chrome.test.sendMessage('LAUNCHED');
};
13 changes: 13 additions & 0 deletions extensions/test/data/app_view/apitest/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "Platform App Test: <appview>",
"version": "2",
"manifest_version": 2,
"permissions": [
"appview"
],
"app": {
"background": {
"scripts": ["test.js"]
}
}
}
7 changes: 7 additions & 0 deletions extensions/test/data/app_view/apitest/skeleton/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<!DOCTYPE html>
<html>
<head>
<body>
<div>Hello World!</div>
</body>
</html>
15 changes: 15 additions & 0 deletions extensions/test/data/app_view/apitest/skeleton/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

chrome.app.runtime.onEmbedRequested.addListener(function(request) {
if (!request.data.foo) {
request.allow('main.html');
return;
} else if (request.data.foo == 'bar') {
request.deny();
} else if (request.data.foo == 'bleep') {
request.allow('main.html');
}
});

11 changes: 11 additions & 0 deletions extensions/test/data/app_view/apitest/skeleton/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"manifest_version": 2,
"name": "skeleton",
"minimum_chrome_version": "24.0.1307.0",
"version": "1.0",
"app": {
"background": {
"scripts": ["main.js"]
}
}
}
7 changes: 7 additions & 0 deletions extensions/test/data/app_view/apitest/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

chrome.app.runtime.onLaunched.addListener(function() {
chrome.app.window.create('main.html', {}, function () {});
});

0 comments on commit 45e87e9

Please sign in to comment.