| 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 814 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 825 [tab_model_ insertTab:tab atIndex:0]; | 825 [tab_model_ insertTab:tab atIndex:0]; |
| 826 [tab setParentTabModel:tab_model_.get()]; | 826 [tab setParentTabModel:tab_model_.get()]; |
| 827 EXPECT_FALSE([tab parentTabModel] == nil); | 827 EXPECT_FALSE([tab parentTabModel] == nil); |
| 828 [tab_model_ closeTabAtIndex:0]; | 828 [tab_model_ closeTabAtIndex:0]; |
| 829 } | 829 } |
| 830 | 830 |
| 831 TEST_F(TabModelTest, PersistSelectionChange) { | 831 TEST_F(TabModelTest, PersistSelectionChange) { |
| 832 TestChromeBrowserState::Builder test_cbs_builder; | 832 TestChromeBrowserState::Builder test_cbs_builder; |
| 833 auto chrome_browser_state = test_cbs_builder.Build(); | 833 auto chrome_browser_state = test_cbs_builder.Build(); |
| 834 | 834 |
| 835 // Tabs register some observers with the ChromeBrowserState in an ObserverList |
| 836 // that assert it is empty in its destructor. As Tab are Objective-C object, |
| 837 // it is necessary to use a local pool to ensure all autoreleased object that |
| 838 // may reference those Tabs are deallocated before the TestChromeBrowserState |
| 839 // is destroyed (this cannot use the TabModelTest ScopedNSAutoreleasePool as |
| 840 // it will be drained after the local variable chrome_browser_state). |
| 841 base::mac::ScopedNSAutoreleasePool pool; |
| 842 |
| 835 NSString* stashPath = | 843 NSString* stashPath = |
| 836 base::SysUTF8ToNSString(chrome_browser_state->GetStatePath().value()); | 844 base::SysUTF8ToNSString(chrome_browser_state->GetStatePath().value()); |
| 837 | 845 |
| 838 base::scoped_nsobject<TabModel> model([[TabModel alloc] | 846 base::scoped_nsobject<TabModel> model([[TabModel alloc] |
| 839 initWithSessionWindow:session_window_.get() | 847 initWithSessionWindow:session_window_.get() |
| 840 sessionService:[SessionServiceIOS sharedService] | 848 sessionService:[SessionServiceIOS sharedService] |
| 841 browserState:chrome_browser_state.get()]); | 849 browserState:chrome_browser_state.get()]); |
| 842 | 850 |
| 843 [model addTabWithURL:kURL referrer:kReferrer windowName:@"window 1"]; | 851 [model addTabWithURL:kURL referrer:kReferrer windowName:@"window 1"]; |
| 844 [model addTabWithURL:kURL referrer:kReferrer windowName:@"window 2"]; | 852 [model addTabWithURL:kURL referrer:kReferrer windowName:@"window 2"]; |
| 845 [model addTabWithURL:kURL referrer:kReferrer windowName:@"window 3"]; | 853 [model addTabWithURL:kURL referrer:kReferrer windowName:@"window 3"]; |
| 846 | 854 |
| 847 ASSERT_EQ(3U, [model count]); | 855 ASSERT_EQ(3U, [model count]); |
| 848 model.get().currentTab = [model tabAtIndex:1]; | 856 model.get().currentTab = [model tabAtIndex:1]; |
| 849 // Force state to flush to disk on the main thread so it can be immediately | 857 // Force state to flush to disk on the main thread so it can be immediately |
| 850 // tested below. | 858 // tested below. |
| 851 SessionWindowIOS* window = [model windowForSavingSession]; | 859 SessionWindowIOS* window = [model windowForSavingSession]; |
| 852 [[SessionServiceIOS sharedService] performSaveWindow:window | 860 [[SessionServiceIOS sharedService] performSaveWindow:window |
| 853 toDirectory:stashPath]; | 861 toDirectory:stashPath]; |
| 854 [model browserStateDestroyed]; | 862 [model browserStateDestroyed]; |
| 855 model.reset(); | 863 model.reset(); |
| 864 |
| 865 // Restoring TabModel session sends asynchronous tasks to IO thread, wait |
| 866 // for them to complete after destroying the TabModel. |
| 856 base::RunLoop().RunUntilIdle(); | 867 base::RunLoop().RunUntilIdle(); |
| 857 | 868 |
| 858 SessionWindowIOS* sessionWindow = [[SessionServiceIOS sharedService] | 869 SessionWindowIOS* sessionWindow = [[SessionServiceIOS sharedService] |
| 859 loadWindowForBrowserState:chrome_browser_state.get()]; | 870 loadWindowForBrowserState:chrome_browser_state.get()]; |
| 860 | 871 |
| 861 // Create tab model from saved session. | 872 // Create tab model from saved session. |
| 862 base::scoped_nsobject<TestSessionService> test_service( | 873 base::scoped_nsobject<TestSessionService> test_service( |
| 863 [[TestSessionService alloc] init]); | 874 [[TestSessionService alloc] init]); |
| 864 | 875 |
| 865 model.reset([[TabModel alloc] | 876 model.reset([[TabModel alloc] |
| 866 initWithSessionWindow:sessionWindow | 877 initWithSessionWindow:sessionWindow |
| 867 sessionService:test_service | 878 sessionService:test_service |
| 868 browserState:chrome_browser_state.get()]); | 879 browserState:chrome_browser_state.get()]); |
| 869 EXPECT_EQ(model.get().currentTab, [model tabAtIndex:1]); | 880 EXPECT_EQ(model.get().currentTab, [model tabAtIndex:1]); |
| 870 [model browserStateDestroyed]; | 881 [model browserStateDestroyed]; |
| 882 model.reset(); |
| 883 |
| 884 // Restoring TabModel session sends asynchronous tasks to IO thread, wait |
| 885 // for them to complete after destroying the TabModel. |
| 886 base::RunLoop().RunUntilIdle(); |
| 871 | 887 |
| 872 // Clean up. | 888 // Clean up. |
| 873 EXPECT_TRUE([[NSFileManager defaultManager] removeItemAtPath:stashPath | 889 EXPECT_TRUE([[NSFileManager defaultManager] removeItemAtPath:stashPath |
| 874 error:nullptr]); | 890 error:nullptr]); |
| 875 } | 891 } |
| 876 | 892 |
| 877 } // anonymous namespace | 893 } // anonymous namespace |
| OLD | NEW |