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

Side by Side Diff: chrome/browser/extensions/extension_install_checker_unittest.cc

Issue 2783813002: Move ChromeRequirementsChecker to //extensions as a PreloadCheck (Closed)
Patch Set: 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
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/extensions/extension_install_checker.h" 5 #include "chrome/browser/extensions/extension_install_checker.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/location.h" 8 #include "base/location.h"
9 #include "base/memory/ptr_util.h" 9 #include "base/memory/ptr_util.h"
10 #include "base/run_loop.h" 10 #include "base/run_loop.h"
(...skipping 16 matching lines...) Expand all
27 // Stubs most of the checks since we are interested in validating the logic in 27 // Stubs most of the checks since we are interested in validating the logic in
28 // the install checker. This class implements a synchronous version of all 28 // the install checker. This class implements a synchronous version of all
29 // checks. 29 // checks.
30 class ExtensionInstallCheckerForTest : public ExtensionInstallChecker { 30 class ExtensionInstallCheckerForTest : public ExtensionInstallChecker {
31 public: 31 public:
32 ExtensionInstallCheckerForTest(int enabled_checks, bool fail_fast) 32 ExtensionInstallCheckerForTest(int enabled_checks, bool fail_fast)
33 : ExtensionInstallChecker(nullptr, nullptr, enabled_checks, fail_fast) {} 33 : ExtensionInstallChecker(nullptr, nullptr, enabled_checks, fail_fast) {}
34 34
35 ~ExtensionInstallCheckerForTest() override {} 35 ~ExtensionInstallCheckerForTest() override {}
36 36
37 void set_requirements_error(const std::string& error) {
38 requirements_error_ = error;
39 }
40
41 bool is_async() const { return is_async_; } 37 bool is_async() const { return is_async_; }
42 void set_is_async(bool is_async) { is_async_ = is_async; } 38 void set_is_async(bool is_async) { is_async_ = is_async; }
43 39
44 protected:
45 void MockCheckRequirements() {
46 if (!is_running())
47 return;
48 std::vector<std::string> errors;
49 if (!requirements_error_.empty())
50 errors.push_back(requirements_error_);
51 OnRequirementsCheckDone(errors);
52 }
53
54 void CheckRequirements() override {
55 if (is_async_) {
56 base::ThreadTaskRunnerHandle::Get()->PostTask(
57 FROM_HERE,
58 base::Bind(&ExtensionInstallCheckerForTest::MockCheckRequirements,
59 base::Unretained(this)));
60 } else {
61 MockCheckRequirements();
62 }
63 }
64
65 private: 40 private:
66 // Whether to run the requirements and blacklist checks asynchronously, as 41 // Whether to run the requirements and blacklist checks asynchronously, as
67 // they often do in ExtensionInstallChecker. 42 // they often do in ExtensionInstallChecker.
68 bool is_async_ = false; 43 bool is_async_ = false;
69
70 // Dummy error for testing.
71 std::string requirements_error_;
72 }; 44 };
73 45
74 class CheckObserver { 46 class CheckObserver {
75 public: 47 public:
76 CheckObserver() : result_(0), call_count_(0) {} 48 CheckObserver() : result_(0), call_count_(0) {}
77 49
78 int result() const { return result_; } 50 int result() const { return result_; }
79 int call_count() const { return call_count_; } 51 int call_count() const { return call_count_; }
80 52
81 void OnChecksComplete(int checks_failed) { 53 void OnChecksComplete(int checks_failed) {
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
114 std::string message) { 86 std::string message) {
115 // The policy check always runs synchronously. 87 // The policy check always runs synchronously.
116 auto policy_check = base::MakeUnique<PreloadCheckStub>(); 88 auto policy_check = base::MakeUnique<PreloadCheckStub>();
117 if (error != PreloadCheck::NONE) { 89 if (error != PreloadCheck::NONE) {
118 policy_check->AddError(error); 90 policy_check->AddError(error);
119 policy_check->set_error_message(base::UTF8ToUTF16(message)); 91 policy_check->set_error_message(base::UTF8ToUTF16(message));
120 } 92 }
121 checker->SetPolicyCheckForTesting(std::move(policy_check)); 93 checker->SetPolicyCheckForTesting(std::move(policy_check));
122 } 94 }
123 95
96 void SetRequirementsError(ExtensionInstallCheckerForTest* checker,
97 PreloadCheck::Error error,
98 const std::string& message) {
99 auto requirements_check = base::MakeUnique<PreloadCheckStub>();
100 requirements_check->set_is_async(checker->is_async());
101 if (error != PreloadCheck::NONE) {
102 requirements_check->AddError(error);
103 requirements_check->set_error_message(base::UTF8ToUTF16(message));
104 }
105 checker->SetRequirementsCheckForTesting(std::move(requirements_check));
106 }
107
124 protected: 108 protected:
125 void SetAllPass(ExtensionInstallCheckerForTest* checker) { 109 void SetAllPass(ExtensionInstallCheckerForTest* checker) {
126 SetBlacklistError(checker, PreloadCheck::NONE); 110 SetBlacklistError(checker, PreloadCheck::NONE);
127 SetPolicyError(checker, PreloadCheck::NONE, ""); 111 SetPolicyError(checker, PreloadCheck::NONE, "");
128 checker->set_requirements_error(""); 112 SetRequirementsError(checker, PreloadCheck::NONE, "");
129 } 113 }
130 114
131 void SetAllErrors(ExtensionInstallCheckerForTest* checker) { 115 void SetAllErrors(ExtensionInstallCheckerForTest* checker) {
132 SetBlacklistError(checker, kBlacklistError); 116 SetBlacklistError(checker, kBlacklistError);
133 SetPolicyError(checker, PreloadCheck::DISALLOWED_BY_POLICY, 117 SetPolicyError(checker, PreloadCheck::DISALLOWED_BY_POLICY,
134 kDummyPolicyError); 118 kDummyPolicyError);
135 checker->set_requirements_error(kDummyRequirementsError); 119 SetRequirementsError(checker, PreloadCheck::NPAPI_NOT_SUPPORTED,
120 kDummyRequirementsError);
136 } 121 }
137 122
138 void ExpectRequirementsPass(const ExtensionInstallCheckerForTest& checker) { 123 void ExpectRequirementsPass(const ExtensionInstallCheckerForTest& checker) {
139 EXPECT_TRUE(checker.requirement_errors().empty()); 124 EXPECT_EQ(base::string16(), checker.requirements_error_message());
140 } 125 }
141 126
142 void ExpectRequirementsError(const char* expected_error, 127 void ExpectRequirementsError(const char* expected_error,
143 const ExtensionInstallCheckerForTest& checker) { 128 const ExtensionInstallCheckerForTest& checker) {
144 EXPECT_FALSE(checker.requirement_errors().empty()); 129 EXPECT_EQ(base::UTF8ToUTF16(expected_error),
145 EXPECT_EQ(std::string(expected_error), 130 checker.requirements_error_message());
146 checker.requirement_errors().front());
147 } 131 }
148 132
149 void ExpectRequirementsError(const ExtensionInstallCheckerForTest& checker) { 133 void ExpectRequirementsError(const ExtensionInstallCheckerForTest& checker) {
150 ExpectRequirementsError(kDummyRequirementsError, checker); 134 ExpectRequirementsError(kDummyRequirementsError, checker);
151 } 135 }
152 136
153 void ExpectBlacklistPass(const ExtensionInstallCheckerForTest& checker) { 137 void ExpectBlacklistPass(const ExtensionInstallCheckerForTest& checker) {
154 EXPECT_EQ(PreloadCheck::NONE, checker.blacklist_error()); 138 EXPECT_EQ(PreloadCheck::NONE, checker.blacklist_error());
155 } 139 }
156 140
157 void ExpectBlacklistError(const ExtensionInstallCheckerForTest& checker) { 141 void ExpectBlacklistError(const ExtensionInstallCheckerForTest& checker) {
158 EXPECT_EQ(kBlacklistError, checker.blacklist_error()); 142 EXPECT_EQ(kBlacklistError, checker.blacklist_error());
159 } 143 }
160 144
161 void ExpectPolicyPass(const ExtensionInstallCheckerForTest& checker) { 145 void ExpectPolicyPass(const ExtensionInstallCheckerForTest& checker) {
162 EXPECT_TRUE(checker.policy_error().empty()); 146 EXPECT_TRUE(checker.policy_error().empty());
163 } 147 }
164 148
165 void ExpectPolicyError(const char* expected_error, 149 void ExpectPolicyError(const char* expected_error,
166 const ExtensionInstallCheckerForTest& checker) { 150 const ExtensionInstallCheckerForTest& checker) {
167 EXPECT_FALSE(checker.policy_error().empty()); 151 EXPECT_FALSE(checker.policy_error().empty());
168 EXPECT_EQ(std::string(expected_error), checker.policy_error()); 152 EXPECT_EQ(base::UTF8ToUTF16(expected_error), checker.policy_error());
169 } 153 }
170 154
171 void ExpectPolicyError(const ExtensionInstallCheckerForTest& checker) { 155 void ExpectPolicyError(const ExtensionInstallCheckerForTest& checker) {
172 ExpectPolicyError(kDummyPolicyError, checker); 156 ExpectPolicyError(kDummyPolicyError, checker);
173 } 157 }
174 158
175 void RunChecker(ExtensionInstallCheckerForTest* checker, 159 void RunChecker(ExtensionInstallCheckerForTest* checker,
176 int expected_result) { 160 int expected_result) {
177 CheckObserver observer; 161 CheckObserver observer;
178 checker->Start(base::Bind(&CheckObserver::OnChecksComplete, 162 checker->Start(base::Bind(&CheckObserver::OnChecksComplete,
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
316 300
317 RunChecker(&checker, 301 RunChecker(&checker,
318 ExtensionInstallChecker::CHECK_REQUIREMENTS); 302 ExtensionInstallChecker::CHECK_REQUIREMENTS);
319 303
320 ExpectRequirementsError(checker); 304 ExpectRequirementsError(checker);
321 ExpectPolicyPass(checker); 305 ExpectPolicyPass(checker);
322 ExpectBlacklistPass(checker); 306 ExpectBlacklistPass(checker);
323 } 307 }
324 308
325 } // namespace extensions 309 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/extension_install_checker.cc ('k') | chrome/browser/extensions/extension_service.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698