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/command_line.h"
9 #include "base/files/file_path.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/path_service.h"
13 #include "base/strings/string_util.h"
14 #include "base/threading/sequenced_worker_pool.h"
15 #include "chrome/browser/extensions/extension_browsertest.h"
16 #include "chrome/browser/extensions/requirements_checker.h"
17 #include "chrome/common/chrome_paths.h"
18 #include "content/public/browser/browser_thread.h"
19 #include "content/public/browser/gpu_data_manager.h"
20 #include "extensions/common/extension.h"
21 #include "extensions/common/file_util.h"
22 #include "gpu/config/gpu_info.h"
23 #include "grit/generated_resources.h"
24 #include "ui/base/l10n/l10n_util.h"
25 #include "ui/gl/gl_switches.h"
27 namespace extensions
{
29 class RequirementsCheckerBrowserTest
: public ExtensionBrowserTest
{
31 scoped_refptr
<const Extension
> LoadExtensionFromDirName(
32 const std::string
& extension_dir_name
) {
33 base::FilePath extension_path
;
34 std::string load_error
;
35 PathService::Get(chrome::DIR_TEST_DATA
, &extension_path
);
36 extension_path
= extension_path
.AppendASCII("requirements_checker")
37 .AppendASCII(extension_dir_name
);
38 scoped_refptr
<const Extension
> extension
= file_util::LoadExtension(
39 extension_path
, Manifest::UNPACKED
, 0, &load_error
);
40 CHECK(load_error
.length() == 0u);
44 void ValidateRequirementErrors(std::vector
<std::string
> expected_errors
,
45 std::vector
<std::string
> actual_errors
) {
46 ASSERT_EQ(expected_errors
, actual_errors
);
47 requirement_errors_
.swap(actual_errors
);
50 // This should only be called once per test instance. Calling more than once
51 // will result in stale information in the GPUDataManager which will throw off
52 // the RequirementsChecker.
53 void BlackListGPUFeatures(const std::vector
<std::string
>& features
) {
54 static const std::string json_blacklist
=
56 " \"name\": \"gpu blacklist\",\n"
57 " \"version\": \"1.0\",\n"
61 " \"features\": [\"" + JoinString(features
, "\", \"") + "\"]\n"
65 gpu::GPUInfo gpu_info
;
66 content::GpuDataManager::GetInstance()->InitializeForTesting(
67 json_blacklist
, gpu_info
);
71 std::vector
<std::string
> requirement_errors_
;
72 RequirementsChecker checker_
;
75 IN_PROC_BROWSER_TEST_F(RequirementsCheckerBrowserTest
, CheckEmptyExtension
) {
76 scoped_refptr
<const Extension
> extension(
77 LoadExtensionFromDirName("no_requirements"));
78 ASSERT_TRUE(extension
.get());
79 checker_
.Check(extension
, base::Bind(
80 &RequirementsCheckerBrowserTest::ValidateRequirementErrors
,
81 base::Unretained(this), std::vector
<std::string
>()));
82 content::BrowserThread::GetBlockingPool()->FlushForTesting();
85 IN_PROC_BROWSER_TEST_F(RequirementsCheckerBrowserTest
, CheckNpapiExtension
) {
86 scoped_refptr
<const Extension
> extension(
87 LoadExtensionFromDirName("require_npapi"));
88 ASSERT_TRUE(extension
.get());
90 std::vector
<std::string
> expected_errors
;
91 // NPAPI plugins are disallowed on ChromeOS.
92 #if defined(OS_CHROMEOS)
93 expected_errors
.push_back(l10n_util::GetStringUTF8(
94 IDS_EXTENSION_NPAPI_NOT_SUPPORTED
));
95 #endif // defined(OS_CHROMEOS)
97 checker_
.Check(extension
, base::Bind(
98 &RequirementsCheckerBrowserTest::ValidateRequirementErrors
,
99 base::Unretained(this), expected_errors
));
100 content::BrowserThread::GetBlockingPool()->FlushForTesting();
103 IN_PROC_BROWSER_TEST_F(RequirementsCheckerBrowserTest
,
104 CheckWindowShapeExtension
) {
105 scoped_refptr
<const Extension
> extension(
106 LoadExtensionFromDirName("require_window_shape"));
107 ASSERT_TRUE(extension
.get());
109 std::vector
<std::string
> expected_errors
;
110 #if !defined(USE_AURA)
111 expected_errors
.push_back(l10n_util::GetStringUTF8(
112 IDS_EXTENSION_WINDOW_SHAPE_NOT_SUPPORTED
));
113 #endif // !defined(USE_AURA)
115 checker_
.Check(extension
, base::Bind(
116 &RequirementsCheckerBrowserTest::ValidateRequirementErrors
,
117 base::Unretained(this), expected_errors
));
118 content::BrowserThread::GetBlockingPool()->FlushForTesting();
121 IN_PROC_BROWSER_TEST_F(RequirementsCheckerBrowserTest
, DisallowWebGL
) {
122 scoped_refptr
<const Extension
> extension(
123 LoadExtensionFromDirName("require_3d"));
124 ASSERT_TRUE(extension
.get());
127 std::vector
<std::string
> blacklisted_features
;
128 blacklisted_features
.push_back("webgl");
129 BlackListGPUFeatures(blacklisted_features
);
130 content::BrowserThread::GetBlockingPool()->FlushForTesting();
132 std::vector
<std::string
> expected_errors
;
133 expected_errors
.push_back(l10n_util::GetStringUTF8(
134 IDS_EXTENSION_WEBGL_NOT_SUPPORTED
));
136 checker_
.Check(extension
, base::Bind(
137 &RequirementsCheckerBrowserTest::ValidateRequirementErrors
,
138 base::Unretained(this), expected_errors
));
139 content::BrowserThread::GetBlockingPool()->FlushForTesting();
142 IN_PROC_BROWSER_TEST_F(RequirementsCheckerBrowserTest
, Check3DExtension
) {
143 scoped_refptr
<const Extension
> extension(
144 LoadExtensionFromDirName("require_3d"));
145 ASSERT_TRUE(extension
.get());
147 std::vector
<std::string
> expected_errors
;
149 if (!content::GpuDataManager::GetInstance()->GpuAccessAllowed(NULL
)) {
150 expected_errors
.push_back(l10n_util::GetStringUTF8(
151 IDS_EXTENSION_WEBGL_NOT_SUPPORTED
));
154 checker_
.Check(extension
, base::Bind(
155 &RequirementsCheckerBrowserTest::ValidateRequirementErrors
,
156 base::Unretained(this), expected_errors
));
157 content::BrowserThread::GetBlockingPool()->FlushForTesting();
160 } // namespace extensions