Index: extensions/browser/preload_check_group_unittest.cc |
diff --git a/extensions/browser/preload_check_group_unittest.cc b/extensions/browser/preload_check_group_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..9a30e2d8ae37d5c881c8fa0d840c676c773e6972 |
--- /dev/null |
+++ b/extensions/browser/preload_check_group_unittest.cc |
@@ -0,0 +1,209 @@ |
+// Copyright 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include <memory> |
+#include <string> |
+#include <vector> |
+ |
+#include "base/memory/ptr_util.h" |
+#include "base/memory/ref_counted.h" |
+#include "base/message_loop/message_loop.h" |
+#include "extensions/browser/preload_check.h" |
+#include "extensions/browser/preload_check_group.h" |
+#include "extensions/browser/preload_check_test_util.h" |
+#include "extensions/common/extension.h" |
+#include "testing/gmock/include/gmock/gmock.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace extensions { |
+ |
+namespace { |
+ |
+using PreloadCheckStubs = std::vector<std::unique_ptr<PreloadCheckStub>>; |
+ |
+// const char kDummyErrorMessage1[] = "Dummy error 1 ocurred"; |
+// const char kDummyErrorMessage2[] = "Dummy error 2 ocurred"; |
+ |
+PreloadCheck::Error kDummyError1 = PreloadCheck::DISALLOWED_BY_POLICY; |
+PreloadCheck::Error kDummyError2 = PreloadCheck::BLACKLISTED_ID; |
+PreloadCheck::Error kDummyError3 = PreloadCheck::BLACKLISTED_UNKNOWN; |
+ |
+std::vector<PreloadCheck*> GetRawPointers(PreloadCheckStubs* checks) { |
+ std::vector<PreloadCheck*> check_ptrs; |
+ for (auto& check : *checks) |
+ check_ptrs.push_back(check.get()); |
+ return check_ptrs; |
+} |
+ |
+} // namespace |
+ |
+class PreloadCheckGroupTest : public testing::Test { |
+ public: |
+ PreloadCheckGroupTest() {} |
+ ~PreloadCheckGroupTest() override {} |
+ |
+ void SetUp() override { |
+ base::DictionaryValue manifest_dict; |
+ manifest_dict.SetString("name", "dummy name"); |
+ manifest_dict.SetString("version", "1"); |
+ std::string error; |
+ |
+ extension_ = |
+ Extension::Create(base::FilePath(), extensions::Manifest::UNPACKED, |
+ manifest_dict, Extension::NO_FLAGS, &error); |
+ EXPECT_TRUE(extension_.get()) << error; |
+ } |
+ |
+ // was 216 lines, including these comments |
+ // now 209 lines, including these comments |
+ std::unique_ptr<PreloadCheckStub> MakeCheck( |
+ bool is_async = false, |
+ PreloadCheck::Error error1 = PreloadCheck::NONE, |
+ PreloadCheck::Error error2 = PreloadCheck::NONE) { |
+ PreloadCheck::Errors errors; |
+ if (error1 != PreloadCheck::NONE) |
+ errors.insert(error1); |
+ if (error2 != PreloadCheck::NONE) |
+ errors.insert(error2); |
+ return base::MakeUnique<PreloadCheckStub>(is_async, errors); |
+ } |
+ |
+ protected: |
+ // Verifies that all checks have started. |
+ void ExpectStarted(const PreloadCheckStubs& checks) { |
+ for (const auto& check : checks) |
+ EXPECT_TRUE(check->started()); |
+ } |
+ |
+ scoped_refptr<Extension> extension_; |
+ |
+ protected: |
+ PreloadCheckRunner runner_; |
+ |
+ private: |
+ // A message loop is required for the asynchronous tests. |
+ base::MessageLoop message_loop_; |
+}; |
+ |
+// Tests multiple succeeding checks. |
+TEST_F(PreloadCheckGroupTest, Succeed) { |
+ PreloadCheckStubs checks; |
+ for (int i = 0; i < 3; i++) |
+ checks.emplace_back(MakeCheck()); |
+ |
+ PreloadCheckGroup check_group(extension_, GetRawPointers(&checks), |
+ /*fail_fast=*/false); |
+ runner_.Run(&check_group); |
+ ExpectStarted(checks); |
+ EXPECT_EQ(0u, runner_.errors().size()); |
+} |
+ |
+// Tests multiple succeeding sync and async checks. |
+TEST_F(PreloadCheckGroupTest, SucceedAsync) { |
+ PreloadCheckStubs checks; |
+ for (int i = 0; i < 2; i++) { |
+ checks.emplace_back(MakeCheck(/*is_async=*/false)); |
+ checks.emplace_back(MakeCheck(/*is_async=*/true)); |
+ } |
+ |
+ PreloadCheckGroup check_group(extension_, GetRawPointers(&checks), |
+ /*fail_fast=*/false); |
+ runner_.RunUntilComplete(&check_group); |
+ ExpectStarted(checks); |
+ EXPECT_EQ(0u, runner_.errors().size()); |
+} |
+ |
+// Tests failing checks without fail_fast. |
+TEST_F(PreloadCheckGroupTest, Fail) { |
+ PreloadCheckStubs checks; |
+ checks.emplace_back(MakeCheck()); |
+ // Add an async check that fails with 2 errors. |
+ checks.emplace_back(MakeCheck(/*is_async=*/true, kDummyError1, kDummyError2)); |
+ // Add an sync check that fails with one error. |
+ checks.emplace_back(MakeCheck(/*is_async=*/false, kDummyError3)); |
+ |
+ PreloadCheckGroup check_group(extension_, GetRawPointers(&checks), |
+ /*fail_fast=*/false); |
+ runner_.Run(&check_group); |
+ ExpectStarted(checks); |
+ EXPECT_FALSE(runner_.called()); |
+ |
+ // The runner is called with all errors. |
+ runner_.WaitForComplete(); |
+ EXPECT_TRUE(runner_.called()); |
+ EXPECT_EQ(3u, runner_.errors().size()); |
+} |
+ |
+// Tests failing synchronous checks with fail_fast. |
+TEST_F(PreloadCheckGroupTest, FailFast) { |
+ PreloadCheckStubs checks; |
+ |
+ // Add a sync check that immediately fails with 2 errors. |
+ checks.emplace_back( |
+ MakeCheck(/*is_async=*/false, kDummyError1, kDummyError2)); |
+ // Add a sync check that would also fail, except it shouldn't be run. |
+ checks.emplace_back(MakeCheck(/*is_async=*/false, kDummyError3)); |
+ |
+ PreloadCheckGroup check_group(extension_, GetRawPointers(&checks), |
+ /*fail_fast=*/true); |
+ runner_.Run(&check_group); |
+ |
+ // After the first check fails, the remaining checks should not be started. |
+ EXPECT_TRUE(runner_.called()); |
+ EXPECT_TRUE(checks[0]->started()); |
+ EXPECT_FALSE(checks[1]->started()); |
+ |
+ // Only the errors from the first failure should have been counted. |
+ EXPECT_THAT(runner_.errors(), |
+ testing::UnorderedElementsAre(kDummyError1, kDummyError2)); |
+ |
+ // Nothing should change after running the message loop. |
+ runner_.WaitForIdle(); |
+ EXPECT_FALSE(checks[1]->started()); |
+} |
+ |
+// Tests failing asynchronous checks with fail_fast. |
+TEST_F(PreloadCheckGroupTest, FailFastAsync) { |
+ PreloadCheckStubs checks; |
+ |
+ // Add a succeeding sync check. |
+ checks.emplace_back(MakeCheck(/*is_asnc=*/false)); |
+ |
+ // Add failing async checks. |
+ checks.emplace_back(MakeCheck(/*is_asnc=*/true, kDummyError1)); |
+ checks.emplace_back(MakeCheck(/*is_asnc=*/true, kDummyError2)); |
+ |
+ PreloadCheckGroup check_group(extension_, GetRawPointers(&checks), |
+ /*fail_fast=*/true); |
+ runner_.Run(&check_group); |
+ |
+ // All checks were started, because the sync checks pass. |
+ ExpectStarted(checks); |
+ EXPECT_FALSE(runner_.called()); |
+ |
+ runner_.WaitForComplete(); |
+ |
+ // The first async check should have failed, triggering fail fast. The |
+ // second async check's failure should be ignored. |
+ EXPECT_THAT(runner_.errors(), testing::UnorderedElementsAre(kDummyError1)); |
+} |
+ |
+// Tests we don't crash when the PreloadCheckGroup is destroyed prematurely. |
+TEST_F(PreloadCheckGroupTest, DestroyPreloadCheckGroup) { |
+ PreloadCheckStubs checks; |
+ checks.emplace_back(MakeCheck(/*is_asnc=*/true, kDummyError1)); |
+ |
+ { |
+ PreloadCheckGroup check_group(extension_, GetRawPointers(&checks), |
+ /*fail_fast=*/true); |
+ runner_.Run(&check_group); |
+ } |
+ |
+ // Checks should have been started, but the runner is never called. |
+ ExpectStarted(checks); |
+ runner_.WaitForIdle(); |
+ EXPECT_FALSE(runner_.called()); |
+} |
+ |
+} // namespace extensions |