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

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

Issue 2783813002: Move ChromeRequirementsChecker to //extensions as a PreloadCheck (Closed)
Patch Set: scoped_refptr nuances, rebase 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/preload_check.h"
20 #include "extensions/browser/preload_check_test_util.h"
21 #include "extensions/common/extension.h"
22 #include "extensions/common/manifest.h"
23 #include "gpu/config/gpu_info.h"
24 #include "testing/gmock/include/gmock/gmock.h"
25 #include "testing/gtest/include/gtest/gtest.h"
26 #include "ui/base/l10n/l10n_util.h"
27
28 namespace extensions {
29
30 namespace {
31
32 // Whether this build supports the window.shape requirement.
33 const bool kSupportsWindowShape =
34 #if defined(USE_AURA)
35 true;
36 #else
37 false;
38 #endif
39
40 // Whether this build supports the plugins.npapi requirement.
41 const bool kSupportsNPAPI =
42 #if defined(OS_POSIX) && !defined(OS_MACOSX)
43 false;
44 #else
45 true;
46 #endif
47
48 // Returns true if a WebGL check might not fail immediately.
49 bool MightSupportWebGL() {
50 return content::GpuDataManager::GetInstance()->GpuAccessAllowed(nullptr);
51 }
52
53 const char kFeaturesKey[] = "requirements.3D.features";
54 const char kFeatureWebGL[] = "webgl";
55 const char kFeatureCSS3d[] = "css3d";
56
57 } // namespace
58
59 class RequirementsCheckerTest : public ExtensionsTest {
60 public:
61 RequirementsCheckerTest() {
62 manifest_dict_ = base::MakeUnique<base::DictionaryValue>();
63 }
64
65 ~RequirementsCheckerTest() override {}
66
67 void CreateExtension() {
68 manifest_dict_->SetString("name", "dummy name");
69 manifest_dict_->SetString("version", "1");
70
71 std::string error;
72 extension_ =
73 Extension::Create(base::FilePath(), Manifest::UNPACKED, *manifest_dict_,
74 Extension::NO_FLAGS, &error);
75 ASSERT_TRUE(extension_.get()) << error;
76 }
77
78 protected:
79 void StartChecker() {
80 checker_ = base::MakeUnique<RequirementsChecker>(extension_);
81 runner_.Run(checker_.get());
82 }
83
84 void RequireWindowShape() {
85 manifest_dict_->SetBoolean("requirements.window.shape", true);
86 }
87
88 void RequireNPAPI() {
89 manifest_dict_->SetBoolean("requirements.plugins.npapi", true);
90 }
91
92 void RequireFeature(const char feature[]) {
93 if (!manifest_dict_->HasKey(kFeaturesKey))
94 manifest_dict_->Set(kFeaturesKey, base::MakeUnique<base::ListValue>());
95 base::ListValue* features_list = nullptr;
96 ASSERT_TRUE(manifest_dict_->GetList(kFeaturesKey, &features_list));
97 features_list->AppendString(feature);
98 }
99
100 // This should only be called once per test instance. Calling more than once
101 // will result in stale information in the GPUDataManager which will throw off
102 // the RequirementsChecker.
103 void BlackListGPUFeatures() {
104 static bool called = false;
105 ASSERT_FALSE(called);
106 called = true;
107
108 static const std::string json_blacklist =
109 R"({
110 "name": "gpu blacklist",
111 "version": "1.0",
112 "entries": [{
113 "id": 1,
114 "features": ["accelerated_webgl"]
115 }]
116 })";
117 content::GpuDataManager::GetInstance()->InitializeForTesting(
118 json_blacklist, gpu::GPUInfo());
119 }
120
121 std::unique_ptr<RequirementsChecker> checker_;
122 PreloadCheckRunner runner_;
123
124 private:
125 content::TestBrowserThreadBundle bundle_;
126 scoped_refptr<Extension> extension_;
127 std::unique_ptr<base::DictionaryValue> manifest_dict_;
128 };
129
130 // Tests no requirements.
131 TEST_F(RequirementsCheckerTest, RequirementsEmpty) {
132 CreateExtension();
133 StartChecker();
134 EXPECT_TRUE(runner_.called());
135 EXPECT_EQ(0u, runner_.errors().size());
136 EXPECT_TRUE(checker_->GetErrorMessage().empty());
137 }
138
139 // Tests fulfilled requirements.
140 TEST_F(RequirementsCheckerTest, RequirementsSuccess) {
141 if (kSupportsWindowShape)
142 RequireWindowShape();
143 if (kSupportsNPAPI)
144 RequireNPAPI();
145 RequireFeature(kFeatureCSS3d);
146
147 CreateExtension();
148 StartChecker();
149 EXPECT_TRUE(runner_.called());
150 EXPECT_EQ(0u, runner_.errors().size());
151 EXPECT_TRUE(checker_->GetErrorMessage().empty());
152 }
153
154 // Tests multiple requirements failing (on some builds).
155 TEST_F(RequirementsCheckerTest, RequirementsFailMultiple) {
156 size_t expected_errors = 0u;
157 if (!kSupportsWindowShape) {
158 RequireWindowShape();
159 expected_errors++;
160 }
161 if (!kSupportsNPAPI) {
162 RequireNPAPI();
163 expected_errors++;
164 }
165 if (!MightSupportWebGL()) {
166 RequireFeature(kFeatureWebGL);
167 expected_errors++;
168 }
169 // css3d should always succeed.
170 RequireFeature(kFeatureCSS3d);
171
172 CreateExtension();
173 StartChecker();
174 EXPECT_TRUE(runner_.called());
175 EXPECT_EQ(expected_errors, runner_.errors().size());
176 EXPECT_EQ(expected_errors == 0, checker_->GetErrorMessage().empty());
177 }
178
179 // Tests a requirement that might fail asynchronously.
180 TEST_F(RequirementsCheckerTest, RequirementsFailWebGL) {
181 BlackListGPUFeatures();
182 RequireFeature(kFeatureWebGL);
183 CreateExtension();
184 StartChecker();
185
186 // TODO(michaelpg): Check that the runner actually finishes, which requires
187 // waiting for the GPU check to succeed: crbug.com/706204.
188 runner_.WaitForIdle();
189 if (runner_.errors().size()) {
190 EXPECT_THAT(runner_.errors(), testing::UnorderedElementsAre(
191 PreloadCheck::WEBGL_NOT_SUPPORTED));
192 EXPECT_FALSE(checker_->GetErrorMessage().empty());
193 }
194 }
195
196 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698