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