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.
8 #include "base/files/file_path.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/path_service.h"
12 #include "base/strings/string_util.h"
13 #include "chrome/browser/extensions/chrome_requirements_checker.h"
14 #include "chrome/browser/extensions/extension_browsertest.h"
15 #include "chrome/common/chrome_paths.h"
16 #include "chrome/grit/generated_resources.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "content/public/browser/gpu_data_manager.h"
19 #include "content/public/test/test_utils.h"
20 #include "extensions/common/extension.h"
21 #include "extensions/common/file_util.h"
22 #include "gpu/config/gpu_info.h"
23 #include "ui/base/l10n/l10n_util.h"
25 namespace extensions
{
27 class RequirementsCheckerBrowserTest
: public ExtensionBrowserTest
{
29 RequirementsCheckerBrowserTest()
30 : checker_(new ChromeRequirementsChecker()) {}
32 scoped_refptr
<const Extension
> LoadExtensionFromDirName(
33 const std::string
& extension_dir_name
) {
34 base::FilePath extension_path
;
35 std::string load_error
;
36 PathService::Get(chrome::DIR_TEST_DATA
, &extension_path
);
37 extension_path
= extension_path
.AppendASCII("requirements_checker")
38 .AppendASCII(extension_dir_name
);
39 scoped_refptr
<const Extension
> extension
= file_util::LoadExtension(
40 extension_path
, Manifest::UNPACKED
, 0, &load_error
);
41 CHECK_EQ(0U, load_error
.length());
45 void ValidateRequirementErrors(
46 const std::vector
<std::string
>& expected_errors
,
47 const std::vector
<std::string
>& actual_errors
) {
48 ASSERT_EQ(expected_errors
, actual_errors
);
51 // This should only be called once per test instance. Calling more than once
52 // will result in stale information in the GPUDataManager which will throw off
53 // the RequirementsChecker.
54 void BlackListGPUFeatures(const std::vector
<std::string
>& features
) {
56 static bool called
= false;
61 static const std::string json_blacklist
=
63 " \"name\": \"gpu blacklist\",\n"
64 " \"version\": \"1.0\",\n"
68 " \"features\": [\"" + base::JoinString(features
, "\", \"") + "\"]\n"
72 gpu::GPUInfo gpu_info
;
73 content::GpuDataManager::GetInstance()->InitializeForTesting(
74 json_blacklist
, gpu_info
);
78 scoped_ptr
<RequirementsChecker
> checker_
;
81 IN_PROC_BROWSER_TEST_F(RequirementsCheckerBrowserTest
, CheckEmptyExtension
) {
82 scoped_refptr
<const Extension
> extension(
83 LoadExtensionFromDirName("no_requirements"));
84 ASSERT_TRUE(extension
.get());
85 checker_
->Check(extension
, base::Bind(
86 &RequirementsCheckerBrowserTest::ValidateRequirementErrors
,
87 base::Unretained(this), std::vector
<std::string
>()));
88 content::RunAllBlockingPoolTasksUntilIdle();
91 IN_PROC_BROWSER_TEST_F(RequirementsCheckerBrowserTest
, CheckNpapiExtension
) {
92 scoped_refptr
<const Extension
> extension(
93 LoadExtensionFromDirName("require_npapi"));
94 ASSERT_TRUE(extension
.get());
96 std::vector
<std::string
> expected_errors
;
97 #if defined(OS_POSIX) && !defined(OS_MACOSX)
98 expected_errors
.push_back(l10n_util::GetStringUTF8(
99 IDS_EXTENSION_NPAPI_NOT_SUPPORTED
));
102 checker_
->Check(extension
, base::Bind(
103 &RequirementsCheckerBrowserTest::ValidateRequirementErrors
,
104 base::Unretained(this), expected_errors
));
105 content::RunAllBlockingPoolTasksUntilIdle();
108 IN_PROC_BROWSER_TEST_F(RequirementsCheckerBrowserTest
,
109 CheckWindowShapeExtension
) {
110 scoped_refptr
<const Extension
> extension(
111 LoadExtensionFromDirName("require_window_shape"));
112 ASSERT_TRUE(extension
.get());
114 std::vector
<std::string
> expected_errors
;
115 #if !defined(USE_AURA)
116 expected_errors
.push_back(l10n_util::GetStringUTF8(
117 IDS_EXTENSION_WINDOW_SHAPE_NOT_SUPPORTED
));
118 #endif // !defined(USE_AURA)
120 checker_
->Check(extension
, base::Bind(
121 &RequirementsCheckerBrowserTest::ValidateRequirementErrors
,
122 base::Unretained(this), expected_errors
));
123 content::RunAllBlockingPoolTasksUntilIdle();
126 IN_PROC_BROWSER_TEST_F(RequirementsCheckerBrowserTest
, DisallowWebGL
) {
127 scoped_refptr
<const Extension
> extension(
128 LoadExtensionFromDirName("require_3d"));
129 ASSERT_TRUE(extension
.get());
132 std::vector
<std::string
> blacklisted_features
;
133 blacklisted_features
.push_back("webgl");
134 BlackListGPUFeatures(blacklisted_features
);
135 content::RunAllBlockingPoolTasksUntilIdle();
137 std::vector
<std::string
> expected_errors
;
138 expected_errors
.push_back(l10n_util::GetStringUTF8(
139 IDS_EXTENSION_WEBGL_NOT_SUPPORTED
));
141 checker_
->Check(extension
, base::Bind(
142 &RequirementsCheckerBrowserTest::ValidateRequirementErrors
,
143 base::Unretained(this), expected_errors
));
144 content::RunAllBlockingPoolTasksUntilIdle();
147 IN_PROC_BROWSER_TEST_F(RequirementsCheckerBrowserTest
, Check3DExtension
) {
148 scoped_refptr
<const Extension
> extension(
149 LoadExtensionFromDirName("require_3d"));
150 ASSERT_TRUE(extension
.get());
152 std::vector
<std::string
> expected_errors
;
154 if (!content::GpuDataManager::GetInstance()->GpuAccessAllowed(NULL
)) {
155 expected_errors
.push_back(l10n_util::GetStringUTF8(
156 IDS_EXTENSION_WEBGL_NOT_SUPPORTED
));
159 checker_
->Check(extension
, base::Bind(
160 &RequirementsCheckerBrowserTest::ValidateRequirementErrors
,
161 base::Unretained(this), expected_errors
));
162 content::RunAllBlockingPoolTasksUntilIdle();
165 } // namespace extensions