michaelpg | a8ea037 | 2017-04-06 20:41:35 | [diff] [blame] | 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 | |
michaelpg | a8ea037 | 2017-04-06 20:41:35 | [diff] [blame] | 10 | #include "base/memory/ref_counted.h" |
| 11 | #include "base/strings/string16.h" |
| 12 | #include "base/strings/utf_string_conversions.h" |
| 13 | #include "base/values.h" |
| 14 | #include "content/public/browser/gpu_data_manager.h" |
michaelpg | a8ea037 | 2017-04-06 20:41:35 | [diff] [blame] | 15 | #include "extensions/browser/extension_system.h" |
| 16 | #include "extensions/browser/extensions_test.h" |
| 17 | #include "extensions/browser/preload_check.h" |
| 18 | #include "extensions/browser/preload_check_test_util.h" |
| 19 | #include "extensions/common/extension.h" |
| 20 | #include "extensions/common/manifest.h" |
| 21 | #include "testing/gmock/include/gmock/gmock.h" |
| 22 | #include "testing/gtest/include/gtest/gtest.h" |
| 23 | #include "ui/base/l10n/l10n_util.h" |
| 24 | |
| 25 | namespace extensions { |
| 26 | |
| 27 | namespace { |
| 28 | |
| 29 | // Whether this build supports the window.shape requirement. |
| 30 | const bool kSupportsWindowShape = |
| 31 | #if defined(USE_AURA) |
| 32 | true; |
| 33 | #else |
| 34 | false; |
| 35 | #endif |
| 36 | |
michaelpg | a8ea037 | 2017-04-06 20:41:35 | [diff] [blame] | 37 | // Returns true if a WebGL check might not fail immediately. |
| 38 | bool MightSupportWebGL() { |
| 39 | return content::GpuDataManager::GetInstance()->GpuAccessAllowed(nullptr); |
| 40 | } |
| 41 | |
| 42 | const char kFeaturesKey[] = "requirements.3D.features"; |
| 43 | const char kFeatureWebGL[] = "webgl"; |
| 44 | const char kFeatureCSS3d[] = "css3d"; |
| 45 | |
| 46 | } // namespace |
| 47 | |
| 48 | class RequirementsCheckerTest : public ExtensionsTest { |
| 49 | public: |
Lukasz Anforowicz | 58d0dac | 2018-03-23 15:48:10 | [diff] [blame] | 50 | RequirementsCheckerTest() { |
Jeremy Roman | 16529d0e | 2017-08-24 18:13:47 | [diff] [blame] | 51 | manifest_dict_ = std::make_unique<base::DictionaryValue>(); |
michaelpg | a8ea037 | 2017-04-06 20:41:35 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | ~RequirementsCheckerTest() override {} |
| 55 | |
| 56 | void CreateExtension() { |
| 57 | manifest_dict_->SetString("name", "dummy name"); |
| 58 | manifest_dict_->SetString("version", "1"); |
Devlin Cronin | 1fa235b6 | 2018-04-12 14:04:07 | [diff] [blame] | 59 | manifest_dict_->SetInteger("manifest_version", 2); |
michaelpg | a8ea037 | 2017-04-06 20:41:35 | [diff] [blame] | 60 | |
| 61 | std::string error; |
| 62 | extension_ = |
| 63 | Extension::Create(base::FilePath(), Manifest::UNPACKED, *manifest_dict_, |
| 64 | Extension::NO_FLAGS, &error); |
| 65 | ASSERT_TRUE(extension_.get()) << error; |
| 66 | } |
| 67 | |
| 68 | protected: |
| 69 | void StartChecker() { |
Jeremy Roman | 16529d0e | 2017-08-24 18:13:47 | [diff] [blame] | 70 | checker_ = std::make_unique<RequirementsChecker>(extension_); |
michaelpg | a8ea037 | 2017-04-06 20:41:35 | [diff] [blame] | 71 | // TODO(michaelpg): This should normally not have to be async. Use Run() |
| 72 | // instead of RunUntilComplete() after crbug.com/708354 is addressed. |
| 73 | runner_.RunUntilComplete(checker_.get()); |
| 74 | } |
| 75 | |
| 76 | void RequireWindowShape() { |
| 77 | manifest_dict_->SetBoolean("requirements.window.shape", true); |
| 78 | } |
| 79 | |
michaelpg | a8ea037 | 2017-04-06 20:41:35 | [diff] [blame] | 80 | void RequireFeature(const char feature[]) { |
| 81 | if (!manifest_dict_->HasKey(kFeaturesKey)) |
Jeremy Roman | 16529d0e | 2017-08-24 18:13:47 | [diff] [blame] | 82 | manifest_dict_->Set(kFeaturesKey, std::make_unique<base::ListValue>()); |
michaelpg | a8ea037 | 2017-04-06 20:41:35 | [diff] [blame] | 83 | base::ListValue* features_list = nullptr; |
| 84 | ASSERT_TRUE(manifest_dict_->GetList(kFeaturesKey, &features_list)); |
| 85 | features_list->AppendString(feature); |
| 86 | } |
| 87 | |
| 88 | std::unique_ptr<RequirementsChecker> checker_; |
| 89 | PreloadCheckRunner runner_; |
| 90 | |
| 91 | private: |
michaelpg | a8ea037 | 2017-04-06 20:41:35 | [diff] [blame] | 92 | scoped_refptr<Extension> extension_; |
| 93 | std::unique_ptr<base::DictionaryValue> manifest_dict_; |
| 94 | }; |
| 95 | |
| 96 | // Tests no requirements. |
| 97 | TEST_F(RequirementsCheckerTest, RequirementsEmpty) { |
| 98 | CreateExtension(); |
| 99 | StartChecker(); |
| 100 | EXPECT_TRUE(runner_.called()); |
| 101 | EXPECT_EQ(0u, runner_.errors().size()); |
| 102 | EXPECT_TRUE(checker_->GetErrorMessage().empty()); |
| 103 | } |
| 104 | |
| 105 | // Tests fulfilled requirements. |
| 106 | TEST_F(RequirementsCheckerTest, RequirementsSuccess) { |
| 107 | if (kSupportsWindowShape) |
| 108 | RequireWindowShape(); |
Karan Bhatia | c8954af | 2017-12-01 23:39:07 | [diff] [blame] | 109 | |
michaelpg | a8ea037 | 2017-04-06 20:41:35 | [diff] [blame] | 110 | RequireFeature(kFeatureCSS3d); |
| 111 | |
| 112 | CreateExtension(); |
| 113 | StartChecker(); |
| 114 | EXPECT_TRUE(runner_.called()); |
| 115 | EXPECT_EQ(0u, runner_.errors().size()); |
| 116 | EXPECT_TRUE(checker_->GetErrorMessage().empty()); |
| 117 | } |
| 118 | |
| 119 | // Tests multiple requirements failing (on some builds). |
| 120 | TEST_F(RequirementsCheckerTest, RequirementsFailMultiple) { |
| 121 | size_t expected_errors = 0u; |
| 122 | if (!kSupportsWindowShape) { |
| 123 | RequireWindowShape(); |
| 124 | expected_errors++; |
| 125 | } |
michaelpg | a8ea037 | 2017-04-06 20:41:35 | [diff] [blame] | 126 | if (!MightSupportWebGL()) { |
| 127 | RequireFeature(kFeatureWebGL); |
| 128 | expected_errors++; |
| 129 | } |
| 130 | // css3d should always succeed. |
| 131 | RequireFeature(kFeatureCSS3d); |
| 132 | |
| 133 | CreateExtension(); |
| 134 | StartChecker(); |
| 135 | EXPECT_TRUE(runner_.called()); |
| 136 | EXPECT_EQ(expected_errors, runner_.errors().size()); |
| 137 | EXPECT_EQ(expected_errors == 0, checker_->GetErrorMessage().empty()); |
| 138 | } |
| 139 | |
| 140 | // Tests a requirement that might fail asynchronously. |
| 141 | TEST_F(RequirementsCheckerTest, RequirementsFailWebGL) { |
| 142 | content::GpuDataManager::GetInstance()->BlacklistWebGLForTesting(); |
| 143 | RequireFeature(kFeatureWebGL); |
| 144 | CreateExtension(); |
| 145 | StartChecker(); |
| 146 | |
| 147 | // TODO(michaelpg): Check that the runner actually finishes, which requires |
| 148 | // waiting for the GPU check to succeed: crbug.com/706204. |
michaelpg | a8ea037 | 2017-04-06 20:41:35 | [diff] [blame] | 149 | if (runner_.errors().size()) { |
| 150 | EXPECT_THAT(runner_.errors(), testing::UnorderedElementsAre( |
| 151 | PreloadCheck::WEBGL_NOT_SUPPORTED)); |
| 152 | EXPECT_FALSE(checker_->GetErrorMessage().empty()); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | } // namespace extensions |