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 #import "ios/chrome/browser/sessions/session_window.h" |
| 6 |
| 7 #import <Foundation/Foundation.h> |
| 8 |
| 9 #include <memory> |
| 10 #include <utility> |
| 11 |
| 12 #include "base/mac/scoped_nsobject.h" |
| 13 #include "base/strings/sys_string_conversions.h" |
| 14 #include "ios/chrome/browser/browser_state/test_chrome_browser_state.h" |
| 15 #import "ios/chrome/browser/sessions/session_service.h" |
| 16 #import "ios/web/navigation/crw_session_controller.h" |
| 17 #include "ios/web/public/test/test_web_thread_bundle.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 #include "testing/gtest_mac.h" |
| 20 #include "testing/platform_test.h" |
| 21 #import "third_party/ocmock/OCMock/OCMock.h" |
| 22 |
| 23 using web::WebStateImpl; |
| 24 |
| 25 @interface SessionWindowIOS (Testing) |
| 26 |
| 27 - (void)clearSessions; |
| 28 |
| 29 @end |
| 30 |
| 31 namespace { |
| 32 |
| 33 class SessionWindowIOSTest : public PlatformTest { |
| 34 protected: |
| 35 void SetUp() override { |
| 36 PlatformTest::SetUp(); |
| 37 TestChromeBrowserState::Builder test_cbs_builder; |
| 38 chrome_browser_state_ = test_cbs_builder.Build(); |
| 39 } |
| 40 |
| 41 WebStateImpl* CreateWebState(NSString* window_name, |
| 42 NSString* opener, |
| 43 BOOL openedByDOM) const { |
| 44 WebStateImpl* webState = new WebStateImpl(chrome_browser_state_.get()); |
| 45 webState->GetNavigationManagerImpl().InitializeSession(window_name, opener, |
| 46 openedByDOM, 0); |
| 47 return webState; |
| 48 } |
| 49 |
| 50 web::TestWebThreadBundle thread_bundle_; |
| 51 // TODO(crbug.com/661639): Switch to TestBrowserState once this test is moved |
| 52 // to be in the ios_web_unittests target. |
| 53 std::unique_ptr<TestChromeBrowserState> chrome_browser_state_; |
| 54 }; |
| 55 |
| 56 TEST_F(SessionWindowIOSTest, InitEmpty) { |
| 57 base::scoped_nsobject<SessionWindowIOS> sessionWindow( |
| 58 [[SessionWindowIOS alloc] init]); |
| 59 EXPECT_TRUE(sessionWindow.get() != nil); |
| 60 } |
| 61 |
| 62 TEST_F(SessionWindowIOSTest, InitAddingSessions) { |
| 63 std::unique_ptr<WebStateImpl> webState1(CreateWebState(@"window1", nil, NO)); |
| 64 std::unique_ptr<WebStateImpl> webState2(CreateWebState(@"window2", nil, NO)); |
| 65 base::scoped_nsobject<SessionWindowIOS> sessionWindow( |
| 66 [[SessionWindowIOS alloc] init]); |
| 67 [sessionWindow addSession:std::move(webState1)]; |
| 68 [sessionWindow addSession:std::move(webState2)]; |
| 69 [sessionWindow setSelectedIndex:1]; |
| 70 |
| 71 EXPECT_TRUE(sessionWindow.get() != nil); |
| 72 EXPECT_EQ(2U, sessionWindow.get().unclaimedSessions); |
| 73 [sessionWindow clearSessions]; |
| 74 EXPECT_EQ(0U, sessionWindow.get().unclaimedSessions); |
| 75 } |
| 76 |
| 77 TEST_F(SessionWindowIOSTest, CodingEncoding) { |
| 78 NSString* windowName1 = @"window1"; |
| 79 NSString* windowName2 = @"window2"; |
| 80 base::scoped_nsobject<SessionWindowIOS> sessionWindow( |
| 81 [[SessionWindowIOS alloc] init]); |
| 82 |
| 83 std::unique_ptr<WebStateImpl> webState1( |
| 84 CreateWebState(windowName1, nil, YES)); |
| 85 NSString* openerId1 = |
| 86 webState1->GetNavigationManagerImpl().GetSessionController().openerId; |
| 87 std::unique_ptr<WebStateImpl> webState2(CreateWebState(windowName2, nil, NO)); |
| 88 NSString* openerId2 = |
| 89 webState2->GetNavigationManagerImpl().GetSessionController().openerId; |
| 90 [sessionWindow addSession:std::move(webState1)]; |
| 91 [sessionWindow addSession:std::move(webState2)]; |
| 92 |
| 93 [sessionWindow setSelectedIndex:1]; |
| 94 |
| 95 NSData* data = [NSKeyedArchiver archivedDataWithRootObject:sessionWindow]; |
| 96 EXPECT_TRUE(data != nil); |
| 97 base::scoped_nsobject<SessionWindowUnarchiver> unarchiver( |
| 98 [[SessionWindowUnarchiver alloc] |
| 99 initForReadingWithData:data |
| 100 browserState:chrome_browser_state_.get()]); |
| 101 SessionWindowIOS* unarchivedObj = [unarchiver decodeObjectForKey:@"root"]; |
| 102 EXPECT_TRUE(unarchivedObj != nil); |
| 103 EXPECT_EQ(unarchivedObj.selectedIndex, sessionWindow.get().selectedIndex); |
| 104 EXPECT_EQ(2U, unarchivedObj.unclaimedSessions); |
| 105 std::unique_ptr<WebStateImpl> unarchivedWebState1 = |
| 106 [unarchivedObj nextSession]; |
| 107 EXPECT_EQ(1U, unarchivedObj.unclaimedSessions); |
| 108 CRWSessionController* unarchivedSession1 = |
| 109 unarchivedWebState1->GetNavigationManagerImpl().GetSessionController(); |
| 110 EXPECT_NSEQ(windowName1, unarchivedSession1.windowName); |
| 111 EXPECT_NSEQ(openerId1, unarchivedSession1.openerId); |
| 112 EXPECT_TRUE(unarchivedSession1.openedByDOM); |
| 113 |
| 114 std::unique_ptr<WebStateImpl> unarchivedWebState2 = |
| 115 [unarchivedObj nextSession]; |
| 116 EXPECT_EQ(0U, unarchivedObj.unclaimedSessions); |
| 117 |
| 118 CRWSessionController* unarchivedSession2 = |
| 119 unarchivedWebState2->GetNavigationManagerImpl().GetSessionController(); |
| 120 EXPECT_NSEQ(windowName2, unarchivedSession2.windowName); |
| 121 EXPECT_NSEQ(openerId2, unarchivedSession2.openerId); |
| 122 EXPECT_FALSE(unarchivedSession2.openedByDOM); |
| 123 } |
| 124 |
| 125 } // anonymous namespace |
OLD | NEW |