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.
5 #include "base/path_service.h"
6 #include "chrome/common/chrome_paths.h"
7 #include "chrome/common/extensions/api/extension_action/action_info.h"
8 #include "chrome/common/extensions/manifest_tests/chrome_manifest_test.h"
9 #include "extensions/common/constants.h"
10 #include "extensions/common/error_utils.h"
11 #include "extensions/common/extension.h"
12 #include "extensions/common/manifest_constants.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 namespace extensions
{
17 namespace errors
= manifest_errors
;
18 namespace keys
= manifest_keys
;
20 class PageActionManifestTest
: public ChromeManifestTest
{
22 base::FilePath
GetTestDataDir() override
{
24 PathService::Get(chrome::DIR_TEST_DATA
, &path
);
25 return path
.AppendASCII("extensions").AppendASCII("page_action");
28 scoped_ptr
<ActionInfo
> LoadAction(const std::string
& manifest_filename
);
31 scoped_ptr
<ActionInfo
> PageActionManifestTest::LoadAction(
32 const std::string
& manifest_filename
) {
33 scoped_refptr
<Extension
> extension
= LoadAndExpectSuccess(
34 manifest_filename
.c_str());
35 const ActionInfo
* page_action_info
=
36 ActionInfo::GetPageActionInfo(extension
.get());
37 EXPECT_TRUE(page_action_info
);
38 if (page_action_info
) {
39 return make_scoped_ptr(new ActionInfo(*page_action_info
));
41 ADD_FAILURE() << "Expected manifest in " << manifest_filename
42 << " to include a page_action section.";
43 return scoped_ptr
<ActionInfo
>();
46 TEST_F(PageActionManifestTest
, ManifestVersion2
) {
47 scoped_refptr
<Extension
> extension(
48 LoadAndExpectSuccess("page_action_manifest_version_2.json"));
49 ASSERT_TRUE(extension
.get());
50 const ActionInfo
* page_action_info
=
51 ActionInfo::GetPageActionInfo(extension
.get());
52 ASSERT_TRUE(page_action_info
);
54 EXPECT_EQ("", page_action_info
->id
);
55 EXPECT_TRUE(page_action_info
->default_icon
.empty());
56 EXPECT_EQ("", page_action_info
->default_title
);
57 EXPECT_TRUE(page_action_info
->default_popup_url
.is_empty());
59 LoadAndExpectError("page_action_manifest_version_2b.json",
60 errors::kInvalidPageActionPopup
);
63 TEST_F(PageActionManifestTest
, LoadPageActionHelper
) {
64 scoped_ptr
<ActionInfo
> action
;
66 // First try with an empty dictionary.
67 action
= LoadAction("page_action_empty.json");
70 // Now setup some values to use in the action.
71 const std::string
id("MyExtensionActionId");
72 const std::string
name("MyExtensionActionName");
73 std::string
img1("image1.png");
75 action
= LoadAction("page_action.json");
77 ASSERT_EQ(id
, action
->id
);
79 // No title, so fall back to name.
80 ASSERT_EQ(name
, action
->default_title
);
82 action
->default_icon
.Get(extension_misc::EXTENSION_ICON_ACTION
,
83 ExtensionIconSet::MATCH_EXACTLY
));
85 // Same test with explicitly set type.
86 action
= LoadAction("page_action_type.json");
89 // Try an action without id key.
90 action
= LoadAction("page_action_no_id.json");
93 // Then try without the name key. It's optional, so no error.
94 action
= LoadAction("page_action_no_name.json");
96 ASSERT_TRUE(action
->default_title
.empty());
98 // Then try without the icon paths key.
99 action
= LoadAction("page_action_no_icon.json");
102 // Now test that we can parse the new format for page actions.
103 const std::string
kTitle("MyExtensionActionTitle");
104 const std::string
kIcon("image1.png");
105 const std::string
kPopupHtmlFile("a_popup.html");
107 action
= LoadAction("page_action_new_format.json");
109 ASSERT_EQ(kTitle
, action
->default_title
);
110 ASSERT_FALSE(action
->default_icon
.empty());
112 // Invalid title should give an error even with a valid name.
113 LoadAndExpectError("page_action_invalid_title.json",
114 errors::kInvalidPageActionDefaultTitle
);
116 // Invalid name should give an error only with no title.
117 action
= LoadAction("page_action_invalid_name.json");
119 ASSERT_EQ(kTitle
, action
->default_title
);
121 LoadAndExpectError("page_action_invalid_name_no_title.json",
122 errors::kInvalidPageActionName
);
124 // Test that keys "popup" and "default_popup" both work, but can not
125 // be used at the same time.
126 // These tests require an extension_url, so we also load the manifest.
128 // Only use "popup", expect success.
129 scoped_refptr
<Extension
> extension
=
130 LoadAndExpectSuccess("page_action_popup.json");
131 const ActionInfo
* extension_action
=
132 ActionInfo::GetPageActionInfo(extension
.get());
133 ASSERT_TRUE(extension_action
);
135 extension
->url().Resolve(kPopupHtmlFile
).spec().c_str(),
136 extension_action
->default_popup_url
.spec().c_str());
138 // Use both "popup" and "default_popup", expect failure.
139 LoadAndExpectError("page_action_popup_and_default_popup.json",
140 ErrorUtils::FormatErrorMessage(
141 errors::kInvalidPageActionOldAndNewKeys
,
142 keys::kPageActionDefaultPopup
,
143 keys::kPageActionPopup
));
145 // Use only "default_popup", expect success.
146 extension
= LoadAndExpectSuccess("page_action_popup.json");
147 extension_action
= ActionInfo::GetPageActionInfo(extension
.get());
148 ASSERT_TRUE(extension_action
);
150 extension
->url().Resolve(kPopupHtmlFile
).spec().c_str(),
151 extension_action
->default_popup_url
.spec().c_str());
153 // Setting default_popup to "" is the same as having no popup.
154 action
= LoadAction("page_action_empty_default_popup.json");
156 EXPECT_TRUE(action
->default_popup_url
.is_empty());
159 action
->default_popup_url
.spec().c_str());
161 // Setting popup to "" is the same as having no popup.
162 action
= LoadAction("page_action_empty_popup.json");
165 EXPECT_TRUE(action
->default_popup_url
.is_empty());
168 action
->default_popup_url
.spec().c_str());
171 } // namespace extensions