Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(200)

Side by Side Diff: chrome/browser/extensions/api/sessions/sessions_apitest.cc

Issue 21022018: Sessions API - previously Session Restore API. Supports restoring currently open foreign windows an… (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: delete test. Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2013 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 #include "chrome/browser/extensions/api/sessions/sessions_api.h"
6
7 #include "base/command_line.h"
8 #include "base/path_service.h"
9 #include "base/strings/stringprintf.h"
10 #include "chrome/browser/extensions/api/tabs/tabs_api.h"
11 #include "chrome/browser/extensions/extension_apitest.h"
12 #include "chrome/browser/extensions/extension_function_test_utils.h"
13 #include "chrome/browser/profiles/profile_manager.h"
14 #include "chrome/browser/sync/glue/session_model_associator.h"
15 #include "chrome/browser/sync/profile_sync_service.h"
16 #include "chrome/browser/sync/profile_sync_service_factory.h"
17 #include "chrome/browser/sync/profile_sync_service_mock.h"
18 #include "chrome/common/chrome_paths.h"
19 #include "chrome/test/base/in_process_browser_test.h"
20 #include "chrome/test/base/test_switches.h"
21 #include "chrome/test/base/testing_browser_process.h"
22
23 namespace utils = extension_function_test_utils;
24
25 namespace extensions {
26
27 namespace {
28
29 // If more sessions are added to session tags, num sessions should be updated.
30 const char* kSessionTags[] = {"tag0", "tag1", "tag2", "tag3", "tag4"};
31 const size_t kNumSessions = 5;
32
33 void BuildSessionSpecifics(const std::string& tag,
34 sync_pb::SessionSpecifics* meta) {
35 meta->set_session_tag(tag);
36 sync_pb::SessionHeader* header = meta->mutable_header();
37 header->set_device_type(sync_pb::SyncEnums_DeviceType_TYPE_LINUX);
38 header->set_client_name(tag);
39 }
40
41 void BuildWindowSpecifics(int window_id,
42 const std::vector<int>& tab_list,
43 sync_pb::SessionSpecifics* meta) {
44 sync_pb::SessionHeader* header = meta->mutable_header();
45 sync_pb::SessionWindow* window = header->add_window();
46 window->set_window_id(window_id);
47 window->set_selected_tab_index(0);
48 window->set_browser_type(sync_pb::SessionWindow_BrowserType_TYPE_TABBED);
49 for (std::vector<int>::const_iterator iter = tab_list.begin();
50 iter != tab_list.end(); ++iter) {
51 window->add_tab(*iter);
52 }
53 }
54
55 void BuildTabSpecifics(const std::string& tag, int window_id, int tab_id,
56 sync_pb::SessionSpecifics* tab_base) {
57 tab_base->set_session_tag(tag);
58 tab_base->set_tab_node_id(0);
59 sync_pb::SessionTab* tab = tab_base->mutable_tab();
60 tab->set_tab_id(tab_id);
61 tab->set_tab_visual_index(1);
62 tab->set_current_navigation_index(0);
63 tab->set_pinned(true);
64 tab->set_extension_app_id("app_id");
65 sync_pb::TabNavigation* navigation = tab->add_navigation();
66 navigation->set_virtual_url("http://foo/1");
67 navigation->set_referrer("referrer");
68 navigation->set_title("title");
69 navigation->set_page_transition(sync_pb::SyncEnums_PageTransition_TYPED);
70 }
71
72 } // namespace
73
74 class ExtensionSessionsTest : public InProcessBrowserTest {
75 public:
76 virtual void SetUpOnMainThread() OVERRIDE;
77 protected:
78 void CreateTestProfileSyncService();
79 void CreateTestExtension();
80 void CreateSessionModels();
81
82 template <class T>
83 scoped_refptr<T> CreateFunction(bool has_callback) {
84 scoped_refptr<T> fn(new T());
85 fn->set_extension(extension_.get());
86 fn->set_has_callback(has_callback);
87 return fn;
88 };
89
90 Browser* browser_;
91 browser_sync::SessionModelAssociator* associator_;
92 scoped_refptr<extensions::Extension> extension_;
93 };
94
95 void ExtensionSessionsTest::SetUpOnMainThread() {
96 CreateTestProfileSyncService();
97 CreateTestExtension();
98 }
99
100 void ExtensionSessionsTest::CreateTestProfileSyncService() {
101 ProfileManager* profile_manager = g_browser_process->profile_manager();
102 base::FilePath path;
103 PathService::Get(chrome::DIR_USER_DATA, &path);
104 path = path.AppendASCII("test_profile");
105 if (!base::PathExists(path))
106 CHECK(file_util::CreateDirectory(path));
107 Profile* profile =
108 Profile::CreateProfile(path, NULL, Profile::CREATE_MODE_SYNCHRONOUS);
109 profile_manager->RegisterTestingProfile(profile, true, false);
110 browser_ = new Browser(Browser::CreateParams(
111 profile, chrome::HOST_DESKTOP_TYPE_NATIVE));
112 ProfileSyncServiceMock* service = static_cast<ProfileSyncServiceMock*>(
113 ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse(
114 profile, &ProfileSyncServiceMock::BuildMockProfileSyncService));
115
116 associator_ = new browser_sync::SessionModelAssociator(
117 static_cast<ProfileSyncService*>(service), true);
118 syncer::ModelTypeSet preferred_types;
119 preferred_types.Put(syncer::SESSIONS);
120 GoogleServiceAuthError no_error(GoogleServiceAuthError::NONE);
121
122 ON_CALL(*service, GetSessionModelAssociator()).WillByDefault(
123 testing::Return(associator_));
124 ON_CALL(*service, GetPreferredDataTypes()).WillByDefault(
125 testing::Return(preferred_types));
126 EXPECT_CALL(*service, GetAuthError()).WillRepeatedly(
127 testing::ReturnRef(no_error));
128 ON_CALL(*service, GetActiveDataTypes()).WillByDefault(
129 testing::Return(preferred_types));
130 EXPECT_CALL(*service, AddObserver(testing::_)).Times(testing::AnyNumber());
131 EXPECT_CALL(*service, RemoveObserver(testing::_)).Times(testing::AnyNumber());
132
133 service->Initialize();
134 }
135
136 void ExtensionSessionsTest::CreateTestExtension() {
137 scoped_ptr<base::DictionaryValue> test_extension_value(
138 utils::ParseDictionary(
139 "{\"name\": \"Test\", \"version\": \"1.0\", "
140 "\"permissions\": [\"sessions\", \"tabs\"]}"));
141 extension_ = utils::CreateExtension(test_extension_value.get());
142 }
143
144 void ExtensionSessionsTest::CreateSessionModels() {
145 for (size_t index = 0; index < kNumSessions; ++index) {
146 // Fill an instance of session specifics with a foreign session's data.
147 sync_pb::SessionSpecifics meta;
148 BuildSessionSpecifics(kSessionTags[index], &meta);
149 SessionID::id_type tab_nums1[] = {5, 10, 13, 17};
150 std::vector<SessionID::id_type> tab_list1(
151 tab_nums1, tab_nums1 + arraysize(tab_nums1));
152 BuildWindowSpecifics(index, tab_list1, &meta);
153 std::vector<sync_pb::SessionSpecifics> tabs1;
154 tabs1.resize(tab_list1.size());
155 for (size_t i = 0; i < tab_list1.size(); ++i) {
156 BuildTabSpecifics(kSessionTags[index], 0, tab_list1[i], &tabs1[i]);
157 }
158
159 associator_->SetCurrentMachineTagForTesting(kSessionTags[index]);
160 // Update associator with the session's meta node containing one window.
161 associator_->AssociateForeignSpecifics(meta, base::Time());
162 // Add tabs for the window.
163 for (std::vector<sync_pb::SessionSpecifics>::iterator iter = tabs1.begin();
164 iter != tabs1.end(); ++iter) {
165 associator_->AssociateForeignSpecifics(*iter, base::Time());
166 }
167 }
168 }
169
170 IN_PROC_BROWSER_TEST_F(ExtensionSessionsTest, GetDevices) {
171 CreateSessionModels();
172
173 scoped_ptr<base::ListValue> result(utils::ToList(
174 utils::RunFunctionAndReturnSingleResult(
175 CreateFunction<SessionsGetDevicesFunction>(true).get(),
176 "[{\"maxResults\": 0}]",
177 browser_)));
178 ASSERT_TRUE(result);
179 ListValue* devices = result.get();
180 EXPECT_EQ(5u, devices->GetSize());
181 DictionaryValue* device = NULL;
182 ListValue* sessions = NULL;
183 for (size_t i = 0; i < devices->GetSize(); ++i) {
184 EXPECT_TRUE(devices->GetDictionary(i, &device));
185 EXPECT_EQ(kSessionTags[i], utils::GetString(device, "info"));
186 EXPECT_TRUE(device->GetList("sessions", &sessions));
187 EXPECT_EQ(0u, sessions->GetSize());
188 }
189 }
190
191 IN_PROC_BROWSER_TEST_F(ExtensionSessionsTest, GetDevicesMaxResults) {
192 CreateSessionModels();
193
194 scoped_ptr<base::ListValue> result(utils::ToList(
195 utils::RunFunctionAndReturnSingleResult(
196 CreateFunction<SessionsGetDevicesFunction>(true).get(),
197 "[]",
198 browser_)));
199 ASSERT_TRUE(result);
200 ListValue* devices = result.get();
201 EXPECT_EQ(5u, devices->GetSize());
202 DictionaryValue* device = NULL;
203 ListValue* sessions = NULL;
204 for (size_t i = 0; i < devices->GetSize(); ++i) {
205 EXPECT_TRUE(devices->GetDictionary(i, &device));
206 EXPECT_EQ(kSessionTags[i], utils::GetString(device, "info"));
207 EXPECT_TRUE(device->GetList("sessions", &sessions));
208 EXPECT_EQ(1u, sessions->GetSize());
209 }
210 }
211
212 IN_PROC_BROWSER_TEST_F(ExtensionSessionsTest, GetDevicesListEmpty) {
213 scoped_ptr<base::ListValue> result(utils::ToList(
214 utils::RunFunctionAndReturnSingleResult(
215 CreateFunction<SessionsGetDevicesFunction>(true).get(),
216 "[]",
217 browser_)));
218
219 ASSERT_TRUE(result);
220 ListValue* devices = result.get();
221 EXPECT_EQ(0u, devices->GetSize());
222 }
223
224 IN_PROC_BROWSER_TEST_F(ExtensionSessionsTest, RestoreForeignSessionWindow) {
225 CreateSessionModels();
226
227 scoped_ptr<base::DictionaryValue> restored_window_session(utils::ToDictionary(
228 utils::RunFunctionAndReturnSingleResult(
229 CreateFunction<SessionsRestoreFunction>(true).get(),
230 "[\"tag3.3\"]",
231 browser_,
232 utils::INCLUDE_INCOGNITO)));
233 ASSERT_TRUE(restored_window_session);
234
235 scoped_ptr<base::ListValue> result(utils::ToList(
236 utils::RunFunctionAndReturnSingleResult(
237 CreateFunction<WindowsGetAllFunction>(true).get(),
238 "[]",
239 browser_)));
240 ASSERT_TRUE(result);
241
242 ListValue* windows = result.get();
243 EXPECT_EQ(2u, windows->GetSize());
244 DictionaryValue* restored_window = NULL;
245 EXPECT_TRUE(restored_window_session->GetDictionary("window",
246 &restored_window));
247 DictionaryValue* window = NULL;
248 int restored_id = utils::GetInteger(restored_window, "id");
249 for (size_t i = 0; i < windows->GetSize(); ++i) {
250 EXPECT_TRUE(windows->GetDictionary(i, &window));
251 if (utils::GetInteger(window, "id") == restored_id)
252 break;
253 }
254 EXPECT_EQ(restored_id, utils::GetInteger(window, "id"));
255 }
256
257 IN_PROC_BROWSER_TEST_F(ExtensionSessionsTest, RestoreForeignSessionInvalidId) {
258 CreateSessionModels();
259
260 EXPECT_TRUE(MatchPattern(utils::RunFunctionAndReturnError(
261 CreateFunction<SessionsRestoreFunction>(true).get(),
262 "[\"tag3.0\"]",
263 browser_), "Invalid session id: \"tag3.0\"."));
264 }
265
266 // Flaky on ChromeOS, times out on OSX Debug http://crbug.com/251199
267 #if defined(OS_CHROMEOS) || (defined(OS_MACOSX) && !defined(NDEBUG))
268 #define MAYBE_SessionsApis DISABLED_SessionsApis
269 #else
270 #define MAYBE_SessionsApis SessionsApis
271 #endif
272 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, MAYBE_SessionsApis) {
273 #if defined(OS_WIN) && defined(USE_ASH)
274 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
275 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
276 return;
277 #endif
278
279 ASSERT_TRUE(RunExtensionSubtest("sessions",
280 "sessions.html")) << message_;
281 }
282
283 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/api/sessions/sessions_api.cc ('k') | chrome/browser/extensions/api/tabs/tabs_api.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698