Commit aa246b7c authored by limasdf's avatar limasdf Committed by Commit bot

Validate windows.create API's state input parameters those cannot live together.

These conditions are coming from windows.update API.
- minimized with 'focused'.
- maximized, fullscreen with 'not-focused'
- Having bounds with minimized, maximized, fullscreen state.
- panel type with minimized, maximized, fullscreen state.

BUG=459841
TEST=browser_tests --gtest_filter=ExtensionWindowCreateTest.*

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

Cr-Commit-Position: refs/heads/master@{#326710}
parent 77e507bd
......@@ -198,6 +198,35 @@ ui::WindowShowState ConvertToWindowShowState(windows::WindowState state) {
return ui::SHOW_STATE_DEFAULT;
}
bool IsValidStateForWindowsCreateFunction(
const windows::Create::Params::CreateData* create_data) {
if (!create_data)
return true;
bool has_bound = create_data->left || create_data->top ||
create_data->width || create_data->height;
bool is_panel =
create_data->type == windows::CreateType::CREATE_TYPE_PANEL ||
create_data->type == windows::CreateType::CREATE_TYPE_DETACHED_PANEL;
switch (create_data->state) {
case windows::WINDOW_STATE_MINIMIZED:
// If minimised, default focused state should be unfocused.
return !(create_data->focused && *create_data->focused) && !has_bound &&
!is_panel;
case windows::WINDOW_STATE_MAXIMIZED:
case windows::WINDOW_STATE_FULLSCREEN:
// If maximised/fullscreen, default focused state should be focused.
return !(create_data->focused && !*create_data->focused) && !has_bound &&
!is_panel;
case windows::WINDOW_STATE_NORMAL:
case windows::WINDOW_STATE_NONE:
return true;
}
NOTREACHED();
return true;
}
} // namespace
void ZoomModeToZoomSettings(ZoomController::ZoomMode zoom_mode,
......@@ -421,6 +450,11 @@ bool WindowsCreateFunction::RunSync() {
return false;
}
if (!IsValidStateForWindowsCreateFunction(create_data)) {
error_ = keys::kInvalidWindowStateError;
return false;
}
Profile* window_profile = GetProfile();
Browser::Type window_type = Browser::TYPE_TABBED;
bool create_panel = false;
......
......@@ -42,8 +42,19 @@ namespace utils = extension_function_test_utils;
namespace {
using ExtensionTabsTest = InProcessBrowserTest;
using ExtensionWindowCreateTest = InProcessBrowserTest;
}
class ExtensionWindowCreateTest : public InProcessBrowserTest {
public:
// Runs chrome.windows.create(), expecting an error.
std::string RunCreateWindowExpectError(const std::string& args) {
scoped_refptr<WindowsCreateFunction> function(new WindowsCreateFunction);
function->set_extension(test_util::CreateEmptyExtension().get());
return api_test_utils::RunFunctionAndReturnError(function.get(), args,
browser()->profile());
}
};
} // namespace
IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, GetWindow) {
int window_id = ExtensionTabUtil::GetWindowId(browser());
......@@ -594,6 +605,42 @@ IN_PROC_BROWSER_TEST_F(ExtensionWindowCreateTest, AcceptState) {
#endif
}
IN_PROC_BROWSER_TEST_F(ExtensionWindowCreateTest, ValidateCreateWindowState) {
EXPECT_TRUE(
MatchPattern(RunCreateWindowExpectError(
"[{\"state\": \"fullscreen\", \"type\": \"panel\"}]"),
keys::kInvalidWindowStateError));
EXPECT_TRUE(
MatchPattern(RunCreateWindowExpectError(
"[{\"state\": \"maximized\", \"type\": \"panel\"}]"),
keys::kInvalidWindowStateError));
EXPECT_TRUE(
MatchPattern(RunCreateWindowExpectError(
"[{\"state\": \"minimized\", \"type\": \"panel\"}]"),
keys::kInvalidWindowStateError));
EXPECT_TRUE(
MatchPattern(RunCreateWindowExpectError(
"[{\"state\": \"minimized\", \"focused\": true}]"),
keys::kInvalidWindowStateError));
EXPECT_TRUE(
MatchPattern(RunCreateWindowExpectError(
"[{\"state\": \"maximized\", \"focused\": false}]"),
keys::kInvalidWindowStateError));
EXPECT_TRUE(
MatchPattern(RunCreateWindowExpectError(
"[{\"state\": \"fullscreen\", \"focused\": false}]"),
keys::kInvalidWindowStateError));
EXPECT_TRUE(MatchPattern(RunCreateWindowExpectError(
"[{\"state\": \"minimized\", \"width\": 500}]"),
keys::kInvalidWindowStateError));
EXPECT_TRUE(MatchPattern(RunCreateWindowExpectError(
"[{\"state\": \"maximized\", \"width\": 500}]"),
keys::kInvalidWindowStateError));
EXPECT_TRUE(MatchPattern(RunCreateWindowExpectError(
"[{\"state\": \"fullscreen\", \"width\": 500}]"),
keys::kInvalidWindowStateError));
}
IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, DuplicateTab) {
content::OpenURLParams params(GURL(url::kAboutBlankURL),
content::Referrer(),
......
......@@ -254,6 +254,6 @@ IN_PROC_BROWSER_TEST_F(ExtensionApiTest, DISABLED_GetViewsOfCreatedWindow) {
<< message_;
}
// Adding a new test? Awesome. But API tests are the old hotness. The
// new hotness is extension_test_utils. See tabs_test.cc for an example.
// Adding a new test? Awesome. But API tests are the old hotness. The new
// hotness is extension_function_test_utils. See tabs_test.cc for an example.
// We are trying to phase out many uses of API tests as they tend to be flaky.
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