| OLD | NEW |
| 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 "ios/chrome/browser/ui/ntp/new_tab_page_controller.h" | 5 #import "ios/chrome/browser/ui/ntp/new_tab_page_controller.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/mac/scoped_nsautorelease_pool.h" | |
| 10 #include "base/mac/scoped_nsobject.h" | |
| 11 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 12 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 13 #include "components/bookmarks/test/bookmark_test_helpers.h" | 11 #include "components/bookmarks/test/bookmark_test_helpers.h" |
| 14 #include "components/prefs/testing_pref_service.h" | 12 #include "components/prefs/testing_pref_service.h" |
| 15 #include "components/search_engines/template_url_service.h" | 13 #include "components/search_engines/template_url_service.h" |
| 16 #include "components/sessions/core/tab_restore_service.h" | 14 #include "components/sessions/core/tab_restore_service.h" |
| 17 #include "ios/chrome/browser/bookmarks/bookmark_model_factory.h" | 15 #include "ios/chrome/browser/bookmarks/bookmark_model_factory.h" |
| 18 #include "ios/chrome/browser/browser_state/test_chrome_browser_state.h" | 16 #include "ios/chrome/browser/browser_state/test_chrome_browser_state.h" |
| 19 #include "ios/chrome/browser/chrome_url_constants.h" | 17 #include "ios/chrome/browser/chrome_url_constants.h" |
| 20 #include "ios/chrome/browser/search_engines/template_url_service_factory.h" | 18 #include "ios/chrome/browser/search_engines/template_url_service_factory.h" |
| 21 #include "ios/chrome/browser/sessions/ios_chrome_tab_restore_service_factory.h" | 19 #include "ios/chrome/browser/sessions/ios_chrome_tab_restore_service_factory.h" |
| 22 #import "ios/chrome/browser/ui/ntp/new_tab_page_view.h" | 20 #import "ios/chrome/browser/ui/ntp/new_tab_page_view.h" |
| 23 #include "ios/chrome/browser/ui/ui_util.h" | 21 #include "ios/chrome/browser/ui/ui_util.h" |
| 24 #include "ios/chrome/test/block_cleanup_test.h" | 22 #include "ios/chrome/test/block_cleanup_test.h" |
| 25 #include "ios/chrome/test/ios_chrome_scoped_testing_local_state.h" | 23 #include "ios/chrome/test/ios_chrome_scoped_testing_local_state.h" |
| 26 #include "ios/chrome/test/testing_application_context.h" | 24 #include "ios/chrome/test/testing_application_context.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/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
| 29 #include "testing/gtest_mac.h" | 27 #include "testing/gtest_mac.h" |
| 30 #import "third_party/ocmock/OCMock/OCMock.h" | 28 #import "third_party/ocmock/OCMock/OCMock.h" |
| 31 #import "third_party/ocmock/gtest_support.h" | 29 #import "third_party/ocmock/gtest_support.h" |
| 32 | 30 |
| 31 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 32 #error "This file requires ARC support." |
| 33 #endif |
| 34 |
| 33 @interface NewTabPageController (TestSupport) | 35 @interface NewTabPageController (TestSupport) |
| 34 - (id<NewTabPagePanelProtocol>)currentController; | 36 - (id<NewTabPagePanelProtocol>)currentController; |
| 35 - (id<NewTabPagePanelProtocol>)bookmarkController; | 37 - (id<NewTabPagePanelProtocol>)bookmarkController; |
| 36 - (id<NewTabPagePanelProtocol>)incognitoController; | 38 - (id<NewTabPagePanelProtocol>)incognitoController; |
| 37 @end | 39 @end |
| 38 | 40 |
| 39 @interface NewTabPageController (PrivateMethods) | 41 @interface NewTabPageController (PrivateMethods) |
| 40 @property(nonatomic, retain) NewTabPageView* ntpView; | 42 @property(nonatomic, retain) NewTabPageView* ntpView; |
| 41 @end | 43 @end |
| 42 | 44 |
| 43 @implementation NewTabPageController (TestSupport) | 45 @implementation NewTabPageController (TestSupport) |
| 44 | 46 |
| 45 - (id<NewTabPagePanelProtocol>)currentController { | 47 - (id<NewTabPagePanelProtocol>)currentController { |
| 46 return currentController_; | 48 return currentController_; |
| 47 } | 49 } |
| 48 | 50 |
| 49 - (id<NewTabPagePanelProtocol>)bookmarkController { | 51 - (id<NewTabPagePanelProtocol>)bookmarkController { |
| 50 return bookmarkController_.get(); | 52 return bookmarkController_.get(); |
| 51 } | 53 } |
| 52 | 54 |
| 53 - (id<NewTabPagePanelProtocol>)incognitoController { | 55 - (id<NewTabPagePanelProtocol>)incognitoController { |
| 54 return incognitoController_.get(); | 56 return incognitoController_; |
| 55 } | 57 } |
| 56 | 58 |
| 57 @end | 59 @end |
| 58 | 60 |
| 59 namespace { | 61 namespace { |
| 60 | 62 |
| 61 class NewTabPageControllerTest : public BlockCleanupTest { | 63 class NewTabPageControllerTest : public BlockCleanupTest { |
| 62 protected: | 64 protected: |
| 63 void SetUp() override { | 65 void SetUp() override { |
| 64 BlockCleanupTest::SetUp(); | 66 BlockCleanupTest::SetUp(); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 77 TemplateURLService* template_url_service = | 79 TemplateURLService* template_url_service = |
| 78 ios::TemplateURLServiceFactory::GetForBrowserState( | 80 ios::TemplateURLServiceFactory::GetForBrowserState( |
| 79 chrome_browser_state_.get()); | 81 chrome_browser_state_.get()); |
| 80 template_url_service->Load(); | 82 template_url_service->Load(); |
| 81 | 83 |
| 82 chrome_browser_state_->CreateBookmarkModel(true); | 84 chrome_browser_state_->CreateBookmarkModel(true); |
| 83 bookmarks::test::WaitForBookmarkModelToLoad( | 85 bookmarks::test::WaitForBookmarkModelToLoad( |
| 84 ios::BookmarkModelFactory::GetForBrowserState( | 86 ios::BookmarkModelFactory::GetForBrowserState( |
| 85 chrome_browser_state_.get())); | 87 chrome_browser_state_.get())); |
| 86 GURL url(kChromeUINewTabURL); | 88 GURL url(kChromeUINewTabURL); |
| 87 controller_.reset([[NewTabPageController alloc] | 89 controller_ = |
| 90 [[NewTabPageController alloc] initWithUrl:url |
| 91 loader:nil |
| 92 focuser:nil |
| 93 ntpObserver:nil |
| 94 browserState:chrome_browser_state_.get() |
| 95 colorCache:nil |
| 96 webToolbarDelegate:nil |
| 97 tabModel:nil]; |
| 98 |
| 99 incognitoController_ = [[NewTabPageController alloc] |
| 88 initWithUrl:url | 100 initWithUrl:url |
| 89 loader:nil | 101 loader:nil |
| 90 focuser:nil | 102 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 | 103 ntpObserver:nil |
| 102 browserState:chrome_browser_state_ | 104 browserState:chrome_browser_state_ |
| 103 ->GetOffTheRecordChromeBrowserState() | 105 ->GetOffTheRecordChromeBrowserState() |
| 104 colorCache:nil | 106 colorCache:nil |
| 105 webToolbarDelegate:nil | 107 webToolbarDelegate:nil |
| 106 tabModel:nil]); | 108 tabModel:nil]; |
| 107 }; | 109 }; |
| 108 | 110 |
| 109 void TearDown() override { | 111 void TearDown() override { |
| 110 incognitoController_.reset(); | 112 incognitoController_ = nil; |
| 111 controller_.reset(); | 113 controller_ = nil; |
| 112 | 114 |
| 113 // There may be blocks released below that have weak references to |profile| | 115 // There may be blocks released below that have weak references to |profile| |
| 114 // owned by chrome_browser_state_. Ensure BlockCleanupTest::TearDown() is | 116 // owned by chrome_browser_state_. Ensure BlockCleanupTest::TearDown() is |
| 115 // called before |chrome_browser_state_| is reset. | 117 // called before |chrome_browser_state_| is reset. |
| 116 BlockCleanupTest::TearDown(); | 118 BlockCleanupTest::TearDown(); |
| 117 chrome_browser_state_.reset(); | 119 chrome_browser_state_.reset(); |
| 118 } | 120 } |
| 119 | 121 |
| 120 web::TestWebThreadBundle thread_bundle_; | 122 web::TestWebThreadBundle thread_bundle_; |
| 121 IOSChromeScopedTestingLocalState local_state_; | 123 IOSChromeScopedTestingLocalState local_state_; |
| 122 std::unique_ptr<TestChromeBrowserState> chrome_browser_state_; | 124 std::unique_ptr<TestChromeBrowserState> chrome_browser_state_; |
| 123 base::scoped_nsobject<NewTabPageController> controller_; | 125 NewTabPageController* controller_; |
| 124 base::scoped_nsobject<NewTabPageController> incognitoController_; | 126 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 }; | 127 }; |
| 129 | 128 |
| 130 TEST_F(NewTabPageControllerTest, NewTabBarItemDidChange) { | 129 TEST_F(NewTabPageControllerTest, NewTabBarItemDidChange) { |
| 131 // Switching the selected index in the NewTabPageBar should cause | 130 // Switching the selected index in the NewTabPageBar should cause |
| 132 // newTabBarItemDidChange to get called. | 131 // newTabBarItemDidChange to get called. |
| 133 NewTabPageBar* bar = [[controller_ ntpView] tabBar]; | 132 NewTabPageBar* bar = [[controller_ ntpView] tabBar]; |
| 134 NSUInteger bookmarkIndex = 0; | 133 NSUInteger bookmarkIndex = 0; |
| 135 UIButton* button = [[bar buttons] objectAtIndex:bookmarkIndex]; | 134 UIButton* button = [[bar buttons] objectAtIndex:bookmarkIndex]; |
| 136 UIControlEvents event = | 135 UIControlEvents event = |
| 137 IsIPadIdiom() ? UIControlEventTouchDown : UIControlEventTouchUpInside; | 136 IsIPadIdiom() ? UIControlEventTouchDown : UIControlEventTouchUpInside; |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 EXPECT_EQ("open_tabs", | 208 EXPECT_EQ("open_tabs", |
| 210 NewTabPage::FragmentFromIdentifier(NewTabPage::kOpenTabsPanel)); | 209 NewTabPage::FragmentFromIdentifier(NewTabPage::kOpenTabsPanel)); |
| 211 EXPECT_EQ("", NewTabPage::FragmentFromIdentifier(NewTabPage::kNone)); | 210 EXPECT_EQ("", NewTabPage::FragmentFromIdentifier(NewTabPage::kNone)); |
| 212 EXPECT_EQ(NewTabPage::kBookmarksPanel, | 211 EXPECT_EQ(NewTabPage::kBookmarksPanel, |
| 213 NewTabPage::IdentifierFromFragment("bookmarks")); | 212 NewTabPage::IdentifierFromFragment("bookmarks")); |
| 214 EXPECT_EQ(NewTabPage::kNone, NewTabPage::IdentifierFromFragment("garbage")); | 213 EXPECT_EQ(NewTabPage::kNone, NewTabPage::IdentifierFromFragment("garbage")); |
| 215 EXPECT_EQ(NewTabPage::kNone, NewTabPage::IdentifierFromFragment("")); | 214 EXPECT_EQ(NewTabPage::kNone, NewTabPage::IdentifierFromFragment("")); |
| 216 } | 215 } |
| 217 | 216 |
| 218 } // anonymous namespace | 217 } // anonymous namespace |
| OLD | NEW |