OLD | NEW |
| (Empty) |
1 // Copyright 2015 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 "net/proxy/load_state_change_coalescer.h" | |
6 | |
7 #include <vector> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/message_loop/message_loop.h" | |
11 #include "base/run_loop.h" | |
12 #include "testing/gtest/include/gtest/gtest.h" | |
13 | |
14 namespace net { | |
15 | |
16 class LoadStateChangeCoalescerTest : public testing::Test { | |
17 protected: | |
18 void SetUp() override { | |
19 coalescer_.reset(new LoadStateChangeCoalescer( | |
20 base::Bind(&LoadStateChangeCoalescerTest::LoadStateChanged, | |
21 base::Unretained(this)), | |
22 base::TimeDelta(), LOAD_STATE_IDLE)); | |
23 } | |
24 | |
25 void TearDown() override { coalescer_.reset(); } | |
26 | |
27 void LoadStateChanged(LoadState load_state) { | |
28 load_state_changes_.push_back(load_state); | |
29 } | |
30 | |
31 void WaitUntilIdle() { base::RunLoop().RunUntilIdle(); } | |
32 | |
33 scoped_ptr<LoadStateChangeCoalescer> coalescer_; | |
34 std::vector<LoadState> load_state_changes_; | |
35 }; | |
36 | |
37 TEST_F(LoadStateChangeCoalescerTest, SingleChange) { | |
38 coalescer_->LoadStateChanged(LOAD_STATE_RESOLVING_PROXY_FOR_URL); | |
39 WaitUntilIdle(); | |
40 ASSERT_EQ(1u, load_state_changes_.size()); | |
41 EXPECT_EQ(LOAD_STATE_RESOLVING_PROXY_FOR_URL, load_state_changes_[0]); | |
42 } | |
43 | |
44 TEST_F(LoadStateChangeCoalescerTest, TwoChangesCoalesce) { | |
45 coalescer_->LoadStateChanged(LOAD_STATE_RESOLVING_PROXY_FOR_URL); | |
46 coalescer_->LoadStateChanged(LOAD_STATE_RESOLVING_HOST_IN_PROXY_SCRIPT); | |
47 WaitUntilIdle(); | |
48 ASSERT_EQ(1u, load_state_changes_.size()); | |
49 EXPECT_EQ(LOAD_STATE_RESOLVING_HOST_IN_PROXY_SCRIPT, load_state_changes_[0]); | |
50 } | |
51 | |
52 TEST_F(LoadStateChangeCoalescerTest, ThreeChangesCoalesce) { | |
53 coalescer_->LoadStateChanged(LOAD_STATE_RESOLVING_PROXY_FOR_URL); | |
54 coalescer_->LoadStateChanged(LOAD_STATE_RESOLVING_HOST_IN_PROXY_SCRIPT); | |
55 coalescer_->LoadStateChanged(LOAD_STATE_DOWNLOADING_PROXY_SCRIPT); | |
56 WaitUntilIdle(); | |
57 ASSERT_EQ(1u, load_state_changes_.size()); | |
58 EXPECT_EQ(LOAD_STATE_DOWNLOADING_PROXY_SCRIPT, load_state_changes_[0]); | |
59 } | |
60 | |
61 TEST_F(LoadStateChangeCoalescerTest, CoalesceToOriginalLoadState) { | |
62 coalescer_->LoadStateChanged(LOAD_STATE_RESOLVING_PROXY_FOR_URL); | |
63 coalescer_->LoadStateChanged(LOAD_STATE_RESOLVING_HOST_IN_PROXY_SCRIPT); | |
64 coalescer_->LoadStateChanged(LOAD_STATE_IDLE); | |
65 WaitUntilIdle(); | |
66 EXPECT_TRUE(load_state_changes_.empty()); | |
67 } | |
68 | |
69 TEST_F(LoadStateChangeCoalescerTest, AlternateLoadStatesWithWait) { | |
70 coalescer_->LoadStateChanged(LOAD_STATE_RESOLVING_HOST_IN_PROXY_SCRIPT); | |
71 coalescer_->LoadStateChanged(LOAD_STATE_RESOLVING_PROXY_FOR_URL); | |
72 WaitUntilIdle(); | |
73 ASSERT_EQ(1u, load_state_changes_.size()); | |
74 EXPECT_EQ(LOAD_STATE_RESOLVING_PROXY_FOR_URL, load_state_changes_[0]); | |
75 | |
76 coalescer_->LoadStateChanged(LOAD_STATE_RESOLVING_HOST_IN_PROXY_SCRIPT); | |
77 coalescer_->LoadStateChanged(LOAD_STATE_IDLE); | |
78 WaitUntilIdle(); | |
79 ASSERT_EQ(2u, load_state_changes_.size()); | |
80 EXPECT_EQ(LOAD_STATE_IDLE, load_state_changes_[1]); | |
81 } | |
82 | |
83 } // namespace net | |
OLD | NEW |