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

Side by Side Diff: extensions/browser/preload_check_group_unittest.cc

Issue 2783143005: Replace ExtensionInstallChecker with generic PreloadCheckGroup (Closed)
Patch Set: fix debug build 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
« no previous file with comments | « extensions/browser/preload_check_group.cc ('k') | extensions/browser/preload_check_test_util.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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(PreloadCheck::Errors());
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(PreloadCheck::Errors());
72 AddAsyncCheck(PreloadCheck::Errors());
73 }
74
75 runner_.RunUntilComplete(check_group_.get());
76 ExpectStarted();
77 EXPECT_EQ(0u, runner_.errors().size());
78 }
79
80 // Tests failing checks.
81 TEST_F(PreloadCheckGroupTest, Fail) {
82 AddCheck(PreloadCheck::Errors());
83 AddAsyncCheck({kDummyError1, kDummyError2});
84 AddCheck({kDummyError3});
85 runner_.Run(check_group_.get());
86
87 ExpectStarted();
88 EXPECT_FALSE(runner_.called());
89
90 // The runner is called with all errors.
91 runner_.WaitForComplete();
92 EXPECT_EQ(3u, runner_.errors().size());
93 }
94
95 // Tests failing synchronous checks with stop_on_first_error.
96 TEST_F(PreloadCheckGroupTest, FailFast) {
97 check_group_->set_stop_on_first_error(true);
98
99 AddCheck({kDummyError1, kDummyError2});
100 AddCheck({kDummyError3});
101 runner_.Run(check_group_.get());
102
103 // After the first check fails, the remaining checks should not be started.
104 EXPECT_TRUE(runner_.called());
105 EXPECT_TRUE(checks_[0]->started());
106 EXPECT_FALSE(checks_[1]->started());
107 EXPECT_THAT(runner_.errors(),
108 testing::UnorderedElementsAre(kDummyError1, kDummyError2));
109 }
110
111 // Tests failing asynchronous checks with stop_on_first_error.
112 TEST_F(PreloadCheckGroupTest, FailFastAsync) {
113 check_group_->set_stop_on_first_error(true);
114
115 AddCheck(PreloadCheck::Errors());
116 AddAsyncCheck(PreloadCheck::Errors());
117 AddAsyncCheck({kDummyError1});
118 AddAsyncCheck({kDummyError2});
119 runner_.Run(check_group_.get());
120
121 // All checks were started, because the sync check passes.
122 ExpectStarted();
123 EXPECT_FALSE(runner_.called());
124 runner_.WaitForComplete();
125
126 // The first async check should have failed, triggering fail fast. The
127 // second async check's failure should be ignored.
128 EXPECT_THAT(runner_.errors(), testing::UnorderedElementsAre(kDummyError1));
129 }
130
131 // Tests we don't crash when the PreloadCheckGroup is destroyed prematurely.
132 TEST_F(PreloadCheckGroupTest, DestroyPreloadCheckGroup) {
133 AddAsyncCheck({kDummyError1});
134 AddAsyncCheck({kDummyError2});
135 runner_.Run(check_group_.get());
136
137 check_group_.reset();
138
139 // Checks should have been started, but the runner is never called.
140 ExpectStarted();
141 runner_.WaitForIdle();
142 EXPECT_FALSE(runner_.called());
143 }
144
145 } // namespace extensions
OLDNEW
« no previous file with comments | « extensions/browser/preload_check_group.cc ('k') | extensions/browser/preload_check_test_util.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698