1 // Copyright (c) 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_backend.h"
10 #include "chrome/browser/sessions/session_service.h"
11 #include "chrome/browser/sessions/session_types.h"
12 #include "components/sessions/serialized_navigation_entry_test_helper.h"
13 #include "components/sessions/session_id.h"
14 #include "content/public/browser/browser_thread.h"
15 #include "content/public/test/test_utils.h"
16 #include "testing/gtest/include/gtest/gtest.h"
20 SessionServiceTestHelper::SessionServiceTestHelper() {}
22 SessionServiceTestHelper::SessionServiceTestHelper(SessionService
* service
)
23 : service_(service
) {}
25 SessionServiceTestHelper::~SessionServiceTestHelper() {}
27 void SessionServiceTestHelper::PrepareTabInWindow(const SessionID
& window_id
,
28 const SessionID
& tab_id
,
31 service()->SetTabWindow(window_id
, tab_id
);
32 service()->SetTabIndexInWindow(window_id
, tab_id
, visual_index
);
34 service()->SetSelectedTabInWindow(window_id
, visual_index
);
37 void SessionServiceTestHelper::SetTabExtensionAppID(
38 const SessionID
& window_id
,
39 const SessionID
& tab_id
,
40 const std::string
& extension_app_id
) {
41 service()->SetTabExtensionAppID(window_id
, tab_id
, extension_app_id
);
44 void SessionServiceTestHelper::SetTabUserAgentOverride(
45 const SessionID
& window_id
,
46 const SessionID
& tab_id
,
47 const std::string
& user_agent_override
) {
48 service()->SetTabUserAgentOverride(window_id
, tab_id
, user_agent_override
);
51 void SessionServiceTestHelper::SetForceBrowserNotAliveWithNoWindows(
52 bool force_browser_not_alive_with_no_windows
) {
53 service()->force_browser_not_alive_with_no_windows_
=
54 force_browser_not_alive_with_no_windows
;
57 // Be sure and null out service to force closing the file.
58 void SessionServiceTestHelper::ReadWindows(
59 std::vector
<SessionWindow
*>* windows
,
60 SessionID::id_type
* active_window_id
) {
62 ScopedVector
<SessionCommand
> read_commands
;
63 backend()->ReadLastSessionCommandsImpl(&(read_commands
.get()));
64 service()->RestoreSessionFromCommands(
65 read_commands
.get(), windows
, active_window_id
);
68 void SessionServiceTestHelper::AssertTabEquals(const SessionID
& window_id
,
69 const SessionID
& tab_id
,
73 const SessionTab
& session_tab
) {
74 EXPECT_EQ(window_id
.id(), session_tab
.window_id
.id());
75 EXPECT_EQ(tab_id
.id(), session_tab
.tab_id
.id());
76 AssertTabEquals(visual_index
, nav_index
, nav_count
, session_tab
);
79 void SessionServiceTestHelper::AssertTabEquals(
83 const SessionTab
& session_tab
) {
84 EXPECT_EQ(visual_index
, session_tab
.tab_visual_index
);
85 EXPECT_EQ(nav_index
, session_tab
.current_navigation_index
);
86 ASSERT_EQ(nav_count
, session_tab
.navigations
.size());
89 // TODO(sky): nuke this and change to call directly into
90 // SerializedNavigationEntryTestHelper.
91 void SessionServiceTestHelper::AssertNavigationEquals(
92 const sessions::SerializedNavigationEntry
& expected
,
93 const sessions::SerializedNavigationEntry
& actual
) {
94 sessions::SerializedNavigationEntryTestHelper::ExpectNavigationEquals(
98 void SessionServiceTestHelper::AssertSingleWindowWithSingleTab(
99 const std::vector
<SessionWindow
*>& windows
,
101 ASSERT_EQ(1U, windows
.size());
102 EXPECT_EQ(1U, windows
[0]->tabs
.size());
103 EXPECT_EQ(nav_count
, windows
[0]->tabs
[0]->navigations
.size());
106 SessionBackend
* SessionServiceTestHelper::backend() {
107 return service_
->backend();
110 void SessionServiceTestHelper::SetService(SessionService
* service
) {
111 service_
.reset(service
);
112 // Execute IO tasks posted by the SessionService.
113 content::RunAllBlockingPoolTasksUntilIdle();
116 void SessionServiceTestHelper::RunTaskOnBackendThread(
117 const tracked_objects::Location
& from_here
,
118 const base::Closure
& task
) {
119 service_
->RunTaskOnBackendThread(from_here
, task
);