Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(331)

Side by Side Diff: ios/chrome/browser/ui/toolbar/toolbar_model_impl_ios_unittest.mm

Issue 2775943002: Revert of [ios] Switches ToolbarModelDelegateIOS to use WebStateList. (Closed)
Patch Set: Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ios/chrome/browser/ui/toolbar/toolbar_model_impl_ios.mm ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 The Chromium Authors. All rights reserved. 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 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 <Foundation/Foundation.h> 5 #import <Foundation/Foundation.h>
6 6
7 #include <memory> 7 #include <memory>
8 8
9 #include "base/mac/scoped_nsobject.h" 9 #include "base/mac/scoped_nsobject.h"
10 #include "base/memory/ptr_util.h"
11 #include "base/message_loop/message_loop.h" 10 #include "base/message_loop/message_loop.h"
12 #include "base/strings/utf_string_conversions.h" 11 #include "base/strings/utf_string_conversions.h"
13 #include "components/bookmarks/browser/bookmark_model.h" 12 #include "components/bookmarks/browser/bookmark_model.h"
14 #include "components/bookmarks/test/bookmark_test_helpers.h" 13 #include "components/bookmarks/test/bookmark_test_helpers.h"
15 #include "components/toolbar/test_toolbar_model.h" 14 #include "components/toolbar/test_toolbar_model.h"
16 #include "ios/chrome/browser/bookmarks/bookmark_model_factory.h" 15 #include "ios/chrome/browser/bookmarks/bookmark_model_factory.h"
17 #include "ios/chrome/browser/browser_state/test_chrome_browser_state.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"
18 #include "ios/chrome/browser/ui/toolbar/toolbar_model_delegate_ios.h" 19 #include "ios/chrome/browser/ui/toolbar/toolbar_model_delegate_ios.h"
19 #include "ios/chrome/browser/ui/toolbar/toolbar_model_impl_ios.h" 20 #include "ios/chrome/browser/ui/toolbar/toolbar_model_impl_ios.h"
20 #import "ios/chrome/browser/xcallback_parameters.h" 21 #import "ios/chrome/browser/xcallback_parameters.h"
21 #include "ios/shared/chrome/browser/tabs/fake_web_state_list_delegate.h"
22 #include "ios/shared/chrome/browser/tabs/web_state_list.h"
23 #import "ios/testing/ocmock_complex_type_helper.h" 22 #import "ios/testing/ocmock_complex_type_helper.h"
24 #import "ios/web/public/test/fakes/test_navigation_manager.h"
25 #import "ios/web/public/test/fakes/test_web_state.h" 23 #import "ios/web/public/test/fakes/test_web_state.h"
26 #include "ios/web/public/test/test_web_thread.h" 24 #include "ios/web/public/test/test_web_thread.h"
27 #include "ios/web/public/test/test_web_thread_bundle.h" 25 #include "ios/web/public/test/test_web_thread_bundle.h"
28 #include "testing/gtest_mac.h" 26 #include "testing/gtest_mac.h"
29 #include "testing/platform_test.h" 27 #include "testing/platform_test.h"
30 #include "third_party/ocmock/gtest_support.h" 28 #include "third_party/ocmock/gtest_support.h"
31 #include "third_party/ocmock/ocmock_extensions.h" 29 #include "third_party/ocmock/ocmock_extensions.h"
32 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
33 namespace { 55 namespace {
34 56
35 static const char kWebUrl[] = "http://www.chromium.org"; 57 static const char kWebUrl[] = "http://www.chromium.org";
36 static const char kNativeUrl[] = "chrome://version"; 58 static const char kNativeUrl[] = "chrome://version";
37 59
38 namespace {
39
40 class ToolbarTestWebState : public web::TestWebState {
41 public:
42 ToolbarTestWebState() : loading_progress_(0) {}
43
44 double GetLoadingProgress() const override { return loading_progress_; }
45 void set_loading_progress(double loading_progress) {
46 loading_progress_ = loading_progress;
47 }
48
49 private:
50 double loading_progress_;
51
52 DISALLOW_COPY_AND_ASSIGN(ToolbarTestWebState);
53 };
54
55 class ToolbarTestNavigationManager : public web::TestNavigationManager {
56 public:
57 ToolbarTestNavigationManager()
58 : can_go_back_(false), can_go_forward_(false) {}
59
60 bool CanGoBack() const override { return can_go_back_; }
61 bool CanGoForward() const override { return can_go_forward_; }
62
63 void set_can_go_back(bool can_go_back) { can_go_back_ = can_go_back; }
64 void set_can_go_forward(bool can_go_forward) {
65 can_go_forward_ = can_go_forward;
66 }
67
68 private:
69 bool can_go_back_;
70 bool can_go_forward_;
71 };
72
73 } // namespace
74
75 class ToolbarModelImplIOSTest : public PlatformTest { 60 class ToolbarModelImplIOSTest : public PlatformTest {
76 protected: 61 protected:
77 void SetUp() override { 62 void SetUp() override {
78 TestChromeBrowserState::Builder test_cbs_builder; 63 TestChromeBrowserState::Builder test_cbs_builder;
79 chrome_browser_state_ = test_cbs_builder.Build(); 64 chrome_browser_state_ = test_cbs_builder.Build();
80 chrome_browser_state_->CreateBookmarkModel(true); 65 chrome_browser_state_->CreateBookmarkModel(true);
81 bookmarks::test::WaitForBookmarkModelToLoad( 66 bookmarks::test::WaitForBookmarkModelToLoad(
82 ios::BookmarkModelFactory::GetForBrowserState( 67 ios::BookmarkModelFactory::GetForBrowserState(
83 chrome_browser_state_.get())); 68 chrome_browser_state_.get()));
84 69
85 // Create a WebStateList that will always return the test WebState as 70 tabModel_.reset([[OCMockObject niceMockForClass:[TabModel class]] retain]);
86 // the active WebState.
87 web_state_list_ = base::MakeUnique<WebStateList>(
88 &web_state_list_delegate_, WebStateList::WebStateOwned);
89 std::unique_ptr<ToolbarTestWebState> web_state =
90 base::MakeUnique<ToolbarTestWebState>();
91 web_state->SetBrowserState(chrome_browser_state_.get());
92 web_state_ = web_state.get();
93 web_state_list_->InsertWebState(0, web_state.release());
94 web_state_list_->ActivateWebStateAt(0);
95 71
96 toolbarModelDelegate_.reset( 72 toolbarModelDelegate_.reset(new ToolbarModelDelegateIOS(tabModel_.get()));
97 new ToolbarModelDelegateIOS(web_state_list_.get()));
98 toolbarModel_.reset(new ToolbarModelImplIOS(toolbarModelDelegate_.get())); 73 toolbarModel_.reset(new ToolbarModelImplIOS(toolbarModelDelegate_.get()));
99 } 74 }
100 75
101 web::TestWebThreadBundle thread_bundle_; 76 web::TestWebThreadBundle thread_bundle_;
102 std::unique_ptr<TestChromeBrowserState> chrome_browser_state_; 77 std::unique_ptr<TestChromeBrowserState> chrome_browser_state_;
103 FakeWebStateListDelegate web_state_list_delegate_; 78 base::scoped_nsobject<TabModel> tabModel_;
104 std::unique_ptr<WebStateList> web_state_list_;
105 ToolbarTestWebState* web_state_;
106 std::unique_ptr<ToolbarModelDelegateIOS> toolbarModelDelegate_; 79 std::unique_ptr<ToolbarModelDelegateIOS> toolbarModelDelegate_;
107 std::unique_ptr<ToolbarModelIOS> toolbarModel_; 80 std::unique_ptr<ToolbarModelIOS> toolbarModel_;
108 }; 81 };
109 82
110 TEST_F(ToolbarModelImplIOSTest, TestWhenCurrentWebStateIsNull) { 83 class ToolbarModelImplIOSTestWebState : public web::TestWebState {
111 // The test fixture adds one WebState to the WebStateList, so remove it before 84 public:
112 // running this test. 85 explicit ToolbarModelImplIOSTestWebState(web::BrowserState* browser_state)
113 ASSERT_EQ(1, web_state_list_->count()); 86 : browser_state_(browser_state) {}
114 std::unique_ptr<web::WebState> closed_web_state( 87
115 web_state_list_->DetachWebStateAt(0)); 88 web::BrowserState* GetBrowserState() const override { return browser_state_; }
116 ASSERT_TRUE(web_state_list_->empty()); 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];
117 105
118 EXPECT_FALSE(toolbarModel_->IsLoading()); 106 EXPECT_FALSE(toolbarModel_->IsLoading());
119 EXPECT_EQ(0, toolbarModel_->GetLoadProgressFraction()); 107 EXPECT_EQ(0, toolbarModel_->GetLoadProgressFraction());
120 EXPECT_FALSE(toolbarModel_->CanGoBack()); 108 EXPECT_FALSE(toolbarModel_->CanGoBack());
121 EXPECT_FALSE(toolbarModel_->CanGoForward()); 109 EXPECT_FALSE(toolbarModel_->CanGoForward());
122 EXPECT_FALSE(toolbarModel_->IsCurrentTabNativePage()); 110 EXPECT_FALSE(toolbarModel_->IsCurrentTabNativePage());
123 EXPECT_FALSE(toolbarModel_->IsCurrentTabBookmarked()); 111 EXPECT_FALSE(toolbarModel_->IsCurrentTabBookmarked());
124 } 112 }
125 113
126 TEST_F(ToolbarModelImplIOSTest, TestIsLoading) { 114 TEST_F(ToolbarModelImplIOSTest, TestIsLoading) {
127 // An active webstate that is loading. 115 OCMockObject* tabModelMock = static_cast<OCMockObject*>(tabModel_.get());
128 web_state_->SetLoading(true); 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()); 129 EXPECT_TRUE(toolbarModel_->IsLoading());
130 130
131 // An active webstate that is not loading. 131 // Make mocks return a current tab that is not loading.
132 web_state_->SetLoading(false); 132 webState.SetLoading(false);
133 EXPECT_FALSE(toolbarModel_->IsLoading()); 133 EXPECT_FALSE(toolbarModel_->IsLoading());
134 134
135 // An active webstate that is pointing at a native URL. 135 // Make mocks return a current tab that is pointing at a native URL.
136 web_state_->SetLoading(true); 136 webState.SetLoading(true);
137 web_state_->SetCurrentURL(GURL(kNativeUrl)); 137 [static_cast<TMITestTabMock*>(tabMock) setUrl:GURL(kNativeUrl)];
138 EXPECT_FALSE(toolbarModel_->IsLoading()); 138 EXPECT_FALSE(toolbarModel_->IsLoading());
139 } 139 }
140 140
141 TEST_F(ToolbarModelImplIOSTest, TestGetLoadProgressFraction) { 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
142 const CGFloat kExpectedProgress = 0.42; 149 const CGFloat kExpectedProgress = 0.42;
143 web_state_->set_loading_progress(kExpectedProgress); 150 web_state.SetLoadingProgress(kExpectedProgress);
144 EXPECT_FLOAT_EQ(kExpectedProgress, toolbarModel_->GetLoadProgressFraction()); 151 EXPECT_FLOAT_EQ(kExpectedProgress, toolbarModel_->GetLoadProgressFraction());
145 } 152 }
146 153
147 TEST_F(ToolbarModelImplIOSTest, TestCanGoBack) { 154 TEST_F(ToolbarModelImplIOSTest, TestCanGoBack) {
148 web_state_->SetNavigationManager( 155 OCMockObject* tabModelMock = static_cast<OCMockObject*>(tabModel_.get());
149 base::MakeUnique<ToolbarTestNavigationManager>()); 156 id tabMock = [[TMITestTabMock alloc]
150 ToolbarTestNavigationManager* manager = 157 initWithRepresentedObject:[OCMockObject mockForClass:[Tab class]]];
151 static_cast<ToolbarTestNavigationManager*>( 158 [[[tabModelMock stub] andReturn:tabMock] currentTab];
152 web_state_->GetNavigationManager());
153 159
154 manager->set_can_go_back(true); 160 [[[tabMock expect] andReturnBool:true] canGoBack];
155 EXPECT_TRUE(toolbarModel_->CanGoBack()); 161 EXPECT_TRUE(toolbarModel_->CanGoBack());
156 162
157 manager->set_can_go_back(false); 163 [[[tabMock expect] andReturnBool:false] canGoBack];
158 EXPECT_FALSE(toolbarModel_->CanGoBack()); 164 EXPECT_FALSE(toolbarModel_->CanGoBack());
159 } 165 }
160 166
161 TEST_F(ToolbarModelImplIOSTest, TestCanGoForward) { 167 TEST_F(ToolbarModelImplIOSTest, TestCanGoForward) {
162 web_state_->SetNavigationManager( 168 OCMockObject* tabModelMock = static_cast<OCMockObject*>(tabModel_.get());
163 base::MakeUnique<ToolbarTestNavigationManager>()); 169 id tabMock = [[TMITestTabMock alloc]
164 ToolbarTestNavigationManager* manager = 170 initWithRepresentedObject:[OCMockObject mockForClass:[Tab class]]];
165 static_cast<ToolbarTestNavigationManager*>( 171 [[[tabModelMock stub] andReturn:tabMock] currentTab];
166 web_state_->GetNavigationManager());
167 172
168 manager->set_can_go_forward(true); 173 [[[tabMock expect] andReturnBool:true] canGoForward];
169 EXPECT_TRUE(toolbarModel_->CanGoForward()); 174 EXPECT_TRUE(toolbarModel_->CanGoForward());
170 175
171 manager->set_can_go_forward(false); 176 [[[tabMock expect] andReturnBool:false] canGoForward];
172 EXPECT_FALSE(toolbarModel_->CanGoForward()); 177 EXPECT_FALSE(toolbarModel_->CanGoForward());
173 } 178 }
174 179
175 TEST_F(ToolbarModelImplIOSTest, TestIsCurrentTabNativePage) { 180 TEST_F(ToolbarModelImplIOSTest, TestIsCurrentTabNativePage) {
176 web_state_->SetCurrentURL(GURL(kNativeUrl)); 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)];
177 EXPECT_TRUE(toolbarModel_->IsCurrentTabNativePage()); 187 EXPECT_TRUE(toolbarModel_->IsCurrentTabNativePage());
178 188
179 web_state_->SetCurrentURL(GURL(kWebUrl)); 189 [tabMock setUrl:GURL(kWebUrl)];
180 EXPECT_FALSE(toolbarModel_->IsCurrentTabNativePage()); 190 EXPECT_FALSE(toolbarModel_->IsCurrentTabNativePage());
181 } 191 }
182 192
183 TEST_F(ToolbarModelImplIOSTest, TestIsCurrentTabBookmarked) { 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
184 // Set the curent tab to |kWebUrl| and create a bookmark for |kWebUrl|, then 200 // Set the curent tab to |kWebUrl| and create a bookmark for |kWebUrl|, then
185 // verify that the toolbar model indicates that the URL is bookmarked. 201 // verify that the toolbar model indicates that the URL is bookmarked.
186 web_state_->SetCurrentURL(GURL(kWebUrl)); 202 [static_cast<TMITestTabMock*>(tabMock) setWebState:&web_state];
203 [static_cast<TMITestTabMock*>(tabMock) setUrl:GURL(kWebUrl)];
187 bookmarks::BookmarkModel* bookmark_model = 204 bookmarks::BookmarkModel* bookmark_model =
188 ios::BookmarkModelFactory::GetForBrowserState( 205 ios::BookmarkModelFactory::GetForBrowserState(
189 chrome_browser_state_.get()); 206 chrome_browser_state_.get());
190 const bookmarks::BookmarkNode* bookmarks = 207 const bookmarks::BookmarkNode* bookmarks =
191 bookmark_model->bookmark_bar_node(); 208 bookmark_model->bookmark_bar_node();
192 const bookmarks::BookmarkNode* node = 209 const bookmarks::BookmarkNode* node =
193 bookmark_model->AddURL(bookmarks, bookmarks->child_count(), 210 bookmark_model->AddURL(bookmarks, bookmarks->child_count(),
194 base::UTF8ToUTF16(kWebUrl), GURL(kWebUrl)); 211 base::UTF8ToUTF16(kWebUrl), GURL(kWebUrl));
195 EXPECT_TRUE(toolbarModel_->IsCurrentTabBookmarked()); 212 EXPECT_TRUE(toolbarModel_->IsCurrentTabBookmarked());
196 213
197 // Remove the bookmark and verify the toolbar model indicates that the URL is 214 // Remove the bookmark and verify the toolbar model indicates that the URL is
198 // not bookmarked. 215 // not bookmarked.
199 bookmark_model->Remove(node); 216 bookmark_model->Remove(node);
200 EXPECT_FALSE(toolbarModel_->IsCurrentTabBookmarked()); 217 EXPECT_FALSE(toolbarModel_->IsCurrentTabBookmarked());
201 } 218 }
202 219
203 } // namespace 220 } // namespace
OLDNEW
« no previous file with comments | « ios/chrome/browser/ui/toolbar/toolbar_model_impl_ios.mm ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698