| 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 <objc/runtime.h> | 5 #import <objc/runtime.h> |
| 6 | 6 |
| 7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
| 8 #include "base/mac/scoped_nsautorelease_pool.h" | 8 #include "base/mac/scoped_nsautorelease_pool.h" |
| 9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 // is not a situation we normally expect to be in because we never | 45 // is not a situation we normally expect to be in because we never |
| 46 // want the session being saved on the main thread in the production app. | 46 // want the session being saved on the main thread in the production app. |
| 47 // We could expose this as part of the service's public API, but again that | 47 // We could expose this as part of the service's public API, but again that |
| 48 // might encourage use where we don't want it. As a result, just use the | 48 // might encourage use where we don't want it. As a result, just use the |
| 49 // known private-for-testing method directly. | 49 // known private-for-testing method directly. |
| 50 @interface SessionServiceIOS (Testing) | 50 @interface SessionServiceIOS (Testing) |
| 51 - (void)performSaveWindow:(SessionWindowIOS*)window | 51 - (void)performSaveWindow:(SessionWindowIOS*)window |
| 52 toDirectory:(NSString*)directory; | 52 toDirectory:(NSString*)directory; |
| 53 @end | 53 @end |
| 54 | 54 |
| 55 @interface TabTest : Tab | |
| 56 | |
| 57 - (instancetype)initWithBrowserState:(ios::ChromeBrowserState*)browserState | |
| 58 lastVisitedTimestamp:(double)lastVisitedTimestamp | |
| 59 tabModel:(TabModel*)tabModel; | |
| 60 @end | |
| 61 | |
| 62 @implementation TabTest | |
| 63 | |
| 64 - (instancetype)initWithBrowserState:(ios::ChromeBrowserState*)browserState | |
| 65 lastVisitedTimestamp:(double)lastVisitedTimestamp | |
| 66 tabModel:(TabModel*)tabModel { | |
| 67 id webControllerMock = | |
| 68 [OCMockObject niceMockForClass:[CRWWebController class]]; | |
| 69 | |
| 70 auto webStateImpl = base::MakeUnique<WebStateImpl>(browserState); | |
| 71 webStateImpl->SetWebController(webControllerMock); | |
| 72 webStateImpl->GetNavigationManagerImpl().InitializeSession(NO); | |
| 73 [webStateImpl->GetNavigationManagerImpl().GetSessionController() | |
| 74 setLastVisitedTimestamp:lastVisitedTimestamp]; | |
| 75 | |
| 76 WebStateImpl* webStateImplPtr = webStateImpl.get(); | |
| 77 [[[webControllerMock stub] andReturnValue:OCMOCK_VALUE(webStateImplPtr)] | |
| 78 webStateImpl]; | |
| 79 BOOL yes = YES; | |
| 80 [[[webControllerMock stub] andReturnValue:OCMOCK_VALUE(yes)] isViewAlive]; | |
| 81 | |
| 82 if ((self = [super initWithWebState:std::move(webStateImpl) | |
| 83 model:tabModel | |
| 84 attachTabHelpers:NO])) { | |
| 85 IOSChromeSessionTabHelper::CreateForWebState(self.webState); | |
| 86 } | |
| 87 return self; | |
| 88 } | |
| 89 | |
| 90 @end | |
| 91 | |
| 92 @interface TabModel (VisibleForTesting) | 55 @interface TabModel (VisibleForTesting) |
| 93 - (SessionWindowIOS*)windowForSavingSession; | 56 - (SessionWindowIOS*)windowForSavingSession; |
| 94 @end | 57 @end |
| 95 | 58 |
| 96 // Defines a TabModelObserver for use in unittests. This class can be used to | 59 // Defines a TabModelObserver for use in unittests. This class can be used to |
| 97 // test if an observer method was called or not. | 60 // test if an observer method was called or not. |
| 98 @interface TabModelObserverPong : NSObject<TabModelObserver> { | 61 @interface TabModelObserverPong : NSObject<TabModelObserver> { |
| 99 // TODO(crbug.com/661989): Add tests for the other observer methods. | 62 // TODO(crbug.com/661989): Add tests for the other observer methods. |
| 100 BOOL tabMovedWasCalled_; | 63 BOOL tabMovedWasCalled_; |
| 101 } | 64 } |
| (...skipping 652 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 754 ASSERT_EQ(3U, [tab_model_ count]); | 717 ASSERT_EQ(3U, [tab_model_ count]); |
| 755 EXPECT_NSEQ(tab1, [tab_model_ tabAtIndex:0]); | 718 EXPECT_NSEQ(tab1, [tab_model_ tabAtIndex:0]); |
| 756 EXPECT_NSEQ(tab2, [tab_model_ tabAtIndex:1]); | 719 EXPECT_NSEQ(tab2, [tab_model_ tabAtIndex:1]); |
| 757 EXPECT_NSEQ(tab0, [tab_model_ tabAtIndex:2]); | 720 EXPECT_NSEQ(tab0, [tab_model_ tabAtIndex:2]); |
| 758 EXPECT_FALSE([tab_model_observer_ tabMovedWasCalled]); | 721 EXPECT_FALSE([tab_model_observer_ tabMovedWasCalled]); |
| 759 } | 722 } |
| 760 | 723 |
| 761 TEST_F(TabModelTest, SetParentModel) { | 724 TEST_F(TabModelTest, SetParentModel) { |
| 762 // Create a tab without a parent model and make sure it doesn't crash. Then | 725 // Create a tab without a parent model and make sure it doesn't crash. Then |
| 763 // set its parent TabModel and make sure that works as well. | 726 // set its parent TabModel and make sure that works as well. |
| 764 base::scoped_nsobject<TabTest> tab([[TabTest alloc] | 727 base::scoped_nsobject<Tab> tab([[Tab alloc] |
| 765 initWithBrowserState:chrome_browser_state_.get() | 728 initWithBrowserState:chrome_browser_state_.get() |
| 766 lastVisitedTimestamp:100 | 729 opener:nil |
| 767 tabModel:nil]); | 730 openedByDOM:NO |
| 731 model:nil]); |
| 768 EXPECT_TRUE([tab parentTabModel] == nil); | 732 EXPECT_TRUE([tab parentTabModel] == nil); |
| 769 [tab_model_ insertTab:tab atIndex:0]; | 733 [tab_model_ insertTab:tab atIndex:0]; |
| 770 [tab setParentTabModel:tab_model_.get()]; | 734 [tab setParentTabModel:tab_model_.get()]; |
| 771 EXPECT_FALSE([tab parentTabModel] == nil); | 735 EXPECT_FALSE([tab parentTabModel] == nil); |
| 772 [tab_model_ closeTabAtIndex:0]; | 736 [tab_model_ closeTabAtIndex:0]; |
| 773 } | 737 } |
| 774 | 738 |
| 775 TEST_F(TabModelTest, PersistSelectionChange) { | 739 TEST_F(TabModelTest, PersistSelectionChange) { |
| 776 TestChromeBrowserState::Builder test_cbs_builder; | 740 TestChromeBrowserState::Builder test_cbs_builder; |
| 777 auto chrome_browser_state = test_cbs_builder.Build(); | 741 auto chrome_browser_state = test_cbs_builder.Build(); |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 845 // Restoring TabModel session sends asynchronous tasks to IO thread, wait | 809 // Restoring TabModel session sends asynchronous tasks to IO thread, wait |
| 846 // for them to complete after destroying the TabModel. | 810 // for them to complete after destroying the TabModel. |
| 847 base::RunLoop().RunUntilIdle(); | 811 base::RunLoop().RunUntilIdle(); |
| 848 | 812 |
| 849 // Clean up. | 813 // Clean up. |
| 850 EXPECT_TRUE([[NSFileManager defaultManager] removeItemAtPath:stashPath | 814 EXPECT_TRUE([[NSFileManager defaultManager] removeItemAtPath:stashPath |
| 851 error:nullptr]); | 815 error:nullptr]); |
| 852 } | 816 } |
| 853 | 817 |
| 854 } // anonymous namespace | 818 } // anonymous namespace |
| OLD | NEW |