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.
5 #include "chrome/browser/sessions/session_service_test_helper.h"
7 #include "base/memory/scoped_vector.h"
8 #include "base/message_loop/message_loop.h"
9 #include "chrome/browser/sessions/session_service.h"
10 #include "components/sessions/base_session_service_test_helper.h"
11 #include "components/sessions/serialized_navigation_entry_test_helper.h"
12 #include "components/sessions/session_backend.h"
13 #include "components/sessions/session_id.h"
14 #include "components/sessions/session_types.h"
15 #include "content/public/browser/browser_thread.h"
16 #include "content/public/test/test_utils.h"
17 #include "testing/gtest/include/gtest/gtest.h"
21 SessionServiceTestHelper::SessionServiceTestHelper() {}
23 SessionServiceTestHelper::SessionServiceTestHelper(SessionService
* service
)
24 : service_(service
) {}
26 SessionServiceTestHelper::~SessionServiceTestHelper() {}
28 void SessionServiceTestHelper::PrepareTabInWindow(const SessionID
& window_id
,
29 const SessionID
& tab_id
,
32 service()->SetTabWindow(window_id
, tab_id
);
33 service()->SetTabIndexInWindow(window_id
, tab_id
, visual_index
);
35 service()->SetSelectedTabInWindow(window_id
, visual_index
);
38 void SessionServiceTestHelper::SetTabExtensionAppID(
39 const SessionID
& window_id
,
40 const SessionID
& tab_id
,
41 const std::string
& extension_app_id
) {
42 service()->SetTabExtensionAppID(window_id
, tab_id
, extension_app_id
);
45 void SessionServiceTestHelper::SetTabUserAgentOverride(
46 const SessionID
& window_id
,
47 const SessionID
& tab_id
,
48 const std::string
& user_agent_override
) {
49 service()->SetTabUserAgentOverride(window_id
, tab_id
, user_agent_override
);
52 void SessionServiceTestHelper::SetForceBrowserNotAliveWithNoWindows(
53 bool force_browser_not_alive_with_no_windows
) {
54 service()->force_browser_not_alive_with_no_windows_
=
55 force_browser_not_alive_with_no_windows
;
58 // Be sure and null out service to force closing the file.
59 void SessionServiceTestHelper::ReadWindows(
60 std::vector
<sessions::SessionWindow
*>* windows
,
61 SessionID::id_type
* active_window_id
) {
63 ScopedVector
<sessions::SessionCommand
> read_commands
;
64 sessions::BaseSessionServiceTestHelper
test_helper(
65 service_
->GetBaseSessionServiceForTest());
66 test_helper
.ReadLastSessionCommands(&read_commands
);
67 RestoreSessionFromCommands(read_commands
, windows
, active_window_id
);
68 service()->RemoveUnusedRestoreWindows(windows
);
71 void SessionServiceTestHelper::AssertTabEquals(
72 const SessionID
& window_id
,
73 const SessionID
& tab_id
,
77 const sessions::SessionTab
& session_tab
) {
78 EXPECT_EQ(window_id
.id(), session_tab
.window_id
.id());
79 EXPECT_EQ(tab_id
.id(), session_tab
.tab_id
.id());
80 AssertTabEquals(visual_index
, nav_index
, nav_count
, session_tab
);
83 void SessionServiceTestHelper::AssertTabEquals(
87 const sessions::SessionTab
& session_tab
) {
88 EXPECT_EQ(visual_index
, session_tab
.tab_visual_index
);
89 EXPECT_EQ(nav_index
, session_tab
.current_navigation_index
);
90 ASSERT_EQ(nav_count
, session_tab
.navigations
.size());
93 // TODO(sky): nuke this and change to call directly into
94 // SerializedNavigationEntryTestHelper.
95 void SessionServiceTestHelper::AssertNavigationEquals(
96 const sessions::SerializedNavigationEntry
& expected
,
97 const sessions::SerializedNavigationEntry
& actual
) {
98 sessions::SerializedNavigationEntryTestHelper::ExpectNavigationEquals(
102 void SessionServiceTestHelper::AssertSingleWindowWithSingleTab(
103 const std::vector
<sessions::SessionWindow
*>& windows
,
105 ASSERT_EQ(1U, windows
.size());
106 EXPECT_EQ(1U, windows
[0]->tabs
.size());
107 EXPECT_EQ(nav_count
, windows
[0]->tabs
[0]->navigations
.size());
110 void SessionServiceTestHelper::SetService(SessionService
* service
) {
111 service_
.reset(service
);
112 // Execute IO tasks posted by the SessionService.
113 content::RunAllBlockingPoolTasksUntilIdle();
116 SessionService
* SessionServiceTestHelper::ReleaseService() {
117 return service_
.release();
120 void SessionServiceTestHelper::RunTaskOnBackendThread(
121 const tracked_objects::Location
& from_here
,
122 const base::Closure
& task
) {
123 sessions::BaseSessionServiceTestHelper
test_helper(
124 service_
->GetBaseSessionServiceForTest());
125 test_helper
.RunTaskOnBackendThread(from_here
, task
);