Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(620)

Side by Side Diff: extensions/browser/requirements_checker_unittest.cc

Issue 2783813002: Move ChromeRequirementsChecker to //extensions as a PreloadCheck (Closed)
Patch Set: minor test cleanup Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 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.
4
5 #include "extensions/browser/requirements_checker.h"
6
7 #include <memory>
8 #include <vector>
9
10 #include "base/memory/ptr_util.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/strings/string16.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/values.h"
15 #include "content/public/browser/gpu_data_manager.h"
16 #include "content/public/test/test_browser_thread_bundle.h"
17 #include "extensions/browser/extension_system.h"
18 #include "extensions/browser/extensions_test.h"
19 #include "extensions/browser/management_policy.h"
20 #include "extensions/browser/mock_extension_system.h"
21 #include "extensions/browser/preload_check.h"
22 #include "extensions/browser/preload_check_test_util.h"
23 #include "extensions/common/extension.h"
24 #include "extensions/common/file_util.h"
25 #include "extensions/common/manifest_handlers/requirements_info.h"
26 #include "gpu/config/gpu_info.h"
27 #include "testing/gmock/include/gmock/gmock.h"
28 #include "testing/gtest/include/gtest/gtest.h"
29 #include "ui/base/l10n/l10n_util.h"
30
31 namespace extensions {
32
33 namespace {
34
35 // Whether this build supports the window.shape requirement.
36 const bool kSupportsWindowShape =
37 #if defined(USE_AURA)
38 true;
39 #else
40 false;
41 #endif
42
43 // Whether this build supports the plugins.npapi requirement.
44 const bool kSupportsNPAPI =
45 #if defined(OS_POSIX) && !defined(OS_MACOSX)
46 false;
47 #else
48 true;
49 #endif
50
51 // Returns true if a WebGL check might not fail immediately.
52 bool MightSupportWebGL() {
53 return content::GpuDataManager::GetInstance()->GpuAccessAllowed(nullptr);
54 }
55
56 const char kFeaturesKey[] = "requirements.3D.features";
57 const char kFeatureWebGL[] = "webgl";
58 const char kFeatureCSS3d[] = "css3d";
59
60 } // namespace
61
62 class RequirementsCheckerTest : public ExtensionsTest {
63 public:
64 RequirementsCheckerTest() {
65 manifest_dict_ = base::MakeUnique<base::DictionaryValue>();
66 }
67
68 ~RequirementsCheckerTest() override {}
69
70 void CreateExtension() {
71 manifest_dict_->SetString("name", "dummy name");
72 manifest_dict_->SetString("version", "1");
73
74 std::string error;
75 extension_ =
76 Extension::Create(base::FilePath(), Manifest::UNPACKED, *manifest_dict_,
77 Extension::NO_FLAGS, &error);
78 EXPECT_TRUE(extension_.get()) << error;
79
80 base::string16 handler_error;
81 ASSERT_TRUE(requirements_handler_.Parse(extension_.get(), &handler_error));
michaelpg 2017/03/30 02:36:09 hmm... so loading/parsing a manifest this way DCHE
michaelpg 2017/03/30 22:49:01 Okay, ManifestHandler::Parse can't be called here
82 }
83
84 protected:
85 void StartChecker() {
86 checker_ = base::MakeUnique<RequirementsChecker>(extension_.get());
87 runner_.Run(checker_.get());
88 }
89
90 void RequireWindowShape() {
91 manifest_dict_->SetBoolean("requirements.window.shape", true);
92 }
93
94 void RequireNPAPI() {
95 manifest_dict_->SetBoolean("requirements.plugins.npapi", true);
96 }
97
98 void RequireFeature(const char feature[]) {
99 if (!manifest_dict_->HasKey(kFeaturesKey))
100 manifest_dict_->Set(kFeaturesKey, base::MakeUnique<base::ListValue>());
101 base::ListValue* features_list;
Devlin 2017/03/30 17:10:55 nit: initialize to nullptr
michaelpg 2017/03/30 22:49:01 Done.
102 ASSERT_TRUE(manifest_dict_->GetList(kFeaturesKey, &features_list));
103 features_list->AppendString(feature);
104 }
105
106 // This should only be called once per test instance. Calling more than once
107 // will result in stale information in the GPUDataManager which will throw off
108 // the RequirementsChecker.
109 void BlackListGPUFeatures() {
110 static bool called = false;
111 CHECK(!called);
Devlin 2017/03/30 17:10:55 ASSERT_FALSE?
michaelpg 2017/03/30 22:49:01 Done. (This was mostly moved from the browser_test
112 called = true;
113
114 static const std::string json_blacklist =
115 "{\n"
Devlin 2017/03/30 17:10:55 this would be prettier with raw string literals. :
michaelpg 2017/03/30 22:49:01 Done.
116 " \"name\": \"gpu blacklist\",\n"
117 " \"version\": \"1.0\",\n"
118 " \"entries\": [\n"
119 " {\n"
120 " \"id\": 1,\n"
121 " \"features\": [\"accelerated_webgl\"]\n"
122 " }\n"
123 " ]\n"
124 "}";
125 gpu::GPUInfo gpu_info;
126 content::GpuDataManager::GetInstance()->InitializeForTesting(json_blacklist,
127 gpu_info);
128 }
129
130 std::unique_ptr<RequirementsChecker> checker_;
131 PreloadCheckRunner runner_;
132
133 private:
134 content::TestBrowserThreadBundle bundle_;
135 RequirementsHandler requirements_handler_;
136 scoped_refptr<Extension> extension_;
137 std::unique_ptr<base::DictionaryValue> manifest_dict_;
138 };
139
140 // Tests no requirements.
141 TEST_F(RequirementsCheckerTest, RequirementsEmpty) {
142 CreateExtension();
143 StartChecker();
144 EXPECT_TRUE(runner_.called());
145 EXPECT_EQ(0u, runner_.errors().size());
146 EXPECT_TRUE(checker_->GetErrorMessage().empty());
147 }
148
149 // Tests fulfilled requirements.
150 TEST_F(RequirementsCheckerTest, RequirementsSuccess) {
151 if (kSupportsWindowShape)
152 RequireWindowShape();
153 if (kSupportsNPAPI)
154 RequireNPAPI();
155 RequireFeature(kFeatureCSS3d);
156
157 CreateExtension();
158 StartChecker();
159 EXPECT_TRUE(runner_.called());
160 EXPECT_EQ(0u, runner_.errors().size());
161 EXPECT_TRUE(checker_->GetErrorMessage().empty());
162 }
163
164 // Tests multiple requirements failing (on some builds).
165 TEST_F(RequirementsCheckerTest, RequirementsFailMultiple) {
166 size_t expected_errors = 0u;
167 if (!kSupportsWindowShape) {
168 RequireWindowShape();
169 expected_errors++;
170 }
171 if (!kSupportsNPAPI) {
172 RequireNPAPI();
173 expected_errors++;
174 }
175 if (!MightSupportWebGL()) {
Devlin 2017/03/30 17:10:55 Won't the blacklist still take effect, even if the
michaelpg 2017/03/30 22:49:01 The blacklist is only set in the following test. T
176 RequireFeature(kFeatureWebGL);
177 expected_errors++;
178 }
179 // css3d should always succeed.
180 RequireFeature(kFeatureCSS3d);
181
182 CreateExtension();
183 StartChecker();
184 EXPECT_TRUE(runner_.called());
185 EXPECT_EQ(expected_errors, runner_.errors().size());
186 EXPECT_EQ(expected_errors == 0, checker_->GetErrorMessage().empty());
187 }
188
189 // Tests a requirement that might fail asynchronously.
190 TEST_F(RequirementsCheckerTest, RequirementsFailWebGL) {
191 BlackListGPUFeatures();
192 RequireFeature(kFeatureWebGL);
193 CreateExtension();
194 StartChecker();
195
196 // TODO(michaelpg): Check that the runner actually finishes, which requires
197 // waiting for the GPU check to succeed: crbug.com/706204.
198 runner_.WaitForIdle();
199 if (runner_.errors().size()) {
200 EXPECT_THAT(runner_.errors(), testing::UnorderedElementsAre(
201 PreloadCheck::WEBGL_NOT_SUPPORTED));
202 EXPECT_FALSE(checker_->GetErrorMessage().empty());
203 }
204 }
205
206 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698