Commit b5b650e0 authored by Toby Huang's avatar Toby Huang Committed by Commit Bot

Add tests for parent permission and error dialogs on Management API

This CL adds browser tests to parent_permission_dialog_view_
browsertest.cc for the Parent Permission and the Extension Install
Blocked By Parent dialogs launching from the extension Management API.

This CL also adds unit tests to management_api_unittest.cc for the
same two dialogs launching from a background page, where there's no
active web contents.

Bug: 1083275
Change-Id: I34db7aee30d5153b8dbc348572522e0f16555d10
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2220816
Commit-Queue: Toby Huang <tobyhuang@chromium.org>
Reviewed-by: default avatarScott Violet <sky@chromium.org>
Reviewed-by: default avatarDan S <danan@chromium.org>
Reviewed-by: default avatarDevlin <rdevlin.cronin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#786157}
parent 47a0e3e7
......@@ -46,6 +46,8 @@
#include "chrome/browser/supervised_user/supervised_user_service_factory.h"
#include "chrome/browser/supervised_user/supervised_user_test_util.h"
#include "content/public/browser/gpu_data_manager.h"
#include "extensions/browser/api/management/management_api_constants.h"
#include "extensions/common/error_utils.h"
#endif
namespace extensions {
......@@ -129,7 +131,8 @@ bool ManagementApiUnitTest::RunSetEnabledFunction(
scoped_refptr<ManagementSetEnabledFunction> function =
base::MakeRefCounted<ManagementSetEnabledFunction>();
function->set_browser_context(profile());
function->SetRenderFrameHost(web_contents->GetMainFrame());
if (web_contents)
function->SetRenderFrameHost(web_contents->GetMainFrame());
base::ListValue args;
args.AppendString(extension_id);
args.AppendBoolean(enabled);
......@@ -1518,6 +1521,143 @@ TEST_F(ManagementApiSupervisedUserTestWithSetup, SetEnabled_PreviouslyAllowed) {
// Parent permission dialog was not opened.
EXPECT_EQ(0, supervised_user_delegate_->show_dialog_count());
}
// Tests launching the Parent Permission Dialog from a background page, where
// there isn't active web contents. The parent approves the request.
TEST_F(ManagementApiSupervisedUserTestWithSetup,
SetEnabled_ParentPermissionApprovedFromBackgroundPage) {
// Preconditions.
ASSERT_TRUE(profile()->IsChild());
// Start with a disabled extension that needs parent permission.
service()->DisableExtension(
extension_->id(), disable_reason::DISABLE_CUSTODIAN_APPROVAL_REQUIRED);
// The parent will approve.
supervised_user_delegate_->set_next_parent_permission_dialog_result(
SupervisedUserExtensionsDelegate::ParentPermissionDialogResult::
kParentPermissionReceived);
// Simulate a call to chrome.management.setEnabled(). It should succeed
// despite a lack of web contents.
std::string error;
bool success = RunSetEnabledFunction(
/*web_contents=*/nullptr, extension_->id(), /*use_user_gesture=*/true,
/*accept_dialog=*/true, &error);
EXPECT_TRUE(success);
EXPECT_TRUE(error.empty());
// Parent Permission Dialog still opened despite the lack of web contents.
EXPECT_EQ(1, supervised_user_delegate_->show_dialog_count());
EXPECT_EQ(0, supervised_user_delegate_->show_block_dialog_count());
// Extension is now enabled.
EXPECT_EQ(1, delegate_->enable_count_);
}
// Tests launching the Parent Permission Dialog from a background page, where
// there isn't active web contents. The parent cancels the request.
TEST_F(ManagementApiSupervisedUserTestWithSetup,
SetEnabled_ParentPermissionCanceledFromBackgroundPage) {
// Preconditions.
ASSERT_TRUE(profile()->IsChild());
// Start with a disabled extension that needs parent permission.
service()->DisableExtension(
extension_->id(), disable_reason::DISABLE_CUSTODIAN_APPROVAL_REQUIRED);
// The parent will cancel.
supervised_user_delegate_->set_next_parent_permission_dialog_result(
SupervisedUserExtensionsDelegate::ParentPermissionDialogResult::
kParentPermissionCanceled);
// Simulate a call to chrome.management.setEnabled() with no web contents.
std::string error;
bool success = RunSetEnabledFunction(
/*web_contents=*/nullptr, extension_->id(), /*use_user_gesture=*/true,
/*accept_dialog=*/true, &error);
EXPECT_FALSE(success);
EXPECT_EQ(extension_management_api_constants::kUserDidNotReEnableError,
error);
// Parent Permission Dialog still opened despite the lack of web contents.
EXPECT_EQ(1, supervised_user_delegate_->show_dialog_count());
EXPECT_EQ(0, supervised_user_delegate_->show_block_dialog_count());
// Extension was not enabled.
EXPECT_EQ(0, delegate_->enable_count_);
}
// Tests launching the Parent Permission Dialog from a background page, where
// there isn't active web contents. The request will fail due to some sort of
// error, such as a network error.
TEST_F(ManagementApiSupervisedUserTestWithSetup,
SetEnabled_ParentPermissionFailedFromBackgroundPage) {
// Preconditions.
ASSERT_TRUE(profile()->IsChild());
// Start with a disabled extension that needs parent permission.
service()->DisableExtension(
extension_->id(), disable_reason::DISABLE_CUSTODIAN_APPROVAL_REQUIRED);
// The request will fail.
supervised_user_delegate_->set_next_parent_permission_dialog_result(
SupervisedUserExtensionsDelegate::ParentPermissionDialogResult::
kParentPermissionFailed);
// Simulate a call to chrome.management.setEnabled() with no web contents.
std::string error;
bool success = RunSetEnabledFunction(
/*web_contents=*/nullptr, extension_->id(), /*use_user_gesture=*/true,
/*accept_dialog=*/true, &error);
EXPECT_FALSE(success);
EXPECT_EQ(extension_management_api_constants::kParentPermissionFailedError,
error);
// Parent Permission Dialog still opened despite the lack of web contents.
EXPECT_EQ(1, supervised_user_delegate_->show_dialog_count());
EXPECT_EQ(0, supervised_user_delegate_->show_block_dialog_count());
// Extension was not enabled.
EXPECT_EQ(0, delegate_->enable_count_);
}
// Tests launching the Extension Install Blocked By Parent Dialog from a
// background page, where there isn't active web contents.
TEST_F(ManagementApiSupervisedUserTestWithSetup,
SetEnabled_ExtensionInstallBlockedByParentFromBackgroundPage) {
// Preconditions.
ASSERT_TRUE(profile()->IsChild());
// Start with a disabled extension that needs parent permission.
service()->DisableExtension(
extension_->id(), disable_reason::DISABLE_CUSTODIAN_APPROVAL_REQUIRED);
// Simulate the parent disabling the "Permissions for sites, apps and
// extensions" toggle.
GetSupervisedUserService()
->SetSupervisedUserExtensionsMayRequestPermissionsPrefForTesting(false);
// Simulate a call to chrome.management.setEnabled(). The enable attempt
// should be blocked.
std::string error;
bool success = RunSetEnabledFunction(
/*web_contents=*/nullptr, extension_->id(), /*use_user_gesture=*/true,
/*accept_dialog=*/true, &error);
EXPECT_FALSE(success);
const std::string expected_error = ErrorUtils::FormatErrorMessage(
extension_management_api_constants::kUserCantModifyError,
extension_->id());
EXPECT_EQ(expected_error, error);
// The Extension Install Blocked By Parent Dialog should have opened despite
// the lack of web contents.
EXPECT_EQ(1, supervised_user_delegate_->show_block_dialog_count());
EXPECT_EQ(0, supervised_user_delegate_->show_dialog_count());
// Extension was not enabled.
EXPECT_EQ(0, delegate_->enable_count_);
}
#endif // BUILDFLAG(ENABLE_SUPERVISED_USERS)
} // namespace
......
......@@ -4,6 +4,7 @@
var assertEq = chrome.test.assertEq;
var assertFalse = chrome.test.assertFalse;
var assertLastError = chrome.test.assertLastError;
var assertNoLastError = chrome.test.assertNoLastError;
var assertTrue = chrome.test.assertTrue;
var fail = chrome.test.fail;
......
<!--
* Copyright 2020 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.
-->
<script src="common.js"></script>
<script src="supervised_user_parent_disabled_permission_for_enable.js"></script>
// Copyright 2020 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 tests = [
// Tries to enable a disabled extension.
function enable() {
chrome.management.getAll(function(items) {
var disabledItem = getItemNamed(items, 'disabled_extension');
var expectedError =
`Extension ${disabledItem.id} cannot be modified by user.`;
checkItem(disabledItem, 'disabled_extension', false, 'extension');
chrome.management.setEnabled(disabledItem.id, true, function() {
assertLastError(expectedError);
chrome.management.get(disabledItem.id, function(stillDisabledItem) {
checkItem(
stillDisabledItem, 'disabled_extension', false, 'extension');
succeed();
});
});
});
}
];
chrome.test.runTests(tests);
<!--
* Copyright 2020 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.
-->
<script src="common.js"></script>
<script src="supervised_user_permission_granted_for_enable.js"></script>
// Copyright 2020 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 tests = [
// Tries to enable a disabled extension.
function enable() {
let onEnabledPromise = new Promise((resolve) => {
chrome.management.onEnabled.addListener(function(info) {
assertEq(info.name, 'disabled_extension');
resolve();
});
});
let setEnabledPromise = new Promise((resolve) => {
chrome.management.getAll(function(items) {
var disabledItem = getItemNamed(items, 'disabled_extension');
checkItem(disabledItem, 'disabled_extension', false, 'extension');
chrome.management.setEnabled(disabledItem.id, true, function() {
chrome.management.get(disabledItem.id, function(nowEnabledItem) {
checkItem(nowEnabledItem, 'disabled_extension', true, 'extension');
resolve();
});
});
});
});
Promise.all([onEnabledPromise, setEnabledPromise]).then(() => {
succeed();
});
}
];
chrome.test.runTests(tests);
<!--
* Copyright 2020 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.
-->
<script src="common.js"></script>
<script src="supervised_user_permission_not_granted_for_enable.js"></script>
// Copyright 2020 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 tests = [
// Tries to enable a disabled extension.
function enable() {
chrome.management.getAll(function(items) {
var disabledItem = getItemNamed(items, 'disabled_extension');
var expectedError = 'The user did not accept the re-enable dialog.';
checkItem(disabledItem, 'disabled_extension', false, 'extension');
chrome.management.setEnabled(disabledItem.id, true, function() {
assertLastError(expectedError);
chrome.management.get(disabledItem.id, function(stillDisabledItem) {
checkItem(
stillDisabledItem, 'disabled_extension', false, 'extension');
succeed();
});
});
});
}
];
chrome.test.runTests(tests);
......@@ -513,7 +513,7 @@ void ManagementSetEnabledFunction::OnInstallPromptDone(bool did_accept) {
->Get(browser_context())
->GetDelegate()
->EnableExtension(browser_context(), extension_id_);
Respond(OneArgument(std::make_unique<base::Value>(true)));
Respond(NoArguments());
} else {
Respond(Error(keys::kUserDidNotReEnableError));
}
......@@ -552,7 +552,7 @@ void ManagementSetEnabledFunction::OnParentPermissionDialogDone(
->Get(browser_context())
->GetDelegate();
delegate->EnableExtension(browser_context(), extension_id_);
Respond(OneArgument(std::make_unique<base::Value>(true)));
Respond(NoArguments());
break;
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment