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