File: preload_check_group_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (148 lines) | stat: -rw-r--r-- 4,534 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "extensions/browser/preload_check_group.h"

#include <memory>
#include <vector>

#include "content/public/test/browser_task_environment.h"
#include "extensions/browser/preload_check_test_util.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace extensions {

namespace {
PreloadCheck::Error kDummyError1 = PreloadCheck::Error::kDisallowedByPolicy;
PreloadCheck::Error kDummyError2 = PreloadCheck::Error::kBlocklistedId;
PreloadCheck::Error kDummyError3 = PreloadCheck::Error::kBlocklistedUnknown;
}

class PreloadCheckGroupTest : public testing::Test {
 public:
  PreloadCheckGroupTest()
      : check_group_(std::make_unique<PreloadCheckGroup>()) {}
  ~PreloadCheckGroupTest() override {}

 protected:
  // Adds a check to |check_group_|, storing its unique_ptr in |checks_|.
  void AddCheck(PreloadCheck::Errors errors, bool is_async = false) {
    auto check_stub = std::make_unique<PreloadCheckStub>(errors);
    check_stub->set_is_async(is_async);
    check_group_->AddCheck(check_stub.get());
    checks_.push_back(std::move(check_stub));
  }

  // Convenience method for add an async check.
  void AddAsyncCheck(PreloadCheck::Errors errors) {
    AddCheck(errors, /*is_async=*/true);
  }

  // Verifies that all checks have started.
  void ExpectStarted() {
    for (const auto& check : checks_)
      EXPECT_TRUE(check->started());
  }

  PreloadCheckRunner runner_;
  std::vector<std::unique_ptr<PreloadCheckStub>> checks_;
  std::unique_ptr<PreloadCheckGroup> check_group_;

 private:
  // Required for the asynchronous tests.
  content::BrowserTaskEnvironment task_environment_;
};

// Tests multiple succeeding checks.
TEST_F(PreloadCheckGroupTest, Succeed) {
  for (int i = 0; i < 3; i++)
    AddCheck(PreloadCheck::Errors());
  runner_.Run(check_group_.get());

  ExpectStarted();
  EXPECT_EQ(0u, runner_.errors().size());
}

// Tests multiple succeeding sync and async checks.
TEST_F(PreloadCheckGroupTest, SucceedAsync) {
  for (int i = 0; i < 2; i++) {
    AddCheck(PreloadCheck::Errors());
    AddAsyncCheck(PreloadCheck::Errors());
  }

  runner_.RunUntilComplete(check_group_.get());
  ExpectStarted();
  EXPECT_EQ(0u, runner_.errors().size());
}

// Tests failing checks.
TEST_F(PreloadCheckGroupTest, Fail) {
  AddCheck(PreloadCheck::Errors());
  AddAsyncCheck({kDummyError1, kDummyError2});
  AddCheck({kDummyError3});
  runner_.Run(check_group_.get());

  ExpectStarted();
  EXPECT_FALSE(runner_.called());

  // The runner is called with all errors.
  runner_.WaitForComplete();
  EXPECT_EQ(3u, runner_.errors().size());
}

// Tests failing synchronous checks with stop_on_first_error.
TEST_F(PreloadCheckGroupTest, FailFast) {
  check_group_->set_stop_on_first_error(true);

  AddCheck({kDummyError1, kDummyError2});
  AddCheck({kDummyError3});
  runner_.Run(check_group_.get());

  // After the first check fails, the remaining checks should not be started.
  EXPECT_TRUE(checks_[0]->started());
  EXPECT_FALSE(checks_[1]->started());

  // The callback of PreloadCheckGroup is called aynchronously.
  runner_.WaitForComplete();
  EXPECT_TRUE(runner_.called());
  EXPECT_THAT(runner_.errors(),
              testing::UnorderedElementsAre(kDummyError1, kDummyError2));
}

// Tests failing asynchronous checks with stop_on_first_error.
TEST_F(PreloadCheckGroupTest, FailFastAsync) {
  check_group_->set_stop_on_first_error(true);

  AddCheck(PreloadCheck::Errors());
  AddAsyncCheck(PreloadCheck::Errors());
  AddAsyncCheck({kDummyError1});
  AddAsyncCheck({kDummyError2});
  runner_.Run(check_group_.get());

  // All checks were started, because the sync check passes.
  ExpectStarted();
  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) {
  AddAsyncCheck({kDummyError1});
  AddAsyncCheck({kDummyError2});
  runner_.Run(check_group_.get());

  check_group_.reset();

  // Checks should have been started, but the runner is never called.
  ExpectStarted();
  runner_.WaitForIdle();
  EXPECT_FALSE(runner_.called());
}

}  // namespace extensions