| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 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 #import "ios/clean/chrome/browser/ui/toolbar/toolbar_mediator.h" | 5 #import "ios/clean/chrome/browser/ui/toolbar/toolbar_mediator.h" |
| 6 | 6 |
| 7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
| 8 #include "ios/chrome/browser/web_state_list/fake_web_state_list_delegate.h" |
| 9 #include "ios/chrome/browser/web_state_list/web_state_list.h" |
| 10 #import "ios/chrome/browser/web_state_list/web_state_list_observer_bridge.h" |
| 8 #import "ios/clean/chrome/browser/ui/toolbar/toolbar_consumer.h" | 11 #import "ios/clean/chrome/browser/ui/toolbar/toolbar_consumer.h" |
| 9 #import "ios/shared/chrome/browser/ui/toolbar/toolbar_test_util.h" | 12 #import "ios/shared/chrome/browser/ui/toolbar/toolbar_test_util.h" |
| 10 #import "ios/web/public/test/fakes/test_navigation_manager.h" | 13 #import "ios/web/public/test/fakes/test_navigation_manager.h" |
| 11 #import "ios/web/public/test/fakes/test_web_state.h" | 14 #import "ios/web/public/test/fakes/test_web_state.h" |
| 12 #import "ios/web/public/web_state/web_state_observer_bridge.h" | 15 #import "ios/web/public/web_state/web_state_observer_bridge.h" |
| 13 #include "testing/platform_test.h" | 16 #include "testing/platform_test.h" |
| 14 #import "third_party/ocmock/OCMock/OCMock.h" | 17 #import "third_party/ocmock/OCMock/OCMock.h" |
| 15 #include "third_party/ocmock/gtest_support.h" | 18 #include "third_party/ocmock/gtest_support.h" |
| 16 | 19 |
| 17 #if !defined(__has_feature) || !__has_feature(objc_arc) | 20 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 18 #error "This file requires ARC support." | 21 #error "This file requires ARC support." |
| 19 #endif | 22 #endif |
| 20 | 23 |
| 21 @interface TestToolbarMediator : ToolbarMediator<CRWWebStateObserver> | 24 @interface TestToolbarMediator |
| 25 : ToolbarMediator<CRWWebStateObserver, WebStateListObserving> |
| 22 @end | 26 @end |
| 23 | 27 |
| 24 @implementation TestToolbarMediator | 28 @implementation TestToolbarMediator |
| 25 @end | 29 @end |
| 26 | 30 |
| 27 namespace { | 31 namespace { |
| 28 | 32 |
| 33 static const int kNumberOfWebStates = 3; |
| 29 static const char kTestUrl[] = "http://www.chromium.org"; | 34 static const char kTestUrl[] = "http://www.chromium.org"; |
| 30 | 35 |
| 31 class ToolbarMediatorTest : public PlatformTest { | 36 class ToolbarMediatorTest : public PlatformTest { |
| 32 public: | 37 public: |
| 33 ToolbarMediatorTest() { | 38 ToolbarMediatorTest() { |
| 34 std::unique_ptr<ToolbarTestNavigationManager> navigation_manager = | 39 std::unique_ptr<ToolbarTestNavigationManager> navigation_manager = |
| 35 base::MakeUnique<ToolbarTestNavigationManager>(); | 40 base::MakeUnique<ToolbarTestNavigationManager>(); |
| 36 navigation_manager_ = navigation_manager.get(); | 41 navigation_manager_ = navigation_manager.get(); |
| 37 test_web_state_.SetNavigationManager(std::move(navigation_manager)); | 42 test_web_state_.SetNavigationManager(std::move(navigation_manager)); |
| 38 test_web_state_.SetLoading(true); | 43 test_web_state_.SetLoading(true); |
| 39 mediator_ = [[TestToolbarMediator alloc] init]; | 44 mediator_ = [[TestToolbarMediator alloc] init]; |
| 40 consumer_ = OCMProtocolMock(@protocol(ToolbarConsumer)); | 45 consumer_ = OCMProtocolMock(@protocol(ToolbarConsumer)); |
| 41 } | 46 } |
| 42 | 47 |
| 48 // Explicitly disconnect the mediator so there won't be any WebStateList |
| 49 // observers when web_state_list_ gets dealloc. |
| 50 ~ToolbarMediatorTest() override { [mediator_ disconnect]; } |
| 51 |
| 43 protected: | 52 protected: |
| 53 void SetUpWebStateList() { |
| 54 web_state_list_ = base::MakeUnique<WebStateList>(&web_state_list_delegate_); |
| 55 for (int i = 0; i < kNumberOfWebStates; i++) { |
| 56 InsertWebState(i); |
| 57 } |
| 58 } |
| 59 |
| 60 void InsertWebState(int index) { |
| 61 auto web_state = base::MakeUnique<web::TestWebState>(); |
| 62 GURL url("http://test/" + std::to_string(index)); |
| 63 web_state->SetCurrentURL(url); |
| 64 web_state_list_->InsertWebState(index, std::move(web_state)); |
| 65 } |
| 66 |
| 44 TestToolbarMediator* mediator_; | 67 TestToolbarMediator* mediator_; |
| 45 ToolbarTestWebState test_web_state_; | 68 ToolbarTestWebState test_web_state_; |
| 46 ToolbarTestNavigationManager* navigation_manager_; | 69 ToolbarTestNavigationManager* navigation_manager_; |
| 70 std::unique_ptr<WebStateList> web_state_list_; |
| 71 FakeWebStateListDelegate web_state_list_delegate_; |
| 47 id consumer_; | 72 id consumer_; |
| 48 }; | 73 }; |
| 49 | 74 |
| 50 // Test no setup is being done on the Toolbar if there's no Webstate. | 75 // Test no setup is being done on the Toolbar if there's no Webstate. |
| 51 TEST_F(ToolbarMediatorTest, TestToolbarSetupWithNoWebstate) { | 76 TEST_F(ToolbarMediatorTest, TestToolbarSetupWithNoWebstate) { |
| 52 mediator_.consumer = consumer_; | 77 mediator_.consumer = consumer_; |
| 53 | 78 |
| 54 [[consumer_ reject] setCanGoForward:NO]; | 79 [[consumer_ reject] setCanGoForward:NO]; |
| 55 [[consumer_ reject] setCanGoBack:NO]; | 80 [[consumer_ reject] setCanGoBack:NO]; |
| 56 [[consumer_ reject] setIsLoading:YES]; | 81 [[consumer_ reject] setIsLoading:YES]; |
| 57 } | 82 } |
| 58 | 83 |
| 59 // Test no setup is being done on the Toolbar if there's no consumer. | 84 // Test no setup is being done on the Toolbar if there's no consumer. |
| 60 TEST_F(ToolbarMediatorTest, TestToolbarSetupWithNoConsumer) { | 85 TEST_F(ToolbarMediatorTest, TestToolbarSetupWithNoConsumer) { |
| 61 mediator_.webState = &test_web_state_; | 86 mediator_.webState = &test_web_state_; |
| 62 | 87 |
| 63 [[consumer_ reject] setCanGoForward:NO]; | 88 [[consumer_ reject] setCanGoForward:NO]; |
| 64 [[consumer_ reject] setCanGoBack:NO]; | 89 [[consumer_ reject] setCanGoBack:NO]; |
| 65 [[consumer_ reject] setIsLoading:YES]; | 90 [[consumer_ reject] setIsLoading:YES]; |
| 66 } | 91 } |
| 67 | 92 |
| 93 // Test no WebstateList related setup is being done on the Toolbar if there's no |
| 94 // WebstateList. |
| 95 TEST_F(ToolbarMediatorTest, TestToolbarSetupWithNoWebstateList) { |
| 96 mediator_.consumer = consumer_; |
| 97 mediator_.webState = &test_web_state_; |
| 98 |
| 99 [[[consumer_ reject] ignoringNonObjectArgs] setTabCount:0]; |
| 100 } |
| 101 |
| 68 // Test the Toolbar Setup gets called when the mediator's WebState and Consumer | 102 // Test the Toolbar Setup gets called when the mediator's WebState and Consumer |
| 69 // have been set. | 103 // have been set. |
| 70 TEST_F(ToolbarMediatorTest, TestToolbarSetup) { | 104 TEST_F(ToolbarMediatorTest, TestToolbarSetup) { |
| 71 mediator_.webState = &test_web_state_; | 105 mediator_.webState = &test_web_state_; |
| 72 mediator_.consumer = consumer_; | 106 mediator_.consumer = consumer_; |
| 73 | 107 |
| 74 [[consumer_ verify] setCanGoForward:NO]; | 108 [[consumer_ verify] setCanGoForward:NO]; |
| 75 [[consumer_ verify] setCanGoBack:NO]; | 109 [[consumer_ verify] setCanGoBack:NO]; |
| 76 [[consumer_ verify] setIsLoading:YES]; | 110 [[consumer_ verify] setIsLoading:YES]; |
| 77 } | 111 } |
| 78 | 112 |
| 79 // Test the Toolbar Setup gets called when the mediator's WebState and Consumer | 113 // Test the Toolbar Setup gets called when the mediator's WebState and Consumer |
| 80 // have been set in reverse order. | 114 // have been set in reverse order. |
| 81 TEST_F(ToolbarMediatorTest, TestToolbarSetupReverse) { | 115 TEST_F(ToolbarMediatorTest, TestToolbarSetupReverse) { |
| 82 mediator_.consumer = consumer_; | 116 mediator_.consumer = consumer_; |
| 83 mediator_.webState = &test_web_state_; | 117 mediator_.webState = &test_web_state_; |
| 84 | 118 |
| 85 [[consumer_ verify] setCanGoForward:NO]; | 119 [[consumer_ verify] setCanGoForward:NO]; |
| 86 [[consumer_ verify] setCanGoBack:NO]; | 120 [[consumer_ verify] setCanGoBack:NO]; |
| 87 [[consumer_ verify] setIsLoading:YES]; | 121 [[consumer_ verify] setIsLoading:YES]; |
| 88 } | 122 } |
| 89 | 123 |
| 124 // Test the WebstateList related setup gets called when the mediator's WebState |
| 125 // and Consumer have been set. |
| 126 TEST_F(ToolbarMediatorTest, TestWebstateListRelatedSetup) { |
| 127 SetUpWebStateList(); |
| 128 mediator_.webStateList = web_state_list_.get(); |
| 129 mediator_.consumer = consumer_; |
| 130 |
| 131 [[consumer_ verify] setTabCount:3]; |
| 132 } |
| 133 |
| 134 // Test the WebstateList related setup gets called when the mediator's WebState |
| 135 // and Consumer have been set in reverse order. |
| 136 TEST_F(ToolbarMediatorTest, TestWebstateListRelatedSetupReverse) { |
| 137 mediator_.consumer = consumer_; |
| 138 SetUpWebStateList(); |
| 139 mediator_.webStateList = web_state_list_.get(); |
| 140 |
| 141 [[consumer_ verify] setTabCount:3]; |
| 142 } |
| 143 |
| 90 // Test the Toolbar is updated when the Webstate observer method DidStartLoading | 144 // Test the Toolbar is updated when the Webstate observer method DidStartLoading |
| 91 // is triggered by SetLoading. | 145 // is triggered by SetLoading. |
| 92 TEST_F(ToolbarMediatorTest, TestDidStartLoading) { | 146 TEST_F(ToolbarMediatorTest, TestDidStartLoading) { |
| 93 // Change the default loading state to false so we can verify the Webstate | 147 // Change the default loading state to false to verify the Webstate |
| 94 // callback with true. | 148 // callback with true. |
| 95 test_web_state_.SetLoading(false); | 149 test_web_state_.SetLoading(false); |
| 96 mediator_.webState = &test_web_state_; | 150 mediator_.webState = &test_web_state_; |
| 97 mediator_.consumer = consumer_; | 151 mediator_.consumer = consumer_; |
| 98 | 152 |
| 99 test_web_state_.SetLoading(true); | 153 test_web_state_.SetLoading(true); |
| 100 [[consumer_ verify] setIsLoading:YES]; | 154 [[consumer_ verify] setIsLoading:YES]; |
| 101 } | 155 } |
| 102 | 156 |
| 103 // Test the Toolbar is updated when the Webstate observer method DidStopLoading | 157 // Test the Toolbar is updated when the Webstate observer method DidStopLoading |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 140 // the correct consumer calls. | 194 // the correct consumer calls. |
| 141 TEST_F(ToolbarMediatorTest, TestTabStripVisible) { | 195 TEST_F(ToolbarMediatorTest, TestTabStripVisible) { |
| 142 mediator_.consumer = consumer_; | 196 mediator_.consumer = consumer_; |
| 143 | 197 |
| 144 [mediator_ broadcastTabStripVisible:YES]; | 198 [mediator_ broadcastTabStripVisible:YES]; |
| 145 [[consumer_ verify] setTabStripVisible:YES]; | 199 [[consumer_ verify] setTabStripVisible:YES]; |
| 146 [mediator_ broadcastTabStripVisible:NO]; | 200 [mediator_ broadcastTabStripVisible:NO]; |
| 147 [[consumer_ verify] setTabStripVisible:NO]; | 201 [[consumer_ verify] setTabStripVisible:NO]; |
| 148 } | 202 } |
| 149 | 203 |
| 204 // Test that increasing the number of Webstates will update the consumer with |
| 205 // the right value. |
| 206 TEST_F(ToolbarMediatorTest, TestIncreaseNumberOfWebstates) { |
| 207 SetUpWebStateList(); |
| 208 mediator_.webStateList = web_state_list_.get(); |
| 209 mediator_.consumer = consumer_; |
| 210 |
| 211 InsertWebState(0); |
| 212 [[consumer_ verify] setTabCount:kNumberOfWebStates + 1]; |
| 213 } |
| 214 |
| 215 // Test that decreasing the number of Webstates will update the consumer with |
| 216 // the right value. |
| 217 TEST_F(ToolbarMediatorTest, TestDecreaseNumberOfWebstates) { |
| 218 SetUpWebStateList(); |
| 219 mediator_.webStateList = web_state_list_.get(); |
| 220 mediator_.consumer = consumer_; |
| 221 |
| 222 web_state_list_->DetachWebStateAt(0); |
| 223 [[consumer_ verify] setTabCount:kNumberOfWebStates - 1]; |
| 224 } |
| 225 |
| 150 } // namespace | 226 } // namespace |
| OLD | NEW |