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 <memory> |
| 6 #include <vector> |
| 7 |
| 8 #include "base/memory/ptr_util.h" |
| 9 #include "base/message_loop/message_loop.h" |
| 10 #include "extensions/browser/preload_check_group.h" |
| 11 #include "extensions/browser/preload_check_test_util.h" |
| 12 #include "testing/gmock/include/gmock/gmock.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 |
| 15 namespace extensions { |
| 16 |
| 17 namespace { |
| 18 PreloadCheck::Error kDummyError1 = PreloadCheck::DISALLOWED_BY_POLICY; |
| 19 PreloadCheck::Error kDummyError2 = PreloadCheck::BLACKLISTED_ID; |
| 20 PreloadCheck::Error kDummyError3 = PreloadCheck::BLACKLISTED_UNKNOWN; |
| 21 } |
| 22 |
| 23 class PreloadCheckGroupTest : public testing::Test { |
| 24 public: |
| 25 PreloadCheckGroupTest() |
| 26 : check_group_(base::MakeUnique<PreloadCheckGroup>()) {} |
| 27 ~PreloadCheckGroupTest() override {} |
| 28 |
| 29 protected: |
| 30 // Adds a check to |check_group_|, storing its unique_ptr in |checks_|. |
| 31 void AddCheck(PreloadCheck::Errors errors = {}, bool is_async = false) { |
| 32 auto check_stub = base::MakeUnique<PreloadCheckStub>(errors); |
| 33 check_stub->set_is_async(is_async); |
| 34 check_group_->AddCheck(check_stub.get()); |
| 35 checks_.push_back(std::move(check_stub)); |
| 36 } |
| 37 |
| 38 // Convenience method for add an async check. |
| 39 void AddAsyncCheck(PreloadCheck::Errors errors = {}) { |
| 40 AddCheck(errors, /*is_async=*/true); |
| 41 } |
| 42 |
| 43 // Verifies that all checks have started. |
| 44 void ExpectStarted() { |
| 45 for (const auto& check : checks_) |
| 46 EXPECT_TRUE(check->started()); |
| 47 } |
| 48 |
| 49 PreloadCheckRunner runner_; |
| 50 std::vector<std::unique_ptr<PreloadCheckStub>> checks_; |
| 51 std::unique_ptr<PreloadCheckGroup> check_group_; |
| 52 |
| 53 private: |
| 54 // A message loop is required for the asynchronous tests. |
| 55 base::MessageLoop message_loop_; |
| 56 }; |
| 57 |
| 58 // Tests multiple succeeding checks. |
| 59 TEST_F(PreloadCheckGroupTest, Succeed) { |
| 60 for (int i = 0; i < 3; i++) |
| 61 AddCheck(); |
| 62 runner_.Run(check_group_.get()); |
| 63 |
| 64 ExpectStarted(); |
| 65 EXPECT_EQ(0u, runner_.errors().size()); |
| 66 } |
| 67 |
| 68 // Tests multiple succeeding sync and async checks. |
| 69 TEST_F(PreloadCheckGroupTest, SucceedAsync) { |
| 70 for (int i = 0; i < 2; i++) { |
| 71 AddCheck(); |
| 72 AddAsyncCheck(); |
| 73 } |
| 74 |
| 75 runner_.RunUntilComplete(check_group_.get()); |
| 76 |
| 77 ExpectStarted(); |
| 78 EXPECT_EQ(0u, runner_.errors().size()); |
| 79 } |
| 80 |
| 81 // Tests failing checks. |
| 82 TEST_F(PreloadCheckGroupTest, Fail) { |
| 83 AddCheck(); |
| 84 AddAsyncCheck({kDummyError1, kDummyError2}); |
| 85 AddCheck({kDummyError3}); |
| 86 runner_.Run(check_group_.get()); |
| 87 |
| 88 ExpectStarted(); |
| 89 EXPECT_FALSE(runner_.called()); |
| 90 |
| 91 // The runner is called with all errors. |
| 92 runner_.WaitForComplete(); |
| 93 EXPECT_TRUE(runner_.called()); |
| 94 EXPECT_EQ(3u, runner_.errors().size()); |
| 95 } |
| 96 |
| 97 // Tests failing synchronous checks with stop_on_first_error. |
| 98 TEST_F(PreloadCheckGroupTest, FailFast) { |
| 99 check_group_->set_stop_on_first_error(true); |
| 100 |
| 101 AddCheck({kDummyError1, kDummyError2}); |
| 102 AddCheck({kDummyError3}); |
| 103 runner_.Run(check_group_.get()); |
| 104 |
| 105 // After the first check fails, the remaining checks should not be started. |
| 106 EXPECT_TRUE(runner_.called()); |
| 107 EXPECT_TRUE(checks_[0]->started()); |
| 108 EXPECT_FALSE(checks_[1]->started()); |
| 109 EXPECT_THAT(runner_.errors(), |
| 110 testing::UnorderedElementsAre(kDummyError1, kDummyError2)); |
| 111 } |
| 112 |
| 113 // Tests failing asynchronous checks with stop_on_first_error. |
| 114 TEST_F(PreloadCheckGroupTest, FailFastAsync) { |
| 115 check_group_->set_stop_on_first_error(true); |
| 116 |
| 117 AddCheck(); |
| 118 AddAsyncCheck(); |
| 119 AddAsyncCheck({kDummyError1}); |
| 120 AddAsyncCheck({kDummyError2}); |
| 121 runner_.Run(check_group_.get()); |
| 122 |
| 123 // All checks were started, because the sync check passes. |
| 124 ExpectStarted(); |
| 125 EXPECT_FALSE(runner_.called()); |
| 126 runner_.WaitForComplete(); |
| 127 |
| 128 // The first async check should have failed, triggering fail fast. The |
| 129 // second async check's failure should be ignored. |
| 130 EXPECT_THAT(runner_.errors(), testing::UnorderedElementsAre(kDummyError1)); |
| 131 } |
| 132 |
| 133 // Tests we don't crash when the PreloadCheckGroup is destroyed prematurely. |
| 134 TEST_F(PreloadCheckGroupTest, DestroyPreloadCheckGroup) { |
| 135 AddAsyncCheck({kDummyError1}); |
| 136 AddAsyncCheck({kDummyError2}); |
| 137 runner_.Run(check_group_.get()); |
| 138 |
| 139 check_group_.reset(); |
| 140 |
| 141 // Checks should have been started, but the runner is never called. |
| 142 ExpectStarted(); |
| 143 runner_.WaitForIdle(); |
| 144 EXPECT_FALSE(runner_.called()); |
| 145 } |
| 146 |
| 147 } // namespace extensions |
OLD | NEW |