Chromium Code Reviews| 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 #import "ios/clean/chrome/browser/ui/toolbar/toolbar_consumer.h" | 8 #import "ios/clean/chrome/browser/ui/toolbar/toolbar_consumer.h" |
| 9 #import "ios/shared/chrome/browser/ui/toolbar/toolbar_test_util.h" | |
| 9 #import "ios/web/public/test/fakes/test_navigation_manager.h" | 10 #import "ios/web/public/test/fakes/test_navigation_manager.h" |
| 10 #import "ios/web/public/test/fakes/test_web_state.h" | 11 #import "ios/web/public/test/fakes/test_web_state.h" |
| 11 #import "ios/web/public/web_state/web_state_observer_bridge.h" | 12 #import "ios/web/public/web_state/web_state_observer_bridge.h" |
| 12 #include "testing/platform_test.h" | 13 #include "testing/platform_test.h" |
| 13 #import "third_party/ocmock/OCMock/OCMock.h" | 14 #import "third_party/ocmock/OCMock/OCMock.h" |
| 14 #include "third_party/ocmock/gtest_support.h" | 15 #include "third_party/ocmock/gtest_support.h" |
| 15 | 16 |
| 16 #if !defined(__has_feature) || !__has_feature(objc_arc) | 17 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 17 #error "This file requires ARC support." | 18 #error "This file requires ARC support." |
| 18 #endif | 19 #endif |
| 19 | 20 |
| 20 @interface TestToolbarMediator : ToolbarMediator<CRWWebStateObserver> | 21 @interface TestToolbarMediator : ToolbarMediator<CRWWebStateObserver> |
| 21 @end | 22 @end |
| 22 | 23 |
| 23 @implementation TestToolbarMediator | 24 @implementation TestToolbarMediator |
| 24 @end | 25 @end |
| 25 | 26 |
| 26 namespace { | 27 namespace { |
| 27 | 28 |
| 28 static const char kTestUrl[] = "http://www.chromium.org"; | 29 static const char kTestUrl[] = "http://www.chromium.org"; |
| 29 | 30 |
| 30 class ToolbarTestNavigationManager : public web::TestNavigationManager { | |
| 31 public: | |
| 32 ToolbarTestNavigationManager() | |
| 33 : can_go_back_(false), can_go_forward_(false) {} | |
| 34 | |
| 35 bool CanGoBack() const override { return can_go_back_; } | |
| 36 bool CanGoForward() const override { return can_go_forward_; } | |
| 37 | |
| 38 void set_can_go_back(bool can_go_back) { can_go_back_ = can_go_back; } | |
| 39 void set_can_go_forward(bool can_go_forward) { | |
| 40 can_go_forward_ = can_go_forward; | |
| 41 } | |
| 42 | |
| 43 private: | |
| 44 bool can_go_back_; | |
| 45 bool can_go_forward_; | |
| 46 }; | |
| 47 | |
| 48 class ToolbarMediatorTest : public PlatformTest { | 31 class ToolbarMediatorTest : public PlatformTest { |
| 49 public: | 32 public: |
| 50 ToolbarMediatorTest() { | 33 ToolbarMediatorTest() { |
| 51 std::unique_ptr<ToolbarTestNavigationManager> navigation_manager = | 34 std::unique_ptr<ToolbarTestNavigationManager> navigation_manager = |
| 52 base::MakeUnique<ToolbarTestNavigationManager>(); | 35 base::MakeUnique<ToolbarTestNavigationManager>(); |
| 53 navigation_manager_ = navigation_manager.get(); | 36 navigation_manager_ = navigation_manager.get(); |
| 54 test_web_state_.SetNavigationManager(std::move(navigation_manager)); | 37 test_web_state_.SetNavigationManager(std::move(navigation_manager)); |
| 55 test_web_state_.SetLoading(true); | 38 test_web_state_.SetLoading(true); |
| 56 mediator_ = [[TestToolbarMediator alloc] init]; | 39 mediator_ = [[TestToolbarMediator alloc] init]; |
| 57 consumer_ = OCMProtocolMock(@protocol(ToolbarConsumer)); | 40 consumer_ = OCMProtocolMock(@protocol(ToolbarConsumer)); |
| 58 } | 41 } |
| 59 | 42 |
| 60 protected: | 43 protected: |
| 61 TestToolbarMediator* mediator_; | 44 TestToolbarMediator* mediator_; |
| 62 web::TestWebState test_web_state_; | 45 ToolbarTestWebState test_web_state_; |
| 63 ToolbarTestNavigationManager* navigation_manager_; | 46 ToolbarTestNavigationManager* navigation_manager_; |
| 64 id consumer_; | 47 id consumer_; |
| 65 }; | 48 }; |
| 66 | 49 |
| 67 // Test no setup is being done on the Toolbar if there's no Webstate. | 50 // Test no setup is being done on the Toolbar if there's no Webstate. |
| 68 TEST_F(ToolbarMediatorTest, TestToolbarSetupWithNoWebstate) { | 51 TEST_F(ToolbarMediatorTest, TestToolbarSetupWithNoWebstate) { |
| 69 mediator_.consumer = consumer_; | 52 mediator_.consumer = consumer_; |
| 70 | 53 |
| 71 [[consumer_ reject] setCanGoForward:NO]; | 54 [[consumer_ reject] setCanGoForward:NO]; |
| 72 [[consumer_ reject] setCanGoBack:NO]; | 55 [[consumer_ reject] setCanGoBack:NO]; |
| 73 [[consumer_ reject] setCurrentPageText:[OCMArg any]]; | |
| 74 [[consumer_ reject] setIsLoading:YES]; | 56 [[consumer_ reject] setIsLoading:YES]; |
| 75 } | 57 } |
| 76 | 58 |
| 77 // Test no setup is being done on the Toolbar if there's no consumer. | 59 // Test no setup is being done on the Toolbar if there's no consumer. |
| 78 TEST_F(ToolbarMediatorTest, TestToolbarSetupWithNoConsumer) { | 60 TEST_F(ToolbarMediatorTest, TestToolbarSetupWithNoConsumer) { |
| 79 mediator_.webState = &test_web_state_; | 61 mediator_.webState = &test_web_state_; |
| 80 | 62 |
| 81 [[consumer_ reject] setCanGoForward:NO]; | 63 [[consumer_ reject] setCanGoForward:NO]; |
| 82 [[consumer_ reject] setCanGoBack:NO]; | 64 [[consumer_ reject] setCanGoBack:NO]; |
| 83 [[consumer_ reject] setCurrentPageText:[OCMArg any]]; | |
| 84 [[consumer_ reject] setIsLoading:YES]; | 65 [[consumer_ reject] setIsLoading:YES]; |
| 85 } | 66 } |
| 86 | 67 |
| 87 // Test the Toolbar Setup gets called when the mediator's WebState and Consumer | 68 // Test the Toolbar Setup gets called when the mediator's WebState and Consumer |
| 88 // have been set. | 69 // have been set. |
| 89 TEST_F(ToolbarMediatorTest, TestToolbarSetup) { | 70 TEST_F(ToolbarMediatorTest, TestToolbarSetup) { |
| 90 mediator_.webState = &test_web_state_; | 71 mediator_.webState = &test_web_state_; |
| 91 mediator_.consumer = consumer_; | 72 mediator_.consumer = consumer_; |
| 92 | 73 |
| 93 [[consumer_ verify] setCanGoForward:NO]; | 74 [[consumer_ verify] setCanGoForward:NO]; |
| 94 [[consumer_ verify] setCanGoBack:NO]; | 75 [[consumer_ verify] setCanGoBack:NO]; |
| 95 [[consumer_ verify] setCurrentPageText:[OCMArg any]]; | |
| 96 [[consumer_ verify] setIsLoading:YES]; | 76 [[consumer_ verify] setIsLoading:YES]; |
| 97 } | 77 } |
| 98 | 78 |
| 99 // Test the Toolbar Setup gets called when the mediator's WebState and Consumer | 79 // Test the Toolbar Setup gets called when the mediator's WebState and Consumer |
| 100 // have been set in reverse order. | 80 // have been set in reverse order. |
| 101 TEST_F(ToolbarMediatorTest, TestToolbarSetupReverse) { | 81 TEST_F(ToolbarMediatorTest, TestToolbarSetupReverse) { |
| 102 mediator_.consumer = consumer_; | 82 mediator_.consumer = consumer_; |
| 103 mediator_.webState = &test_web_state_; | 83 mediator_.webState = &test_web_state_; |
| 104 | 84 |
| 105 [[consumer_ verify] setCanGoForward:NO]; | 85 [[consumer_ verify] setCanGoForward:NO]; |
| 106 [[consumer_ verify] setCanGoBack:NO]; | 86 [[consumer_ verify] setCanGoBack:NO]; |
| 107 [[consumer_ verify] setCurrentPageText:[OCMArg any]]; | |
| 108 [[consumer_ verify] setIsLoading:YES]; | 87 [[consumer_ verify] setIsLoading:YES]; |
| 109 } | 88 } |
| 110 | 89 |
| 111 // Test the Toolbar is updated when the Webstate observer method DidStartLoading | 90 // Test the Toolbar is updated when the Webstate observer method DidStartLoading |
| 112 // is triggered by SetLoading. | 91 // is triggered by SetLoading. |
| 113 TEST_F(ToolbarMediatorTest, TestDidStartLoading) { | 92 TEST_F(ToolbarMediatorTest, TestDidStartLoading) { |
| 114 // Change the default loading state to false so we can verify the Webstate | 93 // Change the default loading state to false so we can verify the Webstate |
| 115 // callback with true. | 94 // callback with true. |
| 116 test_web_state_.SetLoading(false); | 95 test_web_state_.SetLoading(false); |
| 117 mediator_.webState = &test_web_state_; | 96 mediator_.webState = &test_web_state_; |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 138 mediator_.consumer = consumer_; | 117 mediator_.consumer = consumer_; |
| 139 | 118 |
| 140 navigation_manager_->set_can_go_forward(true); | 119 navigation_manager_->set_can_go_forward(true); |
| 141 navigation_manager_->set_can_go_back(true); | 120 navigation_manager_->set_can_go_back(true); |
| 142 | 121 |
| 143 test_web_state_.SetCurrentURL(GURL(kTestUrl)); | 122 test_web_state_.SetCurrentURL(GURL(kTestUrl)); |
| 144 test_web_state_.OnPageLoaded(web::PageLoadCompletionStatus::SUCCESS); | 123 test_web_state_.OnPageLoaded(web::PageLoadCompletionStatus::SUCCESS); |
| 145 | 124 |
| 146 [[consumer_ verify] setCanGoForward:YES]; | 125 [[consumer_ verify] setCanGoForward:YES]; |
| 147 [[consumer_ verify] setCanGoBack:YES]; | 126 [[consumer_ verify] setCanGoBack:YES]; |
| 148 [[consumer_ verify] setCurrentPageText:@"http://www.chromium.org/"]; | 127 } |
| 128 | |
| 129 // Test the Toolbar is updated when the Webstate observer method | |
| 130 // didChangeLoadingProgress is called. | |
| 131 TEST_F(ToolbarMediatorTest, TestLoadingProgress) { | |
| 132 mediator_.webState = &test_web_state_; | |
| 133 mediator_.consumer = consumer_; | |
| 134 | |
| 135 [mediator_ webState:mediator_.webState didChangeLoadingProgress:0.42]; | |
|
sczs
2017/05/05 23:15:45
I've tried to trigger this by opening a URL with t
marq (ping after 24h)
2017/05/09 12:07:31
I don't think you'll get any loading progress upda
| |
| 136 [[consumer_ verify] setLoadingProgress:0.42]; | |
| 149 } | 137 } |
| 150 | 138 |
| 151 } // namespace | 139 } // namespace |
| OLD | NEW |