OLD | NEW |
(Empty) | |
| 1 // Copyright 2012 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 #import <Foundation/Foundation.h> |
| 6 |
| 7 #include <memory> |
| 8 |
| 9 #include "base/mac/scoped_nsobject.h" |
| 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/strings/utf_string_conversions.h" |
| 12 #include "components/bookmarks/browser/bookmark_model.h" |
| 13 #include "components/bookmarks/test/bookmark_test_helpers.h" |
| 14 #include "components/toolbar/test_toolbar_model.h" |
| 15 #include "ios/chrome/browser/bookmarks/bookmark_model_factory.h" |
| 16 #include "ios/chrome/browser/browser_state/test_chrome_browser_state.h" |
| 17 #import "ios/chrome/browser/tabs/tab.h" |
| 18 #import "ios/chrome/browser/tabs/tab_model.h" |
| 19 #include "ios/chrome/browser/ui/toolbar/toolbar_model_delegate_ios.h" |
| 20 #include "ios/chrome/browser/ui/toolbar/toolbar_model_impl_ios.h" |
| 21 #import "ios/chrome/browser/xcallback_parameters.h" |
| 22 #import "ios/testing/ocmock_complex_type_helper.h" |
| 23 #include "ios/web/public/test/test_web_state.h" |
| 24 #include "ios/web/public/test/test_web_thread.h" |
| 25 #include "ios/web/public/test/test_web_thread_bundle.h" |
| 26 #include "testing/gtest_mac.h" |
| 27 #include "testing/platform_test.h" |
| 28 #include "third_party/ocmock/gtest_support.h" |
| 29 #include "third_party/ocmock/ocmock_extensions.h" |
| 30 |
| 31 @interface TMITestTabMock : OCMockComplexTypeHelper { |
| 32 GURL url_; |
| 33 web::WebState* web_state_; |
| 34 } |
| 35 |
| 36 @property(nonatomic, assign) const GURL& url; |
| 37 @property(nonatomic, assign) web::WebState* webState; |
| 38 @end |
| 39 |
| 40 @implementation TMITestTabMock |
| 41 - (const GURL&)url { |
| 42 return url_; |
| 43 } |
| 44 - (void)setUrl:(const GURL&)url { |
| 45 url_ = url; |
| 46 } |
| 47 - (web::WebState*)webState { |
| 48 return web_state_; |
| 49 } |
| 50 - (void)setWebState:(web::WebState*)web_state { |
| 51 web_state_ = web_state; |
| 52 } |
| 53 @end |
| 54 |
| 55 namespace { |
| 56 |
| 57 static const char kWebUrl[] = "http://www.chromium.org"; |
| 58 static const char kNativeUrl[] = "chrome://version"; |
| 59 |
| 60 class ToolbarModelImplIOSTest : public PlatformTest { |
| 61 protected: |
| 62 void SetUp() override { |
| 63 TestChromeBrowserState::Builder test_cbs_builder; |
| 64 chrome_browser_state_ = test_cbs_builder.Build(); |
| 65 chrome_browser_state_->CreateBookmarkModel(true); |
| 66 bookmarks::test::WaitForBookmarkModelToLoad( |
| 67 ios::BookmarkModelFactory::GetForBrowserState( |
| 68 chrome_browser_state_.get())); |
| 69 |
| 70 tabModel_.reset([[OCMockObject niceMockForClass:[TabModel class]] retain]); |
| 71 |
| 72 toolbarModelDelegate_.reset(new ToolbarModelDelegateIOS(tabModel_.get())); |
| 73 toolbarModel_.reset(new ToolbarModelImplIOS(toolbarModelDelegate_.get())); |
| 74 } |
| 75 |
| 76 web::TestWebThreadBundle thread_bundle_; |
| 77 std::unique_ptr<TestChromeBrowserState> chrome_browser_state_; |
| 78 base::scoped_nsobject<TabModel> tabModel_; |
| 79 std::unique_ptr<ToolbarModelDelegateIOS> toolbarModelDelegate_; |
| 80 std::unique_ptr<ToolbarModelIOS> toolbarModel_; |
| 81 }; |
| 82 |
| 83 class ToolbarModelImplIOSTestWebState : public web::TestWebState { |
| 84 public: |
| 85 explicit ToolbarModelImplIOSTestWebState(web::BrowserState* browser_state) |
| 86 : browser_state_(browser_state) {} |
| 87 |
| 88 web::BrowserState* GetBrowserState() const override { return browser_state_; } |
| 89 double GetLoadingProgress() const override { return loading_progress_; } |
| 90 void SetLoadingProgress(double loading_progress) { |
| 91 loading_progress_ = loading_progress; |
| 92 } |
| 93 |
| 94 private: |
| 95 web::BrowserState* browser_state_; |
| 96 double loading_progress_; |
| 97 |
| 98 DISALLOW_COPY_AND_ASSIGN(ToolbarModelImplIOSTestWebState); |
| 99 }; |
| 100 |
| 101 TEST_F(ToolbarModelImplIOSTest, TestWhenCurrentTabIsNull) { |
| 102 // Make a mock to always return NULL for the current tab. |
| 103 OCMockObject* tabModelMock = static_cast<OCMockObject*>(tabModel_.get()); |
| 104 [[[tabModelMock stub] andReturn:NULL] currentTab]; |
| 105 |
| 106 EXPECT_FALSE(toolbarModel_->IsLoading()); |
| 107 EXPECT_EQ(0, toolbarModel_->GetLoadProgressFraction()); |
| 108 EXPECT_FALSE(toolbarModel_->CanGoBack()); |
| 109 EXPECT_FALSE(toolbarModel_->CanGoForward()); |
| 110 EXPECT_FALSE(toolbarModel_->IsCurrentTabNativePage()); |
| 111 EXPECT_FALSE(toolbarModel_->IsCurrentTabBookmarked()); |
| 112 } |
| 113 |
| 114 TEST_F(ToolbarModelImplIOSTest, TestIsLoading) { |
| 115 OCMockObject* tabModelMock = static_cast<OCMockObject*>(tabModel_.get()); |
| 116 id tabMock = [[TMITestTabMock alloc] |
| 117 initWithRepresentedObject:[OCMockObject mockForClass:[Tab class]]]; |
| 118 |
| 119 // Make mocks return a current tab with a null web state. |
| 120 [[[tabModelMock stub] andReturn:tabMock] currentTab]; |
| 121 [tabMock setWebState:nullptr]; |
| 122 [static_cast<TMITestTabMock*>(tabMock) setUrl:GURL(kWebUrl)]; |
| 123 EXPECT_FALSE(toolbarModel_->IsLoading()); |
| 124 |
| 125 // Make mocks return a current tab that is loading. |
| 126 web::TestWebState webState; |
| 127 [tabMock setWebState:&webState]; |
| 128 webState.SetLoading(true); |
| 129 EXPECT_TRUE(toolbarModel_->IsLoading()); |
| 130 |
| 131 // Make mocks return a current tab that is not loading. |
| 132 webState.SetLoading(false); |
| 133 EXPECT_FALSE(toolbarModel_->IsLoading()); |
| 134 |
| 135 // Make mocks return a current tab that is pointing at a native URL. |
| 136 webState.SetLoading(true); |
| 137 [static_cast<TMITestTabMock*>(tabMock) setUrl:GURL(kNativeUrl)]; |
| 138 EXPECT_FALSE(toolbarModel_->IsLoading()); |
| 139 } |
| 140 |
| 141 TEST_F(ToolbarModelImplIOSTest, TestGetLoadProgressFraction) { |
| 142 ToolbarModelImplIOSTestWebState web_state(chrome_browser_state_.get()); |
| 143 OCMockObject* tabModelMock = static_cast<OCMockObject*>(tabModel_.get()); |
| 144 id tabMock = [[TMITestTabMock alloc] |
| 145 initWithRepresentedObject:[OCMockObject mockForClass:[Tab class]]]; |
| 146 [static_cast<TMITestTabMock*>(tabMock) setWebState:&web_state]; |
| 147 [[[tabModelMock stub] andReturn:tabMock] currentTab]; |
| 148 |
| 149 const CGFloat kExpectedProgress = 0.42; |
| 150 web_state.SetLoadingProgress(kExpectedProgress); |
| 151 EXPECT_FLOAT_EQ(kExpectedProgress, toolbarModel_->GetLoadProgressFraction()); |
| 152 } |
| 153 |
| 154 TEST_F(ToolbarModelImplIOSTest, TestCanGoBack) { |
| 155 OCMockObject* tabModelMock = static_cast<OCMockObject*>(tabModel_.get()); |
| 156 id tabMock = [[TMITestTabMock alloc] |
| 157 initWithRepresentedObject:[OCMockObject mockForClass:[Tab class]]]; |
| 158 [[[tabModelMock stub] andReturn:tabMock] currentTab]; |
| 159 |
| 160 [[[tabMock expect] andReturnBool:true] canGoBack]; |
| 161 EXPECT_TRUE(toolbarModel_->CanGoBack()); |
| 162 |
| 163 [[[tabMock expect] andReturnBool:false] canGoBack]; |
| 164 EXPECT_FALSE(toolbarModel_->CanGoBack()); |
| 165 } |
| 166 |
| 167 TEST_F(ToolbarModelImplIOSTest, TestCanGoForward) { |
| 168 OCMockObject* tabModelMock = static_cast<OCMockObject*>(tabModel_.get()); |
| 169 id tabMock = [[TMITestTabMock alloc] |
| 170 initWithRepresentedObject:[OCMockObject mockForClass:[Tab class]]]; |
| 171 [[[tabModelMock stub] andReturn:tabMock] currentTab]; |
| 172 |
| 173 [[[tabMock expect] andReturnBool:true] canGoForward]; |
| 174 EXPECT_TRUE(toolbarModel_->CanGoForward()); |
| 175 |
| 176 [[[tabMock expect] andReturnBool:false] canGoForward]; |
| 177 EXPECT_FALSE(toolbarModel_->CanGoForward()); |
| 178 } |
| 179 |
| 180 TEST_F(ToolbarModelImplIOSTest, TestIsCurrentTabNativePage) { |
| 181 OCMockObject* tabModelMock = static_cast<OCMockObject*>(tabModel_.get()); |
| 182 id tabMock = [[TMITestTabMock alloc] |
| 183 initWithRepresentedObject:[OCMockObject mockForClass:[Tab class]]]; |
| 184 [[[tabModelMock stub] andReturn:tabMock] currentTab]; |
| 185 |
| 186 [tabMock setUrl:GURL(kNativeUrl)]; |
| 187 EXPECT_TRUE(toolbarModel_->IsCurrentTabNativePage()); |
| 188 |
| 189 [tabMock setUrl:GURL(kWebUrl)]; |
| 190 EXPECT_FALSE(toolbarModel_->IsCurrentTabNativePage()); |
| 191 } |
| 192 |
| 193 TEST_F(ToolbarModelImplIOSTest, TestIsCurrentTabBookmarked) { |
| 194 ToolbarModelImplIOSTestWebState web_state(chrome_browser_state_.get()); |
| 195 OCMockObject* tabModelMock = static_cast<OCMockObject*>(tabModel_.get()); |
| 196 id tabMock = [[TMITestTabMock alloc] |
| 197 initWithRepresentedObject:[OCMockObject mockForClass:[Tab class]]]; |
| 198 [[[tabModelMock stub] andReturn:tabMock] currentTab]; |
| 199 |
| 200 // Set the curent tab to |kWebUrl| and create a bookmark for |kWebUrl|, then |
| 201 // verify that the toolbar model indicates that the URL is bookmarked. |
| 202 [static_cast<TMITestTabMock*>(tabMock) setWebState:&web_state]; |
| 203 [static_cast<TMITestTabMock*>(tabMock) setUrl:GURL(kWebUrl)]; |
| 204 bookmarks::BookmarkModel* bookmark_model = |
| 205 ios::BookmarkModelFactory::GetForBrowserState( |
| 206 chrome_browser_state_.get()); |
| 207 const bookmarks::BookmarkNode* bookmarks = |
| 208 bookmark_model->bookmark_bar_node(); |
| 209 const bookmarks::BookmarkNode* node = |
| 210 bookmark_model->AddURL(bookmarks, bookmarks->child_count(), |
| 211 base::UTF8ToUTF16(kWebUrl), GURL(kWebUrl)); |
| 212 EXPECT_TRUE(toolbarModel_->IsCurrentTabBookmarked()); |
| 213 |
| 214 // Remove the bookmark and verify the toolbar model indicates that the URL is |
| 215 // not bookmarked. |
| 216 bookmark_model->Remove(node); |
| 217 EXPECT_FALSE(toolbarModel_->IsCurrentTabBookmarked()); |
| 218 } |
| 219 |
| 220 } // namespace |
OLD | NEW |