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.
5 #include "net/proxy/load_state_change_coalescer.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/run_loop.h"
12 #include "testing/gtest/include/gtest/gtest.h"
16 class LoadStateChangeCoalescerTest
: public testing::Test
{
18 void SetUp() override
{
19 coalescer_
.reset(new LoadStateChangeCoalescer(
20 base::Bind(&LoadStateChangeCoalescerTest::LoadStateChanged
,
21 base::Unretained(this)),
22 base::TimeDelta(), LOAD_STATE_IDLE
));
25 void TearDown() override
{ coalescer_
.reset(); }
27 void LoadStateChanged(LoadState load_state
) {
28 load_state_changes_
.push_back(load_state
);
31 void WaitUntilIdle() { base::RunLoop().RunUntilIdle(); }
33 scoped_ptr
<LoadStateChangeCoalescer
> coalescer_
;
34 std::vector
<LoadState
> load_state_changes_
;
37 TEST_F(LoadStateChangeCoalescerTest
, SingleChange
) {
38 coalescer_
->LoadStateChanged(LOAD_STATE_RESOLVING_PROXY_FOR_URL
);
40 ASSERT_EQ(1u, load_state_changes_
.size());
41 EXPECT_EQ(LOAD_STATE_RESOLVING_PROXY_FOR_URL
, load_state_changes_
[0]);
44 TEST_F(LoadStateChangeCoalescerTest
, TwoChangesCoalesce
) {
45 coalescer_
->LoadStateChanged(LOAD_STATE_RESOLVING_PROXY_FOR_URL
);
46 coalescer_
->LoadStateChanged(LOAD_STATE_RESOLVING_HOST_IN_PROXY_SCRIPT
);
48 ASSERT_EQ(1u, load_state_changes_
.size());
49 EXPECT_EQ(LOAD_STATE_RESOLVING_HOST_IN_PROXY_SCRIPT
, load_state_changes_
[0]);
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
);
57 ASSERT_EQ(1u, load_state_changes_
.size());
58 EXPECT_EQ(LOAD_STATE_DOWNLOADING_PROXY_SCRIPT
, load_state_changes_
[0]);
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
);
66 EXPECT_TRUE(load_state_changes_
.empty());
69 TEST_F(LoadStateChangeCoalescerTest
, AlternateLoadStatesWithWait
) {
70 coalescer_
->LoadStateChanged(LOAD_STATE_RESOLVING_HOST_IN_PROXY_SCRIPT
);
71 coalescer_
->LoadStateChanged(LOAD_STATE_RESOLVING_PROXY_FOR_URL
);
73 ASSERT_EQ(1u, load_state_changes_
.size());
74 EXPECT_EQ(LOAD_STATE_RESOLVING_PROXY_FOR_URL
, load_state_changes_
[0]);
76 coalescer_
->LoadStateChanged(LOAD_STATE_RESOLVING_HOST_IN_PROXY_SCRIPT
);
77 coalescer_
->LoadStateChanged(LOAD_STATE_IDLE
);
79 ASSERT_EQ(2u, load_state_changes_
.size());
80 EXPECT_EQ(LOAD_STATE_IDLE
, load_state_changes_
[1]);