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

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

Issue 2770223003: [ios] Switches ToolbarModelDelegateIOS to use WebStateList. (Closed)
Patch Set: Fix compile. Created 3 years, 8 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
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"
10 #include "base/message_loop/message_loop.h" 11 #include "base/message_loop/message_loop.h"
11 #include "base/strings/utf_string_conversions.h" 12 #include "base/strings/utf_string_conversions.h"
12 #include "components/bookmarks/browser/bookmark_model.h" 13 #include "components/bookmarks/browser/bookmark_model.h"
13 #include "components/bookmarks/test/bookmark_test_helpers.h" 14 #include "components/bookmarks/test/bookmark_test_helpers.h"
14 #include "components/toolbar/test_toolbar_model.h" 15 #include "components/toolbar/test_toolbar_model.h"
15 #include "ios/chrome/browser/bookmarks/bookmark_model_factory.h" 16 #include "ios/chrome/browser/bookmarks/bookmark_model_factory.h"
16 #include "ios/chrome/browser/browser_state/test_chrome_browser_state.h" 17 #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" 18 #include "ios/chrome/browser/ui/toolbar/toolbar_model_delegate_ios.h"
20 #include "ios/chrome/browser/ui/toolbar/toolbar_model_impl_ios.h" 19 #include "ios/chrome/browser/ui/toolbar/toolbar_model_impl_ios.h"
21 #import "ios/chrome/browser/xcallback_parameters.h" 20 #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"
22 #import "ios/testing/ocmock_complex_type_helper.h" 23 #import "ios/testing/ocmock_complex_type_helper.h"
24 #import "ios/web/public/test/fakes/test_navigation_manager.h"
23 #import "ios/web/public/test/fakes/test_web_state.h" 25 #import "ios/web/public/test/fakes/test_web_state.h"
24 #include "ios/web/public/test/test_web_thread.h" 26 #include "ios/web/public/test/test_web_thread.h"
25 #include "ios/web/public/test/test_web_thread_bundle.h" 27 #include "ios/web/public/test/test_web_thread_bundle.h"
26 #include "testing/gtest_mac.h" 28 #include "testing/gtest_mac.h"
27 #include "testing/platform_test.h" 29 #include "testing/platform_test.h"
28 #include "third_party/ocmock/gtest_support.h" 30 #include "third_party/ocmock/gtest_support.h"
29 #include "third_party/ocmock/ocmock_extensions.h" 31 #include "third_party/ocmock/ocmock_extensions.h"
30 32
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 { 33 namespace {
56 34
57 static const char kWebUrl[] = "http://www.chromium.org"; 35 static const char kWebUrl[] = "http://www.chromium.org";
58 static const char kNativeUrl[] = "chrome://version"; 36 static const char kNativeUrl[] = "chrome://version";
59 37
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
60 class ToolbarModelImplIOSTest : public PlatformTest { 75 class ToolbarModelImplIOSTest : public PlatformTest {
61 protected: 76 protected:
62 void SetUp() override { 77 void SetUp() override {
63 TestChromeBrowserState::Builder test_cbs_builder; 78 TestChromeBrowserState::Builder test_cbs_builder;
64 chrome_browser_state_ = test_cbs_builder.Build(); 79 chrome_browser_state_ = test_cbs_builder.Build();
65 chrome_browser_state_->CreateBookmarkModel(true); 80 chrome_browser_state_->CreateBookmarkModel(true);
66 bookmarks::test::WaitForBookmarkModelToLoad( 81 bookmarks::test::WaitForBookmarkModelToLoad(
67 ios::BookmarkModelFactory::GetForBrowserState( 82 ios::BookmarkModelFactory::GetForBrowserState(
68 chrome_browser_state_.get())); 83 chrome_browser_state_.get()));
69 84
70 tabModel_.reset([[OCMockObject niceMockForClass:[TabModel class]] retain]); 85 // Create a WebStateList that will always return the test WebState as
86 // the active WebState.
87 web_state_list_ = base::MakeUnique<WebStateList>(
sdefresne 2017/03/24 15:09:52 Should we have a TestWebStateList that always cont
rohitrao (ping after 24h) 2017/03/24 15:40:21 I think that would probably be useful? We can add
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);
sdefresne 2017/03/24 15:09:52 Should WebStateList always mark the first WebState
rohitrao (ping after 24h) 2017/03/24 15:40:21 No, I think it's better for callers to do this exp
71 95
72 toolbarModelDelegate_.reset(new ToolbarModelDelegateIOS(tabModel_.get())); 96 toolbarModelDelegate_.reset(
97 new ToolbarModelDelegateIOS(web_state_list_.get()));
73 toolbarModel_.reset(new ToolbarModelImplIOS(toolbarModelDelegate_.get())); 98 toolbarModel_.reset(new ToolbarModelImplIOS(toolbarModelDelegate_.get()));
74 } 99 }
75 100
76 web::TestWebThreadBundle thread_bundle_; 101 web::TestWebThreadBundle thread_bundle_;
77 std::unique_ptr<TestChromeBrowserState> chrome_browser_state_; 102 std::unique_ptr<TestChromeBrowserState> chrome_browser_state_;
78 base::scoped_nsobject<TabModel> tabModel_; 103 FakeWebStateListDelegate web_state_list_delegate_;
104 std::unique_ptr<WebStateList> web_state_list_;
105 ToolbarTestWebState* web_state_;
79 std::unique_ptr<ToolbarModelDelegateIOS> toolbarModelDelegate_; 106 std::unique_ptr<ToolbarModelDelegateIOS> toolbarModelDelegate_;
80 std::unique_ptr<ToolbarModelIOS> toolbarModel_; 107 std::unique_ptr<ToolbarModelIOS> toolbarModel_;
81 }; 108 };
82 109
83 class ToolbarModelImplIOSTestWebState : public web::TestWebState { 110 TEST_F(ToolbarModelImplIOSTest, TestWhenCurrentWebStateIsNull) {
84 public: 111 // The test fixture adds one WebState to the WebStateList, so remove it before
85 explicit ToolbarModelImplIOSTestWebState(web::BrowserState* browser_state) 112 // running this test.
86 : browser_state_(browser_state) {} 113 ASSERT_EQ(1, web_state_list_->count());
87 114 std::unique_ptr<web::WebState> closed_web_state(
88 web::BrowserState* GetBrowserState() const override { return browser_state_; } 115 web_state_list_->DetachWebStateAt(0));
89 double GetLoadingProgress() const override { return loading_progress_; } 116 ASSERT_TRUE(web_state_list_->empty());
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 117
106 EXPECT_FALSE(toolbarModel_->IsLoading()); 118 EXPECT_FALSE(toolbarModel_->IsLoading());
107 EXPECT_EQ(0, toolbarModel_->GetLoadProgressFraction()); 119 EXPECT_EQ(0, toolbarModel_->GetLoadProgressFraction());
108 EXPECT_FALSE(toolbarModel_->CanGoBack()); 120 EXPECT_FALSE(toolbarModel_->CanGoBack());
109 EXPECT_FALSE(toolbarModel_->CanGoForward()); 121 EXPECT_FALSE(toolbarModel_->CanGoForward());
110 EXPECT_FALSE(toolbarModel_->IsCurrentTabNativePage()); 122 EXPECT_FALSE(toolbarModel_->IsCurrentTabNativePage());
111 EXPECT_FALSE(toolbarModel_->IsCurrentTabBookmarked()); 123 EXPECT_FALSE(toolbarModel_->IsCurrentTabBookmarked());
112 } 124 }
113 125
114 TEST_F(ToolbarModelImplIOSTest, TestIsLoading) { 126 TEST_F(ToolbarModelImplIOSTest, TestIsLoading) {
115 OCMockObject* tabModelMock = static_cast<OCMockObject*>(tabModel_.get()); 127 // An active webstate that is loading.
116 id tabMock = [[TMITestTabMock alloc] 128 web_state_->SetLoading(true);
117 initWithRepresentedObject:[OCMockObject mockForClass:[Tab class]]]; 129 EXPECT_TRUE(toolbarModel_->IsLoading());
118 130
119 // Make mocks return a current tab with a null web state. 131 // An active webstate that is not loading.
120 [[[tabModelMock stub] andReturn:tabMock] currentTab]; 132 web_state_->SetLoading(false);
121 [tabMock setWebState:nullptr];
122 [static_cast<TMITestTabMock*>(tabMock) setUrl:GURL(kWebUrl)];
123 EXPECT_FALSE(toolbarModel_->IsLoading()); 133 EXPECT_FALSE(toolbarModel_->IsLoading());
124 134
125 // Make mocks return a current tab that is loading. 135 // An active webstate that is pointing at a native URL.
126 web::TestWebState webState; 136 web_state_->SetLoading(true);
127 [tabMock setWebState:&webState]; 137 web_state_->SetCurrentURL(GURL(kNativeUrl));
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()); 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
149 const CGFloat kExpectedProgress = 0.42; 142 const CGFloat kExpectedProgress = 0.42;
150 web_state.SetLoadingProgress(kExpectedProgress); 143 web_state_->set_loading_progress(kExpectedProgress);
151 EXPECT_FLOAT_EQ(kExpectedProgress, toolbarModel_->GetLoadProgressFraction()); 144 EXPECT_FLOAT_EQ(kExpectedProgress, toolbarModel_->GetLoadProgressFraction());
152 } 145 }
153 146
154 TEST_F(ToolbarModelImplIOSTest, TestCanGoBack) { 147 TEST_F(ToolbarModelImplIOSTest, TestCanGoBack) {
155 OCMockObject* tabModelMock = static_cast<OCMockObject*>(tabModel_.get()); 148 web_state_->SetNavigationManager(
156 id tabMock = [[TMITestTabMock alloc] 149 base::MakeUnique<ToolbarTestNavigationManager>());
157 initWithRepresentedObject:[OCMockObject mockForClass:[Tab class]]]; 150 ToolbarTestNavigationManager* manager =
158 [[[tabModelMock stub] andReturn:tabMock] currentTab]; 151 static_cast<ToolbarTestNavigationManager*>(
152 web_state_->GetNavigationManager());
159 153
160 [[[tabMock expect] andReturnBool:true] canGoBack]; 154 manager->set_can_go_back(true);
161 EXPECT_TRUE(toolbarModel_->CanGoBack()); 155 EXPECT_TRUE(toolbarModel_->CanGoBack());
162 156
163 [[[tabMock expect] andReturnBool:false] canGoBack]; 157 manager->set_can_go_back(false);
164 EXPECT_FALSE(toolbarModel_->CanGoBack()); 158 EXPECT_FALSE(toolbarModel_->CanGoBack());
165 } 159 }
166 160
167 TEST_F(ToolbarModelImplIOSTest, TestCanGoForward) { 161 TEST_F(ToolbarModelImplIOSTest, TestCanGoForward) {
168 OCMockObject* tabModelMock = static_cast<OCMockObject*>(tabModel_.get()); 162 web_state_->SetNavigationManager(
169 id tabMock = [[TMITestTabMock alloc] 163 base::MakeUnique<ToolbarTestNavigationManager>());
170 initWithRepresentedObject:[OCMockObject mockForClass:[Tab class]]]; 164 ToolbarTestNavigationManager* manager =
171 [[[tabModelMock stub] andReturn:tabMock] currentTab]; 165 static_cast<ToolbarTestNavigationManager*>(
166 web_state_->GetNavigationManager());
172 167
173 [[[tabMock expect] andReturnBool:true] canGoForward]; 168 manager->set_can_go_forward(true);
174 EXPECT_TRUE(toolbarModel_->CanGoForward()); 169 EXPECT_TRUE(toolbarModel_->CanGoForward());
175 170
176 [[[tabMock expect] andReturnBool:false] canGoForward]; 171 manager->set_can_go_forward(false);
177 EXPECT_FALSE(toolbarModel_->CanGoForward()); 172 EXPECT_FALSE(toolbarModel_->CanGoForward());
178 } 173 }
179 174
180 TEST_F(ToolbarModelImplIOSTest, TestIsCurrentTabNativePage) { 175 TEST_F(ToolbarModelImplIOSTest, TestIsCurrentTabNativePage) {
181 OCMockObject* tabModelMock = static_cast<OCMockObject*>(tabModel_.get()); 176 web_state_->SetCurrentURL(GURL(kNativeUrl));
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()); 177 EXPECT_TRUE(toolbarModel_->IsCurrentTabNativePage());
188 178
189 [tabMock setUrl:GURL(kWebUrl)]; 179 web_state_->SetCurrentURL(GURL(kWebUrl));
190 EXPECT_FALSE(toolbarModel_->IsCurrentTabNativePage()); 180 EXPECT_FALSE(toolbarModel_->IsCurrentTabNativePage());
191 } 181 }
192 182
193 TEST_F(ToolbarModelImplIOSTest, TestIsCurrentTabBookmarked) { 183 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 184 // 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. 185 // verify that the toolbar model indicates that the URL is bookmarked.
202 [static_cast<TMITestTabMock*>(tabMock) setWebState:&web_state]; 186 web_state_->SetCurrentURL(GURL(kWebUrl));
203 [static_cast<TMITestTabMock*>(tabMock) setUrl:GURL(kWebUrl)];
204 bookmarks::BookmarkModel* bookmark_model = 187 bookmarks::BookmarkModel* bookmark_model =
205 ios::BookmarkModelFactory::GetForBrowserState( 188 ios::BookmarkModelFactory::GetForBrowserState(
206 chrome_browser_state_.get()); 189 chrome_browser_state_.get());
207 const bookmarks::BookmarkNode* bookmarks = 190 const bookmarks::BookmarkNode* bookmarks =
208 bookmark_model->bookmark_bar_node(); 191 bookmark_model->bookmark_bar_node();
209 const bookmarks::BookmarkNode* node = 192 const bookmarks::BookmarkNode* node =
210 bookmark_model->AddURL(bookmarks, bookmarks->child_count(), 193 bookmark_model->AddURL(bookmarks, bookmarks->child_count(),
211 base::UTF8ToUTF16(kWebUrl), GURL(kWebUrl)); 194 base::UTF8ToUTF16(kWebUrl), GURL(kWebUrl));
212 EXPECT_TRUE(toolbarModel_->IsCurrentTabBookmarked()); 195 EXPECT_TRUE(toolbarModel_->IsCurrentTabBookmarked());
213 196
214 // Remove the bookmark and verify the toolbar model indicates that the URL is 197 // Remove the bookmark and verify the toolbar model indicates that the URL is
215 // not bookmarked. 198 // not bookmarked.
216 bookmark_model->Remove(node); 199 bookmark_model->Remove(node);
217 EXPECT_FALSE(toolbarModel_->IsCurrentTabBookmarked()); 200 EXPECT_FALSE(toolbarModel_->IsCurrentTabBookmarked());
218 } 201 }
219 202
220 } // namespace 203 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698