#include "chrome/browser/extensions/extension_apitest.h"
#include "chrome/browser/extensions/extension_function_test_utils.h"
#include "chrome/browser/profiles/profile_manager.h"
-#include "chrome/browser/sync/glue/local_device_info_provider_mock.h"
#include "chrome/browser/sync/open_tabs_ui_delegate.h"
#include "chrome/browser/sync/profile_sync_components_factory_mock.h"
#include "chrome/browser/sync/profile_sync_service.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/test_switches.h"
#include "chrome/test/base/testing_browser_process.h"
+#include "components/sync_driver/local_device_info_provider_mock.h"
#include "sync/api/attachments/attachment_id.h"
#include "sync/api/fake_sync_change_processor.h"
#include "sync/api/sync_error_factory_mock.h"
namespace {
-// If more sessions are added to session tags, num sessions should be updated.
-const char* kSessionTags[] = {"tag0", "tag1", "tag2", "tag3", "tag4"};
-const size_t kNumSessions = 5;
+// Fake session tabs (used to construct arbitrary device info) and tab IDs
+// (used to construct arbitrary tab info) to use in all tests.
+const char* const kSessionTags[] = {"tag0", "tag1", "tag2", "tag3", "tag4"};
+const SessionID::id_type kTabIDs[] = {5, 10, 13, 17};
void BuildSessionSpecifics(const std::string& tag,
sync_pb::SessionSpecifics* meta) {
}
}
-void BuildTabSpecifics(const std::string& tag, int window_id, int tab_id,
+void BuildTabSpecifics(const std::string& tag,
+ int tab_id,
sync_pb::SessionSpecifics* tab_base) {
tab_base->set_session_tag(tag);
tab_base->set_tab_node_id(0);
tab->set_extension_app_id("app_id");
sync_pb::TabNavigation* navigation = tab->add_navigation();
navigation->set_virtual_url("http://foo/1");
- navigation->set_referrer("referrer");
- navigation->set_title("title");
+ navigation->set_favicon_url("http://foo/favicon.ico");
+ navigation->set_referrer("MyReferrer");
+ navigation->set_title("MyTitle");
navigation->set_page_transition(sync_pb::SyncEnums_PageTransition_TYPED);
}
-} // namespace
-
class ExtensionSessionsTest : public InProcessBrowserTest {
public:
- virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE;
- virtual void SetUpOnMainThread() OVERRIDE;
+ void SetUpCommandLine(CommandLine* command_line) override;
+ void SetUpOnMainThread() override;
+
protected:
static KeyedService* BuildProfileSyncService(
content::BrowserContext* profile);
fn->set_extension(extension_.get());
fn->set_has_callback(has_callback);
return fn;
- };
+ }
Browser* browser_;
- scoped_refptr<extensions::Extension> extension_;
+ scoped_refptr<Extension> extension_;
};
void ExtensionSessionsTest::SetUpCommandLine(CommandLine* command_line) {
factory->SetLocalDeviceInfoProvider(
scoped_ptr<sync_driver::LocalDeviceInfoProvider>(
- new browser_sync::LocalDeviceInfoProviderMock(
+ new sync_driver::LocalDeviceInfoProviderMock(
kSessionTags[0],
"machine name",
"Chromium 10k",
void ExtensionSessionsTest::CreateSessionModels() {
syncer::SyncDataList initial_data;
- for (size_t index = 0; index < kNumSessions; ++index) {
+ for (size_t index = 0; index < arraysize(kSessionTags); ++index) {
// Fill an instance of session specifics with a foreign session's data.
sync_pb::SessionSpecifics meta;
BuildSessionSpecifics(kSessionTags[index], &meta);
- SessionID::id_type tab_nums1[] = {5, 10, 13, 17};
- std::vector<SessionID::id_type> tab_list1(
- tab_nums1, tab_nums1 + arraysize(tab_nums1));
- BuildWindowSpecifics(index, tab_list1, &meta);
- std::vector<sync_pb::SessionSpecifics> tabs1;
- tabs1.resize(tab_list1.size());
- for (size_t i = 0; i < tab_list1.size(); ++i) {
- BuildTabSpecifics(kSessionTags[index], 0, tab_list1[i], &tabs1[i]);
+ std::vector<SessionID::id_type> tab_list(kTabIDs,
+ kTabIDs + arraysize(kTabIDs));
+ BuildWindowSpecifics(index, tab_list, &meta);
+ std::vector<sync_pb::SessionSpecifics> tabs(tab_list.size());
+ for (size_t i = 0; i < tab_list.size(); ++i) {
+ BuildTabSpecifics(kSessionTags[index], tab_list[i], &tabs[i]);
}
sync_pb::EntitySpecifics entity;
base::Time(),
syncer::AttachmentIdList(),
syncer::AttachmentServiceProxyForTest::Create()));
- for (size_t i = 0; i < tabs1.size(); i++) {
+ for (size_t i = 0; i < tabs.size(); i++) {
sync_pb::EntitySpecifics entity;
- entity.mutable_session()->CopyFrom(tabs1[i]);
+ entity.mutable_session()->CopyFrom(tabs[i]);
initial_data.push_back(syncer::SyncData::CreateRemoteData(
i + 2,
entity,
new syncer::SyncErrorFactoryMock()));
}
+testing::AssertionResult CheckSessionModels(const base::ListValue& devices,
+ size_t num_sessions) {
+ EXPECT_EQ(5u, devices.GetSize());
+ const base::DictionaryValue* device = NULL;
+ const base::ListValue* sessions = NULL;
+ for (size_t i = 0; i < devices.GetSize(); ++i) {
+ EXPECT_TRUE(devices.GetDictionary(i, &device));
+ EXPECT_EQ(kSessionTags[i], utils::GetString(device, "info"));
+ EXPECT_EQ(kSessionTags[i], utils::GetString(device, "deviceName"));
+ EXPECT_TRUE(device->GetList("sessions", &sessions));
+ EXPECT_EQ(num_sessions, sessions->GetSize());
+ // Because this test is hurried, really there are only ever 0 or 1
+ // sessions, and if 1, that will be a Window. Grab it.
+ if (num_sessions == 0)
+ continue;
+ const base::DictionaryValue* session = NULL;
+ EXPECT_TRUE(sessions->GetDictionary(0, &session));
+ const base::DictionaryValue* window = NULL;
+ EXPECT_TRUE(session->GetDictionary("window", &window));
+ // Only the tabs are interesting.
+ const base::ListValue* tabs = NULL;
+ EXPECT_TRUE(window->GetList("tabs", &tabs));
+ EXPECT_EQ(arraysize(kTabIDs), tabs->GetSize());
+ for (size_t j = 0; j < tabs->GetSize(); ++j) {
+ const base::DictionaryValue* tab = NULL;
+ EXPECT_TRUE(tabs->GetDictionary(j, &tab));
+ EXPECT_FALSE(tab->HasKey("id")); // sessions API does not give tab IDs
+ EXPECT_EQ(static_cast<int>(j), utils::GetInteger(tab, "index"));
+ EXPECT_EQ(0, utils::GetInteger(tab, "windowId"));
+ // Test setup code always sets tab 0 to selected (which means active in
+ // extension terminology).
+ EXPECT_EQ(j == 0, utils::GetBoolean(tab, "active"));
+ // While selected/highlighted are different to active, and should always
+ // be false.
+ EXPECT_FALSE(utils::GetBoolean(tab, "selected"));
+ EXPECT_FALSE(utils::GetBoolean(tab, "highlighted"));
+ EXPECT_FALSE(utils::GetBoolean(tab, "incognito"));
+ EXPECT_TRUE(utils::GetBoolean(tab, "pinned"));
+ EXPECT_EQ("http://foo/1", utils::GetString(tab, "url"));
+ EXPECT_EQ("MyTitle", utils::GetString(tab, "title"));
+ EXPECT_EQ("http://foo/favicon.ico", utils::GetString(tab, "favIconUrl"));
+ EXPECT_EQ(base::StringPrintf("%s.%d", kSessionTags[i], kTabIDs[j]),
+ utils::GetString(tab, "sessionId"));
+ }
+ }
+ return testing::AssertionSuccess();
+}
+
IN_PROC_BROWSER_TEST_F(ExtensionSessionsTest, GetDevices) {
CreateSessionModels();
-
scoped_ptr<base::ListValue> result(utils::ToList(
utils::RunFunctionAndReturnSingleResult(
CreateFunction<SessionsGetDevicesFunction>(true).get(),
"[{\"maxResults\": 0}]",
browser_)));
ASSERT_TRUE(result);
- base::ListValue* devices = result.get();
- EXPECT_EQ(5u, devices->GetSize());
- base::DictionaryValue* device = NULL;
- base::ListValue* sessions = NULL;
- for (size_t i = 0; i < devices->GetSize(); ++i) {
- EXPECT_TRUE(devices->GetDictionary(i, &device));
- EXPECT_EQ(kSessionTags[i], utils::GetString(device, "info"));
- EXPECT_EQ(kSessionTags[i], utils::GetString(device, "deviceName"));
- EXPECT_TRUE(device->GetList("sessions", &sessions));
- EXPECT_EQ(0u, sessions->GetSize());
- }
+ EXPECT_TRUE(CheckSessionModels(*result, 0u));
}
IN_PROC_BROWSER_TEST_F(ExtensionSessionsTest, GetDevicesMaxResults) {
CreateSessionModels();
-
scoped_ptr<base::ListValue> result(utils::ToList(
utils::RunFunctionAndReturnSingleResult(
CreateFunction<SessionsGetDevicesFunction>(true).get(),
"[]",
browser_)));
ASSERT_TRUE(result);
- base::ListValue* devices = result.get();
- EXPECT_EQ(5u, devices->GetSize());
- base::DictionaryValue* device = NULL;
- base::ListValue* sessions = NULL;
- for (size_t i = 0; i < devices->GetSize(); ++i) {
- EXPECT_TRUE(devices->GetDictionary(i, &device));
- EXPECT_EQ(kSessionTags[i], utils::GetString(device, "info"));
- EXPECT_EQ(kSessionTags[i], utils::GetString(device, "deviceName"));
- EXPECT_TRUE(device->GetList("sessions", &sessions));
- EXPECT_EQ(1u, sessions->GetSize());
- }
+ EXPECT_TRUE(CheckSessionModels(*result, 1u));
}
IN_PROC_BROWSER_TEST_F(ExtensionSessionsTest, GetDevicesListEmpty) {
"sessions.html")) << message_;
}
+} // namespace
+
} // namespace extensions