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 "ios/chrome/browser/sessions/session_window.h" | 5 #import "ios/chrome/browser/sessions/session_window.h" |
6 | 6 |
7 #import <Foundation/Foundation.h> | 7 #import <Foundation/Foundation.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <utility> | 10 #include <utility> |
11 | 11 |
12 #include "base/mac/scoped_nsobject.h" | 12 #include "base/mac/scoped_nsobject.h" |
13 #include "base/strings/sys_string_conversions.h" | 13 #include "base/strings/sys_string_conversions.h" |
14 #include "ios/chrome/browser/browser_state/test_chrome_browser_state.h" | 14 #include "ios/chrome/browser/browser_state/test_chrome_browser_state.h" |
15 #import "ios/chrome/browser/sessions/session_service.h" | 15 #import "ios/chrome/browser/sessions/session_service.h" |
16 #import "ios/web/navigation/crw_session_controller.h" | 16 #import "ios/web/navigation/crw_session_controller.h" |
17 #import "ios/web/public/crw_session_storage.h" | 17 #import "ios/web/public/crw_session_storage.h" |
18 #include "ios/web/public/test/test_web_thread_bundle.h" | 18 #include "ios/web/public/test/test_web_thread_bundle.h" |
19 #import "ios/web/public/web_state/web_state.h" | 19 #import "ios/web/public/web_state/web_state.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
21 #include "testing/gtest_mac.h" | 21 #include "testing/gtest_mac.h" |
22 #include "testing/platform_test.h" | 22 #include "testing/platform_test.h" |
23 #import "third_party/ocmock/OCMock/OCMock.h" | 23 #import "third_party/ocmock/OCMock/OCMock.h" |
24 | 24 |
25 using web::WebStateImpl; | |
26 | |
27 @interface SessionWindowIOS (Testing) | 25 @interface SessionWindowIOS (Testing) |
28 | 26 |
29 - (void)clearSessions; | 27 - (void)clearSessions; |
30 | 28 |
31 @end | 29 @end |
32 | 30 |
33 namespace { | 31 namespace { |
34 | 32 |
35 class SessionWindowIOSTest : public PlatformTest { | 33 class SessionWindowIOSTest : public PlatformTest { |
36 protected: | 34 protected: |
37 void SetUp() override { | 35 void SetUp() override { |
38 PlatformTest::SetUp(); | 36 PlatformTest::SetUp(); |
39 TestChromeBrowserState::Builder test_cbs_builder; | 37 TestChromeBrowserState::Builder test_cbs_builder; |
40 chrome_browser_state_ = test_cbs_builder.Build(); | 38 chrome_browser_state_ = test_cbs_builder.Build(); |
41 } | 39 } |
42 | 40 |
43 WebStateImpl* CreateWebState(BOOL openedByDOM) const { | 41 std::unique_ptr<web::WebState> CreateWebState(bool opened_by_dom) const { |
44 WebStateImpl* webState = new WebStateImpl(chrome_browser_state_.get()); | 42 web::WebState::CreateParams params(chrome_browser_state_.get()); |
45 webState->GetNavigationManagerImpl().InitializeSession(openedByDOM); | 43 params.opened_by_dom = opened_by_dom; |
46 return webState; | 44 return web::WebState::Create(params); |
47 } | 45 } |
48 | 46 |
49 web::TestWebThreadBundle thread_bundle_; | 47 web::TestWebThreadBundle thread_bundle_; |
50 // TODO(crbug.com/661639): Switch to TestBrowserState once this test is moved | 48 // TODO(crbug.com/661639): Switch to TestBrowserState once this test is moved |
51 // to be in the ios_web_unittests target. | 49 // to be in the ios_web_unittests target. |
52 std::unique_ptr<TestChromeBrowserState> chrome_browser_state_; | 50 std::unique_ptr<TestChromeBrowserState> chrome_browser_state_; |
53 }; | 51 }; |
54 | 52 |
55 TEST_F(SessionWindowIOSTest, InitEmpty) { | 53 TEST_F(SessionWindowIOSTest, InitEmpty) { |
56 base::scoped_nsobject<SessionWindowIOS> sessionWindow( | 54 base::scoped_nsobject<SessionWindowIOS> sessionWindow( |
57 [[SessionWindowIOS alloc] init]); | 55 [[SessionWindowIOS alloc] init]); |
58 EXPECT_TRUE(sessionWindow.get() != nil); | 56 EXPECT_TRUE(sessionWindow.get() != nil); |
59 } | 57 } |
60 | 58 |
61 TEST_F(SessionWindowIOSTest, InitAddingSessions) { | 59 TEST_F(SessionWindowIOSTest, InitAddingSessions) { |
62 std::unique_ptr<WebStateImpl> webState1(CreateWebState(NO)); | 60 std::unique_ptr<web::WebState> webState1 = CreateWebState(false); |
Eugene But (OOO till 7-30)
2017/03/16 15:31:19
nit: false /*opened_by_dom*/
kkhorimoto
2017/03/17 23:04:49
Done.
| |
63 std::unique_ptr<WebStateImpl> webState2(CreateWebState(NO)); | 61 std::unique_ptr<web::WebState> webState2 = CreateWebState(false); |
64 base::scoped_nsobject<SessionWindowIOS> sessionWindow( | 62 base::scoped_nsobject<SessionWindowIOS> sessionWindow( |
65 [[SessionWindowIOS alloc] init]); | 63 [[SessionWindowIOS alloc] init]); |
66 [sessionWindow addSerializedSessionStorage:webState1->BuildSessionStorage()]; | 64 [sessionWindow addSerializedSessionStorage:webState1->BuildSessionStorage()]; |
67 [sessionWindow addSerializedSessionStorage:webState2->BuildSessionStorage()]; | 65 [sessionWindow addSerializedSessionStorage:webState2->BuildSessionStorage()]; |
68 [sessionWindow setSelectedIndex:1]; | 66 [sessionWindow setSelectedIndex:1]; |
69 | 67 |
70 EXPECT_TRUE(sessionWindow.get() != nil); | 68 EXPECT_TRUE(sessionWindow.get() != nil); |
71 EXPECT_EQ(2U, sessionWindow.get().sessions.count); | 69 EXPECT_EQ(2U, sessionWindow.get().sessions.count); |
72 [sessionWindow clearSessions]; | 70 [sessionWindow clearSessions]; |
73 EXPECT_EQ(0U, sessionWindow.get().sessions.count); | 71 EXPECT_EQ(0U, sessionWindow.get().sessions.count); |
74 } | 72 } |
75 | 73 |
76 TEST_F(SessionWindowIOSTest, CodingEncoding) { | 74 TEST_F(SessionWindowIOSTest, CodingEncoding) { |
77 base::scoped_nsobject<SessionWindowIOS> sessionWindow( | 75 base::scoped_nsobject<SessionWindowIOS> sessionWindow( |
78 [[SessionWindowIOS alloc] init]); | 76 [[SessionWindowIOS alloc] init]); |
79 | 77 |
80 std::unique_ptr<WebStateImpl> webState1(CreateWebState(YES)); | 78 std::unique_ptr<web::WebState> webState1 = CreateWebState(true); |
Eugene But (OOO till 7-30)
2017/03/16 15:31:19
ditto
kkhorimoto
2017/03/17 23:04:49
Done.
| |
81 std::unique_ptr<WebStateImpl> webState2(CreateWebState(NO)); | 79 std::unique_ptr<web::WebState> webState2 = CreateWebState(false); |
82 [sessionWindow addSerializedSessionStorage:webState1->BuildSessionStorage()]; | 80 [sessionWindow addSerializedSessionStorage:webState1->BuildSessionStorage()]; |
83 [sessionWindow addSerializedSessionStorage:webState2->BuildSessionStorage()]; | 81 [sessionWindow addSerializedSessionStorage:webState2->BuildSessionStorage()]; |
84 | 82 |
85 [sessionWindow setSelectedIndex:1]; | 83 [sessionWindow setSelectedIndex:1]; |
86 | 84 |
87 NSData* data = [NSKeyedArchiver archivedDataWithRootObject:sessionWindow]; | 85 NSData* data = [NSKeyedArchiver archivedDataWithRootObject:sessionWindow]; |
88 EXPECT_TRUE(data != nil); | 86 EXPECT_TRUE(data != nil); |
89 base::scoped_nsobject<SessionWindowUnarchiver> unarchiver( | 87 base::scoped_nsobject<SessionWindowUnarchiver> unarchiver( |
90 [[SessionWindowUnarchiver alloc] | 88 [[SessionWindowUnarchiver alloc] |
91 initForReadingWithData:data | 89 initForReadingWithData:data |
92 browserState:chrome_browser_state_.get()]); | 90 browserState:chrome_browser_state_.get()]); |
93 SessionWindowIOS* unarchivedObj = [unarchiver decodeObjectForKey:@"root"]; | 91 SessionWindowIOS* unarchivedObj = [unarchiver decodeObjectForKey:@"root"]; |
94 EXPECT_TRUE(unarchivedObj != nil); | 92 EXPECT_TRUE(unarchivedObj != nil); |
95 EXPECT_EQ(unarchivedObj.selectedIndex, sessionWindow.get().selectedIndex); | 93 EXPECT_EQ(unarchivedObj.selectedIndex, sessionWindow.get().selectedIndex); |
96 NSArray* sessions = unarchivedObj.sessions; | 94 NSArray* sessions = unarchivedObj.sessions; |
97 ASSERT_EQ(2U, sessions.count); | 95 ASSERT_EQ(2U, sessions.count); |
98 CRWSessionStorage* unarchivedSession1 = sessions[0]; | 96 CRWSessionStorage* unarchivedSession1 = sessions[0]; |
99 EXPECT_TRUE(unarchivedSession1.openedByDOM); | 97 EXPECT_TRUE(unarchivedSession1.openedByDOM); |
100 | 98 |
101 CRWSessionStorage* unarchivedSession2 = sessions[1]; | 99 CRWSessionStorage* unarchivedSession2 = sessions[1]; |
102 EXPECT_FALSE(unarchivedSession2.openedByDOM); | 100 EXPECT_FALSE(unarchivedSession2.openedByDOM); |
103 } | 101 } |
104 | 102 |
105 } // anonymous namespace | 103 } // anonymous namespace |
OLD | NEW |