diff options
author | limasdf <limasdf@gmail.com> | 2016-03-10 03:14:33 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-03-10 11:15:37 +0000 |
commit | 2fc1c838798d0c76f1c135f1791d3989ab7a2b01 (patch) | |
tree | 3576eb540f370455cbb456b91d4c25ab12c46983 | |
parent | 37f57d892222dc0ed816ae004074d7c80600ad46 (diff) | |
download | chromium_src-2fc1c838798d0c76f1c135f1791d3989ab7a2b01.zip chromium_src-2fc1c838798d0c76f1c135f1791d3989ab7a2b01.tar.gz chromium_src-2fc1c838798d0c76f1c135f1791d3989ab7a2b01.tar.bz2 |
chrome.browserAction.setBadgeBackgroundColor accepts color keyword
Accepts color keyword like 'black', 'blue' from setBadgeBackgroundColor.
Also, if unable to parse color string, returns proper error message
TEST=browser_tests --gtest_filter=BrowserActionApiTest.BadgeBackgroundColor
extensions_unittests --gtest_filter=ImageUtilTest.*
BUG=593194
Review URL: https://codereview.chromium.org/1781563003
Cr-Commit-Position: refs/heads/master@{#380386}
6 files changed, 43 insertions, 1 deletions
diff --git a/chrome/browser/extensions/api/extension_action/browser_action_apitest.cc b/chrome/browser/extensions/api/extension_action/browser_action_apitest.cc index 95fbfd1f..79e6051 100644 --- a/chrome/browser/extensions/api/extension_action/browser_action_apitest.cc +++ b/chrome/browser/extensions/api/extension_action/browser_action_apitest.cc @@ -632,6 +632,14 @@ IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, BadgeBackgroundColor) { // Test that hsl() values 'hsl(120, 100%, 50%)' set color correctly. ASSERT_EQ(SkColorSetARGB(255, 0, 255, 0), action->GetBadgeBackgroundColor(ExtensionAction::kDefaultTabId)); + + // Test basic color keyword set correctly. + ui_test_utils::NavigateToURL(browser(), + GURL(extension->GetResourceURL("update4.html"))); + ASSERT_TRUE(catcher.GetNextResult()); + + ASSERT_EQ(SkColorSetARGB(255, 0, 0, 255), + action->GetBadgeBackgroundColor(ExtensionAction::kDefaultTabId)); } IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, Getters) { diff --git a/chrome/browser/extensions/api/extension_action/extension_action_api.cc b/chrome/browser/extensions/api/extension_action/extension_action_api.cc index b749865..4a6ca8d 100644 --- a/chrome/browser/extensions/api/extension_action/extension_action_api.cc +++ b/chrome/browser/extensions/api/extension_action/extension_action_api.cc @@ -56,6 +56,8 @@ const char kNoTabError[] = "No tab with id: *."; const char kOpenPopupError[] = "Failed to show popup either because there is an existing popup or another " "error occurred."; +const char kInvalidColorError[] = + "The color specification could not be parsed."; } // namespace @@ -580,8 +582,10 @@ bool ExtensionActionSetBadgeBackgroundColorFunction::RunExtensionAction() { } else if (color_value->IsType(base::Value::TYPE_STRING)) { std::string color_string; EXTENSION_FUNCTION_VALIDATE(details_->GetString("color", &color_string)); - if (!image_util::ParseCssColorString(color_string, &color)) + if (!image_util::ParseCssColorString(color_string, &color)) { + error_ = kInvalidColorError; return false; + } } extension_action_->SetBadgeBackgroundColor(tab_id_, color); diff --git a/chrome/test/data/extensions/api_test/browser_action/color/update4.html b/chrome/test/data/extensions/api_test/browser_action/color/update4.html new file mode 100644 index 0000000..5ff3511 --- /dev/null +++ b/chrome/test/data/extensions/api_test/browser_action/color/update4.html @@ -0,0 +1,7 @@ +<!-- + * Copyright 2016 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="update4.js"></script> diff --git a/chrome/test/data/extensions/api_test/browser_action/color/update4.js b/chrome/test/data/extensions/api_test/browser_action/color/update4.js new file mode 100644 index 0000000..f8a15a3 --- /dev/null +++ b/chrome/test/data/extensions/api_test/browser_action/color/update4.js @@ -0,0 +1,6 @@ +// Copyright 2016 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.browserAction.setBadgeBackgroundColor({color: 'blue'}); +chrome.test.notifyPass(); diff --git a/extensions/common/image_util.cc b/extensions/common/image_util.cc index efad19b..6a8e13a 100644 --- a/extensions/common/image_util.cc +++ b/extensions/common/image_util.cc @@ -13,6 +13,7 @@ #include "base/strings/stringprintf.h" #include "third_party/re2/src/re2/re2.h" #include "third_party/skia/include/core/SkColor.h" +#include "third_party/skia/include/utils/SkParse.h" #include "ui/gfx/color_utils.h" namespace extensions { @@ -29,6 +30,11 @@ bool ParseCssColorString(const std::string& color_string, SkColor* result) { NOTIMPLEMENTED(); return false; } + if (SkParse::FindNamedColor(color_string.c_str(), color_string.size(), + result) != nullptr) { + return true; + } + return false; } diff --git a/extensions/common/image_util_unittest.cc b/extensions/common/image_util_unittest.cc index 8e7d80f..e4a81bb 100644 --- a/extensions/common/image_util_unittest.cc +++ b/extensions/common/image_util_unittest.cc @@ -108,4 +108,15 @@ TEST(ImageUtilTest, AcceptHsla) { RunPassHslTest("hsla(0, 100%, 50%, 1)", SK_ColorRED); } +TEST(ImageUtilTest, BasicColorKeyword) { + SkColor color = 0; + EXPECT_TRUE(image_util::ParseCssColorString("red", &color)); + EXPECT_EQ(color, SK_ColorRED); + + EXPECT_TRUE(image_util::ParseCssColorString("blue", &color)); + EXPECT_EQ(color, SK_ColorBLUE); + + EXPECT_FALSE(image_util::ParseCssColorString("my_red", &color)); +} + } // namespace extensions |