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