This source file includes following definitions.
- enable_count_
- IsCommandIdEnabled
- ExecuteCommand
- execute_count
- enable_count
- got_changes_
- OnIconChanged
- OnMenuStructureChanged
- got_changes
- StartRoutingTo
- Stop
- WaitForLoadFromLastSession
- GetTabRestoreService
- GetOpenTabsDelegate
- RegisterRecentTabs
- GetLocalDeviceInfo
- GetLocalSyncCacheGUID
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "chrome/browser/ui/toolbar/recent_tabs_sub_menu_model.h"
#include "base/command_line.h"
#include "base/run_loop.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/sessions/session_service.h"
#include "chrome/browser/sessions/session_service_factory.h"
#include "chrome/browser/sessions/session_types.h"
#include "chrome/browser/sessions/persistent_tab_restore_service.h"
#include "chrome/browser/sessions/tab_restore_service_factory.h"
#include "chrome/browser/sync/glue/session_model_associator.h"
#include "chrome/browser/sync/glue/synced_session.h"
#include "chrome/browser/sync/profile_sync_service_mock.h"
#include "chrome/browser/sync/sessions2/sessions_sync_manager.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_tabstrip.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/toolbar/recent_tabs_builder_test_helper.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/test/base/browser_with_test_window_test.h"
#include "chrome/test/base/menu_model_test.h"
#include "chrome/test/base/testing_profile.h"
#include "components/sessions/serialized_navigation_entry_test_helper.h"
#include "content/public/browser/browser_thread.h"
#include "grit/generated_resources.h"
#include "sync/api/fake_sync_change_processor.h"
#include "sync/api/sync_error_factory_mock.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
class TestRecentTabsSubMenuModel : public RecentTabsSubMenuModel {
public:
TestRecentTabsSubMenuModel(ui::AcceleratorProvider* provider,
Browser* browser,
browser_sync::OpenTabsUIDelegate* delegate)
: RecentTabsSubMenuModel(provider, browser, delegate),
execute_count_(0),
enable_count_(0) {
}
virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE {
bool val = RecentTabsSubMenuModel::IsCommandIdEnabled(command_id);
if (val)
++enable_count_;
return val;
}
virtual void ExecuteCommand(int command_id, int event_flags) OVERRIDE {
++execute_count_;
}
int execute_count() const { return execute_count_; }
int enable_count() const { return enable_count_; }
private:
int execute_count_;
int mutable enable_count_;
DISALLOW_COPY_AND_ASSIGN(TestRecentTabsSubMenuModel);
};
class TestRecentTabsMenuModelDelegate : public ui::MenuModelDelegate {
public:
explicit TestRecentTabsMenuModelDelegate(ui::MenuModel* model)
: model_(model),
got_changes_(false) {
model_->SetMenuModelDelegate(this);
}
virtual ~TestRecentTabsMenuModelDelegate() {
model_->SetMenuModelDelegate(NULL);
}
virtual void OnIconChanged(int index) OVERRIDE {
}
virtual void OnMenuStructureChanged() OVERRIDE {
got_changes_ = true;
}
bool got_changes() const { return got_changes_; }
private:
ui::MenuModel* model_;
bool got_changes_;
DISALLOW_COPY_AND_ASSIGN(TestRecentTabsMenuModelDelegate);
};
class DummyRouter : public browser_sync::LocalSessionEventRouter {
public:
virtual ~DummyRouter() {}
virtual void StartRoutingTo(
browser_sync::LocalSessionEventHandler* handler) OVERRIDE {}
virtual void Stop() OVERRIDE {}
};
}
class RecentTabsSubMenuModelTest
: public BrowserWithTestWindowTest,
public browser_sync::SessionsSyncManager::SyncInternalApiDelegate {
public:
RecentTabsSubMenuModelTest()
: sync_service_(&testing_profile_) {
if (!CommandLine::ForCurrentProcess()->HasSwitch(
switches::kDisableSyncSessionsV2)) {
manager_.reset(new browser_sync::SessionsSyncManager(
&testing_profile_,
this,
scoped_ptr<browser_sync::LocalSessionEventRouter>(
new DummyRouter())));
manager_->MergeDataAndStartSyncing(
syncer::SESSIONS,
syncer::SyncDataList(),
scoped_ptr<syncer::SyncChangeProcessor>(
new syncer::FakeSyncChangeProcessor),
scoped_ptr<syncer::SyncErrorFactory>(
new syncer::SyncErrorFactoryMock));
} else {
associator_.reset(new browser_sync::SessionModelAssociator(
&sync_service_, true));
associator_->SetCurrentMachineTagForTesting(
GetLocalSyncCacheGUID());
}
}
void WaitForLoadFromLastSession() {
content::BrowserThread::GetBlockingPool()->FlushForTesting();
base::RunLoop().RunUntilIdle();
content::BrowserThread::GetBlockingPool()->FlushForTesting();
}
static KeyedService* GetTabRestoreService(
content::BrowserContext* browser_context) {
return new PersistentTabRestoreService(
Profile::FromBrowserContext(browser_context), NULL);
}
browser_sync::OpenTabsUIDelegate* GetOpenTabsDelegate() {
if (!CommandLine::ForCurrentProcess()->HasSwitch(
switches::kDisableSyncSessionsV2)) {
return manager_.get();
} else {
return associator_.get();
}
}
void RegisterRecentTabs(RecentTabsBuilderTestHelper* helper) {
if (!CommandLine::ForCurrentProcess()->HasSwitch(
switches::kDisableSyncSessionsV2)) {
helper->ExportToSessionsSyncManager(manager_.get());
} else {
helper->ExportToSessionModelAssociator(associator_.get());
}
}
virtual scoped_ptr<browser_sync::DeviceInfo> GetLocalDeviceInfo()
const OVERRIDE {
return scoped_ptr<browser_sync::DeviceInfo>(
new browser_sync::DeviceInfo(GetLocalSyncCacheGUID(),
"Test Machine",
"Chromium 10k",
"Chrome 10k",
sync_pb::SyncEnums_DeviceType_TYPE_LINUX));
}
virtual std::string GetLocalSyncCacheGUID() const OVERRIDE {
return "RecentTabsSubMenuModelTest";
}
private:
TestingProfile testing_profile_;
testing::NiceMock<ProfileSyncServiceMock> sync_service_;
scoped_ptr<browser_sync::SessionModelAssociator> associator_;
scoped_ptr<browser_sync::SessionsSyncManager> manager_;
};
TEST_F(RecentTabsSubMenuModelTest, NoTabs) {
TestRecentTabsSubMenuModel model(NULL, browser(), NULL);
int num_items = model.GetItemCount();
EXPECT_EQ(3, num_items);
EXPECT_FALSE(model.IsEnabledAt(0));
EXPECT_FALSE(model.IsEnabledAt(2));
EXPECT_EQ(0, model.enable_count());
EXPECT_EQ(NULL, model.GetLabelFontListAt(0));
EXPECT_EQ(NULL, model.GetLabelFontListAt(1));
EXPECT_EQ(NULL, model.GetLabelFontListAt(2));
std::string url;
base::string16 title;
EXPECT_FALSE(model.GetURLAndTitleForItemAtIndex(0, &url, &title));
EXPECT_FALSE(model.GetURLAndTitleForItemAtIndex(1, &url, &title));
EXPECT_FALSE(model.GetURLAndTitleForItemAtIndex(2, &url, &title));
}
TEST_F(RecentTabsSubMenuModelTest, RecentlyClosedTabsFromCurrentSession) {
TabRestoreServiceFactory::GetInstance()->SetTestingFactory(
profile(), RecentTabsSubMenuModelTest::GetTabRestoreService);
AddTab(browser(), GURL("http://foo/1"));
AddTab(browser(), GURL("http://foo/2"));
browser()->tab_strip_model()->CloseAllTabs();
TestRecentTabsSubMenuModel model(NULL, browser(), NULL);
int num_items = model.GetItemCount();
EXPECT_EQ(5, num_items);
EXPECT_FALSE(model.IsEnabledAt(0));
EXPECT_TRUE(model.IsEnabledAt(1));
EXPECT_TRUE(model.IsEnabledAt(2));
model.ActivatedAt(1);
model.ActivatedAt(2);
EXPECT_FALSE(model.IsEnabledAt(4));
EXPECT_EQ(2, model.enable_count());
EXPECT_EQ(2, model.execute_count());
EXPECT_TRUE(model.GetLabelFontListAt(0) != NULL);
EXPECT_EQ(NULL, model.GetLabelFontListAt(1));
EXPECT_EQ(NULL, model.GetLabelFontListAt(2));
EXPECT_EQ(NULL, model.GetLabelFontListAt(3));
EXPECT_EQ(NULL, model.GetLabelFontListAt(4));
std::string url;
base::string16 title;
EXPECT_FALSE(model.GetURLAndTitleForItemAtIndex(0, &url, &title));
EXPECT_TRUE(model.GetURLAndTitleForItemAtIndex(1, &url, &title));
EXPECT_TRUE(model.GetURLAndTitleForItemAtIndex(2, &url, &title));
EXPECT_FALSE(model.GetURLAndTitleForItemAtIndex(3, &url, &title));
EXPECT_FALSE(model.GetURLAndTitleForItemAtIndex(4, &url, &title));
}
#if defined(OS_MACOSX)
#define MAYBE_RecentlyClosedTabsAndWindowsFromLastSession \
DISABLED_RecentlyClosedTabsAndWindowsFromLastSession
#else
#define MAYBE_RecentlyClosedTabsAndWindowsFromLastSession \
RecentlyClosedTabsAndWindowsFromLastSession
#endif
TEST_F(RecentTabsSubMenuModelTest,
MAYBE_RecentlyClosedTabsAndWindowsFromLastSession) {
TabRestoreServiceFactory::GetInstance()->SetTestingFactory(
profile(), RecentTabsSubMenuModelTest::GetTabRestoreService);
AddTab(browser(), GURL("http://wnd/tab0"));
AddTab(browser(), GURL("http://wnd/tab1"));
browser()->tab_strip_model()->CloseAllTabs();
SessionService* session_service = new SessionService(profile());
SessionServiceFactory::SetForTestProfile(profile(), session_service);
SessionID tab_id;
SessionID window_id;
session_service->SetWindowType(
window_id, Browser::TYPE_TABBED, SessionService::TYPE_NORMAL);
session_service->SetTabWindow(window_id, tab_id);
session_service->SetTabIndexInWindow(window_id, tab_id, 0);
session_service->SetSelectedTabInWindow(window_id, 0);
session_service->UpdateTabNavigation(
window_id, tab_id,
sessions::SerializedNavigationEntryTestHelper::CreateNavigation(
"http://wnd1/tab0", "title"));
profile()->set_last_session_exited_cleanly(false);
SessionServiceFactory::GetForProfile(profile())->
MoveCurrentSessionToLastSession();
TabRestoreServiceFactory::GetInstance()->SetTestingFactory(
profile(), RecentTabsSubMenuModelTest::GetTabRestoreService);
content::BrowserThread::GetBlockingPool()->FlushForTesting();
TestRecentTabsSubMenuModel model(NULL, browser(), NULL);
TestRecentTabsMenuModelDelegate delegate(&model);
EXPECT_FALSE(delegate.got_changes());
int num_items = model.GetItemCount();
EXPECT_EQ(3, num_items);
EXPECT_FALSE(model.IsEnabledAt(0));
EXPECT_EQ(ui::MenuModel::TYPE_SEPARATOR, model.GetTypeAt(1));
EXPECT_FALSE(model.IsEnabledAt(2));
EXPECT_EQ(0, model.enable_count());
WaitForLoadFromLastSession();
EXPECT_TRUE(delegate.got_changes());
num_items = model.GetItemCount();
EXPECT_EQ(6, num_items);
EXPECT_FALSE(model.IsEnabledAt(0));
EXPECT_TRUE(model.IsEnabledAt(1));
EXPECT_TRUE(model.IsEnabledAt(2));
EXPECT_TRUE(model.IsEnabledAt(3));
model.ActivatedAt(1);
model.ActivatedAt(2);
model.ActivatedAt(3);
EXPECT_EQ(ui::MenuModel::TYPE_SEPARATOR, model.GetTypeAt(4));
EXPECT_FALSE(model.IsEnabledAt(5));
EXPECT_EQ(3, model.enable_count());
EXPECT_EQ(3, model.execute_count());
EXPECT_TRUE(model.GetLabelFontListAt(0) != NULL);
EXPECT_EQ(NULL, model.GetLabelFontListAt(1));
EXPECT_EQ(NULL, model.GetLabelFontListAt(2));
EXPECT_EQ(NULL, model.GetLabelFontListAt(3));
EXPECT_EQ(NULL, model.GetLabelFontListAt(4));
EXPECT_EQ(NULL, model.GetLabelFontListAt(5));
std::string url;
base::string16 title;
EXPECT_FALSE(model.GetURLAndTitleForItemAtIndex(0, &url, &title));
EXPECT_FALSE(model.GetURLAndTitleForItemAtIndex(1, &url, &title));
EXPECT_TRUE(model.GetURLAndTitleForItemAtIndex(2, &url, &title));
EXPECT_TRUE(model.GetURLAndTitleForItemAtIndex(3, &url, &title));
EXPECT_FALSE(model.GetURLAndTitleForItemAtIndex(4, &url, &title));
EXPECT_FALSE(model.GetURLAndTitleForItemAtIndex(5, &url, &title));
}
TEST_F(RecentTabsSubMenuModelTest, OtherDevices) {
base::Time timestamp = base::Time::Now();
const base::TimeDelta time_delta = base::TimeDelta::FromMinutes(10);
RecentTabsBuilderTestHelper recent_tabs_builder;
recent_tabs_builder.AddSession();
recent_tabs_builder.AddWindow(0);
for (int i = 0; i < 3; ++i) {
timestamp -= time_delta;
recent_tabs_builder.AddTabWithInfo(0, 0, timestamp, base::string16());
}
recent_tabs_builder.AddSession();
recent_tabs_builder.AddWindow(1);
recent_tabs_builder.AddWindow(1);
timestamp -= time_delta;
recent_tabs_builder.AddTabWithInfo(1, 0, timestamp, base::string16());
timestamp -= time_delta;
recent_tabs_builder.AddTabWithInfo(1, 1, timestamp, base::string16());
timestamp -= time_delta;
recent_tabs_builder.AddTabWithInfo(1, 1, timestamp, base::string16());
RegisterRecentTabs(&recent_tabs_builder);
TestRecentTabsSubMenuModel model(NULL, browser(), GetOpenTabsDelegate());
int num_items = model.GetItemCount();
EXPECT_EQ(13, num_items);
model.ActivatedAt(0);
EXPECT_FALSE(model.IsEnabledAt(0));
model.ActivatedAt(3);
EXPECT_TRUE(model.IsEnabledAt(3));
model.ActivatedAt(4);
EXPECT_TRUE(model.IsEnabledAt(4));
model.ActivatedAt(5);
EXPECT_TRUE(model.IsEnabledAt(5));
model.ActivatedAt(8);
EXPECT_TRUE(model.IsEnabledAt(8));
model.ActivatedAt(9);
EXPECT_TRUE(model.IsEnabledAt(9));
model.ActivatedAt(10);
EXPECT_TRUE(model.IsEnabledAt(10));
EXPECT_TRUE(model.IsEnabledAt(12));
EXPECT_EQ(7, model.enable_count());
EXPECT_EQ(7, model.execute_count());
EXPECT_EQ(NULL, model.GetLabelFontListAt(0));
EXPECT_EQ(NULL, model.GetLabelFontListAt(1));
EXPECT_TRUE(model.GetLabelFontListAt(2) != NULL);
EXPECT_EQ(NULL, model.GetLabelFontListAt(3));
EXPECT_EQ(NULL, model.GetLabelFontListAt(4));
EXPECT_EQ(NULL, model.GetLabelFontListAt(5));
EXPECT_EQ(NULL, model.GetLabelFontListAt(6));
EXPECT_TRUE(model.GetLabelFontListAt(7) != NULL);
EXPECT_EQ(NULL, model.GetLabelFontListAt(8));
EXPECT_EQ(NULL, model.GetLabelFontListAt(9));
EXPECT_EQ(NULL, model.GetLabelFontListAt(10));
EXPECT_EQ(NULL, model.GetLabelFontListAt(11));
EXPECT_EQ(NULL, model.GetLabelFontListAt(12));
std::string url;
base::string16 title;
EXPECT_FALSE(model.GetURLAndTitleForItemAtIndex(0, &url, &title));
EXPECT_FALSE(model.GetURLAndTitleForItemAtIndex(1, &url, &title));
EXPECT_FALSE(model.GetURLAndTitleForItemAtIndex(2, &url, &title));
EXPECT_TRUE(model.GetURLAndTitleForItemAtIndex(3, &url, &title));
EXPECT_TRUE(model.GetURLAndTitleForItemAtIndex(4, &url, &title));
EXPECT_TRUE(model.GetURLAndTitleForItemAtIndex(5, &url, &title));
EXPECT_FALSE(model.GetURLAndTitleForItemAtIndex(6, &url, &title));
EXPECT_FALSE(model.GetURLAndTitleForItemAtIndex(7, &url, &title));
EXPECT_TRUE(model.GetURLAndTitleForItemAtIndex(8, &url, &title));
EXPECT_TRUE(model.GetURLAndTitleForItemAtIndex(9, &url, &title));
EXPECT_TRUE(model.GetURLAndTitleForItemAtIndex(10, &url, &title));
EXPECT_FALSE(model.GetURLAndTitleForItemAtIndex(11, &url, &title));
EXPECT_FALSE(model.GetURLAndTitleForItemAtIndex(12, &url, &title));
}
TEST_F(RecentTabsSubMenuModelTest, MaxSessionsAndRecency) {
RecentTabsBuilderTestHelper recent_tabs_builder;
for (int s = 0; s < 4; ++s) {
recent_tabs_builder.AddSession();
recent_tabs_builder.AddWindow(s);
recent_tabs_builder.AddTab(s, 0);
}
RegisterRecentTabs(&recent_tabs_builder);
TestRecentTabsSubMenuModel model(NULL, browser(), GetOpenTabsDelegate());
int num_items = model.GetItemCount();
EXPECT_EQ(12, num_items);
std::vector<base::string16> tab_titles =
recent_tabs_builder.GetTabTitlesSortedByRecency();
EXPECT_EQ(tab_titles[0], model.GetLabelAt(3));
EXPECT_EQ(tab_titles[1], model.GetLabelAt(6));
EXPECT_EQ(tab_titles[2], model.GetLabelAt(9));
}
TEST_F(RecentTabsSubMenuModelTest, MaxTabsPerSessionAndRecency) {
RecentTabsBuilderTestHelper recent_tabs_builder;
recent_tabs_builder.AddSession();
for (int w = 0; w < 2; ++w) {
recent_tabs_builder.AddWindow(0);
for (int t = 0; t < 5; ++t)
recent_tabs_builder.AddTab(0, w);
}
RegisterRecentTabs(&recent_tabs_builder);
TestRecentTabsSubMenuModel model(NULL, browser(), GetOpenTabsDelegate());
int num_items = model.GetItemCount();
EXPECT_EQ(9, num_items);
std::vector<base::string16> tab_titles =
recent_tabs_builder.GetTabTitlesSortedByRecency();
for (int i = 0; i < 4; ++i)
EXPECT_EQ(tab_titles[i], model.GetLabelAt(i + 3));
}
TEST_F(RecentTabsSubMenuModelTest, MaxWidth) {
RecentTabsBuilderTestHelper recent_tabs_builder;
recent_tabs_builder.AddSession();
recent_tabs_builder.AddWindow(0);
recent_tabs_builder.AddTab(0, 0);
RegisterRecentTabs(&recent_tabs_builder);
TestRecentTabsSubMenuModel model(NULL, browser(), GetOpenTabsDelegate());
EXPECT_EQ(6, model.GetItemCount());
EXPECT_EQ(-1, model.GetMaxWidthForItemAtIndex(0));
EXPECT_NE(-1, model.GetMaxWidthForItemAtIndex(1));
EXPECT_NE(-1, model.GetMaxWidthForItemAtIndex(2));
EXPECT_NE(-1, model.GetMaxWidthForItemAtIndex(3));
}
TEST_F(RecentTabsSubMenuModelTest, MaxWidthNoDevices) {
TestRecentTabsSubMenuModel model(NULL, browser(), NULL);
EXPECT_EQ(3, model.GetItemCount());
EXPECT_EQ(-1, model.GetMaxWidthForItemAtIndex(0));
EXPECT_NE(-1, model.GetMaxWidthForItemAtIndex(1));
EXPECT_EQ(-1, model.GetMaxWidthForItemAtIndex(2));
}