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 "ios/chrome/browser/ui/ntp/new_tab_page_controller.h" |
| 6 |
| 7 #include <memory> |
| 8 |
| 9 #include "base/mac/scoped_nsautorelease_pool.h" |
| 10 #include "base/mac/scoped_nsobject.h" |
| 11 #include "base/memory/ptr_util.h" |
| 12 #include "base/message_loop/message_loop.h" |
| 13 #include "components/bookmarks/test/bookmark_test_helpers.h" |
| 14 #include "components/prefs/testing_pref_service.h" |
| 15 #include "components/search_engines/template_url_service.h" |
| 16 #include "components/sessions/core/tab_restore_service.h" |
| 17 #include "ios/chrome/browser/bookmarks/bookmark_model_factory.h" |
| 18 #include "ios/chrome/browser/browser_state/test_chrome_browser_state.h" |
| 19 #include "ios/chrome/browser/chrome_url_constants.h" |
| 20 #include "ios/chrome/browser/search_engines/template_url_service_factory.h" |
| 21 #include "ios/chrome/browser/sessions/ios_chrome_tab_restore_service_factory.h" |
| 22 #import "ios/chrome/browser/ui/ntp/new_tab_page_view.h" |
| 23 #include "ios/chrome/browser/ui/ui_util.h" |
| 24 #include "ios/chrome/test/block_cleanup_test.h" |
| 25 #include "ios/chrome/test/ios_chrome_scoped_testing_local_state.h" |
| 26 #include "ios/chrome/test/testing_application_context.h" |
| 27 #include "ios/web/public/test/test_web_thread_bundle.h" |
| 28 #include "testing/gtest/include/gtest/gtest.h" |
| 29 #include "testing/gtest_mac.h" |
| 30 #import "third_party/ocmock/OCMock/OCMock.h" |
| 31 #import "third_party/ocmock/gtest_support.h" |
| 32 |
| 33 @interface NewTabPageController (TestSupport) |
| 34 - (id<NewTabPagePanelProtocol>)currentController; |
| 35 - (id<NewTabPagePanelProtocol>)bookmarkController; |
| 36 - (id<NewTabPagePanelProtocol>)incognitoController; |
| 37 @end |
| 38 |
| 39 @interface NewTabPageController (PrivateMethods) |
| 40 @property(nonatomic, retain) NewTabPageView* ntpView; |
| 41 @end |
| 42 |
| 43 @implementation NewTabPageController (TestSupport) |
| 44 |
| 45 - (id<NewTabPagePanelProtocol>)currentController { |
| 46 return currentController_; |
| 47 } |
| 48 |
| 49 - (id<NewTabPagePanelProtocol>)bookmarkController { |
| 50 return bookmarkController_.get(); |
| 51 } |
| 52 |
| 53 - (id<NewTabPagePanelProtocol>)incognitoController { |
| 54 return incognitoController_.get(); |
| 55 } |
| 56 |
| 57 @end |
| 58 |
| 59 namespace { |
| 60 |
| 61 class NewTabPageControllerTest : public BlockCleanupTest { |
| 62 protected: |
| 63 void SetUp() override { |
| 64 BlockCleanupTest::SetUp(); |
| 65 |
| 66 // Set up a test ChromeBrowserState instance. |
| 67 TestChromeBrowserState::Builder test_cbs_builder; |
| 68 test_cbs_builder.AddTestingFactory( |
| 69 IOSChromeTabRestoreServiceFactory::GetInstance(), |
| 70 IOSChromeTabRestoreServiceFactory::GetDefaultFactory()); |
| 71 test_cbs_builder.AddTestingFactory( |
| 72 ios::TemplateURLServiceFactory::GetInstance(), |
| 73 ios::TemplateURLServiceFactory::GetDefaultFactory()); |
| 74 chrome_browser_state_ = test_cbs_builder.Build(); |
| 75 |
| 76 // Load TemplateURLService. |
| 77 TemplateURLService* template_url_service = |
| 78 ios::TemplateURLServiceFactory::GetForBrowserState( |
| 79 chrome_browser_state_.get()); |
| 80 template_url_service->Load(); |
| 81 |
| 82 chrome_browser_state_->CreateBookmarkModel(true); |
| 83 bookmarks::test::WaitForBookmarkModelToLoad( |
| 84 ios::BookmarkModelFactory::GetForBrowserState( |
| 85 chrome_browser_state_.get())); |
| 86 GURL url(kChromeUINewTabURL); |
| 87 controller_.reset([[NewTabPageController alloc] |
| 88 initWithUrl:url |
| 89 loader:nil |
| 90 focuser:nil |
| 91 ntpObserver:nil |
| 92 browserState:chrome_browser_state_.get() |
| 93 colorCache:nil |
| 94 webToolbarDelegate:nil |
| 95 tabModel:nil]); |
| 96 |
| 97 incognitoController_.reset([[NewTabPageController alloc] |
| 98 initWithUrl:url |
| 99 loader:nil |
| 100 focuser:nil |
| 101 ntpObserver:nil |
| 102 browserState:chrome_browser_state_ |
| 103 ->GetOffTheRecordChromeBrowserState() |
| 104 colorCache:nil |
| 105 webToolbarDelegate:nil |
| 106 tabModel:nil]); |
| 107 }; |
| 108 |
| 109 void TearDown() override { |
| 110 incognitoController_.reset(); |
| 111 controller_.reset(); |
| 112 |
| 113 // There may be blocks released below that have weak references to |profile| |
| 114 // owned by chrome_browser_state_. Ensure BlockCleanupTest::TearDown() is |
| 115 // called before |chrome_browser_state_| is reset. |
| 116 BlockCleanupTest::TearDown(); |
| 117 chrome_browser_state_.reset(); |
| 118 } |
| 119 |
| 120 web::TestWebThreadBundle thread_bundle_; |
| 121 IOSChromeScopedTestingLocalState local_state_; |
| 122 std::unique_ptr<TestChromeBrowserState> chrome_browser_state_; |
| 123 base::scoped_nsobject<NewTabPageController> controller_; |
| 124 base::scoped_nsobject<NewTabPageController> incognitoController_; |
| 125 // The pool has to be the last declared field because it must be destroyed |
| 126 // first. |
| 127 base::mac::ScopedNSAutoreleasePool pool_; |
| 128 }; |
| 129 |
| 130 TEST_F(NewTabPageControllerTest, NewTabBarItemDidChange) { |
| 131 // Switching the selected index in the NewTabPageBar should cause |
| 132 // newTabBarItemDidChange to get called. |
| 133 NewTabPageBar* bar = [[controller_ ntpView] tabBar]; |
| 134 NSUInteger bookmarkIndex = 0; |
| 135 UIButton* button = [[bar buttons] objectAtIndex:bookmarkIndex]; |
| 136 UIControlEvents event = |
| 137 IsIPadIdiom() ? UIControlEventTouchDown : UIControlEventTouchUpInside; |
| 138 [button sendActionsForControlEvents:event]; |
| 139 |
| 140 // Expecting bookmarks panel to be loaded now and to be the current controller |
| 141 // on iPad but not iPhone. |
| 142 // Deliberately comparing pointers. |
| 143 if (IsIPadIdiom()) { |
| 144 EXPECT_EQ([controller_ currentController], |
| 145 (id<NewTabPagePanelProtocol>)[controller_ bookmarkController]); |
| 146 } else { |
| 147 EXPECT_NE([controller_ currentController], |
| 148 (id<NewTabPagePanelProtocol>)[controller_ bookmarkController]); |
| 149 } |
| 150 } |
| 151 |
| 152 TEST_F(NewTabPageControllerTest, SelectBookmarkPanel) { |
| 153 // Expecting on start up that the bookmarkController does not exist. |
| 154 // Deliberately comparing pointers. |
| 155 EXPECT_NE([controller_ currentController], |
| 156 (id<NewTabPagePanelProtocol>)[controller_ bookmarkController]); |
| 157 |
| 158 // Switching to the Bookmarks panel. |
| 159 [controller_ selectPanel:NewTabPage::kBookmarksPanel]; |
| 160 |
| 161 // Expecting bookmarks panel to be loaded now and to be the current controller |
| 162 // on iPad but not iPhone. |
| 163 // Deliberately comparing pointers. |
| 164 if (IsIPadIdiom()) { |
| 165 EXPECT_EQ([controller_ currentController], |
| 166 (id<NewTabPagePanelProtocol>)[controller_ bookmarkController]); |
| 167 } else { |
| 168 EXPECT_NE([controller_ currentController], |
| 169 (id<NewTabPagePanelProtocol>)[controller_ bookmarkController]); |
| 170 } |
| 171 } |
| 172 |
| 173 TEST_F(NewTabPageControllerTest, SelectIncognitoPanel) { |
| 174 // Expect on start up that the Incognito panel is the default. |
| 175 EXPECT_EQ( |
| 176 (id<NewTabPagePanelProtocol>)[incognitoController_ incognitoController], |
| 177 [incognitoController_ currentController]); |
| 178 |
| 179 // Switch to the Bookmarks panel. |
| 180 [incognitoController_ selectPanel:NewTabPage::kBookmarksPanel]; |
| 181 |
| 182 // Expecting bookmarks panel to be loaded now and to be the current controller |
| 183 // on iPad but not iPhone. |
| 184 // Deliberately comparing pointers. |
| 185 if (IsIPadIdiom()) { |
| 186 EXPECT_EQ( |
| 187 [incognitoController_ currentController], |
| 188 (id<NewTabPagePanelProtocol>)[incognitoController_ bookmarkController]); |
| 189 } else { |
| 190 EXPECT_NE( |
| 191 [incognitoController_ currentController], |
| 192 (id<NewTabPagePanelProtocol>)[incognitoController_ bookmarkController]); |
| 193 } |
| 194 } |
| 195 |
| 196 TEST_F(NewTabPageControllerTest, TestWantsLocationBarHintText) { |
| 197 // Default NTP doesn't show location bar hint text on iPad, and it does on |
| 198 // iPhone. |
| 199 if (IsIPadIdiom()) |
| 200 EXPECT_EQ(NO, [controller_ wantsLocationBarHintText]); |
| 201 else |
| 202 EXPECT_EQ(YES, [controller_ wantsLocationBarHintText]); |
| 203 |
| 204 // Default incognito always does. |
| 205 EXPECT_EQ(YES, [incognitoController_ wantsLocationBarHintText]); |
| 206 } |
| 207 |
| 208 TEST_F(NewTabPageControllerTest, NewTabPageIdentifierConversion) { |
| 209 EXPECT_EQ("open_tabs", |
| 210 NewTabPage::FragmentFromIdentifier(NewTabPage::kOpenTabsPanel)); |
| 211 EXPECT_EQ("", NewTabPage::FragmentFromIdentifier(NewTabPage::kNone)); |
| 212 EXPECT_EQ(NewTabPage::kBookmarksPanel, |
| 213 NewTabPage::IdentifierFromFragment("bookmarks")); |
| 214 EXPECT_EQ(NewTabPage::kNone, NewTabPage::IdentifierFromFragment("garbage")); |
| 215 EXPECT_EQ(NewTabPage::kNone, NewTabPage::IdentifierFromFragment("")); |
| 216 } |
| 217 |
| 218 } // anonymous namespace |
OLD | NEW |