| 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 #include "base/mac/scoped_nsautorelease_pool.h" | |
| 6 #include "base/memory/ptr_util.h" | |
| 7 #include "ios/chrome/browser/browser_state/test_chrome_browser_state.h" | |
| 8 #include "ios/chrome/browser/browser_state/test_chrome_browser_state_manager.h" | |
| 9 #import "ios/chrome/browser/sessions/session_window.h" | |
| 10 #import "ios/chrome/browser/sessions/test_session_service.h" | |
| 11 #import "ios/chrome/browser/tabs/tab.h" | |
| 12 #import "ios/chrome/browser/tabs/tab_model.h" | |
| 13 #import "ios/chrome/browser/tabs/tab_model_order_controller.h" | |
| 14 #include "ios/chrome/test/ios_chrome_scoped_testing_chrome_browser_state_manager
.h" | |
| 15 #include "ios/web/public/referrer.h" | |
| 16 #include "ios/web/public/test/test_web_thread_bundle.h" | |
| 17 #include "ios/web/public/web_thread.h" | |
| 18 #include "testing/platform_test.h" | |
| 19 #import "third_party/ocmock/OCMock/OCMock.h" | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 23 class TabModelOrderControllerTest : public PlatformTest { | |
| 24 protected: | |
| 25 TabModelOrderControllerTest() | |
| 26 : thread_bundle_(web::TestWebThreadBundle::IO_MAINLOOP), | |
| 27 scoped_browser_state_manager_( | |
| 28 base::MakeUnique<TestChromeBrowserStateManager>(base::FilePath())) { | |
| 29 } | |
| 30 | |
| 31 void SetUp() override { | |
| 32 DCHECK_CURRENTLY_ON(web::WebThread::UI); | |
| 33 PlatformTest::SetUp(); | |
| 34 | |
| 35 TestChromeBrowserState::Builder test_cbs_builder; | |
| 36 chrome_browser_state_ = test_cbs_builder.Build(); | |
| 37 | |
| 38 dummy_tab_.reset([[Tab alloc] | |
| 39 initWithWindowName:nil | |
| 40 opener:nullptr | |
| 41 openedByDOM:NO | |
| 42 model:nil | |
| 43 browserState:chrome_browser_state_.get()]); | |
| 44 | |
| 45 sessionWindow_.reset([[SessionWindowIOS new] retain]); | |
| 46 | |
| 47 base::scoped_nsobject<TestSessionService> test_service( | |
| 48 [[TestSessionService alloc] init]); | |
| 49 tabModel_.reset([[TabModel alloc] | |
| 50 initWithSessionWindow:sessionWindow_ | |
| 51 sessionService:test_service | |
| 52 browserState:chrome_browser_state_.get()]); | |
| 53 | |
| 54 orderController_.reset( | |
| 55 [[TabModelOrderController alloc] initWithTabModel:tabModel_]); | |
| 56 | |
| 57 // Values to use when creating new tabs. | |
| 58 url_ = GURL("https://www.some.url.com"); | |
| 59 referrer_ = web::Referrer(GURL("https://www.some.referer.com"), | |
| 60 web::ReferrerPolicyDefault); | |
| 61 } | |
| 62 | |
| 63 void TearDown() override { | |
| 64 [dummy_tab_ close]; | |
| 65 | |
| 66 [tabModel_ browserStateDestroyed]; | |
| 67 | |
| 68 PlatformTest::TearDown(); | |
| 69 } | |
| 70 | |
| 71 Tab* addTabToParentInBackground(Tab* parentTab) { | |
| 72 return [tabModel_ | |
| 73 insertOrUpdateTabWithURL:url_ | |
| 74 referrer:referrer_ | |
| 75 transition:ui::PAGE_TRANSITION_LINK | |
| 76 windowName:nil | |
| 77 opener:parentTab | |
| 78 openedByDOM:NO | |
| 79 atIndex:TabModelConstants::kTabPositionAutomatically | |
| 80 inBackground:YES]; | |
| 81 } | |
| 82 | |
| 83 GURL url_; | |
| 84 web::Referrer referrer_; | |
| 85 web::TestWebThreadBundle thread_bundle_; | |
| 86 IOSChromeScopedTestingChromeBrowserStateManager scoped_browser_state_manager_; | |
| 87 base::scoped_nsobject<SessionWindowIOS> sessionWindow_; | |
| 88 std::unique_ptr<TestChromeBrowserState> chrome_browser_state_; | |
| 89 base::mac::ScopedNSAutoreleasePool pool_; | |
| 90 base::scoped_nsobject<Tab> dummy_tab_; | |
| 91 base::scoped_nsobject<TabModelOrderController> orderController_; | |
| 92 base::scoped_nsobject<TabModel> tabModel_; | |
| 93 }; | |
| 94 | |
| 95 // Verifies that tabs added in the background (e.g. from context menu -> Open in | |
| 96 // new tab) are inserted in the proper order. | |
| 97 TEST_F(TabModelOrderControllerTest, DetermineInsertionIndexForBackgroundTabs) { | |
| 98 // Add |parentTab|, then add |childTab| in the background. | |
| 99 Tab* parentTab = [tabModel_ insertTabWithURL:url_ | |
| 100 referrer:referrer_ | |
| 101 windowName:nil | |
| 102 opener:nil | |
| 103 atIndex:0]; | |
| 104 Tab* childTab = addTabToParentInBackground(parentTab); | |
| 105 | |
| 106 // Verify a second child would be inserted in an index after the first child. | |
| 107 int index = [orderController_ | |
| 108 insertionIndexForTab:dummy_tab_ | |
| 109 transition:ui::PAGE_TRANSITION_LINK | |
| 110 opener:parentTab | |
| 111 adjacency:TabModelOrderConstants::kAdjacentAfter]; | |
| 112 EXPECT_EQ(2, index); | |
| 113 | |
| 114 // Now add a child of the child, then verify that a second child of the parent | |
| 115 // would be inserted before the child's child. | |
| 116 addTabToParentInBackground(childTab); | |
| 117 index = [orderController_ | |
| 118 insertionIndexForTab:dummy_tab_ | |
| 119 transition:ui::PAGE_TRANSITION_LINK | |
| 120 opener:parentTab | |
| 121 adjacency:TabModelOrderConstants::kAdjacentAfter]; | |
| 122 EXPECT_EQ(2, index); | |
| 123 } | |
| 124 } // anonymous namespace | |
| OLD | NEW |