This source file includes following definitions.
- GetPrimaryDisplayArea
- GetPrimaryWorkArea
- GetDisplayAreaMatching
- GetWorkAreaMatching
- IsAutoHidingDesktopBarEnabled
- GetDesktopBarThickness
- GetDesktopBarVisibility
- IsFullScreen
- EnableAutoHidingDesktopBar
- SetPrimaryDisplay
- SetSecondaryDisplay
- SetDesktopBarVisibility
- SetDesktopBarThickness
- EnableFullScreenMode
- mock_display_settings_enabled_
- SkipTestIfIceWM
- SkipTestIfCompizWM
- SetUpCommandLine
- SetUpOnMainThread
- WaitForPanelActiveState
- WaitForWindowSizeAvailable
- WaitForBoundsAnimationFinished
- profile
- CreatePanelWithParams
- CreatePanelWithBounds
- CreatePanel
- CreateDockedPanel
- CreateDetachedPanel
- CreateStackedPanel
- CreateInactivePanel
- CreateInactiveDockedPanel
- CreateInactiveDetachedPanel
- ActivatePanel
- DeactivatePanel
- CreateNativePanelTesting
- CreateExtension
- CloseWindowAndWait
- MoveMouseAndWaitForExpansionStateChange
- MoveMouse
- MakePanelName
- WmSupportWindowActivation
#include "chrome/browser/ui/panels/base_panel_browser_test.h"
#include "base/bind.h"
#include "base/command_line.h"
#include "base/memory/weak_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/path_service.h"
#include "base/strings/string_number_conversions.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/panels/detached_panel_collection.h"
#include "chrome/browser/ui/panels/native_panel.h"
#include "chrome/browser/ui/panels/panel_collection.h"
#include "chrome/browser/ui/panels/panel_mouse_watcher.h"
#include "chrome/browser/ui/panels/stacked_panel_collection.h"
#include "chrome/browser/ui/panels/test_panel_active_state_observer.h"
#include "chrome/browser/ui/panels/test_panel_mouse_watcher.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/test/base/interactive_test_utils.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/browser/notification_service.h"
#include "content/public/common/url_constants.h"
#include "content/public/test/web_contents_tester.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/common/manifest_constants.h"
#include "sync/api/string_ordinal.h"
#if defined(OS_LINUX)
#include "chrome/browser/ui/browser_window.h"
#include "ui/base/x/x11_util.h"
#endif
#if defined(OS_LINUX) && !defined(USE_AURA)
#include "ui/base/x/active_window_watcher_x.h"
#endif
#if defined(OS_MACOSX)
#include "base/mac/scoped_nsautorelease_pool.h"
#include "chrome/browser/ui/cocoa/run_loop_testing.h"
#endif
using content::WebContentsTester;
using extensions::Extension;
namespace {
const gfx::Rect kTestingPrimaryDisplayArea = gfx::Rect(0, 0, 800, 600);
const gfx::Rect kTestingPrimaryWorkArea = gfx::Rect(0, 0, 800, 580);
struct MockDesktopBar {
bool auto_hiding_enabled;
DisplaySettingsProvider::DesktopBarVisibility visibility;
int thickness;
};
class MockDisplaySettingsProviderImpl :
public BasePanelBrowserTest::MockDisplaySettingsProvider {
public:
explicit MockDisplaySettingsProviderImpl();
virtual ~MockDisplaySettingsProviderImpl() { }
virtual gfx::Rect GetPrimaryDisplayArea() const OVERRIDE;
virtual gfx::Rect GetPrimaryWorkArea() const OVERRIDE;
virtual gfx::Rect GetDisplayAreaMatching(
const gfx::Rect& bounds) const OVERRIDE;
virtual gfx::Rect GetWorkAreaMatching(
const gfx::Rect& bounds) const OVERRIDE;
virtual bool IsAutoHidingDesktopBarEnabled(
DesktopBarAlignment alignment) OVERRIDE;
virtual int GetDesktopBarThickness(
DesktopBarAlignment alignment) const OVERRIDE;
virtual DesktopBarVisibility GetDesktopBarVisibility(
DesktopBarAlignment alignment) const OVERRIDE;
virtual bool IsFullScreen() OVERRIDE;
virtual void SetPrimaryDisplay(
const gfx::Rect& display_area, const gfx::Rect& work_area) OVERRIDE;
virtual void SetSecondaryDisplay(
const gfx::Rect& display_area, const gfx::Rect& work_area) OVERRIDE;
virtual void EnableAutoHidingDesktopBar(DesktopBarAlignment alignment,
bool enabled,
int thickness) OVERRIDE;
virtual void SetDesktopBarVisibility(
DesktopBarAlignment alignment, DesktopBarVisibility visibility) OVERRIDE;
virtual void SetDesktopBarThickness(DesktopBarAlignment alignment,
int thickness) OVERRIDE;
virtual void EnableFullScreenMode(bool enabled) OVERRIDE;
private:
gfx::Rect primary_display_area_;
gfx::Rect primary_work_area_;
gfx::Rect secondary_display_area_;
gfx::Rect secondary_work_area_;
MockDesktopBar mock_desktop_bars[3];
bool full_screen_enabled_;
DISALLOW_COPY_AND_ASSIGN(MockDisplaySettingsProviderImpl);
};
MockDisplaySettingsProviderImpl::MockDisplaySettingsProviderImpl()
: full_screen_enabled_(false) {
memset(mock_desktop_bars, 0, sizeof(mock_desktop_bars));
}
gfx::Rect MockDisplaySettingsProviderImpl::GetPrimaryDisplayArea() const {
return primary_display_area_;
}
gfx::Rect MockDisplaySettingsProviderImpl::GetPrimaryWorkArea() const {
return primary_work_area_;
}
gfx::Rect MockDisplaySettingsProviderImpl::GetDisplayAreaMatching(
const gfx::Rect& bounds) const {
if (secondary_display_area_.IsEmpty())
return primary_display_area_;
gfx::Rect primary_intersection =
gfx::IntersectRects(bounds, primary_display_area_);
int primary_intersection_size =
primary_intersection.width() * primary_intersection.height();
gfx::Rect secondary_intersection =
gfx::IntersectRects(bounds, secondary_display_area_);
int secondary_intersection_size =
secondary_intersection.width() * secondary_intersection.height();
return primary_intersection_size >= secondary_intersection_size ?
primary_display_area_ : secondary_display_area_;
}
gfx::Rect MockDisplaySettingsProviderImpl::GetWorkAreaMatching(
const gfx::Rect& bounds) const {
if (secondary_work_area_.IsEmpty())
return primary_work_area_;
gfx::Rect primary_intersection =
gfx::IntersectRects(bounds, primary_work_area_);
int primary_intersection_size =
primary_intersection.width() * primary_intersection.height();
gfx::Rect secondary_intersection =
gfx::IntersectRects(bounds, secondary_work_area_);
int secondary_intersection_size =
secondary_intersection.width() * secondary_intersection.height();
return primary_intersection_size >= secondary_intersection_size ?
primary_work_area_ : secondary_work_area_;
}
bool MockDisplaySettingsProviderImpl::IsAutoHidingDesktopBarEnabled(
DesktopBarAlignment alignment) {
return mock_desktop_bars[static_cast<int>(alignment)].auto_hiding_enabled;
}
int MockDisplaySettingsProviderImpl::GetDesktopBarThickness(
DesktopBarAlignment alignment) const {
return mock_desktop_bars[static_cast<int>(alignment)].thickness;
}
DisplaySettingsProvider::DesktopBarVisibility
MockDisplaySettingsProviderImpl::GetDesktopBarVisibility(
DesktopBarAlignment alignment) const {
return mock_desktop_bars[static_cast<int>(alignment)].visibility;
}
bool MockDisplaySettingsProviderImpl::IsFullScreen() {
return full_screen_enabled_;
}
void MockDisplaySettingsProviderImpl::EnableAutoHidingDesktopBar(
DesktopBarAlignment alignment, bool enabled, int thickness) {
MockDesktopBar* bar = &(mock_desktop_bars[static_cast<int>(alignment)]);
bar->auto_hiding_enabled = enabled;
bar->thickness = thickness;
}
void MockDisplaySettingsProviderImpl::SetPrimaryDisplay(
const gfx::Rect& display_area, const gfx::Rect& work_area) {
DCHECK(display_area.Contains(work_area));
primary_display_area_ = display_area;
primary_work_area_ = work_area;
OnDisplaySettingsChanged();
}
void MockDisplaySettingsProviderImpl::SetSecondaryDisplay(
const gfx::Rect& display_area, const gfx::Rect& work_area) {
DCHECK(display_area.Contains(work_area));
secondary_display_area_ = display_area;
secondary_work_area_ = work_area;
OnDisplaySettingsChanged();
}
void MockDisplaySettingsProviderImpl::SetDesktopBarVisibility(
DesktopBarAlignment alignment, DesktopBarVisibility visibility) {
MockDesktopBar* bar = &(mock_desktop_bars[static_cast<int>(alignment)]);
if (!bar->auto_hiding_enabled)
return;
if (visibility == bar->visibility)
return;
bar->visibility = visibility;
FOR_EACH_OBSERVER(
DesktopBarObserver,
desktop_bar_observers(),
OnAutoHidingDesktopBarVisibilityChanged(alignment, visibility));
}
void MockDisplaySettingsProviderImpl::SetDesktopBarThickness(
DesktopBarAlignment alignment, int thickness) {
MockDesktopBar* bar = &(mock_desktop_bars[static_cast<int>(alignment)]);
if (!bar->auto_hiding_enabled)
return;
if (thickness == bar->thickness)
return;
bar->thickness = thickness;
FOR_EACH_OBSERVER(
DesktopBarObserver,
desktop_bar_observers(),
OnAutoHidingDesktopBarThicknessChanged(alignment, thickness));
}
void MockDisplaySettingsProviderImpl::EnableFullScreenMode(bool enabled) {
full_screen_enabled_ = enabled;
CheckFullScreenMode(PERFORM_FULLSCREEN_CHECK);
}
}
const base::FilePath::CharType* BasePanelBrowserTest::kTestDir =
FILE_PATH_LITERAL("panels");
BasePanelBrowserTest::BasePanelBrowserTest()
: InProcessBrowserTest(),
mock_display_settings_enabled_(true) {
}
BasePanelBrowserTest::~BasePanelBrowserTest() {
}
bool BasePanelBrowserTest::SkipTestIfIceWM() {
#if defined(OS_LINUX)
return ui::GuessWindowManager() == ui::WM_ICE_WM;
#else
return false;
#endif
}
bool BasePanelBrowserTest::SkipTestIfCompizWM() {
#if defined(OS_LINUX)
return ui::GuessWindowManager() == ui::WM_COMPIZ;
#else
return false;
#endif
}
void BasePanelBrowserTest::SetUpCommandLine(CommandLine* command_line) {
command_line->AppendSwitch(switches::kEnablePanels);
}
void BasePanelBrowserTest::SetUpOnMainThread() {
InProcessBrowserTest::SetUpOnMainThread();
if (mock_display_settings_enabled_) {
mock_display_settings_provider_ = new MockDisplaySettingsProviderImpl();
mock_display_settings_provider_->SetPrimaryDisplay(
kTestingPrimaryDisplayArea, kTestingPrimaryWorkArea);
PanelManager::SetDisplaySettingsProviderForTesting(
mock_display_settings_provider_);
}
PanelManager* panel_manager = PanelManager::GetInstance();
panel_manager->enable_auto_sizing(false);
PanelManager::shorten_time_intervals_for_testing();
PanelMouseWatcher* mouse_watcher = new TestPanelMouseWatcher();
panel_manager->SetMouseWatcherForTesting(mouse_watcher);
ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
}
void BasePanelBrowserTest::WaitForPanelActiveState(
Panel* panel, ActiveState expected_state) {
DCHECK(expected_state == SHOW_AS_ACTIVE ||
expected_state == SHOW_AS_INACTIVE);
#if defined(OS_MACOSX)
scoped_ptr<NativePanelTesting> panel_testing(
CreateNativePanelTesting(panel));
ASSERT_TRUE(panel_testing->EnsureApplicationRunOnForeground()) <<
"Failed to bring application to foreground. Bail out.";
#endif
PanelActiveStateObserver signal(panel, expected_state == SHOW_AS_ACTIVE);
signal.Wait();
}
void BasePanelBrowserTest::WaitForWindowSizeAvailable(Panel* panel) {
scoped_ptr<NativePanelTesting> panel_testing(
CreateNativePanelTesting(panel));
content::WindowedNotificationObserver signal(
chrome::NOTIFICATION_PANEL_WINDOW_SIZE_KNOWN,
content::Source<Panel>(panel));
if (panel_testing->IsWindowSizeKnown())
return;
signal.Wait();
EXPECT_TRUE(panel_testing->IsWindowSizeKnown());
}
void BasePanelBrowserTest::WaitForBoundsAnimationFinished(Panel* panel) {
scoped_ptr<NativePanelTesting> panel_testing(
CreateNativePanelTesting(panel));
while (panel_testing->IsAnimatingBounds()) {
content::WindowedNotificationObserver signal(
chrome::NOTIFICATION_PANEL_BOUNDS_ANIMATIONS_FINISHED,
content::Source<Panel>(panel));
if (!panel_testing->IsAnimatingBounds())
return;
signal.Wait();
}
}
BasePanelBrowserTest::CreatePanelParams::CreatePanelParams(
const std::string& name,
const gfx::Rect& bounds,
ActiveState show_flag)
: name(name),
bounds(bounds),
show_flag(show_flag),
wait_for_fully_created(true),
expected_active_state(show_flag),
create_mode(PanelManager::CREATE_AS_DOCKED),
profile(NULL) {
}
Panel* BasePanelBrowserTest::CreatePanelWithParams(
const CreatePanelParams& params) {
#if defined(OS_MACOSX)
base::mac::ScopedNSAutoreleasePool autorelease_pool;
#endif
content::WindowedNotificationObserver observer(
content::NOTIFICATION_LOAD_STOP,
content::NotificationService::AllSources());
PanelManager* manager = PanelManager::GetInstance();
Panel* panel = manager->CreatePanel(
params.name,
params.profile ? params.profile : browser()->profile(),
params.url,
params.bounds,
params.create_mode);
if (!params.url.is_empty())
observer.Wait();
if (!manager->auto_sizing_enabled() ||
params.bounds.width() || params.bounds.height()) {
EXPECT_FALSE(panel->auto_resizable());
} else {
EXPECT_TRUE(panel->auto_resizable());
}
if (params.show_flag == SHOW_AS_ACTIVE) {
panel->Show();
} else {
panel->ShowInactive();
}
if (params.wait_for_fully_created) {
base::MessageLoopForUI::current()->RunUntilIdle();
#if defined(OS_LINUX)
if (params.expected_active_state == SHOW_AS_INACTIVE &&
ui::GuessWindowManager() == ui::WM_ICE_WM) {
WaitForPanelActiveState(panel, SHOW_AS_ACTIVE);
browser()->window()->Activate();
}
#endif
WaitForPanelActiveState(panel, params.expected_active_state);
WaitForWindowSizeAvailable(panel);
WaitForBoundsAnimationFinished(panel);
}
return panel;
}
Panel* BasePanelBrowserTest::CreatePanelWithBounds(
const std::string& panel_name, const gfx::Rect& bounds) {
CreatePanelParams params(panel_name, bounds, SHOW_AS_ACTIVE);
return CreatePanelWithParams(params);
}
Panel* BasePanelBrowserTest::CreatePanel(const std::string& panel_name) {
CreatePanelParams params(panel_name, gfx::Rect(), SHOW_AS_ACTIVE);
return CreatePanelWithParams(params);
}
Panel* BasePanelBrowserTest::CreateDockedPanel(const std::string& name,
const gfx::Rect& bounds) {
Panel* panel = CreatePanelWithBounds(name, bounds);
EXPECT_EQ(PanelCollection::DOCKED, panel->collection()->type());
return panel;
}
Panel* BasePanelBrowserTest::CreateDetachedPanel(const std::string& name,
const gfx::Rect& bounds) {
Panel* panel = CreatePanelWithBounds(name, bounds);
PanelManager* panel_manager = panel->manager();
panel_manager->MovePanelToCollection(panel,
panel_manager->detached_collection(),
PanelCollection::DEFAULT_POSITION);
EXPECT_EQ(PanelCollection::DETACHED, panel->collection()->type());
panel->SetPanelBounds(bounds);
WaitForBoundsAnimationFinished(panel);
return panel;
}
Panel* BasePanelBrowserTest::CreateStackedPanel(const std::string& name,
const gfx::Rect& bounds,
StackedPanelCollection* stack) {
Panel* panel = CreateDetachedPanel(name, bounds);
panel->manager()->MovePanelToCollection(
panel,
stack,
static_cast<PanelCollection::PositioningMask>(
PanelCollection::DEFAULT_POSITION |
PanelCollection::COLLAPSE_TO_FIT));
EXPECT_EQ(PanelCollection::STACKED, panel->collection()->type());
WaitForBoundsAnimationFinished(panel);
return panel;
}
Panel* BasePanelBrowserTest::CreateInactivePanel(const std::string& name) {
Panel* panel = CreatePanel(name);
DeactivatePanel(panel);
WaitForPanelActiveState(panel, SHOW_AS_INACTIVE);
return panel;
}
Panel* BasePanelBrowserTest::CreateInactiveDockedPanel(
const std::string& name, const gfx::Rect& bounds) {
Panel* panel = CreateDockedPanel(name, bounds);
DeactivatePanel(panel);
WaitForPanelActiveState(panel, SHOW_AS_INACTIVE);
return panel;
}
Panel* BasePanelBrowserTest::CreateInactiveDetachedPanel(
const std::string& name, const gfx::Rect& bounds) {
Panel* panel = CreateDetachedPanel(name, bounds);
DeactivatePanel(panel);
WaitForPanelActiveState(panel, SHOW_AS_INACTIVE);
return panel;
}
void BasePanelBrowserTest::ActivatePanel(Panel* panel) {
#if defined(OS_LINUX)
std::vector<Panel*> panels = PanelManager::GetInstance()->panels();
for (std::vector<Panel*>::const_iterator iter = panels.begin();
iter != panels.end(); ++iter) {
Panel* current_panel = *iter;
if (panel != current_panel)
current_panel->Deactivate();
}
#endif
panel->Activate();
}
void BasePanelBrowserTest::DeactivatePanel(Panel* panel) {
#if defined(OS_LINUX)
panel->Deactivate();
#else
browser()->window()->Activate();
#endif
}
NativePanelTesting* BasePanelBrowserTest::CreateNativePanelTesting(
Panel* panel) {
return panel->native_panel()->CreateNativePanelTesting();
}
scoped_refptr<Extension> BasePanelBrowserTest::CreateExtension(
const base::FilePath::StringType& path,
extensions::Manifest::Location location,
const base::DictionaryValue& extra_value) {
extensions::ExtensionPrefs* extension_prefs =
extensions::ExtensionPrefs::Get(browser()->profile());
base::FilePath full_path = extension_prefs->install_directory().Append(path);
scoped_ptr<base::DictionaryValue> input_value(extra_value.DeepCopy());
input_value->SetString(extensions::manifest_keys::kVersion, "1.0.0.0");
input_value->SetString(extensions::manifest_keys::kName, "Sample Extension");
std::string error;
scoped_refptr<Extension> extension = Extension::Create(
full_path, location, *input_value, Extension::NO_FLAGS, &error);
EXPECT_TRUE(extension.get());
EXPECT_STREQ("", error.c_str());
browser()->profile()->GetExtensionService()->
OnExtensionInstalled(extension.get(),
syncer::StringOrdinal(),
false ,
extensions::NOT_BLACKLISTED,
false );
return extension;
}
void BasePanelBrowserTest::CloseWindowAndWait(Panel* panel) {
PanelManager* manager = PanelManager::GetInstance();
int panel_count = manager->num_panels();
content::WindowedNotificationObserver signal(
chrome::NOTIFICATION_PANEL_CLOSED,
content::Source<Panel>(panel));
panel->Close();
signal.Wait();
EXPECT_EQ(panel_count - 1, manager->num_panels());
#if defined(OS_MACOSX)
AutoreleasePool()->Recycle();
chrome::testing::NSRunLoopRunAllPending();
#endif
}
void BasePanelBrowserTest::MoveMouseAndWaitForExpansionStateChange(
Panel* panel,
const gfx::Point& position) {
content::WindowedNotificationObserver signal(
chrome::NOTIFICATION_PANEL_CHANGED_EXPANSION_STATE,
content::Source<Panel>(panel));
MoveMouse(position);
signal.Wait();
}
void BasePanelBrowserTest::MoveMouse(const gfx::Point& position) {
PanelManager::GetInstance()->mouse_watcher()->NotifyMouseMovement(position);
}
std::string BasePanelBrowserTest::MakePanelName(int index) {
std::string panel_name("Panel");
return panel_name + base::IntToString(index);
}
bool BasePanelBrowserTest::WmSupportWindowActivation() {
#if defined(OS_LINUX) && !defined(USE_AURA)
return ui::ActiveWindowWatcherX::WMSupportsActivation();
#else
return true;
#endif
}