OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ios/chrome/browser/web/auto_reload_controller.h" | 5 #include "ios/chrome/browser/web/auto_reload_controller.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 | 8 |
9 #include "base/mac/scoped_nsobject.h" | |
10 #include "base/timer/mock_timer.h" | 9 #include "base/timer/mock_timer.h" |
11 #include "base/timer/timer.h" | 10 #include "base/timer/timer.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
13 #include "testing/gtest_mac.h" | 12 #include "testing/gtest_mac.h" |
14 | 13 |
| 14 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 15 #error "This file requires ARC support." |
| 16 #endif |
| 17 |
15 @interface AutoReloadController (Testing) | 18 @interface AutoReloadController (Testing) |
16 | 19 |
17 - (void)setTimerForTesting:(std::unique_ptr<base::Timer>)timer; | 20 - (void)setTimerForTesting:(std::unique_ptr<base::Timer>)timer; |
18 | 21 |
19 @end | 22 @end |
20 | 23 |
21 @interface TestAutoReloadDelegate : NSObject<AutoReloadDelegate> | 24 @interface TestAutoReloadDelegate : NSObject<AutoReloadDelegate> |
22 @end | 25 @end |
23 | 26 |
24 @implementation TestAutoReloadDelegate { | 27 @implementation TestAutoReloadDelegate { |
(...skipping 10 matching lines...) Expand all Loading... |
35 | 38 |
36 @end | 39 @end |
37 | 40 |
38 class AutoReloadControllerTest : public testing::Test { | 41 class AutoReloadControllerTest : public testing::Test { |
39 public: | 42 public: |
40 AutoReloadControllerTest() : timer_(new base::MockTimer(false, false)) {} | 43 AutoReloadControllerTest() : timer_(new base::MockTimer(false, false)) {} |
41 | 44 |
42 protected: | 45 protected: |
43 void SetUp() override { | 46 void SetUp() override { |
44 testing::Test::SetUp(); | 47 testing::Test::SetUp(); |
45 delegate_.reset([[TestAutoReloadDelegate alloc] init]); | 48 delegate_ = [[TestAutoReloadDelegate alloc] init]; |
46 controller_.reset([[AutoReloadController alloc] | 49 controller_ = [[AutoReloadController alloc] initWithDelegate:delegate_ |
47 initWithDelegate:delegate_.get() | 50 onlineStatus:YES]; |
48 onlineStatus:YES]); | |
49 // Note: even though setTimerForTesting theoretically passes ownership of | 51 // Note: even though setTimerForTesting theoretically passes ownership of |
50 // the timer to the controller, this class retains a weak pointer to the | 52 // 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 | 53 // 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 | 54 // safe is that this class owns the controller which now owns the timer, so |
53 // the timer has the same lifetime as this class. | 55 // the timer has the same lifetime as this class. |
54 [controller_ setTimerForTesting:std::unique_ptr<base::Timer>(timer_)]; | 56 [controller_ setTimerForTesting:std::unique_ptr<base::Timer>(timer_)]; |
55 } | 57 } |
56 | 58 |
57 // Synthesize a failing page load. | 59 // Synthesize a failing page load. |
58 void DoFailingLoad(const GURL& url) { | 60 void DoFailingLoad(const GURL& url) { |
59 [controller_ loadStartedForURL:url]; | 61 [controller_ loadStartedForURL:url]; |
60 [controller_ loadFailedForURL:url wasPost:NO]; | 62 [controller_ loadFailedForURL:url wasPost:NO]; |
61 } | 63 } |
62 | 64 |
63 // Synthesize a succeeding page load. | 65 // Synthesize a succeeding page load. |
64 void DoSucceedingLoad(const GURL& url) { | 66 void DoSucceedingLoad(const GURL& url) { |
65 [controller_ loadStartedForURL:url]; | 67 [controller_ loadStartedForURL:url]; |
66 [controller_ loadFinishedForURL:url wasPost:NO]; | 68 [controller_ loadFinishedForURL:url wasPost:NO]; |
67 } | 69 } |
68 | 70 |
69 base::scoped_nsobject<TestAutoReloadDelegate> delegate_; | 71 TestAutoReloadDelegate* delegate_; |
70 base::scoped_nsobject<AutoReloadController> controller_; | 72 AutoReloadController* controller_; |
71 base::MockTimer* timer_; // weak | 73 base::MockTimer* timer_; // weak |
72 }; | 74 }; |
73 | 75 |
74 TEST_F(AutoReloadControllerTest, AutoReloadSucceeds) { | 76 TEST_F(AutoReloadControllerTest, AutoReloadSucceeds) { |
75 const GURL kTestUrl("https://www.google.com"); | 77 const GURL kTestUrl("https://www.google.com"); |
76 DoFailingLoad(kTestUrl); | 78 DoFailingLoad(kTestUrl); |
77 EXPECT_TRUE(timer_->IsRunning()); | 79 EXPECT_TRUE(timer_->IsRunning()); |
78 EXPECT_EQ(0, [delegate_ reloads]); | 80 EXPECT_EQ(0, [delegate_ reloads]); |
79 timer_->Fire(); | 81 timer_->Fire(); |
80 EXPECT_EQ(1, [delegate_ reloads]); | 82 EXPECT_EQ(1, [delegate_ reloads]); |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 EXPECT_TRUE(timer_->IsRunning()); | 177 EXPECT_TRUE(timer_->IsRunning()); |
176 EXPECT_EQ(delay, timer_->GetCurrentDelay()); | 178 EXPECT_EQ(delay, timer_->GetCurrentDelay()); |
177 } | 179 } |
178 | 180 |
179 TEST_F(AutoReloadControllerTest, AutoReloadDoesNotReloadPosts) { | 181 TEST_F(AutoReloadControllerTest, AutoReloadDoesNotReloadPosts) { |
180 const GURL kTestUrl("https://www.google.com"); | 182 const GURL kTestUrl("https://www.google.com"); |
181 [controller_ loadStartedForURL:kTestUrl]; | 183 [controller_ loadStartedForURL:kTestUrl]; |
182 [controller_ loadFailedForURL:kTestUrl wasPost:YES]; | 184 [controller_ loadFailedForURL:kTestUrl wasPost:YES]; |
183 EXPECT_FALSE(timer_->IsRunning()); | 185 EXPECT_FALSE(timer_->IsRunning()); |
184 } | 186 } |
OLD | NEW |