OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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 "ios/chrome/browser/web/auto_reload_controller.h" |
| 6 |
| 7 #include <memory> |
| 8 |
| 9 #include "base/mac/scoped_nsobject.h" |
| 10 #include "base/timer/mock_timer.h" |
| 11 #include "base/timer/timer.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 #include "testing/gtest_mac.h" |
| 14 |
| 15 @interface AutoReloadController (Testing) |
| 16 |
| 17 - (void)setTimerForTesting:(std::unique_ptr<base::Timer>)timer; |
| 18 |
| 19 @end |
| 20 |
| 21 @interface TestAutoReloadDelegate : NSObject<AutoReloadDelegate> |
| 22 @end |
| 23 |
| 24 @implementation TestAutoReloadDelegate { |
| 25 int reloads_; |
| 26 } |
| 27 |
| 28 - (void)reload { |
| 29 reloads_++; |
| 30 } |
| 31 |
| 32 - (int)reloads { |
| 33 return reloads_; |
| 34 } |
| 35 |
| 36 @end |
| 37 |
| 38 class AutoReloadControllerTest : public testing::Test { |
| 39 public: |
| 40 AutoReloadControllerTest() : timer_(new base::MockTimer(false, false)) {} |
| 41 |
| 42 protected: |
| 43 void SetUp() override { |
| 44 testing::Test::SetUp(); |
| 45 delegate_.reset([[TestAutoReloadDelegate alloc] init]); |
| 46 controller_.reset([[AutoReloadController alloc] |
| 47 initWithDelegate:delegate_.get() |
| 48 onlineStatus:YES]); |
| 49 // Note: even though setTimerForTesting theoretically passes ownership of |
| 50 // the timer to the controller, this class retains a weak pointer to the |
| 51 // timer so it can query or fire it for testing. The only reason this is |
| 52 // safe is that this class owns the controller which now owns the timer, so |
| 53 // the timer has the same lifetime as this class. |
| 54 [controller_ setTimerForTesting:std::unique_ptr<base::Timer>(timer_)]; |
| 55 } |
| 56 |
| 57 // Synthesize a failing page load. |
| 58 void DoFailingLoad(const GURL& url) { |
| 59 [controller_ loadStartedForURL:url]; |
| 60 [controller_ loadFailedForURL:url wasPost:NO]; |
| 61 } |
| 62 |
| 63 // Synthesize a succeeding page load. |
| 64 void DoSucceedingLoad(const GURL& url) { |
| 65 [controller_ loadStartedForURL:url]; |
| 66 [controller_ loadFinishedForURL:url wasPost:NO]; |
| 67 } |
| 68 |
| 69 base::scoped_nsobject<TestAutoReloadDelegate> delegate_; |
| 70 base::scoped_nsobject<AutoReloadController> controller_; |
| 71 base::MockTimer* timer_; // weak |
| 72 }; |
| 73 |
| 74 TEST_F(AutoReloadControllerTest, AutoReloadSucceeds) { |
| 75 const GURL kTestUrl("https://www.google.com"); |
| 76 DoFailingLoad(kTestUrl); |
| 77 EXPECT_TRUE(timer_->IsRunning()); |
| 78 EXPECT_EQ(0, [delegate_ reloads]); |
| 79 timer_->Fire(); |
| 80 EXPECT_EQ(1, [delegate_ reloads]); |
| 81 DoSucceedingLoad(kTestUrl); |
| 82 EXPECT_FALSE(timer_->IsRunning()); |
| 83 } |
| 84 |
| 85 TEST_F(AutoReloadControllerTest, AutoReloadRetries) { |
| 86 const GURL kTestUrl("https://www.google.com"); |
| 87 DoFailingLoad(kTestUrl); |
| 88 EXPECT_EQ(0, [delegate_ reloads]); |
| 89 timer_->Fire(); |
| 90 DoFailingLoad(kTestUrl); |
| 91 EXPECT_EQ(1, [delegate_ reloads]); |
| 92 timer_->Fire(); |
| 93 DoFailingLoad(kTestUrl); |
| 94 EXPECT_EQ(2, [delegate_ reloads]); |
| 95 timer_->Fire(); |
| 96 DoSucceedingLoad(kTestUrl); |
| 97 } |
| 98 |
| 99 TEST_F(AutoReloadControllerTest, AutoReloadBacksOff) { |
| 100 const GURL kTestUrl("https://www.google.com"); |
| 101 DoFailingLoad(kTestUrl); |
| 102 base::TimeDelta previous = timer_->GetCurrentDelay(); |
| 103 timer_->Fire(); |
| 104 DoFailingLoad(kTestUrl); |
| 105 int tries = 0; |
| 106 const int kMaxTries = 20; |
| 107 while (tries < kMaxTries && timer_->GetCurrentDelay() != previous) { |
| 108 previous = timer_->GetCurrentDelay(); |
| 109 timer_->Fire(); |
| 110 DoFailingLoad(kTestUrl); |
| 111 tries++; |
| 112 } |
| 113 |
| 114 EXPECT_NE(tries, 20); |
| 115 } |
| 116 |
| 117 TEST_F(AutoReloadControllerTest, AutoReloadStopsOnUserLoadStart) { |
| 118 const GURL kTestUrl("https://www.google.com"); |
| 119 const GURL kOtherUrl("https://mail.google.com"); |
| 120 DoFailingLoad(kTestUrl); |
| 121 EXPECT_TRUE(timer_->IsRunning()); |
| 122 [controller_ loadStartedForURL:kOtherUrl]; |
| 123 EXPECT_FALSE(timer_->IsRunning()); |
| 124 } |
| 125 |
| 126 TEST_F(AutoReloadControllerTest, AutoReloadBackoffResetsOnUserLoadStart) { |
| 127 const GURL kTestUrl("https://www.google.com"); |
| 128 const GURL kOtherUrl("https://mail.google.com"); |
| 129 base::TimeDelta first_delay; |
| 130 base::TimeDelta second_delay; |
| 131 DoFailingLoad(kTestUrl); |
| 132 EXPECT_TRUE(timer_->IsRunning()); |
| 133 first_delay = timer_->GetCurrentDelay(); |
| 134 timer_->Fire(); |
| 135 DoFailingLoad(kTestUrl); |
| 136 EXPECT_TRUE(timer_->IsRunning()); |
| 137 second_delay = timer_->GetCurrentDelay(); |
| 138 EXPECT_NE(first_delay, second_delay); |
| 139 |
| 140 DoFailingLoad(kOtherUrl); |
| 141 EXPECT_TRUE(timer_->IsRunning()); |
| 142 EXPECT_EQ(first_delay, timer_->GetCurrentDelay()); |
| 143 timer_->Fire(); |
| 144 DoFailingLoad(kOtherUrl); |
| 145 EXPECT_EQ(second_delay, timer_->GetCurrentDelay()); |
| 146 } |
| 147 |
| 148 TEST_F(AutoReloadControllerTest, AutoReloadStopsAtOffline) { |
| 149 const GURL kTestUrl("https://www.google.com"); |
| 150 DoFailingLoad(kTestUrl); |
| 151 EXPECT_TRUE(timer_->IsRunning()); |
| 152 [controller_ networkStateChangedToOnline:NO]; |
| 153 EXPECT_FALSE(timer_->IsRunning()); |
| 154 [controller_ networkStateChangedToOnline:YES]; |
| 155 EXPECT_TRUE(timer_->IsRunning()); |
| 156 } |
| 157 |
| 158 TEST_F(AutoReloadControllerTest, AutoReloadDoesntStartWhileOffline) { |
| 159 const GURL kTestUrl("https://www.google.com"); |
| 160 [controller_ loadStartedForURL:kTestUrl]; |
| 161 [controller_ networkStateChangedToOnline:NO]; |
| 162 [controller_ loadFailedForURL:kTestUrl wasPost:NO]; |
| 163 EXPECT_FALSE(timer_->IsRunning()); |
| 164 [controller_ networkStateChangedToOnline:YES]; |
| 165 EXPECT_TRUE(timer_->IsRunning()); |
| 166 } |
| 167 |
| 168 TEST_F(AutoReloadControllerTest, AutoReloadDoesNotBackoffAtNetworkChange) { |
| 169 const GURL kTestUrl("https://www.google.com"); |
| 170 DoFailingLoad(kTestUrl); |
| 171 EXPECT_TRUE(timer_->IsRunning()); |
| 172 base::TimeDelta delay = timer_->GetCurrentDelay(); |
| 173 [controller_ networkStateChangedToOnline:NO]; |
| 174 [controller_ networkStateChangedToOnline:YES]; |
| 175 EXPECT_TRUE(timer_->IsRunning()); |
| 176 EXPECT_EQ(delay, timer_->GetCurrentDelay()); |
| 177 } |
| 178 |
| 179 TEST_F(AutoReloadControllerTest, AutoReloadDoesNotReloadPosts) { |
| 180 const GURL kTestUrl("https://www.google.com"); |
| 181 [controller_ loadStartedForURL:kTestUrl]; |
| 182 [controller_ loadFailedForURL:kTestUrl wasPost:YES]; |
| 183 EXPECT_FALSE(timer_->IsRunning()); |
| 184 } |
OLD | NEW |