Index: ios/chrome/browser/ui/toolbar/toolbar_model_impl_ios_unittest.mm |
diff --git a/ios/chrome/browser/ui/toolbar/toolbar_model_impl_ios_unittest.mm b/ios/chrome/browser/ui/toolbar/toolbar_model_impl_ios_unittest.mm |
index 308a1b0dcd3ef5bf95f8211925f1222d488b35b9..588dd944b01e0e520f5716dd60347521e6c39055 100644 |
--- a/ios/chrome/browser/ui/toolbar/toolbar_model_impl_ios_unittest.mm |
+++ b/ios/chrome/browser/ui/toolbar/toolbar_model_impl_ios_unittest.mm |
@@ -7,6 +7,7 @@ |
#include <memory> |
#include "base/mac/scoped_nsobject.h" |
+#include "base/memory/ptr_util.h" |
#include "base/message_loop/message_loop.h" |
#include "base/strings/utf_string_conversions.h" |
#include "components/bookmarks/browser/bookmark_model.h" |
@@ -14,12 +15,13 @@ |
#include "components/toolbar/test_toolbar_model.h" |
#include "ios/chrome/browser/bookmarks/bookmark_model_factory.h" |
#include "ios/chrome/browser/browser_state/test_chrome_browser_state.h" |
-#import "ios/chrome/browser/tabs/tab.h" |
-#import "ios/chrome/browser/tabs/tab_model.h" |
#include "ios/chrome/browser/ui/toolbar/toolbar_model_delegate_ios.h" |
#include "ios/chrome/browser/ui/toolbar/toolbar_model_impl_ios.h" |
#import "ios/chrome/browser/xcallback_parameters.h" |
+#include "ios/shared/chrome/browser/tabs/fake_web_state_list_delegate.h" |
+#include "ios/shared/chrome/browser/tabs/web_state_list.h" |
#import "ios/testing/ocmock_complex_type_helper.h" |
+#import "ios/web/public/test/fakes/test_navigation_manager.h" |
#import "ios/web/public/test/fakes/test_web_state.h" |
#include "ios/web/public/test/test_web_thread.h" |
#include "ios/web/public/test/test_web_thread_bundle.h" |
@@ -28,35 +30,48 @@ |
#include "third_party/ocmock/gtest_support.h" |
#include "third_party/ocmock/ocmock_extensions.h" |
-@interface TMITestTabMock : OCMockComplexTypeHelper { |
- GURL url_; |
- web::WebState* web_state_; |
-} |
- |
-@property(nonatomic, assign) const GURL& url; |
-@property(nonatomic, assign) web::WebState* webState; |
-@end |
- |
-@implementation TMITestTabMock |
-- (const GURL&)url { |
- return url_; |
-} |
-- (void)setUrl:(const GURL&)url { |
- url_ = url; |
-} |
-- (web::WebState*)webState { |
- return web_state_; |
-} |
-- (void)setWebState:(web::WebState*)web_state { |
- web_state_ = web_state; |
-} |
-@end |
- |
namespace { |
static const char kWebUrl[] = "http://www.chromium.org"; |
static const char kNativeUrl[] = "chrome://version"; |
+namespace { |
+ |
+class ToolbarTestWebState : public web::TestWebState { |
+ public: |
+ ToolbarTestWebState() : loading_progress_(0) {} |
+ |
+ double GetLoadingProgress() const override { return loading_progress_; } |
+ void set_loading_progress(double loading_progress) { |
+ loading_progress_ = loading_progress; |
+ } |
+ |
+ private: |
+ double loading_progress_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(ToolbarTestWebState); |
+}; |
+ |
+class ToolbarTestNavigationManager : public web::TestNavigationManager { |
+ public: |
+ ToolbarTestNavigationManager() |
+ : can_go_back_(false), can_go_forward_(false) {} |
+ |
+ bool CanGoBack() const override { return can_go_back_; } |
+ bool CanGoForward() const override { return can_go_forward_; } |
+ |
+ void set_can_go_back(bool can_go_back) { can_go_back_ = can_go_back; } |
+ void set_can_go_forward(bool can_go_forward) { |
+ can_go_forward_ = can_go_forward; |
+ } |
+ |
+ private: |
+ bool can_go_back_; |
+ bool can_go_forward_; |
+}; |
+ |
+} // namespace |
+ |
class ToolbarModelImplIOSTest : public PlatformTest { |
protected: |
void SetUp() override { |
@@ -67,41 +82,38 @@ class ToolbarModelImplIOSTest : public PlatformTest { |
ios::BookmarkModelFactory::GetForBrowserState( |
chrome_browser_state_.get())); |
- tabModel_.reset([[OCMockObject niceMockForClass:[TabModel class]] retain]); |
- |
- toolbarModelDelegate_.reset(new ToolbarModelDelegateIOS(tabModel_.get())); |
+ // Create a WebStateList that will always return the test WebState as |
+ // the active WebState. |
+ web_state_list_ = base::MakeUnique<WebStateList>( |
+ &web_state_list_delegate_, WebStateList::WebStateOwned); |
+ std::unique_ptr<ToolbarTestWebState> web_state = |
+ base::MakeUnique<ToolbarTestWebState>(); |
+ web_state->SetBrowserState(chrome_browser_state_.get()); |
+ web_state_ = web_state.get(); |
+ web_state_list_->InsertWebState(0, web_state.release()); |
+ web_state_list_->ActivateWebStateAt(0); |
+ |
+ toolbarModelDelegate_.reset( |
+ new ToolbarModelDelegateIOS(web_state_list_.get())); |
toolbarModel_.reset(new ToolbarModelImplIOS(toolbarModelDelegate_.get())); |
} |
web::TestWebThreadBundle thread_bundle_; |
std::unique_ptr<TestChromeBrowserState> chrome_browser_state_; |
- base::scoped_nsobject<TabModel> tabModel_; |
+ FakeWebStateListDelegate web_state_list_delegate_; |
+ std::unique_ptr<WebStateList> web_state_list_; |
+ ToolbarTestWebState* web_state_; |
std::unique_ptr<ToolbarModelDelegateIOS> toolbarModelDelegate_; |
std::unique_ptr<ToolbarModelIOS> toolbarModel_; |
}; |
-class ToolbarModelImplIOSTestWebState : public web::TestWebState { |
- public: |
- explicit ToolbarModelImplIOSTestWebState(web::BrowserState* browser_state) |
- : browser_state_(browser_state) {} |
- |
- web::BrowserState* GetBrowserState() const override { return browser_state_; } |
- double GetLoadingProgress() const override { return loading_progress_; } |
- void SetLoadingProgress(double loading_progress) { |
- loading_progress_ = loading_progress; |
- } |
- |
- private: |
- web::BrowserState* browser_state_; |
- double loading_progress_; |
- |
- DISALLOW_COPY_AND_ASSIGN(ToolbarModelImplIOSTestWebState); |
-}; |
- |
-TEST_F(ToolbarModelImplIOSTest, TestWhenCurrentTabIsNull) { |
- // Make a mock to always return NULL for the current tab. |
- OCMockObject* tabModelMock = static_cast<OCMockObject*>(tabModel_.get()); |
- [[[tabModelMock stub] andReturn:NULL] currentTab]; |
+TEST_F(ToolbarModelImplIOSTest, TestWhenCurrentWebStateIsNull) { |
+ // The test fixture adds one WebState to the WebStateList, so remove it before |
+ // running this test. |
+ ASSERT_EQ(1, web_state_list_->count()); |
+ std::unique_ptr<web::WebState> closed_web_state( |
+ web_state_list_->DetachWebStateAt(0)); |
+ ASSERT_TRUE(web_state_list_->empty()); |
EXPECT_FALSE(toolbarModel_->IsLoading()); |
EXPECT_EQ(0, toolbarModel_->GetLoadProgressFraction()); |
@@ -112,95 +124,66 @@ TEST_F(ToolbarModelImplIOSTest, TestWhenCurrentTabIsNull) { |
} |
TEST_F(ToolbarModelImplIOSTest, TestIsLoading) { |
- OCMockObject* tabModelMock = static_cast<OCMockObject*>(tabModel_.get()); |
- id tabMock = [[TMITestTabMock alloc] |
- initWithRepresentedObject:[OCMockObject mockForClass:[Tab class]]]; |
- |
- // Make mocks return a current tab with a null web state. |
- [[[tabModelMock stub] andReturn:tabMock] currentTab]; |
- [tabMock setWebState:nullptr]; |
- [static_cast<TMITestTabMock*>(tabMock) setUrl:GURL(kWebUrl)]; |
- EXPECT_FALSE(toolbarModel_->IsLoading()); |
- |
- // Make mocks return a current tab that is loading. |
- web::TestWebState webState; |
- [tabMock setWebState:&webState]; |
- webState.SetLoading(true); |
+ // An active webstate that is loading. |
+ web_state_->SetLoading(true); |
EXPECT_TRUE(toolbarModel_->IsLoading()); |
- // Make mocks return a current tab that is not loading. |
- webState.SetLoading(false); |
+ // An active webstate that is not loading. |
+ web_state_->SetLoading(false); |
EXPECT_FALSE(toolbarModel_->IsLoading()); |
- // Make mocks return a current tab that is pointing at a native URL. |
- webState.SetLoading(true); |
- [static_cast<TMITestTabMock*>(tabMock) setUrl:GURL(kNativeUrl)]; |
+ // An active webstate that is pointing at a native URL. |
+ web_state_->SetLoading(true); |
+ web_state_->SetCurrentURL(GURL(kNativeUrl)); |
EXPECT_FALSE(toolbarModel_->IsLoading()); |
} |
TEST_F(ToolbarModelImplIOSTest, TestGetLoadProgressFraction) { |
- ToolbarModelImplIOSTestWebState web_state(chrome_browser_state_.get()); |
- OCMockObject* tabModelMock = static_cast<OCMockObject*>(tabModel_.get()); |
- id tabMock = [[TMITestTabMock alloc] |
- initWithRepresentedObject:[OCMockObject mockForClass:[Tab class]]]; |
- [static_cast<TMITestTabMock*>(tabMock) setWebState:&web_state]; |
- [[[tabModelMock stub] andReturn:tabMock] currentTab]; |
- |
const CGFloat kExpectedProgress = 0.42; |
- web_state.SetLoadingProgress(kExpectedProgress); |
+ web_state_->set_loading_progress(kExpectedProgress); |
EXPECT_FLOAT_EQ(kExpectedProgress, toolbarModel_->GetLoadProgressFraction()); |
} |
TEST_F(ToolbarModelImplIOSTest, TestCanGoBack) { |
- OCMockObject* tabModelMock = static_cast<OCMockObject*>(tabModel_.get()); |
- id tabMock = [[TMITestTabMock alloc] |
- initWithRepresentedObject:[OCMockObject mockForClass:[Tab class]]]; |
- [[[tabModelMock stub] andReturn:tabMock] currentTab]; |
+ web_state_->SetNavigationManager( |
+ base::MakeUnique<ToolbarTestNavigationManager>()); |
+ ToolbarTestNavigationManager* manager = |
+ static_cast<ToolbarTestNavigationManager*>( |
+ web_state_->GetNavigationManager()); |
- [[[tabMock expect] andReturnBool:true] canGoBack]; |
+ manager->set_can_go_back(true); |
EXPECT_TRUE(toolbarModel_->CanGoBack()); |
- [[[tabMock expect] andReturnBool:false] canGoBack]; |
+ manager->set_can_go_back(false); |
EXPECT_FALSE(toolbarModel_->CanGoBack()); |
} |
TEST_F(ToolbarModelImplIOSTest, TestCanGoForward) { |
- OCMockObject* tabModelMock = static_cast<OCMockObject*>(tabModel_.get()); |
- id tabMock = [[TMITestTabMock alloc] |
- initWithRepresentedObject:[OCMockObject mockForClass:[Tab class]]]; |
- [[[tabModelMock stub] andReturn:tabMock] currentTab]; |
+ web_state_->SetNavigationManager( |
+ base::MakeUnique<ToolbarTestNavigationManager>()); |
+ ToolbarTestNavigationManager* manager = |
+ static_cast<ToolbarTestNavigationManager*>( |
+ web_state_->GetNavigationManager()); |
- [[[tabMock expect] andReturnBool:true] canGoForward]; |
+ manager->set_can_go_forward(true); |
EXPECT_TRUE(toolbarModel_->CanGoForward()); |
- [[[tabMock expect] andReturnBool:false] canGoForward]; |
+ manager->set_can_go_forward(false); |
EXPECT_FALSE(toolbarModel_->CanGoForward()); |
} |
TEST_F(ToolbarModelImplIOSTest, TestIsCurrentTabNativePage) { |
- OCMockObject* tabModelMock = static_cast<OCMockObject*>(tabModel_.get()); |
- id tabMock = [[TMITestTabMock alloc] |
- initWithRepresentedObject:[OCMockObject mockForClass:[Tab class]]]; |
- [[[tabModelMock stub] andReturn:tabMock] currentTab]; |
- |
- [tabMock setUrl:GURL(kNativeUrl)]; |
+ web_state_->SetCurrentURL(GURL(kNativeUrl)); |
EXPECT_TRUE(toolbarModel_->IsCurrentTabNativePage()); |
- [tabMock setUrl:GURL(kWebUrl)]; |
+ web_state_->SetCurrentURL(GURL(kWebUrl)); |
EXPECT_FALSE(toolbarModel_->IsCurrentTabNativePage()); |
} |
TEST_F(ToolbarModelImplIOSTest, TestIsCurrentTabBookmarked) { |
- ToolbarModelImplIOSTestWebState web_state(chrome_browser_state_.get()); |
- OCMockObject* tabModelMock = static_cast<OCMockObject*>(tabModel_.get()); |
- id tabMock = [[TMITestTabMock alloc] |
- initWithRepresentedObject:[OCMockObject mockForClass:[Tab class]]]; |
- [[[tabModelMock stub] andReturn:tabMock] currentTab]; |
- |
// Set the curent tab to |kWebUrl| and create a bookmark for |kWebUrl|, then |
// verify that the toolbar model indicates that the URL is bookmarked. |
- [static_cast<TMITestTabMock*>(tabMock) setWebState:&web_state]; |
- [static_cast<TMITestTabMock*>(tabMock) setUrl:GURL(kWebUrl)]; |
+ web_state_->SetCurrentURL(GURL(kWebUrl)); |
bookmarks::BookmarkModel* bookmark_model = |
ios::BookmarkModelFactory::GetForBrowserState( |
chrome_browser_state_.get()); |