This source file includes following definitions.
- RedirectHostsToTestData
- UndoRedirectHostsToTestData
- FailedJobFactory
- MakeRequestFailOnIO
- UndoMakeRequestFailOnIO
- CheckCanOpenURL
- CheckURLIsBlocked
- DownloadAndVerifyFile
- CountScreenshots
- IsWebGLEnabled
- IsJavascriptEnabled
- CopyPluginListAndQuit
- CopyValueAndQuit
- GetPluginList
- GetFlashPlugin
- SetPluginEnabled
- CountPluginsOnIOThread
- CountPlugins
- FlushBlacklistPolicy
- ContainsVisibleElement
- output_mute_changed_count
- OnOutputMuteChanged
- message_loop_runner_
- Wait
- WebContentsDestroyed
- DidStopLoading
- window_
- OnAppWindowAdded
- OnAppWindowIconChanged
- OnAppWindowRemoved
- WaitForAppWindow
- SetUp
- SetUpInProcessBrowserTestFixture
- SetUpOnMainThread
- ServeContentTestData
- SetScreenshotPolicy
- OnScreenshotCompleted
- TestScreenshotFile
- extension_service
- InstallExtension
- LoadUnpackedExtension
- UninstallExtension
- UpdateProviderPolicy
- PerformClick
- SetUpInProcessBrowserTestFixture
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IsNonSwitchArgument
- SetUpCommandLine
- SetUpInProcessBrowserTestFixture
- HomepageIsNotNTP
- HomepageIsNTP
- ListOfURLs
- NTP
- Last
- IN_PROC_BROWSER_TEST_P
- IN_PROC_BROWSER_TEST_P
- SetUpInProcessBrowserTestFixture
- IN_PROC_BROWSER_TEST_F
- ConfigurePolicyMap
- Accept
- FinishAudioTest
- FinishVideoTest
- IN_PROC_BROWSER_TEST_P
- IN_PROC_BROWSER_TEST_P
- IN_PROC_BROWSER_TEST_P
- IN_PROC_BROWSER_TEST_P
- SetUpInProcessBrowserTestFixture
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
#include <algorithm>
#include <string>
#include <vector>
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/callback.h"
#include "base/command_line.h"
#include "base/file_util.h"
#include "base/files/file_enumerator.h"
#include "base/files/file_path.h"
#include "base/files/scoped_temp_dir.h"
#include "base/memory/ref_counted.h"
#include "base/path_service.h"
#include "base/prefs/pref_service.h"
#include "base/run_loop.h"
#include "base/strings/string16.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/test_file_util.h"
#include "base/time/time.h"
#include "base/values.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/autocomplete/autocomplete_controller.h"
#include "chrome/browser/background/background_contents_service.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/content_settings/tab_specific_content_settings.h"
#include "chrome/browser/devtools/devtools_window.h"
#include "chrome/browser/download/download_prefs.h"
#include "chrome/browser/extensions/api/messaging/native_message_process_host.h"
#include "chrome/browser/extensions/crx_installer.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/unpacked_installer.h"
#include "chrome/browser/extensions/updater/extension_cache_fake.h"
#include "chrome/browser/extensions/updater/extension_updater.h"
#include "chrome/browser/infobars/infobar.h"
#include "chrome/browser/infobars/infobar_manager.h"
#include "chrome/browser/infobars/infobar_service.h"
#include "chrome/browser/media/media_capture_devices_dispatcher.h"
#include "chrome/browser/media/media_stream_devices_controller.h"
#include "chrome/browser/metrics/variations/variations_service.h"
#include "chrome/browser/net/url_request_mock_util.h"
#include "chrome/browser/plugins/plugin_prefs.h"
#include "chrome/browser/policy/cloud/test_request_interceptor.h"
#include "chrome/browser/policy/profile_policy_connector.h"
#include "chrome/browser/policy/profile_policy_connector_factory.h"
#include "chrome/browser/prefs/session_startup_pref.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/search/instant_service.h"
#include "chrome/browser/search/instant_service_factory.h"
#include "chrome/browser/search/search.h"
#include "chrome/browser/search_engines/template_url.h"
#include "chrome/browser/search_engines/template_url_service.h"
#include "chrome/browser/search_engines/template_url_service_factory.h"
#include "chrome/browser/translate/translate_infobar_delegate.h"
#include "chrome/browser/translate/translate_service.h"
#include "chrome/browser/translate/translate_tab_helper.h"
#include "chrome/browser/ui/bookmarks/bookmark_bar.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_tabstrip.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/host_desktop.h"
#include "chrome/browser/ui/omnibox/location_bar.h"
#include "chrome/browser/ui/omnibox/omnibox_edit_model.h"
#include "chrome/browser/ui/omnibox/omnibox_view.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/content_settings.h"
#include "chrome/common/content_settings_pattern.h"
#include "chrome/common/extensions/extension_constants.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/test_switches.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/policy/core/browser/browser_policy_connector.h"
#include "components/policy/core/common/external_data_fetcher.h"
#include "components/policy/core/common/mock_configuration_policy_provider.h"
#include "components/policy/core/common/policy_map.h"
#include "components/policy/core/common/policy_pref_names.h"
#include "components/policy/core/common/policy_service.h"
#include "components/policy/core/common/policy_service_impl.h"
#include "content/public/browser/browser_child_process_host_iterator.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/child_process_data.h"
#include "content/public/browser/download_item.h"
#include "content/public/browser/download_manager.h"
#include "content/public/browser/gpu_data_manager.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_source.h"
#include "content/public/browser/notification_types.h"
#include "content/public/browser/plugin_service.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/content_constants.h"
#include "content/public/common/content_paths.h"
#include "content/public/common/page_transition_types.h"
#include "content/public/common/process_type.h"
#include "content/public/common/result_codes.h"
#include "content/public/common/url_constants.h"
#include "content/public/common/webplugininfo.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/download_test_observer.h"
#include "content/public/test/mock_notification_observer.h"
#include "content/public/test/test_navigation_observer.h"
#include "content/public/test/test_utils.h"
#include "content/test/net/url_request_failed_job.h"
#include "content/test/net/url_request_mock_http_job.h"
#include "extensions/browser/extension_host.h"
#include "extensions/browser/extension_system.h"
#include "extensions/browser/process_manager.h"
#include "extensions/common/constants.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_set.h"
#include "grit/generated_resources.h"
#include "net/base/net_errors.h"
#include "net/base/net_util.h"
#include "net/base/url_util.h"
#include "net/http/http_stream_factory.h"
#include "net/url_request/url_request.h"
#include "net/url_request/url_request_filter.h"
#include "policy/policy_constants.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/WebKit/public/web/WebInputEvent.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#include "url/gurl.h"
#if defined(OS_CHROMEOS)
#include "ash/accelerators/accelerator_controller.h"
#include "ash/accelerators/accelerator_table.h"
#include "ash/magnifier/magnifier_constants.h"
#include "ash/shell.h"
#include "chrome/browser/chromeos/accessibility/accessibility_manager.h"
#include "chrome/browser/chromeos/accessibility/magnification_manager.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/ui/ash/screenshot_taker.h"
#include "chromeos/audio/cras_audio_handler.h"
#endif
#if !defined(OS_MACOSX)
#include "apps/app_window.h"
#include "apps/app_window_registry.h"
#include "apps/ui/native_app_window.h"
#include "base/basictypes.h"
#include "base/compiler_specific.h"
#include "chrome/browser/ui/extensions/application_launch.h"
#include "ui/base/window_open_disposition.h"
#endif
using content::BrowserThread;
using content::URLRequestMockHTTPJob;
using testing::Mock;
using testing::Return;
using testing::_;
namespace policy {
namespace {
#if defined(OS_CHROMEOS)
const int kOneHourInMs = 60 * 60 * 1000;
const int kThreeHoursInMs = 180 * 60 * 1000;
#endif
const char kURL[] = "http://example.com";
const char kCookieValue[] = "converted=true";
const char kCookieOptions[] = ";expires=Wed Jan 01 3000 00:00:00 GMT";
const base::FilePath::CharType kTestExtensionsDir[] =
FILE_PATH_LITERAL("extensions");
const base::FilePath::CharType kGoodCrxName[] = FILE_PATH_LITERAL("good.crx");
const base::FilePath::CharType kAdBlockCrxName[] =
FILE_PATH_LITERAL("adblock.crx");
const base::FilePath::CharType kHostedAppCrxName[] =
FILE_PATH_LITERAL("hosted_app.crx");
const char kGoodCrxId[] = "ldnnhddmnhbkjipkidpdiheffobcpfmf";
const char kAdBlockCrxId[] = "dojnnbeimaimaojcialkkgajdnefpgcn";
const char kHostedAppCrxId[] = "kbmnembihfiondgfjekmnmcbddelicoi";
const base::FilePath::CharType kGood2CrxManifestName[] =
FILE_PATH_LITERAL("good2_update_manifest.xml");
const base::FilePath::CharType kGoodV1CrxManifestName[] =
FILE_PATH_LITERAL("good_v1_update_manifest.xml");
const base::FilePath::CharType kGoodUnpackedExt[] =
FILE_PATH_LITERAL("good_unpacked");
const base::FilePath::CharType kAppUnpackedExt[] =
FILE_PATH_LITERAL("app");
#if !defined(OS_MACOSX)
const base::FilePath::CharType kUnpackedFullscreenAppName[] =
FILE_PATH_LITERAL("fullscreen_app");
#endif
void RedirectHostsToTestData(const char* const urls[], size_t size) {
net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance();
base::FilePath base_path;
PathService::Get(chrome::DIR_TEST_DATA, &base_path);
for (size_t i = 0; i < size; ++i) {
const GURL url(urls[i]);
EXPECT_TRUE(url.is_valid());
filter->AddUrlProtocolHandler(url,
URLRequestMockHTTPJob::CreateProtocolHandler(base_path));
}
}
void UndoRedirectHostsToTestData(const char* const urls[], size_t size) {
net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance();
for (size_t i = 0; i < size; ++i) {
const GURL url(urls[i]);
EXPECT_TRUE(url.is_valid());
filter->RemoveUrlHandler(url);
}
}
net::URLRequestJob* FailedJobFactory(
net::URLRequest* request,
net::NetworkDelegate* network_delegate,
const std::string& scheme) {
return new content::URLRequestFailedJob(
request, network_delegate, net::ERR_CONNECTION_RESET);
}
class MakeRequestFail {
public:
explicit MakeRequestFail(const std::string& host) : host_(host) {
BrowserThread::PostTaskAndReply(
BrowserThread::IO, FROM_HERE,
base::Bind(MakeRequestFailOnIO, host_),
base::MessageLoop::QuitClosure());
content::RunMessageLoop();
}
~MakeRequestFail() {
BrowserThread::PostTaskAndReply(
BrowserThread::IO, FROM_HERE,
base::Bind(UndoMakeRequestFailOnIO, host_),
base::MessageLoop::QuitClosure());
content::RunMessageLoop();
}
private:
static void MakeRequestFailOnIO(const std::string& host) {
net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance();
filter->AddHostnameHandler("http", host, &FailedJobFactory);
filter->AddHostnameHandler("https", host, &FailedJobFactory);
}
static void UndoMakeRequestFailOnIO(const std::string& host) {
net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance();
filter->RemoveHostnameHandler("http", host);
filter->RemoveHostnameHandler("https", host);
}
const std::string host_;
};
void CheckCanOpenURL(Browser* browser, const char* spec) {
GURL url(spec);
ui_test_utils::NavigateToURL(browser, url);
content::WebContents* contents =
browser->tab_strip_model()->GetActiveWebContents();
EXPECT_EQ(url, contents->GetURL());
base::string16 title = base::UTF8ToUTF16(url.spec() + " was blocked");
EXPECT_NE(title, contents->GetTitle());
}
void CheckURLIsBlocked(Browser* browser, const char* spec) {
GURL url(spec);
ui_test_utils::NavigateToURL(browser, url);
content::WebContents* contents =
browser->tab_strip_model()->GetActiveWebContents();
EXPECT_EQ(url, contents->GetURL());
base::string16 title = base::UTF8ToUTF16(url.spec() + " was blocked");
EXPECT_EQ(title, contents->GetTitle());
bool result = false;
EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
contents,
"var textContent = document.body.textContent;"
"var hasError = textContent.indexOf('ERR_BLOCKED_BY_ADMINISTRATOR') >= 0;"
"domAutomationController.send(hasError);",
&result));
EXPECT_TRUE(result);
}
void DownloadAndVerifyFile(
Browser* browser, const base::FilePath& dir, const base::FilePath& file) {
content::DownloadManager* download_manager =
content::BrowserContext::GetDownloadManager(browser->profile());
content::DownloadTestObserverTerminal observer(
download_manager, 1,
content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL);
GURL url(URLRequestMockHTTPJob::GetMockUrl(file));
base::FilePath downloaded = dir.Append(file);
EXPECT_FALSE(base::PathExists(downloaded));
ui_test_utils::NavigateToURLWithDisposition(
browser, url, CURRENT_TAB,
ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
observer.WaitForFinished();
EXPECT_EQ(
1u, observer.NumDownloadsSeenInState(content::DownloadItem::COMPLETE));
EXPECT_TRUE(base::PathExists(downloaded));
base::FileEnumerator enumerator(dir, false, base::FileEnumerator::FILES);
EXPECT_EQ(file, enumerator.Next().BaseName());
EXPECT_EQ(base::FilePath(), enumerator.Next());
}
#if defined(OS_CHROMEOS)
int CountScreenshots() {
DownloadPrefs* download_prefs = DownloadPrefs::FromBrowserContext(
ProfileManager::GetActiveUserProfile());
base::FileEnumerator enumerator(download_prefs->DownloadPath(),
false, base::FileEnumerator::FILES,
"Screenshot*");
int count = 0;
while (!enumerator.Next().empty())
count++;
return count;
}
#endif
bool IsWebGLEnabled(content::WebContents* contents) {
bool result = false;
EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
contents,
"var canvas = document.createElement('canvas');"
"var context = canvas.getContext('webgl');"
"domAutomationController.send(context != null);",
&result));
return result;
}
bool IsJavascriptEnabled(content::WebContents* contents) {
scoped_ptr<base::Value> value = content::ExecuteScriptAndGetValue(
contents->GetMainFrame(), "123");
int result = 0;
if (!value->GetAsInteger(&result))
EXPECT_EQ(base::Value::TYPE_NULL, value->GetType());
return result == 123;
}
void CopyPluginListAndQuit(std::vector<content::WebPluginInfo>* out,
const std::vector<content::WebPluginInfo>& in) {
*out = in;
base::MessageLoop::current()->QuitWhenIdle();
}
template<typename T>
void CopyValueAndQuit(T* out, T in) {
*out = in;
base::MessageLoop::current()->QuitWhenIdle();
}
void GetPluginList(std::vector<content::WebPluginInfo>* plugins) {
content::PluginService* service = content::PluginService::GetInstance();
service->GetPlugins(base::Bind(CopyPluginListAndQuit, plugins));
content::RunMessageLoop();
}
const content::WebPluginInfo* GetFlashPlugin(
const std::vector<content::WebPluginInfo>& plugins) {
const content::WebPluginInfo* flash = NULL;
for (size_t i = 0; i < plugins.size(); ++i) {
if (plugins[i].name == base::ASCIIToUTF16(content::kFlashPluginName)) {
flash = &plugins[i];
break;
}
}
#if defined(OFFICIAL_BUILD)
EXPECT_TRUE(flash);
#else
if (!flash)
LOG(INFO) << "Test skipped because the Flash plugin couldn't be found.";
#endif
return flash;
}
bool SetPluginEnabled(PluginPrefs* plugin_prefs,
const content::WebPluginInfo* plugin,
bool enabled) {
bool ok = false;
plugin_prefs->EnablePlugin(enabled, plugin->path,
base::Bind(CopyValueAndQuit<bool>, &ok));
content::RunMessageLoop();
return ok;
}
int CountPluginsOnIOThread() {
int count = 0;
for (content::BrowserChildProcessHostIterator iter; !iter.Done(); ++iter) {
if (iter.GetData().process_type == content::PROCESS_TYPE_PLUGIN ||
iter.GetData().process_type == content::PROCESS_TYPE_PPAPI_PLUGIN) {
count++;
}
}
return count;
}
int CountPlugins() {
int count = -1;
BrowserThread::PostTaskAndReplyWithResult(
BrowserThread::IO, FROM_HERE,
base::Bind(CountPluginsOnIOThread),
base::Bind(CopyValueAndQuit<int>, &count));
content::RunMessageLoop();
EXPECT_GE(count, 0);
return count;
}
void FlushBlacklistPolicy() {
content::RunAllPendingInMessageLoop(BrowserThread::IO);
content::RunAllPendingInMessageLoop(BrowserThread::FILE);
content::RunAllPendingInMessageLoop(BrowserThread::IO);
}
bool ContainsVisibleElement(content::WebContents* contents,
const std::string& id) {
bool result;
EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
contents,
"var elem = document.getElementById('" + id + "');"
"domAutomationController.send(!!elem && !elem.hidden);",
&result));
return result;
}
#if defined(OS_CHROMEOS)
class TestAudioObserver : public chromeos::CrasAudioHandler::AudioObserver {
public:
TestAudioObserver() : output_mute_changed_count_(0) {
}
int output_mute_changed_count() const {
return output_mute_changed_count_;
}
virtual ~TestAudioObserver() {}
protected:
virtual void OnOutputMuteChanged() OVERRIDE {
++output_mute_changed_count_;
}
private:
int output_mute_changed_count_;
DISALLOW_COPY_AND_ASSIGN(TestAudioObserver);
};
#endif
class WebContentsLoadedOrDestroyedWatcher
: public content::WebContentsObserver {
public:
explicit WebContentsLoadedOrDestroyedWatcher(
content::WebContents* web_contents);
virtual ~WebContentsLoadedOrDestroyedWatcher();
void Wait();
virtual void WebContentsDestroyed(
content::WebContents* web_contents) OVERRIDE;
virtual void DidStopLoading(
content::RenderViewHost* render_view_host) OVERRIDE;
private:
scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
DISALLOW_COPY_AND_ASSIGN(WebContentsLoadedOrDestroyedWatcher);
};
WebContentsLoadedOrDestroyedWatcher::WebContentsLoadedOrDestroyedWatcher(
content::WebContents* web_contents)
: content::WebContentsObserver(web_contents),
message_loop_runner_(new content::MessageLoopRunner) {
}
WebContentsLoadedOrDestroyedWatcher::~WebContentsLoadedOrDestroyedWatcher() {}
void WebContentsLoadedOrDestroyedWatcher::Wait() {
message_loop_runner_->Run();
}
void WebContentsLoadedOrDestroyedWatcher::WebContentsDestroyed(
content::WebContents* web_contents) {
message_loop_runner_->Quit();
}
void WebContentsLoadedOrDestroyedWatcher::DidStopLoading(
content::RenderViewHost* render_view_host) {
message_loop_runner_->Quit();
}
#if !defined(OS_MACOSX)
class TestAddAppWindowObserver : public apps::AppWindowRegistry::Observer {
public:
explicit TestAddAppWindowObserver(apps::AppWindowRegistry* registry);
virtual ~TestAddAppWindowObserver();
virtual void OnAppWindowAdded(apps::AppWindow* app_window) OVERRIDE;
virtual void OnAppWindowIconChanged(apps::AppWindow* app_window) OVERRIDE;
virtual void OnAppWindowRemoved(apps::AppWindow* app_window) OVERRIDE;
apps::AppWindow* WaitForAppWindow();
private:
apps::AppWindowRegistry* registry_;
apps::AppWindow* window_;
base::RunLoop run_loop_;
DISALLOW_COPY_AND_ASSIGN(TestAddAppWindowObserver);
};
TestAddAppWindowObserver::TestAddAppWindowObserver(
apps::AppWindowRegistry* registry)
: registry_(registry), window_(NULL) {
registry_->AddObserver(this);
}
TestAddAppWindowObserver::~TestAddAppWindowObserver() {
registry_->RemoveObserver(this);
}
void TestAddAppWindowObserver::OnAppWindowAdded(apps::AppWindow* app_window) {
window_ = app_window;
run_loop_.Quit();
}
void TestAddAppWindowObserver::OnAppWindowIconChanged(
apps::AppWindow* app_window) {}
void TestAddAppWindowObserver::OnAppWindowRemoved(apps::AppWindow* app_window) {
}
apps::AppWindow* TestAddAppWindowObserver::WaitForAppWindow() {
run_loop_.Run();
return window_;
}
#endif
}
class PolicyTest : public InProcessBrowserTest {
protected:
PolicyTest() {}
virtual ~PolicyTest() {}
virtual void SetUp() OVERRIDE {
test_extension_cache_.reset(new extensions::ExtensionCacheFake());
InProcessBrowserTest::SetUp();
}
virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
CommandLine::ForCurrentProcess()->AppendSwitch("noerrdialogs");
EXPECT_CALL(provider_, IsInitializationComplete(_))
.WillRepeatedly(Return(true));
BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_);
}
virtual void SetUpOnMainThread() OVERRIDE {
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
base::Bind(chrome_browser_net::SetUrlRequestMocksEnabled, true));
}
void ServeContentTestData() {
base::FilePath root_http;
PathService::Get(content::DIR_TEST_DATA, &root_http);
BrowserThread::PostTaskAndReply(
BrowserThread::IO, FROM_HERE,
base::Bind(URLRequestMockHTTPJob::AddUrlHandler, root_http),
base::MessageLoop::current()->QuitWhenIdleClosure());
content::RunMessageLoop();
}
void SetScreenshotPolicy(bool enabled) {
PolicyMap policies;
policies.Set(key::kDisableScreenshots,
POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
base::Value::CreateBooleanValue(!enabled), NULL);
UpdateProviderPolicy(policies);
}
#if defined(OS_CHROMEOS)
class QuitMessageLoopAfterScreenshot : public ScreenshotTakerObserver {
public:
virtual void OnScreenshotCompleted(
ScreenshotTakerObserver::Result screenshot_result,
const base::FilePath& screenshot_path) OVERRIDE {
BrowserThread::PostTaskAndReply(BrowserThread::IO,
FROM_HERE,
base::Bind(base::DoNothing),
base::MessageLoop::QuitClosure());
}
virtual ~QuitMessageLoopAfterScreenshot() {}
};
void TestScreenshotFile(bool enabled) {
scoped_ptr<ScreenshotTaker> screenshot_taker(new ScreenshotTaker);
screenshot_taker->AddObserver(&observer_);
ash::Shell::GetInstance()->accelerator_controller()->SetScreenshotDelegate(
screenshot_taker.PassAs<ash::ScreenshotDelegate>());
SetScreenshotPolicy(enabled);
ash::Shell::GetInstance()->accelerator_controller()->PerformAction(
ash::TAKE_SCREENSHOT, ui::Accelerator());
content::RunMessageLoop();
}
#endif
ExtensionService* extension_service() {
extensions::ExtensionSystem* system =
extensions::ExtensionSystem::Get(browser()->profile());
return system->extension_service();
}
const extensions::Extension* InstallExtension(
const base::FilePath::StringType& name) {
base::FilePath extension_path(ui_test_utils::GetTestFilePath(
base::FilePath(kTestExtensionsDir), base::FilePath(name)));
scoped_refptr<extensions::CrxInstaller> installer =
extensions::CrxInstaller::CreateSilent(extension_service());
installer->set_allow_silent_install(true);
installer->set_install_cause(extension_misc::INSTALL_CAUSE_UPDATE);
installer->set_creation_flags(extensions::Extension::FROM_WEBSTORE);
content::WindowedNotificationObserver observer(
chrome::NOTIFICATION_CRX_INSTALLER_DONE,
content::NotificationService::AllSources());
installer->InstallCrx(extension_path);
observer.Wait();
content::Details<const extensions::Extension> details = observer.details();
return details.ptr();
}
const extensions::Extension* LoadUnpackedExtension(
const base::FilePath::StringType& name, bool expect_success) {
base::FilePath extension_path(ui_test_utils::GetTestFilePath(
base::FilePath(kTestExtensionsDir), base::FilePath(name)));
scoped_refptr<extensions::UnpackedInstaller> installer =
extensions::UnpackedInstaller::Create(extension_service());
content::WindowedNotificationObserver observer(
expect_success ? chrome::NOTIFICATION_EXTENSION_LOADED
: chrome::NOTIFICATION_EXTENSION_LOAD_ERROR,
content::NotificationService::AllSources());
installer->Load(extension_path);
observer.Wait();
const extensions::ExtensionSet* extensions =
extension_service()->extensions();
for (extensions::ExtensionSet::const_iterator it = extensions->begin();
it != extensions->end(); ++it) {
if ((*it)->path() == extension_path)
return it->get();
}
return NULL;
}
void UninstallExtension(const std::string& id, bool expect_success) {
content::WindowedNotificationObserver observer(
expect_success ? chrome::NOTIFICATION_EXTENSION_UNINSTALLED
: chrome::NOTIFICATION_EXTENSION_UNINSTALL_NOT_ALLOWED,
content::NotificationService::AllSources());
extension_service()->UninstallExtension(id, false, NULL);
observer.Wait();
}
void UpdateProviderPolicy(const PolicyMap& policy) {
provider_.UpdateChromePolicy(policy);
DCHECK(base::MessageLoop::current());
base::RunLoop loop;
loop.RunUntilIdle();
}
void PerformClick(int x, int y) {
content::WebContents* contents =
browser()->tab_strip_model()->GetActiveWebContents();
blink::WebMouseEvent click_event;
click_event.type = blink::WebInputEvent::MouseDown;
click_event.button = blink::WebMouseEvent::ButtonLeft;
click_event.clickCount = 1;
click_event.x = x;
click_event.y = y;
contents->GetRenderViewHost()->ForwardMouseEvent(click_event);
click_event.type = blink::WebInputEvent::MouseUp;
contents->GetRenderViewHost()->ForwardMouseEvent(click_event);
}
MockConfigurationPolicyProvider provider_;
scoped_ptr<extensions::ExtensionCacheFake> test_extension_cache_;
#if defined(OS_CHROMEOS)
QuitMessageLoopAfterScreenshot observer_;
#endif
};
#if defined(OS_WIN)
class LocalePolicyTest : public PolicyTest {
public:
LocalePolicyTest() {}
virtual ~LocalePolicyTest() {}
virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
PolicyTest::SetUpInProcessBrowserTestFixture();
PolicyMap policies;
policies.Set(
key::kApplicationLocaleValue, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
base::Value::CreateStringValue("fr"), NULL);
provider_.UpdateChromePolicy(policies);
ResourceBundle::CleanupSharedInstance();
}
};
IN_PROC_BROWSER_TEST_F(LocalePolicyTest, ApplicationLocaleValue) {
EXPECT_EQ("fr", g_browser_process->GetApplicationLocale());
ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUINewTabURL));
base::string16 french_title = l10n_util::GetStringUTF16(IDS_NEW_TAB_TITLE);
base::string16 title;
EXPECT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(), &title));
EXPECT_EQ(french_title, title);
std::string loaded =
ui::ResourceBundle::GetSharedInstance().ReloadLocaleResources("en-US");
EXPECT_EQ("en-US", loaded);
base::string16 english_title = l10n_util::GetStringUTF16(IDS_NEW_TAB_TITLE);
EXPECT_NE(french_title, english_title);
}
#endif
IN_PROC_BROWSER_TEST_F(PolicyTest, BookmarkBarEnabled) {
#if defined(OS_WIN) && defined(USE_ASH)
if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
return;
#endif
PrefService* prefs = browser()->profile()->GetPrefs();
EXPECT_FALSE(prefs->IsManagedPreference(prefs::kShowBookmarkBar));
EXPECT_FALSE(prefs->GetBoolean(prefs::kShowBookmarkBar));
EXPECT_EQ(BookmarkBar::HIDDEN, browser()->bookmark_bar_state());
PolicyMap policies;
policies.Set(key::kBookmarkBarEnabled, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, base::Value::CreateBooleanValue(true), NULL);
UpdateProviderPolicy(policies);
EXPECT_TRUE(prefs->IsManagedPreference(prefs::kShowBookmarkBar));
EXPECT_TRUE(prefs->GetBoolean(prefs::kShowBookmarkBar));
EXPECT_EQ(BookmarkBar::SHOW, browser()->bookmark_bar_state());
ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUINewTabURL));
EXPECT_EQ(BookmarkBar::SHOW, browser()->bookmark_bar_state());
policies.Set(key::kBookmarkBarEnabled, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, base::Value::CreateBooleanValue(false), NULL);
UpdateProviderPolicy(policies);
EXPECT_TRUE(prefs->IsManagedPreference(prefs::kShowBookmarkBar));
EXPECT_FALSE(prefs->GetBoolean(prefs::kShowBookmarkBar));
EXPECT_EQ(BookmarkBar::HIDDEN, browser()->bookmark_bar_state());
policies.Clear();
UpdateProviderPolicy(policies);
EXPECT_FALSE(prefs->IsManagedPreference(prefs::kShowBookmarkBar));
EXPECT_FALSE(prefs->GetBoolean(prefs::kShowBookmarkBar));
EXPECT_EQ(BookmarkBar::DETACHED, browser()->bookmark_bar_state());
}
IN_PROC_BROWSER_TEST_F(PolicyTest, PRE_PRE_DefaultCookiesSetting) {
Profile* profile = browser()->profile();
GURL url(kURL);
EXPECT_TRUE(content::GetCookies(profile, url).empty());
std::string value = std::string(kCookieValue) + std::string(kCookieOptions);
EXPECT_TRUE(content::SetCookie(profile, url, value));
EXPECT_EQ(kCookieValue, GetCookies(profile, url));
}
IN_PROC_BROWSER_TEST_F(PolicyTest, PRE_DefaultCookiesSetting) {
EXPECT_EQ(kCookieValue, GetCookies(browser()->profile(), GURL(kURL)));
PolicyMap policies;
policies.Set(key::kDefaultCookiesSetting, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, base::Value::CreateIntegerValue(4), NULL);
UpdateProviderPolicy(policies);
}
IN_PROC_BROWSER_TEST_F(PolicyTest, DefaultCookiesSetting) {
EXPECT_TRUE(GetCookies(browser()->profile(), GURL(kURL)).empty());
}
IN_PROC_BROWSER_TEST_F(PolicyTest, DefaultSearchProvider) {
MakeRequestFail make_request_fail("search.example");
const base::string16 kKeyword(base::ASCIIToUTF16("testsearch"));
const std::string kSearchURL("http://search.example/search?q={searchTerms}");
const std::string kAlternateURL0(
"http://search.example/search#q={searchTerms}");
const std::string kAlternateURL1("http://search.example/#q={searchTerms}");
const std::string kSearchTermsReplacementKey("zekey");
const std::string kImageURL("http://test.com/searchbyimage/upload");
const std::string kImageURLPostParams(
"image_content=content,image_url=http://test.com/test.png");
const std::string kNewTabURL("http://search.example/newtab");
TemplateURLService* service = TemplateURLServiceFactory::GetForProfile(
browser()->profile());
ui_test_utils::WaitForTemplateURLServiceToLoad(service);
TemplateURL* default_search = service->GetDefaultSearchProvider();
ASSERT_TRUE(default_search);
EXPECT_NE(kKeyword, default_search->keyword());
EXPECT_NE(kSearchURL, default_search->url());
EXPECT_FALSE(
default_search->alternate_urls().size() == 2 &&
default_search->alternate_urls()[0] == kAlternateURL0 &&
default_search->alternate_urls()[1] == kAlternateURL1 &&
default_search->search_terms_replacement_key() ==
kSearchTermsReplacementKey &&
default_search->image_url() == kImageURL &&
default_search->image_url_post_params() == kImageURLPostParams &&
default_search->new_tab_url() == kNewTabURL);
PolicyMap policies;
policies.Set(key::kDefaultSearchProviderEnabled, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, base::Value::CreateBooleanValue(true), NULL);
policies.Set(key::kDefaultSearchProviderKeyword,
POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
base::Value::CreateStringValue(kKeyword), NULL);
policies.Set(key::kDefaultSearchProviderSearchURL,
POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
base::Value::CreateStringValue(kSearchURL), NULL);
base::ListValue* alternate_urls = new base::ListValue();
alternate_urls->AppendString(kAlternateURL0);
alternate_urls->AppendString(kAlternateURL1);
policies.Set(key::kDefaultSearchProviderAlternateURLs, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, alternate_urls, NULL);
policies.Set(key::kDefaultSearchProviderSearchTermsReplacementKey,
POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
base::Value::CreateStringValue(kSearchTermsReplacementKey),
NULL);
policies.Set(key::kDefaultSearchProviderImageURL,
POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
base::Value::CreateStringValue(kImageURL),
NULL);
policies.Set(key::kDefaultSearchProviderImageURLPostParams,
POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
base::Value::CreateStringValue(kImageURLPostParams),
NULL);
policies.Set(key::kDefaultSearchProviderNewTabURL,
POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
base::Value::CreateStringValue(kNewTabURL),
NULL);
UpdateProviderPolicy(policies);
default_search = service->GetDefaultSearchProvider();
ASSERT_TRUE(default_search);
EXPECT_EQ(kKeyword, default_search->keyword());
EXPECT_EQ(kSearchURL, default_search->url());
EXPECT_EQ(2U, default_search->alternate_urls().size());
EXPECT_EQ(kAlternateURL0, default_search->alternate_urls()[0]);
EXPECT_EQ(kAlternateURL1, default_search->alternate_urls()[1]);
EXPECT_EQ(kSearchTermsReplacementKey,
default_search->search_terms_replacement_key());
EXPECT_EQ(kImageURL, default_search->image_url());
EXPECT_EQ(kImageURLPostParams, default_search->image_url_post_params());
EXPECT_EQ(kNewTabURL, default_search->new_tab_url());
chrome::FocusLocationBar(browser());
LocationBar* location_bar = browser()->window()->GetLocationBar();
ui_test_utils::SendToOmniboxAndSubmit(location_bar, "stuff to search for");
OmniboxEditModel* model = location_bar->GetOmniboxView()->model();
EXPECT_TRUE(model->CurrentMatch(NULL).destination_url.is_valid());
content::WebContents* web_contents =
browser()->tab_strip_model()->GetActiveWebContents();
GURL expected("http://search.example/search?q=stuff+to+search+for");
EXPECT_EQ(expected, web_contents->GetURL());
ui_test_utils::NavigateToURL(browser(), GURL(content::kAboutBlankURL));
policies.Set(key::kDefaultSearchProviderEnabled, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, base::Value::CreateBooleanValue(false), NULL);
EXPECT_TRUE(service->GetDefaultSearchProvider());
UpdateProviderPolicy(policies);
EXPECT_FALSE(service->GetDefaultSearchProvider());
ui_test_utils::SendToOmniboxAndSubmit(location_bar, "should not work");
EXPECT_FALSE(model->CurrentMatch(NULL).destination_url.is_valid());
EXPECT_EQ(GURL(content::kAboutBlankURL), web_contents->GetURL());
}
IN_PROC_BROWSER_TEST_F(PolicyTest, PolicyPreprocessing) {
PolicyMap policies;
policies.Set(key::kProxyServerMode,
POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER,
base::Value::CreateIntegerValue(3),
NULL);
UpdateProviderPolicy(policies);
PolicyMap expected;
scoped_ptr<base::DictionaryValue> expected_value(new base::DictionaryValue);
expected_value->SetInteger(key::kProxyServerMode, 3);
expected.Set(key::kProxySettings,
POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER,
expected_value.release(),
NULL);
const PolicyMap& actual_from_browser =
g_browser_process->browser_policy_connector()
->GetPolicyService()
->GetPolicies(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()));
EXPECT_TRUE(expected.Equals(actual_from_browser));
const PolicyMap& actual_from_profile =
ProfilePolicyConnectorFactory::GetForProfile(browser()->profile())
->policy_service()
->GetPolicies(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()));
EXPECT_TRUE(expected.Equals(actual_from_profile));
}
IN_PROC_BROWSER_TEST_F(PolicyTest, ForceSafeSearch) {
MakeRequestFail make_request_fail("google.com");
TemplateURLService* service = TemplateURLServiceFactory::GetForProfile(
browser()->profile());
ui_test_utils::WaitForTemplateURLServiceToLoad(service);
content::TestNavigationObserver no_safesearch_observer(
browser()->tab_strip_model()->GetActiveWebContents());
chrome::FocusLocationBar(browser());
LocationBar* location_bar = browser()->window()->GetLocationBar();
ui_test_utils::SendToOmniboxAndSubmit(location_bar, "http://google.com/");
OmniboxEditModel* model = location_bar->GetOmniboxView()->model();
no_safesearch_observer.Wait();
EXPECT_TRUE(model->CurrentMatch(NULL).destination_url.is_valid());
content::WebContents* web_contents =
browser()->tab_strip_model()->GetActiveWebContents();
GURL expected_without("http://google.com/");
EXPECT_EQ(expected_without, web_contents->GetURL());
PrefService* prefs = browser()->profile()->GetPrefs();
EXPECT_FALSE(prefs->IsManagedPreference(prefs::kForceSafeSearch));
EXPECT_FALSE(prefs->GetBoolean(prefs::kForceSafeSearch));
PolicyMap policies;
policies.Set(key::kForceSafeSearch, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, base::Value::CreateBooleanValue(true), NULL);
UpdateProviderPolicy(policies);
EXPECT_TRUE(prefs->IsManagedPreference(prefs::kForceSafeSearch));
EXPECT_TRUE(prefs->GetBoolean(prefs::kForceSafeSearch));
content::TestNavigationObserver safesearch_observer(
browser()->tab_strip_model()->GetActiveWebContents());
chrome::FocusLocationBar(browser());
ui_test_utils::SendToOmniboxAndSubmit(location_bar, "http://google.com/");
safesearch_observer.Wait();
EXPECT_TRUE(model->CurrentMatch(NULL).destination_url.is_valid());
web_contents = browser()->tab_strip_model()->GetActiveWebContents();
std::string expected_url("http://google.com/?");
expected_url += std::string(chrome::kSafeSearchSafeParameter) + "&" +
chrome::kSafeSearchSsuiParameter;
GURL expected_with_parameters(expected_url);
EXPECT_EQ(expected_with_parameters, web_contents->GetURL());
}
IN_PROC_BROWSER_TEST_F(PolicyTest, ReplaceSearchTerms) {
MakeRequestFail make_request_fail("search.example");
chrome::EnableQueryExtractionForTesting();
const base::string16 kKeyword(base::ASCIIToUTF16("testsearch"));
const std::string kSearchURL("https://www.google.com/search?q={searchTerms}");
const std::string kInstantURL("http://does/not/exist");
const std::string kAlternateURL0(
"https://www.google.com/search#q={searchTerms}");
const std::string kAlternateURL1("https://www.google.com/#q={searchTerms}");
const std::string kSearchTermsReplacementKey(
"{google:instantExtendedEnabledKey}");
TemplateURLService* service = TemplateURLServiceFactory::GetForProfile(
browser()->profile());
ui_test_utils::WaitForTemplateURLServiceToLoad(service);
TemplateURL* default_search = service->GetDefaultSearchProvider();
ASSERT_TRUE(default_search);
EXPECT_NE(kKeyword, default_search->keyword());
EXPECT_NE(kSearchURL, default_search->url());
EXPECT_NE(kInstantURL, default_search->instant_url());
EXPECT_FALSE(
default_search->alternate_urls().size() == 2 &&
default_search->alternate_urls()[0] == kAlternateURL0 &&
default_search->alternate_urls()[1] == kAlternateURL1);
PolicyMap policies;
policies.Set(key::kDefaultSearchProviderEnabled, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, base::Value::CreateBooleanValue(true), NULL);
policies.Set(key::kDefaultSearchProviderKeyword,
POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
base::Value::CreateStringValue(kKeyword), NULL);
policies.Set(key::kDefaultSearchProviderSearchURL,
POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
base::Value::CreateStringValue(kSearchURL), NULL);
policies.Set(key::kDefaultSearchProviderInstantURL,
POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
base::Value::CreateStringValue(kInstantURL), NULL);
base::ListValue* alternate_urls = new base::ListValue();
alternate_urls->AppendString(kAlternateURL0);
alternate_urls->AppendString(kAlternateURL1);
policies.Set(key::kDefaultSearchProviderAlternateURLs, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, alternate_urls, NULL);
policies.Set(key::kDefaultSearchProviderSearchTermsReplacementKey,
POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
base::Value::CreateStringValue(kSearchTermsReplacementKey),
NULL);
UpdateProviderPolicy(policies);
default_search = service->GetDefaultSearchProvider();
ASSERT_TRUE(default_search);
EXPECT_EQ(kKeyword, default_search->keyword());
EXPECT_EQ(kSearchURL, default_search->url());
EXPECT_EQ(kInstantURL, default_search->instant_url());
EXPECT_EQ(2U, default_search->alternate_urls().size());
EXPECT_EQ(kAlternateURL0, default_search->alternate_urls()[0]);
EXPECT_EQ(kAlternateURL1, default_search->alternate_urls()[1]);
InstantService* instant_service =
InstantServiceFactory::GetForProfile(browser()->profile());
instant_service->AddInstantProcess(browser()->tab_strip_model()->
GetActiveWebContents()->GetRenderProcessHost()->GetID());
chrome::FocusLocationBar(browser());
LocationBar* location_bar = browser()->window()->GetLocationBar();
OmniboxView* omnibox_view = location_bar->GetOmniboxView();
ui_test_utils::SendToOmniboxAndSubmit(location_bar,
"https://www.google.com/?espv=1#q=foobar");
EXPECT_TRUE(
browser()->toolbar_model()->WouldPerformSearchTermReplacement(false));
EXPECT_EQ(base::ASCIIToUTF16("foobar"), omnibox_view->GetText());
chrome::FocusLocationBar(browser());
ui_test_utils::SendToOmniboxAndSubmit(location_bar,
"https://www.google.com/?q=foobar");
EXPECT_FALSE(
browser()->toolbar_model()->WouldPerformSearchTermReplacement(false));
EXPECT_EQ(base::ASCIIToUTF16("https://www.google.com/?q=foobar"),
omnibox_view->GetText());
chrome::FocusLocationBar(browser());
ui_test_utils::SendToOmniboxAndSubmit(location_bar,
"https://www.google.com/search?espv=1#q=banana");
EXPECT_TRUE(
browser()->toolbar_model()->WouldPerformSearchTermReplacement(false));
EXPECT_EQ(base::ASCIIToUTF16("banana"), omnibox_view->GetText());
chrome::FocusLocationBar(browser());
ui_test_utils::SendToOmniboxAndSubmit(location_bar,
"https://www.google.com/search?q=tractor+parts&espv=1");
EXPECT_TRUE(
browser()->toolbar_model()->WouldPerformSearchTermReplacement(false));
EXPECT_EQ(base::ASCIIToUTF16("tractor parts"), omnibox_view->GetText());
chrome::FocusLocationBar(browser());
ui_test_utils::SendToOmniboxAndSubmit(location_bar,
"https://www.google.com/search?q=tractor+parts&espv=1#q=foobar");
EXPECT_TRUE(
browser()->toolbar_model()->WouldPerformSearchTermReplacement(false));
EXPECT_EQ(base::ASCIIToUTF16("foobar"), omnibox_view->GetText());
}
IN_PROC_BROWSER_TEST_F(PolicyTest, Disable3DAPIs) {
if (!content::GpuDataManager::GetInstance()->GpuAccessAllowed(NULL))
return;
ui_test_utils::NavigateToURL(browser(), GURL(content::kAboutBlankURL));
content::WebContents* contents =
browser()->tab_strip_model()->GetActiveWebContents();
EXPECT_TRUE(IsWebGLEnabled(contents));
PolicyMap policies;
policies.Set(key::kDisable3DAPIs, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, base::Value::CreateBooleanValue(true), NULL);
UpdateProviderPolicy(policies);
content::CrashTab(contents);
EXPECT_TRUE(chrome::ExecuteCommand(browser(), IDC_RELOAD));
EXPECT_FALSE(IsWebGLEnabled(contents));
policies.Set(key::kDisable3DAPIs, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, base::Value::CreateBooleanValue(false), NULL);
UpdateProviderPolicy(policies);
content::CrashTab(contents);
EXPECT_TRUE(chrome::ExecuteCommand(browser(), IDC_RELOAD));
EXPECT_TRUE(IsWebGLEnabled(contents));
}
IN_PROC_BROWSER_TEST_F(PolicyTest, DisableSpdy) {
EXPECT_TRUE(net::HttpStreamFactory::spdy_enabled());
PolicyMap policies;
policies.Set(key::kDisableSpdy, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, base::Value::CreateBooleanValue(true), NULL);
UpdateProviderPolicy(policies);
content::RunAllPendingInMessageLoop();
EXPECT_FALSE(net::HttpStreamFactory::spdy_enabled());
browser()->profile()->GetPrefs()->SetBoolean(prefs::kDisableSpdy, true);
policies.Set(key::kDisableSpdy, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, base::Value::CreateBooleanValue(false), NULL);
UpdateProviderPolicy(policies);
content::RunAllPendingInMessageLoop();
EXPECT_TRUE(net::HttpStreamFactory::spdy_enabled());
}
IN_PROC_BROWSER_TEST_F(PolicyTest, DisabledPlugins) {
std::vector<content::WebPluginInfo> plugins;
GetPluginList(&plugins);
const content::WebPluginInfo* flash = GetFlashPlugin(plugins);
if (!flash)
return;
PluginPrefs* plugin_prefs =
PluginPrefs::GetForProfile(browser()->profile()).get();
EXPECT_TRUE(plugin_prefs->IsPluginEnabled(*flash));
EXPECT_TRUE(SetPluginEnabled(plugin_prefs, flash, false));
EXPECT_FALSE(plugin_prefs->IsPluginEnabled(*flash));
EXPECT_TRUE(SetPluginEnabled(plugin_prefs, flash, true));
EXPECT_TRUE(plugin_prefs->IsPluginEnabled(*flash));
base::ListValue disabled_plugins;
disabled_plugins.Append(base::Value::CreateStringValue("*Flash*"));
PolicyMap policies;
policies.Set(key::kDisabledPlugins, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, disabled_plugins.DeepCopy(), NULL);
UpdateProviderPolicy(policies);
EXPECT_FALSE(plugin_prefs->IsPluginEnabled(*flash));
EXPECT_FALSE(SetPluginEnabled(plugin_prefs, flash, true));
EXPECT_FALSE(plugin_prefs->IsPluginEnabled(*flash));
}
IN_PROC_BROWSER_TEST_F(PolicyTest, DisabledPluginsExceptions) {
std::vector<content::WebPluginInfo> plugins;
GetPluginList(&plugins);
const content::WebPluginInfo* flash = GetFlashPlugin(plugins);
if (!flash)
return;
PluginPrefs* plugin_prefs =
PluginPrefs::GetForProfile(browser()->profile()).get();
EXPECT_TRUE(plugin_prefs->IsPluginEnabled(*flash));
base::ListValue disabled_plugins;
disabled_plugins.Append(base::Value::CreateStringValue("*"));
PolicyMap policies;
policies.Set(key::kDisabledPlugins, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, disabled_plugins.DeepCopy(), NULL);
UpdateProviderPolicy(policies);
EXPECT_FALSE(plugin_prefs->IsPluginEnabled(*flash));
EXPECT_FALSE(SetPluginEnabled(plugin_prefs, flash, true));
EXPECT_FALSE(plugin_prefs->IsPluginEnabled(*flash));
base::ListValue disabled_plugins_exceptions;
disabled_plugins_exceptions.Append(
base::Value::CreateStringValue("*Flash*"));
policies.Set(key::kDisabledPluginsExceptions, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, disabled_plugins_exceptions.DeepCopy(), NULL);
UpdateProviderPolicy(policies);
EXPECT_TRUE(plugin_prefs->IsPluginEnabled(*flash));
EXPECT_TRUE(SetPluginEnabled(plugin_prefs, flash, false));
EXPECT_FALSE(plugin_prefs->IsPluginEnabled(*flash));
EXPECT_TRUE(SetPluginEnabled(plugin_prefs, flash, true));
EXPECT_TRUE(plugin_prefs->IsPluginEnabled(*flash));
}
IN_PROC_BROWSER_TEST_F(PolicyTest, EnabledPlugins) {
std::vector<content::WebPluginInfo> plugins;
GetPluginList(&plugins);
const content::WebPluginInfo* flash = GetFlashPlugin(plugins);
if (!flash)
return;
PluginPrefs* plugin_prefs =
PluginPrefs::GetForProfile(browser()->profile()).get();
EXPECT_TRUE(plugin_prefs->IsPluginEnabled(*flash));
EXPECT_TRUE(SetPluginEnabled(plugin_prefs, flash, false));
EXPECT_FALSE(plugin_prefs->IsPluginEnabled(*flash));
base::ListValue plugin_list;
plugin_list.Append(base::Value::CreateStringValue(content::kFlashPluginName));
PolicyMap policies;
policies.Set(key::kEnabledPlugins, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, plugin_list.DeepCopy(), NULL);
UpdateProviderPolicy(policies);
EXPECT_TRUE(plugin_prefs->IsPluginEnabled(*flash));
EXPECT_FALSE(SetPluginEnabled(plugin_prefs, flash, false));
EXPECT_TRUE(plugin_prefs->IsPluginEnabled(*flash));
policies.Set(key::kDisabledPlugins, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, plugin_list.DeepCopy(), NULL);
UpdateProviderPolicy(policies);
EXPECT_TRUE(plugin_prefs->IsPluginEnabled(*flash));
}
IN_PROC_BROWSER_TEST_F(PolicyTest, AlwaysAuthorizePlugins) {
if (!base::PathExists(ui_test_utils::GetTestFilePath(
base::FilePath(FILE_PATH_LITERAL("plugin")),
base::FilePath(FILE_PATH_LITERAL("quicktime.html"))))) {
LOG(INFO) <<
"Test skipped because plugin/quicktime.html test file wasn't found.";
return;
}
ServeContentTestData();
EXPECT_EQ(0, CountPlugins());
content::WebContents* contents =
browser()->tab_strip_model()->GetActiveWebContents();
ASSERT_TRUE(contents);
InfoBarService* infobar_service = InfoBarService::FromWebContents(contents);
ASSERT_TRUE(infobar_service);
InfoBarManager* infobar_manager = infobar_service->infobar_manager();
EXPECT_EQ(0u, infobar_manager->infobar_count());
base::FilePath path(FILE_PATH_LITERAL("plugin/quicktime.html"));
GURL url(URLRequestMockHTTPJob::GetMockUrl(path));
ui_test_utils::NavigateToURL(browser(), url);
ASSERT_EQ(1u, infobar_manager->infobar_count());
EXPECT_TRUE(
infobar_manager->infobar_at(0)->delegate()->AsConfirmInfoBarDelegate());
EXPECT_EQ(0, CountPlugins());
PolicyMap policies;
policies.Set(key::kAlwaysAuthorizePlugins, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, base::Value::CreateBooleanValue(true), NULL);
UpdateProviderPolicy(policies);
ui_test_utils::NavigateToURL(browser(), url);
EXPECT_EQ(0u, infobar_manager->infobar_count());
EXPECT_EQ(1, CountPlugins());
}
IN_PROC_BROWSER_TEST_F(PolicyTest, DeveloperToolsDisabled) {
EXPECT_TRUE(chrome::ExecuteCommand(browser(), IDC_DEV_TOOLS));
content::WebContents* contents =
browser()->tab_strip_model()->GetActiveWebContents();
DevToolsWindow *devtools_window =
DevToolsWindow::GetInstanceForInspectedRenderViewHost(
contents->GetRenderViewHost());
EXPECT_TRUE(devtools_window);
PolicyMap policies;
policies.Set(key::kDeveloperToolsDisabled, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, base::Value::CreateBooleanValue(true), NULL);
content::WindowedNotificationObserver close_observer(
content::NOTIFICATION_WEB_CONTENTS_DESTROYED,
content::Source<content::WebContents>(devtools_window->web_contents()));
UpdateProviderPolicy(policies);
close_observer.Wait();
EXPECT_FALSE(DevToolsWindow::GetInstanceForInspectedRenderViewHost(
contents->GetRenderViewHost()));
EXPECT_FALSE(chrome::ExecuteCommand(browser(), IDC_DEV_TOOLS));
EXPECT_FALSE(DevToolsWindow::GetInstanceForInspectedRenderViewHost(
contents->GetRenderViewHost()));
}
IN_PROC_BROWSER_TEST_F(PolicyTest, DISABLED_WebStoreIconHidden) {
#if defined(OS_WIN) && defined(USE_ASH)
if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
return;
#endif
ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUINewTabURL));
content::WebContents* contents =
browser()->tab_strip_model()->GetActiveWebContents();
#if !defined(OS_CHROMEOS)
EXPECT_TRUE(ContainsVisibleElement(contents,
"ahfgeienlihckogmohjhadlkjgocpleb"));
#endif
if (ContainsVisibleElement(contents, "footer"))
EXPECT_TRUE(ContainsVisibleElement(contents, "chrome-web-store-link"));
PolicyMap policies;
policies.Set(key::kHideWebStoreIcon, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, base::Value::CreateBooleanValue(true), NULL);
UpdateProviderPolicy(policies);
ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUINewTabURL));
EXPECT_FALSE(ContainsVisibleElement(contents,
"ahfgeienlihckogmohjhadlkjgocpleb"));
EXPECT_FALSE(ContainsVisibleElement(contents, "chrome-web-store-link"));
}
IN_PROC_BROWSER_TEST_F(PolicyTest, DownloadDirectory) {
base::ScopedTempDir initial_dir;
ASSERT_TRUE(initial_dir.CreateUniqueTempDir());
browser()->profile()->GetPrefs()->SetFilePath(
prefs::kDownloadDefaultDirectory, initial_dir.path());
browser()->profile()->GetPrefs()->SetBoolean(
prefs::kPromptForDownload, false);
base::FilePath file(FILE_PATH_LITERAL("download-test1.lib"));
DownloadAndVerifyFile(browser(), initial_dir.path(), file);
base::DieFileDie(initial_dir.path().Append(file), false);
base::ScopedTempDir forced_dir;
ASSERT_TRUE(forced_dir.CreateUniqueTempDir());
PolicyMap policies;
policies.Set(key::kDownloadDirectory, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER,
base::Value::CreateStringValue(forced_dir.path().value()), NULL);
UpdateProviderPolicy(policies);
DownloadAndVerifyFile(browser(), forced_dir.path(), file);
EXPECT_FALSE(base::PathExists(initial_dir.path().Append(file)));
}
IN_PROC_BROWSER_TEST_F(PolicyTest, ExtensionInstallBlacklistSelective) {
ExtensionService* service = extension_service();
ASSERT_FALSE(service->GetExtensionById(kGoodCrxId, true));
ASSERT_FALSE(service->GetExtensionById(kAdBlockCrxId, true));
base::ListValue blacklist;
blacklist.Append(base::Value::CreateStringValue(kGoodCrxId));
PolicyMap policies;
policies.Set(key::kExtensionInstallBlacklist, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, blacklist.DeepCopy(), NULL);
UpdateProviderPolicy(policies);
EXPECT_FALSE(InstallExtension(kGoodCrxName));
EXPECT_FALSE(service->GetExtensionById(kGoodCrxId, true));
const extensions::Extension* adblock = InstallExtension(kAdBlockCrxName);
ASSERT_TRUE(adblock);
EXPECT_EQ(kAdBlockCrxId, adblock->id());
EXPECT_EQ(adblock,
service->GetExtensionById(kAdBlockCrxId, true));
}
#if defined(OS_WIN)
#define MAYBE_ExtensionInstallBlacklistWildcard DISABLED_ExtensionInstallBlacklistWildcard
#else
#define MAYBE_ExtensionInstallBlacklistWildcard ExtensionInstallBlacklistWildcard
#endif
IN_PROC_BROWSER_TEST_F(PolicyTest, MAYBE_ExtensionInstallBlacklistWildcard) {
EXPECT_TRUE(InstallExtension(kAdBlockCrxName));
ExtensionService* service = extension_service();
ASSERT_FALSE(service->GetExtensionById(kGoodCrxId, true));
ASSERT_TRUE(service->GetExtensionById(kAdBlockCrxId, true));
base::ListValue blacklist;
blacklist.Append(base::Value::CreateStringValue("*"));
PolicyMap policies;
policies.Set(key::kExtensionInstallBlacklist, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, blacklist.DeepCopy(), NULL);
UpdateProviderPolicy(policies);
ASSERT_FALSE(service->GetExtensionById(kAdBlockCrxId, true));
EXPECT_FALSE(InstallExtension(kAdBlockCrxName));
EXPECT_FALSE(service->GetExtensionById(kAdBlockCrxId, true));
EXPECT_FALSE(InstallExtension(kGoodCrxName));
EXPECT_FALSE(service->GetExtensionById(kGoodCrxId, true));
}
IN_PROC_BROWSER_TEST_F(PolicyTest, ExtensionInstallWhitelist) {
ExtensionService* service = extension_service();
ASSERT_FALSE(service->GetExtensionById(kGoodCrxId, true));
ASSERT_FALSE(service->GetExtensionById(kAdBlockCrxId, true));
base::ListValue blacklist;
blacklist.Append(base::Value::CreateStringValue("*"));
base::ListValue whitelist;
whitelist.Append(base::Value::CreateStringValue(kGoodCrxId));
PolicyMap policies;
policies.Set(key::kExtensionInstallBlacklist, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, blacklist.DeepCopy(), NULL);
policies.Set(key::kExtensionInstallWhitelist, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, whitelist.DeepCopy(), NULL);
UpdateProviderPolicy(policies);
EXPECT_FALSE(InstallExtension(kAdBlockCrxName));
EXPECT_FALSE(service->GetExtensionById(kAdBlockCrxId, true));
const extensions::Extension* good = InstallExtension(kGoodCrxName);
ASSERT_TRUE(good);
EXPECT_EQ(kGoodCrxId, good->id());
EXPECT_EQ(good, service->GetExtensionById(kGoodCrxId, true));
UninstallExtension(kGoodCrxId, true);
}
IN_PROC_BROWSER_TEST_F(PolicyTest, ExtensionInstallForcelist) {
ExtensionService* service = extension_service();
ASSERT_FALSE(service->GetExtensionById(kGoodCrxId, true));
base::FilePath path =
base::FilePath(kTestExtensionsDir).Append(kGoodV1CrxManifestName);
GURL url(URLRequestMockHTTPJob::GetMockUrl(path));
base::ListValue forcelist;
forcelist.Append(base::Value::CreateStringValue(base::StringPrintf(
"%s;%s", kGoodCrxId, url.spec().c_str())));
PolicyMap policies;
policies.Set(key::kExtensionInstallForcelist, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, forcelist.DeepCopy(), NULL);
content::WindowedNotificationObserver observer(
chrome::NOTIFICATION_EXTENSION_INSTALLED,
content::NotificationService::AllSources());
UpdateProviderPolicy(policies);
observer.Wait();
EXPECT_TRUE(service->GetExtensionById(kGoodCrxId, true));
UninstallExtension(kGoodCrxId, false);
LoadUnpackedExtension(kGoodUnpackedExt, false);
LoadUnpackedExtension(kAppUnpackedExt, true);
const std::string old_version_number =
service->GetExtensionById(kGoodCrxId, true)->version()->GetString();
base::FilePath test_path;
ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_path));
TestRequestInterceptor interceptor(
"update.extension",
BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
interceptor.PushJobCallback(
TestRequestInterceptor::FileJob(
test_path.Append(kTestExtensionsDir).Append(kGood2CrxManifestName)));
extensions::ExtensionUpdater* updater = service->updater();
extensions::ExtensionUpdater::CheckParams params;
params.install_immediately = true;
content::WindowedNotificationObserver update_observer(
chrome::NOTIFICATION_EXTENSION_INSTALLED,
content::NotificationService::AllSources());
updater->CheckNow(params);
update_observer.Wait();
const base::Version* new_version =
service->GetExtensionById(kGoodCrxId, true)->version();
ASSERT_TRUE(new_version->IsValid());
base::Version old_version(old_version_number);
ASSERT_TRUE(old_version.IsValid());
EXPECT_EQ(1, new_version->CompareTo(old_version));
EXPECT_EQ(0u, interceptor.GetPendingSize());
extensions::ProcessManager* manager =
extensions::ExtensionSystem::Get(browser()->profile())->process_manager();
extensions::ProcessManager::ViewSet all_views = manager->GetAllViews();
for (extensions::ProcessManager::ViewSet::const_iterator iter =
all_views.begin();
iter != all_views.end();) {
if (!(*iter)->IsLoading()) {
++iter;
} else {
content::WebContents* web_contents =
content::WebContents::FromRenderViewHost(*iter);
ASSERT_TRUE(web_contents);
WebContentsLoadedOrDestroyedWatcher(web_contents).Wait();
all_views = manager->GetAllViews();
iter = all_views.begin();
}
}
BackgroundContentsService::
SetRestartDelayForForceInstalledAppsAndExtensionsForTesting(0);
content::WindowedNotificationObserver extension_crashed_observer(
chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED,
content::NotificationService::AllSources());
content::WindowedNotificationObserver extension_loaded_observer(
chrome::NOTIFICATION_EXTENSION_LOADED,
content::NotificationService::AllSources());
extensions::ExtensionHost* extension_host =
extensions::ExtensionSystem::Get(browser()->profile())->
process_manager()->GetBackgroundHostForExtension(kGoodCrxId);
base::KillProcess(extension_host->render_process_host()->GetHandle(),
content::RESULT_CODE_KILLED, false);
extension_crashed_observer.Wait();
extension_loaded_observer.Wait();
}
IN_PROC_BROWSER_TEST_F(PolicyTest, ExtensionAllowedTypes) {
ExtensionService* service = extension_service();
ASSERT_FALSE(service->GetExtensionById(kGoodCrxId, true));
ASSERT_FALSE(service->GetExtensionById(kHostedAppCrxId, true));
base::ListValue allowed_types;
allowed_types.AppendString("hosted_app");
PolicyMap policies;
policies.Set(key::kExtensionAllowedTypes, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, allowed_types.DeepCopy(), NULL);
UpdateProviderPolicy(policies);
EXPECT_FALSE(InstallExtension(kGoodCrxName));
EXPECT_FALSE(service->GetExtensionById(kGoodCrxId, true));
const extensions::Extension* hosted_app = InstallExtension(kHostedAppCrxName);
ASSERT_TRUE(hosted_app);
EXPECT_EQ(kHostedAppCrxId, hosted_app->id());
EXPECT_EQ(hosted_app, service->GetExtensionById(kHostedAppCrxId, true));
UninstallExtension(kHostedAppCrxId, true);
}
#if defined(OS_WIN)
#define MAYBE_ExtensionInstallSources DISABLED_ExtensionInstallSources
#else
#define MAYBE_ExtensionInstallSources ExtensionInstallSources
#endif
IN_PROC_BROWSER_TEST_F(PolicyTest, MAYBE_ExtensionInstallSources) {
CommandLine::ForCurrentProcess()->AppendSwitchASCII(
switches::kAppsGalleryInstallAutoConfirmForTests, "accept");
const GURL install_source_url(URLRequestMockHTTPJob::GetMockUrl(
base::FilePath(FILE_PATH_LITERAL("extensions/*"))));
const GURL referrer_url(URLRequestMockHTTPJob::GetMockUrl(
base::FilePath(FILE_PATH_LITERAL("policy/*"))));
const GURL download_page_url(URLRequestMockHTTPJob::GetMockUrl(base::FilePath(
FILE_PATH_LITERAL("policy/extension_install_sources_test.html"))));
ui_test_utils::NavigateToURL(browser(), download_page_url);
content::DownloadTestObserverTerminal download_observer(
content::BrowserContext::GetDownloadManager(browser()->profile()), 1,
content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_DENY);
PerformClick(0, 0);
download_observer.WaitForFinished();
base::ListValue install_sources;
install_sources.AppendString(install_source_url.spec());
install_sources.AppendString(referrer_url.spec());
PolicyMap policies;
policies.Set(key::kExtensionInstallSources, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, install_sources.DeepCopy(), NULL);
UpdateProviderPolicy(policies);
content::WindowedNotificationObserver observer(
chrome::NOTIFICATION_EXTENSION_INSTALLED,
content::NotificationService::AllSources());
PerformClick(1, 0);
observer.Wait();
EXPECT_FALSE(extension_service()->GetExtensionById(kGoodCrxId, true));
EXPECT_TRUE(extension_service()->GetExtensionById(kAdBlockCrxId, false));
}
IN_PROC_BROWSER_TEST_F(PolicyTest, HomepageLocation) {
#if defined(OS_WIN) && defined(USE_ASH)
if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
return;
#endif
browser()->profile()->GetPrefs()->SetString(
prefs::kHomePage, chrome::kChromeUIPolicyURL);
browser()->profile()->GetPrefs()->SetBoolean(
prefs::kHomePageIsNewTabPage, false);
EXPECT_EQ(GURL(chrome::kChromeUIPolicyURL),
browser()->profile()->GetHomePage());
content::WebContents* contents =
browser()->tab_strip_model()->GetActiveWebContents();
EXPECT_EQ(GURL(content::kAboutBlankURL), contents->GetURL());
EXPECT_TRUE(chrome::ExecuteCommand(browser(), IDC_HOME));
EXPECT_EQ(GURL(chrome::kChromeUIPolicyURL), contents->GetURL());
PolicyMap policies;
policies.Set(key::kHomepageLocation, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER,
base::Value::CreateStringValue(chrome::kChromeUICreditsURL),
NULL);
UpdateProviderPolicy(policies);
EXPECT_TRUE(chrome::ExecuteCommand(browser(), IDC_HOME));
content::WaitForLoadStop(contents);
EXPECT_EQ(GURL(chrome::kChromeUICreditsURL), contents->GetURL());
policies.Set(key::kHomepageIsNewTabPage, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, base::Value::CreateBooleanValue(true), NULL);
UpdateProviderPolicy(policies);
EXPECT_TRUE(chrome::ExecuteCommand(browser(), IDC_HOME));
content::WaitForLoadStop(contents);
EXPECT_EQ(GURL(chrome::kChromeUINewTabURL), contents->GetURL());
}
IN_PROC_BROWSER_TEST_F(PolicyTest, IncognitoEnabled) {
const BrowserList* active_browser_list =
BrowserList::GetInstance(chrome::GetActiveDesktop());
EXPECT_EQ(1u, active_browser_list->size());
EXPECT_FALSE(BrowserList::IsOffTheRecordSessionActive());
PolicyMap policies;
policies.Set(key::kIncognitoEnabled, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, base::Value::CreateBooleanValue(false), NULL);
UpdateProviderPolicy(policies);
EXPECT_FALSE(chrome::ExecuteCommand(browser(), IDC_NEW_INCOGNITO_WINDOW));
EXPECT_EQ(1u, active_browser_list->size());
EXPECT_FALSE(BrowserList::IsOffTheRecordSessionActive());
policies.Set(key::kIncognitoEnabled, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, base::Value::CreateBooleanValue(true), NULL);
UpdateProviderPolicy(policies);
EXPECT_TRUE(chrome::ExecuteCommand(browser(), IDC_NEW_INCOGNITO_WINDOW));
EXPECT_EQ(2u, active_browser_list->size());
EXPECT_TRUE(BrowserList::IsOffTheRecordSessionActive());
}
IN_PROC_BROWSER_TEST_F(PolicyTest, Javascript) {
content::WebContents* contents =
browser()->tab_strip_model()->GetActiveWebContents();
EXPECT_TRUE(IsJavascriptEnabled(contents));
EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_DEV_TOOLS));
EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_DEV_TOOLS_CONSOLE));
EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_DEV_TOOLS_DEVICES));
PolicyMap policies;
policies.Set(key::kJavascriptEnabled, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, base::Value::CreateBooleanValue(false), NULL);
UpdateProviderPolicy(policies);
ui_test_utils::NavigateToURL(browser(), GURL(content::kAboutBlankURL));
EXPECT_FALSE(IsJavascriptEnabled(contents));
EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_DEV_TOOLS));
EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_DEV_TOOLS_CONSOLE));
EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_DEV_TOOLS_DEVICES));
ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIAboutURL));
EXPECT_TRUE(IsJavascriptEnabled(contents));
ui_test_utils::NavigateToURL(browser(), GURL(content::kAboutBlankURL));
EXPECT_FALSE(IsJavascriptEnabled(contents));
policies.Set(key::kDefaultJavaScriptSetting, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER,
base::Value::CreateIntegerValue(CONTENT_SETTING_ALLOW), NULL);
UpdateProviderPolicy(policies);
ui_test_utils::NavigateToURL(browser(), GURL(content::kAboutBlankURL));
EXPECT_TRUE(IsJavascriptEnabled(contents));
}
IN_PROC_BROWSER_TEST_F(PolicyTest, SavingBrowserHistoryDisabled) {
PolicyMap policies;
policies.Set(key::kSavingBrowserHistoryDisabled, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, base::Value::CreateBooleanValue(true), NULL);
UpdateProviderPolicy(policies);
GURL url = ui_test_utils::GetTestUrl(
base::FilePath(base::FilePath::kCurrentDirectory),
base::FilePath(FILE_PATH_LITERAL("empty.html")));
ui_test_utils::NavigateToURL(browser(), url);
ui_test_utils::HistoryEnumerator enumerator1(browser()->profile());
EXPECT_EQ(0u, enumerator1.urls().size());
policies.Set(key::kSavingBrowserHistoryDisabled, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, base::Value::CreateBooleanValue(false), NULL);
UpdateProviderPolicy(policies);
ui_test_utils::NavigateToURL(browser(), url);
ui_test_utils::HistoryEnumerator enumerator2(browser()->profile());
ASSERT_EQ(1u, enumerator2.urls().size());
EXPECT_EQ(url, enumerator2.urls()[0]);
}
IN_PROC_BROWSER_TEST_F(PolicyTest, DISABLED_TranslateEnabled) {
TranslateService::SetUseInfobar(true);
content::WebContents* contents =
browser()->tab_strip_model()->GetActiveWebContents();
ASSERT_TRUE(contents);
InfoBarService* infobar_service = InfoBarService::FromWebContents(contents);
ASSERT_TRUE(infobar_service);
InfoBarManager* infobar_manager = infobar_service->infobar_manager();
EXPECT_EQ(0u, infobar_manager->infobar_count());
PolicyMap policies;
policies.Set(key::kTranslateEnabled, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, base::Value::CreateBooleanValue(true), NULL);
UpdateProviderPolicy(policies);
GURL url = ui_test_utils::GetTestUrl(
base::FilePath(),
base::FilePath(FILE_PATH_LITERAL("translate/fr_test.html")));
content::WindowedNotificationObserver language_observer1(
chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED,
content::NotificationService::AllSources());
ui_test_utils::NavigateToURL(browser(), url);
language_observer1.Wait();
TranslateTabHelper* translate_tab_helper =
TranslateTabHelper::FromWebContents(contents);
ASSERT_TRUE(translate_tab_helper);
LanguageState& language_state = translate_tab_helper->GetLanguageState();
EXPECT_EQ("fr", language_state.original_language());
EXPECT_TRUE(language_state.page_needs_translation());
EXPECT_FALSE(language_state.translation_pending());
EXPECT_FALSE(language_state.translation_declined());
EXPECT_FALSE(language_state.IsPageTranslated());
ASSERT_EQ(1u, infobar_manager->infobar_count());
InfoBar* infobar = infobar_manager->infobar_at(0);
TranslateInfoBarDelegate* translate_infobar_delegate =
infobar->delegate()->AsTranslateInfoBarDelegate();
ASSERT_TRUE(translate_infobar_delegate);
EXPECT_EQ(TranslateTabHelper::BEFORE_TRANSLATE,
translate_infobar_delegate->translate_step());
EXPECT_EQ("fr", translate_infobar_delegate->original_language_code());
infobar_manager->RemoveInfoBar(infobar);
EXPECT_EQ(0u, infobar_manager->infobar_count());
policies.Set(key::kTranslateEnabled, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, base::Value::CreateBooleanValue(false), NULL);
UpdateProviderPolicy(policies);
content::WindowedNotificationObserver language_observer2(
chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED,
content::NotificationService::AllSources());
ui_test_utils::NavigateToURL(browser(), url);
language_observer2.Wait();
EXPECT_EQ(0u, infobar_manager->infobar_count());
}
IN_PROC_BROWSER_TEST_F(PolicyTest, URLBlacklist) {
const char* kURLS[] = {
"http://aaa.com/empty.html",
"http://bbb.com/empty.html",
"http://sub.bbb.com/empty.html",
"http://bbb.com/policy/blank.html",
};
{
base::RunLoop loop;
BrowserThread::PostTaskAndReply(
BrowserThread::IO, FROM_HERE,
base::Bind(RedirectHostsToTestData, kURLS, arraysize(kURLS)),
loop.QuitClosure());
loop.Run();
}
CheckCanOpenURL(browser(), kURLS[1]);
base::ListValue blacklist;
blacklist.Append(base::Value::CreateStringValue("bbb.com"));
PolicyMap policies;
policies.Set(key::kURLBlacklist, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, blacklist.DeepCopy(), NULL);
UpdateProviderPolicy(policies);
FlushBlacklistPolicy();
CheckCanOpenURL(browser(), kURLS[0]);
for (size_t i = 1; i < arraysize(kURLS); ++i)
CheckURLIsBlocked(browser(), kURLS[i]);
base::ListValue whitelist;
whitelist.Append(base::Value::CreateStringValue("sub.bbb.com"));
whitelist.Append(base::Value::CreateStringValue("bbb.com/policy"));
policies.Set(key::kURLWhitelist, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, whitelist.DeepCopy(), NULL);
UpdateProviderPolicy(policies);
FlushBlacklistPolicy();
CheckURLIsBlocked(browser(), kURLS[1]);
CheckCanOpenURL(browser(), kURLS[2]);
CheckCanOpenURL(browser(), kURLS[3]);
{
base::RunLoop loop;
BrowserThread::PostTaskAndReply(
BrowserThread::IO, FROM_HERE,
base::Bind(UndoRedirectHostsToTestData, kURLS, arraysize(kURLS)),
loop.QuitClosure());
loop.Run();
}
}
IN_PROC_BROWSER_TEST_F(PolicyTest, DISABLED_FileURLBlacklist) {
base::FilePath test_path;
PathService::Get(chrome::DIR_TEST_DATA, &test_path);
const std::string base_path = "file://" + test_path.AsUTF8Unsafe() +"/";
const std::string folder_path = base_path + "apptest/";
const std::string file_path1 = base_path + "title1.html";
const std::string file_path2 = folder_path + "basic.html";
CheckCanOpenURL(browser(), file_path1.c_str());
CheckCanOpenURL(browser(), file_path2.c_str());
base::ListValue blacklist;
blacklist.Append(base::Value::CreateStringValue("file://*"));
PolicyMap policies;
policies.Set(key::kURLBlacklist, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, blacklist.DeepCopy(), NULL);
UpdateProviderPolicy(policies);
FlushBlacklistPolicy();
CheckURLIsBlocked(browser(), file_path1.c_str());
CheckURLIsBlocked(browser(), file_path2.c_str());
blacklist.Remove(base::StringValue("file://*"), NULL);
policies.Set(key::kURLBlacklist, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, blacklist.DeepCopy(), NULL);
UpdateProviderPolicy(policies);
FlushBlacklistPolicy();
PrefService* prefs = browser()->profile()->GetPrefs();
const base::ListValue* list_url = prefs->GetList(policy_prefs::kUrlBlacklist);
EXPECT_EQ(list_url->Find(base::StringValue("file://*")),
list_url->end());
base::ListValue disabledscheme;
disabledscheme.Append(base::Value::CreateStringValue("file"));
policies.Set(key::kDisabledSchemes, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, disabledscheme.DeepCopy(), NULL);
UpdateProviderPolicy(policies);
FlushBlacklistPolicy();
list_url = prefs->GetList(policy_prefs::kUrlBlacklist);
EXPECT_NE(list_url->Find(base::StringValue("file://*")),
list_url->end());
base::ListValue whitelist;
whitelist.Append(base::Value::CreateStringValue(base_path));
policies.Set(key::kURLWhitelist, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, whitelist.DeepCopy(), NULL);
blacklist.Append(base::Value::CreateStringValue(folder_path));
policies.Set(key::kURLBlacklist, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, blacklist.DeepCopy(), NULL);
UpdateProviderPolicy(policies);
FlushBlacklistPolicy();
CheckCanOpenURL(browser(), file_path1.c_str());
CheckURLIsBlocked(browser(), file_path2.c_str());
}
#if !defined(OS_MACOSX)
IN_PROC_BROWSER_TEST_F(PolicyTest, FullscreenAllowedBrowser) {
PolicyMap policies;
policies.Set(key::kFullscreenAllowed,
POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
base::Value::CreateBooleanValue(false), NULL);
UpdateProviderPolicy(policies);
BrowserWindow* browser_window = browser()->window();
ASSERT_TRUE(browser_window);
EXPECT_FALSE(browser_window->IsFullscreen());
chrome::ToggleFullscreenMode(browser());
EXPECT_FALSE(browser_window->IsFullscreen());
}
IN_PROC_BROWSER_TEST_F(PolicyTest, FullscreenAllowedApp) {
PolicyMap policies;
policies.Set(key::kFullscreenAllowed,
POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
base::Value::CreateBooleanValue(false), NULL);
UpdateProviderPolicy(policies);
const extensions::Extension* extension =
LoadUnpackedExtension(kUnpackedFullscreenAppName, true);
ASSERT_TRUE(extension);
TestAddAppWindowObserver add_window_observer(
apps::AppWindowRegistry::Get(browser()->profile()));
OpenApplication(AppLaunchParams(browser()->profile(),
extension,
extensions::LAUNCH_CONTAINER_NONE,
NEW_WINDOW));
apps::AppWindow* window = add_window_observer.WaitForAppWindow();
ASSERT_TRUE(window);
EXPECT_FALSE(window->GetBaseWindow()->IsFullscreen());
EXPECT_TRUE(content::ExecuteScript(
window->web_contents(),
"chrome.app.window.current().fullscreen();"));
EXPECT_FALSE(window->GetBaseWindow()->IsFullscreen());
window->Fullscreen();
EXPECT_FALSE(window->GetBaseWindow()->IsFullscreen());
}
#endif
#if defined(OS_CHROMEOS)
IN_PROC_BROWSER_TEST_F(PolicyTest, DisableScreenshotsFile) {
int screenshot_count = CountScreenshots();
TestScreenshotFile(true);
ASSERT_EQ(CountScreenshots(), screenshot_count + 1);
TestScreenshotFile(false);
ASSERT_EQ(CountScreenshots(), screenshot_count + 1);
}
IN_PROC_BROWSER_TEST_F(PolicyTest, DisableAudioOutput) {
chromeos::CrasAudioHandler* audio_handler = chromeos::CrasAudioHandler::Get();
scoped_ptr<TestAudioObserver> test_observer(new TestAudioObserver);
audio_handler->AddAudioObserver(test_observer.get());
bool prior_state = audio_handler->IsOutputMuted();
audio_handler->SetOutputMute(false);
EXPECT_FALSE(audio_handler->IsOutputMuted());
EXPECT_EQ(1, test_observer->output_mute_changed_count());
PolicyMap policies;
policies.Set(key::kAudioOutputAllowed, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, base::Value::CreateBooleanValue(false), NULL);
UpdateProviderPolicy(policies);
EXPECT_TRUE(audio_handler->IsOutputMuted());
audio_handler->SetOutputMute(false);
EXPECT_TRUE(audio_handler->IsOutputMuted());
EXPECT_EQ(1, test_observer->output_mute_changed_count());
policies.Set(key::kAudioOutputAllowed, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, base::Value::CreateBooleanValue(true), NULL);
UpdateProviderPolicy(policies);
EXPECT_FALSE(audio_handler->IsOutputMuted());
EXPECT_EQ(1, test_observer->output_mute_changed_count());
audio_handler->SetOutputMute(true);
EXPECT_TRUE(audio_handler->IsOutputMuted());
EXPECT_EQ(2, test_observer->output_mute_changed_count());
audio_handler->SetOutputMute(prior_state);
audio_handler->RemoveAudioObserver(test_observer.get());
}
IN_PROC_BROWSER_TEST_F(PolicyTest, DISABLED_PRE_SessionLengthLimit) {
g_browser_process->local_state()->SetInt64(
prefs::kSessionStartTime,
(base::TimeTicks::Now() - base::TimeDelta::FromHours(2))
.ToInternalValue());
}
IN_PROC_BROWSER_TEST_F(PolicyTest, DISABLED_SessionLengthLimit) {
content::MockNotificationObserver observer;
content::NotificationRegistrar registrar;
registrar.Add(&observer,
chrome::NOTIFICATION_APP_TERMINATING,
content::NotificationService::AllSources());
EXPECT_CALL(observer, Observe(chrome::NOTIFICATION_APP_TERMINATING, _, _))
.Times(0);
PolicyMap policies;
policies.Set(key::kSessionLengthLimit, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER,
base::Value::CreateIntegerValue(kThreeHoursInMs),
NULL);
UpdateProviderPolicy(policies);
base::RunLoop().RunUntilIdle();
Mock::VerifyAndClearExpectations(&observer);
EXPECT_CALL(observer, Observe(chrome::NOTIFICATION_APP_TERMINATING, _, _));
policies.Set(key::kSessionLengthLimit, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER,
base::Value::CreateIntegerValue(kOneHourInMs),
NULL);
UpdateProviderPolicy(policies);
base::RunLoop().RunUntilIdle();
Mock::VerifyAndClearExpectations(&observer);
}
IN_PROC_BROWSER_TEST_F(PolicyTest,
DISABLED_PRE_WaitForInitialUserActivityUsatisfied) {
g_browser_process->local_state()->SetInt64(
prefs::kSessionStartTime,
(base::TimeTicks::Now() - base::TimeDelta::FromHours(2))
.ToInternalValue());
}
IN_PROC_BROWSER_TEST_F(PolicyTest,
DISABLED_WaitForInitialUserActivityUsatisfied) {
content::MockNotificationObserver observer;
content::NotificationRegistrar registrar;
registrar.Add(&observer,
chrome::NOTIFICATION_APP_TERMINATING,
content::NotificationService::AllSources());
PolicyMap policies;
policies.Set(key::kWaitForInitialUserActivity, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER,
new base::FundamentalValue(true),
NULL);
UpdateProviderPolicy(policies);
base::RunLoop().RunUntilIdle();
EXPECT_CALL(observer, Observe(chrome::NOTIFICATION_APP_TERMINATING, _, _))
.Times(0);
policies.Set(key::kSessionLengthLimit, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER,
base::Value::CreateIntegerValue(kOneHourInMs),
NULL);
UpdateProviderPolicy(policies);
base::RunLoop().RunUntilIdle();
Mock::VerifyAndClearExpectations(&observer);
}
IN_PROC_BROWSER_TEST_F(PolicyTest,
DISABLED_PRE_WaitForInitialUserActivitySatisfied) {
g_browser_process->local_state()->SetInt64(
prefs::kSessionStartTime,
(base::TimeTicks::Now() - base::TimeDelta::FromHours(2))
.ToInternalValue());
g_browser_process->local_state()->SetBoolean(
prefs::kSessionUserActivitySeen,
true);
}
IN_PROC_BROWSER_TEST_F(PolicyTest,
DISABLED_WaitForInitialUserActivitySatisfied) {
content::MockNotificationObserver observer;
content::NotificationRegistrar registrar;
registrar.Add(&observer,
chrome::NOTIFICATION_APP_TERMINATING,
content::NotificationService::AllSources());
EXPECT_CALL(observer, Observe(chrome::NOTIFICATION_APP_TERMINATING, _, _))
.Times(0);
PolicyMap policies;
policies.Set(key::kWaitForInitialUserActivity, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER,
new base::FundamentalValue(true),
NULL);
policies.Set(key::kSessionLengthLimit, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER,
base::Value::CreateIntegerValue(kThreeHoursInMs),
NULL);
UpdateProviderPolicy(policies);
base::RunLoop().RunUntilIdle();
Mock::VerifyAndClearExpectations(&observer);
EXPECT_CALL(observer, Observe(chrome::NOTIFICATION_APP_TERMINATING, _, _));
policies.Set(key::kSessionLengthLimit, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER,
base::Value::CreateIntegerValue(kOneHourInMs),
NULL);
UpdateProviderPolicy(policies);
base::RunLoop().RunUntilIdle();
Mock::VerifyAndClearExpectations(&observer);
}
IN_PROC_BROWSER_TEST_F(PolicyTest, LargeCursorEnabled) {
chromeos::AccessibilityManager* accessibility_manager =
chromeos::AccessibilityManager::Get();
accessibility_manager->EnableLargeCursor(true);
EXPECT_TRUE(accessibility_manager->IsLargeCursorEnabled());
PolicyMap policies;
policies.Set(key::kLargeCursorEnabled, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER,
base::Value::CreateBooleanValue(false), NULL);
UpdateProviderPolicy(policies);
EXPECT_FALSE(accessibility_manager->IsLargeCursorEnabled());
accessibility_manager->EnableLargeCursor(true);
EXPECT_FALSE(accessibility_manager->IsLargeCursorEnabled());
}
IN_PROC_BROWSER_TEST_F(PolicyTest, SpokenFeedbackEnabled) {
chromeos::AccessibilityManager* accessibility_manager =
chromeos::AccessibilityManager::Get();
accessibility_manager->EnableSpokenFeedback(
true, ash::A11Y_NOTIFICATION_NONE);
EXPECT_TRUE(accessibility_manager->IsSpokenFeedbackEnabled());
PolicyMap policies;
policies.Set(key::kSpokenFeedbackEnabled, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER,
base::Value::CreateBooleanValue(false), NULL);
UpdateProviderPolicy(policies);
EXPECT_FALSE(accessibility_manager->IsSpokenFeedbackEnabled());
accessibility_manager->EnableSpokenFeedback(
true, ash::A11Y_NOTIFICATION_NONE);
EXPECT_FALSE(accessibility_manager->IsSpokenFeedbackEnabled());
}
IN_PROC_BROWSER_TEST_F(PolicyTest, HighContrastEnabled) {
chromeos::AccessibilityManager* accessibility_manager =
chromeos::AccessibilityManager::Get();
accessibility_manager->EnableHighContrast(true);
EXPECT_TRUE(accessibility_manager->IsHighContrastEnabled());
PolicyMap policies;
policies.Set(key::kHighContrastEnabled, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER,
base::Value::CreateBooleanValue(false), NULL);
UpdateProviderPolicy(policies);
EXPECT_FALSE(accessibility_manager->IsHighContrastEnabled());
accessibility_manager->EnableHighContrast(true);
EXPECT_FALSE(accessibility_manager->IsHighContrastEnabled());
}
IN_PROC_BROWSER_TEST_F(PolicyTest, ScreenMagnifierTypeNone) {
chromeos::MagnificationManager* magnification_manager =
chromeos::MagnificationManager::Get();
magnification_manager->SetMagnifierType(ash::MAGNIFIER_FULL);
magnification_manager->SetMagnifierEnabled(true);
EXPECT_EQ(ash::MAGNIFIER_FULL, magnification_manager->GetMagnifierType());
EXPECT_TRUE(magnification_manager->IsMagnifierEnabled());
PolicyMap policies;
policies.Set(key::kScreenMagnifierType, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER,
base::Value::CreateIntegerValue(0), NULL);
UpdateProviderPolicy(policies);
EXPECT_FALSE(magnification_manager->IsMagnifierEnabled());
magnification_manager->SetMagnifierEnabled(true);
EXPECT_FALSE(magnification_manager->IsMagnifierEnabled());
}
IN_PROC_BROWSER_TEST_F(PolicyTest, ScreenMagnifierTypeFull) {
chromeos::MagnificationManager* magnification_manager =
chromeos::MagnificationManager::Get();
EXPECT_FALSE(magnification_manager->IsMagnifierEnabled());
PolicyMap policies;
policies.Set(key::kScreenMagnifierType, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER,
base::Value::CreateIntegerValue(ash::MAGNIFIER_FULL), NULL);
UpdateProviderPolicy(policies);
EXPECT_EQ(ash::MAGNIFIER_FULL, magnification_manager->GetMagnifierType());
EXPECT_TRUE(magnification_manager->IsMagnifierEnabled());
magnification_manager->SetMagnifierEnabled(false);
EXPECT_TRUE(magnification_manager->IsMagnifierEnabled());
}
IN_PROC_BROWSER_TEST_F(PolicyTest, VirtualKeyboardEnabled) {
chromeos::AccessibilityManager* accessibility_manager =
chromeos::AccessibilityManager::Get();
accessibility_manager->EnableVirtualKeyboard(true);
EXPECT_TRUE(accessibility_manager->IsVirtualKeyboardEnabled());
PolicyMap policies;
policies.Set(key::kVirtualKeyboardEnabled, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER,
base::Value::CreateBooleanValue(false), NULL);
UpdateProviderPolicy(policies);
EXPECT_FALSE(accessibility_manager->IsVirtualKeyboardEnabled());
accessibility_manager->EnableVirtualKeyboard(true);
EXPECT_FALSE(accessibility_manager->IsVirtualKeyboardEnabled());
}
#endif
namespace {
static const char* kRestoredURLs[] = {
"http://aaa.com/empty.html",
"http://bbb.com/empty.html",
};
bool IsNonSwitchArgument(const CommandLine::StringType& s) {
return s.empty() || s[0] != '-';
}
}
class RestoreOnStartupPolicyTest
: public PolicyTest,
public testing::WithParamInterface<
void (RestoreOnStartupPolicyTest::*)(void)> {
public:
RestoreOnStartupPolicyTest() {}
virtual ~RestoreOnStartupPolicyTest() {}
#if defined(OS_CHROMEOS)
virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
command_line->AppendSwitch(switches::kCreateBrowserOnStartupForTests);
PolicyTest::SetUpCommandLine(command_line);
}
#endif
virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
PolicyTest::SetUpInProcessBrowserTestFixture();
(this->*(GetParam()))();
CommandLine* command_line = CommandLine::ForCurrentProcess();
CommandLine::StringVector argv = command_line->argv();
argv.erase(std::remove_if(++argv.begin(), argv.end(), IsNonSwitchArgument),
argv.end());
command_line->InitFromArgv(argv);
ASSERT_TRUE(std::equal(argv.begin(), argv.end(),
command_line->argv().begin()));
RedirectHostsToTestData(kRestoredURLs, arraysize(kRestoredURLs));
}
void HomepageIsNotNTP() {
PolicyMap policies;
policies.Set(
key::kRestoreOnStartup, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
base::Value::CreateIntegerValue(
SessionStartupPref::kPrefValueHomePage),
NULL);
policies.Set(
key::kHomepageIsNewTabPage, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
base::Value::CreateBooleanValue(false), NULL);
policies.Set(
key::kHomepageLocation, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
base::Value::CreateStringValue(kRestoredURLs[1]), NULL);
provider_.UpdateChromePolicy(policies);
expected_urls_.push_back(GURL(kRestoredURLs[1]));
}
void HomepageIsNTP() {
PolicyMap policies;
policies.Set(
key::kRestoreOnStartup, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
base::Value::CreateIntegerValue(
SessionStartupPref::kPrefValueHomePage),
NULL);
policies.Set(
key::kHomepageIsNewTabPage, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
base::Value::CreateBooleanValue(true), NULL);
provider_.UpdateChromePolicy(policies);
expected_urls_.push_back(GURL(chrome::kChromeUINewTabURL));
}
void ListOfURLs() {
base::ListValue urls;
for (size_t i = 0; i < arraysize(kRestoredURLs); ++i) {
urls.Append(base::Value::CreateStringValue(kRestoredURLs[i]));
expected_urls_.push_back(GURL(kRestoredURLs[i]));
}
PolicyMap policies;
policies.Set(
key::kRestoreOnStartup, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
base::Value::CreateIntegerValue(SessionStartupPref::kPrefValueURLs),
NULL);
policies.Set(
key::kRestoreOnStartupURLs, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
urls.DeepCopy(), NULL);
provider_.UpdateChromePolicy(policies);
}
void NTP() {
PolicyMap policies;
policies.Set(
key::kRestoreOnStartup, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
base::Value::CreateIntegerValue(SessionStartupPref::kPrefValueNewTab),
NULL);
provider_.UpdateChromePolicy(policies);
expected_urls_.push_back(GURL(chrome::kChromeUINewTabURL));
}
void Last() {
PolicyMap policies;
policies.Set(
key::kRestoreOnStartup, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
base::Value::CreateIntegerValue(SessionStartupPref::kPrefValueLast),
NULL);
provider_.UpdateChromePolicy(policies);
for (size_t i = 0; i < arraysize(kRestoredURLs); ++i)
expected_urls_.push_back(GURL(kRestoredURLs[i]));
}
std::vector<GURL> expected_urls_;
};
IN_PROC_BROWSER_TEST_P(RestoreOnStartupPolicyTest, PRE_RunTest) {
ui_test_utils::NavigateToURL(browser(), GURL(kRestoredURLs[0]));
for (size_t i = 1; i < arraysize(kRestoredURLs); ++i) {
content::WindowedNotificationObserver observer(
content::NOTIFICATION_LOAD_STOP,
content::NotificationService::AllSources());
chrome::AddSelectedTabWithURL(browser(), GURL(kRestoredURLs[i]),
content::PAGE_TRANSITION_LINK);
observer.Wait();
}
}
IN_PROC_BROWSER_TEST_P(RestoreOnStartupPolicyTest, RunTest) {
#if defined(OS_WIN) && defined(USE_ASH)
if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
return;
#endif
TabStripModel* model = browser()->tab_strip_model();
int size = static_cast<int>(expected_urls_.size());
EXPECT_EQ(size, model->count());
for (int i = 0; i < size && i < model->count(); ++i) {
EXPECT_EQ(expected_urls_[i], model->GetWebContentsAt(i)->GetURL());
}
}
INSTANTIATE_TEST_CASE_P(
RestoreOnStartupPolicyTestInstance,
RestoreOnStartupPolicyTest,
testing::Values(&RestoreOnStartupPolicyTest::HomepageIsNotNTP,
&RestoreOnStartupPolicyTest::HomepageIsNTP,
&RestoreOnStartupPolicyTest::ListOfURLs,
&RestoreOnStartupPolicyTest::NTP,
&RestoreOnStartupPolicyTest::Last));
class PolicyStatisticsCollectorTest : public PolicyTest {
public:
PolicyStatisticsCollectorTest() {}
virtual ~PolicyStatisticsCollectorTest() {}
virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
PolicyTest::SetUpInProcessBrowserTestFixture();
PolicyMap policies;
policies.Set(
key::kShowHomeButton, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
base::Value::CreateBooleanValue(true), NULL);
policies.Set(
key::kBookmarkBarEnabled, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
base::Value::CreateBooleanValue(false), NULL);
policies.Set(
key::kHomepageLocation, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
base::Value::CreateStringValue("http://chromium.org"), NULL);
provider_.UpdateChromePolicy(policies);
}
};
IN_PROC_BROWSER_TEST_F(PolicyStatisticsCollectorTest, Startup) {
content::RunAllPendingInMessageLoop();
GURL kAboutHistograms = GURL(std::string(content::kAboutScheme) +
std::string(content::kStandardSchemeSeparator) +
std::string(content::kChromeUIHistogramHost));
ui_test_utils::NavigateToURL(browser(), kAboutHistograms);
content::WebContents* contents =
browser()->tab_strip_model()->GetActiveWebContents();
std::string text;
ASSERT_TRUE(content::ExecuteScriptAndExtractString(
contents,
"var nodes = document.querySelectorAll('body > pre');"
"var result = '';"
"for (var i = 0; i < nodes.length; ++i) {"
" var text = nodes[i].innerHTML;"
" if (text.indexOf('Histogram: Enterprise.Policies') === 0) {"
" result = text;"
" break;"
" }"
"}"
"domAutomationController.send(result);",
&text));
ASSERT_FALSE(text.empty());
const std::string kExpectedLabel =
"Histogram: Enterprise.Policies recorded 3 samples";
EXPECT_EQ(kExpectedLabel, text.substr(0, kExpectedLabel.size()));
EXPECT_NE(std::string::npos, text.find("<br>1 ---"));
EXPECT_NE(std::string::npos, text.find("<br>35 ---"));
EXPECT_NE(std::string::npos, text.find("<br>82 ---"));
}
class MediaStreamDevicesControllerBrowserTest
: public PolicyTest,
public testing::WithParamInterface<bool> {
public:
MediaStreamDevicesControllerBrowserTest()
: request_url_allowed_via_whitelist_(false) {
policy_value_ = GetParam();
}
virtual ~MediaStreamDevicesControllerBrowserTest() {}
void ConfigurePolicyMap(PolicyMap* policies, const char* policy_name,
const char* whitelist_policy,
const char* allow_rule) {
policies->Set(policy_name, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
base::Value::CreateBooleanValue(policy_value_), NULL);
if (whitelist_policy) {
CommandLine::ForCurrentProcess()->AppendSwitch(switches::kKioskMode);
request_url_ = GURL("http://www.example.com/foo");
base::ListValue* list = new base::ListValue();
if (allow_rule) {
list->AppendString(allow_rule);
request_url_allowed_via_whitelist_ = true;
} else {
list->AppendString(ContentSettingsPattern::Wildcard().ToString());
request_url_allowed_via_whitelist_ = false;
}
policies->Set(whitelist_policy, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, list, NULL);
}
}
void Accept(const content::MediaStreamDevices& devices,
content::MediaStreamRequestResult result,
scoped_ptr<content::MediaStreamUI> ui) {
if (policy_value_ || request_url_allowed_via_whitelist_) {
ASSERT_EQ(1U, devices.size());
ASSERT_EQ("fake_dev", devices[0].id);
} else {
ASSERT_EQ(0U, devices.size());
}
}
void FinishAudioTest() {
content::MediaStreamRequest request(0, 0, 0,
request_url_.GetOrigin(), false,
content::MEDIA_DEVICE_ACCESS,
std::string(), std::string(),
content::MEDIA_DEVICE_AUDIO_CAPTURE,
content::MEDIA_NO_SERVICE);
MediaStreamDevicesController controller(
browser()->tab_strip_model()->GetActiveWebContents(), request,
base::Bind(&MediaStreamDevicesControllerBrowserTest::Accept, this));
controller.Accept(false);
base::MessageLoop::current()->QuitWhenIdle();
}
void FinishVideoTest() {
content::MediaStreamRequest request(0, 0, 0,
request_url_.GetOrigin(), false,
content::MEDIA_DEVICE_ACCESS,
std::string(),
std::string(),
content::MEDIA_NO_SERVICE,
content::MEDIA_DEVICE_VIDEO_CAPTURE);
MediaStreamDevicesController controller(
browser()->tab_strip_model()->GetActiveWebContents(), request,
base::Bind(&MediaStreamDevicesControllerBrowserTest::Accept, this));
controller.Accept(false);
base::MessageLoop::current()->QuitWhenIdle();
}
bool policy_value_;
bool request_url_allowed_via_whitelist_;
GURL request_url_;
static const char kExampleRequestPattern[];
};
const char MediaStreamDevicesControllerBrowserTest::kExampleRequestPattern[] =
"http://[*.]example.com/";
IN_PROC_BROWSER_TEST_P(MediaStreamDevicesControllerBrowserTest,
AudioCaptureAllowed) {
content::MediaStreamDevices audio_devices;
content::MediaStreamDevice fake_audio_device(
content::MEDIA_DEVICE_AUDIO_CAPTURE, "fake_dev", "Fake Audio Device");
audio_devices.push_back(fake_audio_device);
PolicyMap policies;
ConfigurePolicyMap(&policies, key::kAudioCaptureAllowed, NULL, NULL);
UpdateProviderPolicy(policies);
content::BrowserThread::PostTaskAndReply(
content::BrowserThread::IO, FROM_HERE,
base::Bind(&MediaCaptureDevicesDispatcher::SetTestAudioCaptureDevices,
base::Unretained(MediaCaptureDevicesDispatcher::GetInstance()),
audio_devices),
base::Bind(&MediaStreamDevicesControllerBrowserTest::FinishAudioTest,
this));
base::MessageLoop::current()->Run();
}
IN_PROC_BROWSER_TEST_P(MediaStreamDevicesControllerBrowserTest,
AudioCaptureAllowedUrls) {
content::MediaStreamDevices audio_devices;
content::MediaStreamDevice fake_audio_device(
content::MEDIA_DEVICE_AUDIO_CAPTURE, "fake_dev", "Fake Audio Device");
audio_devices.push_back(fake_audio_device);
const char* allow_pattern[] = {
kExampleRequestPattern,
NULL,
};
for (size_t i = 0; i < arraysize(allow_pattern); ++i) {
PolicyMap policies;
ConfigurePolicyMap(&policies, key::kAudioCaptureAllowed,
key::kAudioCaptureAllowedUrls, allow_pattern[i]);
UpdateProviderPolicy(policies);
content::BrowserThread::PostTaskAndReply(
content::BrowserThread::IO, FROM_HERE,
base::Bind(
&MediaCaptureDevicesDispatcher::SetTestAudioCaptureDevices,
base::Unretained(MediaCaptureDevicesDispatcher::GetInstance()),
audio_devices),
base::Bind(
&MediaStreamDevicesControllerBrowserTest::FinishAudioTest,
this));
base::MessageLoop::current()->Run();
}
}
IN_PROC_BROWSER_TEST_P(MediaStreamDevicesControllerBrowserTest,
VideoCaptureAllowed) {
content::MediaStreamDevices video_devices;
content::MediaStreamDevice fake_video_device(
content::MEDIA_DEVICE_VIDEO_CAPTURE, "fake_dev", "Fake Video Device");
video_devices.push_back(fake_video_device);
PolicyMap policies;
ConfigurePolicyMap(&policies, key::kVideoCaptureAllowed, NULL, NULL);
UpdateProviderPolicy(policies);
content::BrowserThread::PostTaskAndReply(
content::BrowserThread::IO, FROM_HERE,
base::Bind(&MediaCaptureDevicesDispatcher::SetTestVideoCaptureDevices,
base::Unretained(MediaCaptureDevicesDispatcher::GetInstance()),
video_devices),
base::Bind(&MediaStreamDevicesControllerBrowserTest::FinishVideoTest,
this));
base::MessageLoop::current()->Run();
}
IN_PROC_BROWSER_TEST_P(MediaStreamDevicesControllerBrowserTest,
VideoCaptureAllowedUrls) {
content::MediaStreamDevices video_devices;
content::MediaStreamDevice fake_video_device(
content::MEDIA_DEVICE_VIDEO_CAPTURE, "fake_dev", "Fake Video Device");
video_devices.push_back(fake_video_device);
const char* allow_pattern[] = {
kExampleRequestPattern,
NULL,
};
for (size_t i = 0; i < arraysize(allow_pattern); ++i) {
PolicyMap policies;
ConfigurePolicyMap(&policies, key::kVideoCaptureAllowed,
key::kVideoCaptureAllowedUrls, allow_pattern[i]);
UpdateProviderPolicy(policies);
content::BrowserThread::PostTaskAndReply(
content::BrowserThread::IO, FROM_HERE,
base::Bind(&MediaCaptureDevicesDispatcher::SetTestVideoCaptureDevices,
base::Unretained(MediaCaptureDevicesDispatcher::GetInstance()),
video_devices),
base::Bind(
&MediaStreamDevicesControllerBrowserTest::FinishVideoTest,
this));
base::MessageLoop::current()->Run();
}
}
INSTANTIATE_TEST_CASE_P(MediaStreamDevicesControllerBrowserTestInstance,
MediaStreamDevicesControllerBrowserTest,
testing::Bool());
#if !defined(OS_CHROMEOS)
class PolicyVariationsServiceTest : public PolicyTest {
public:
virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
PolicyTest::SetUpInProcessBrowserTestFixture();
PolicyMap policies;
policies.Set(
key::kVariationsRestrictParameter,
POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER,
base::Value::CreateStringValue("restricted"),
NULL);
provider_.UpdateChromePolicy(policies);
}
};
IN_PROC_BROWSER_TEST_F(PolicyVariationsServiceTest, VariationsURLIsValid) {
const std::string default_variations_url =
chrome_variations::VariationsService::
GetDefaultVariationsServerURLForTesting();
const GURL url =
chrome_variations::VariationsService::GetVariationsServerURL(
g_browser_process->local_state());
EXPECT_TRUE(StartsWithASCII(url.spec(), default_variations_url, true));
std::string value;
EXPECT_TRUE(net::GetValueForKeyInQuery(url, "restrict", &value));
EXPECT_EQ("restricted", value);
}
IN_PROC_BROWSER_TEST_F(PolicyTest, NativeMessagingBlacklistSelective) {
base::ListValue blacklist;
blacklist.Append(base::Value::CreateStringValue("host.name"));
PolicyMap policies;
policies.Set(key::kNativeMessagingBlacklist, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, blacklist.DeepCopy(), NULL);
UpdateProviderPolicy(policies);
PrefService* prefs = browser()->profile()->GetPrefs();
EXPECT_FALSE(extensions::NativeMessageProcessHost::IsHostAllowed(
prefs, "host.name"));
EXPECT_TRUE(extensions::NativeMessageProcessHost::IsHostAllowed(
prefs, "other.host.name"));
}
IN_PROC_BROWSER_TEST_F(PolicyTest, NativeMessagingBlacklistWildcard) {
base::ListValue blacklist;
blacklist.Append(base::Value::CreateStringValue("*"));
PolicyMap policies;
policies.Set(key::kNativeMessagingBlacklist, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, blacklist.DeepCopy(), NULL);
UpdateProviderPolicy(policies);
PrefService* prefs = browser()->profile()->GetPrefs();
EXPECT_FALSE(extensions::NativeMessageProcessHost::IsHostAllowed(
prefs, "host.name"));
EXPECT_FALSE(extensions::NativeMessageProcessHost::IsHostAllowed(
prefs, "other.host.name"));
}
IN_PROC_BROWSER_TEST_F(PolicyTest, NativeMessagingWhitelist) {
base::ListValue blacklist;
blacklist.Append(base::Value::CreateStringValue("*"));
base::ListValue whitelist;
whitelist.Append(base::Value::CreateStringValue("host.name"));
PolicyMap policies;
policies.Set(key::kNativeMessagingBlacklist, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, blacklist.DeepCopy(), NULL);
policies.Set(key::kNativeMessagingWhitelist, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, whitelist.DeepCopy(), NULL);
UpdateProviderPolicy(policies);
PrefService* prefs = browser()->profile()->GetPrefs();
EXPECT_TRUE(extensions::NativeMessageProcessHost::IsHostAllowed(
prefs, "host.name"));
EXPECT_FALSE(extensions::NativeMessageProcessHost::IsHostAllowed(
prefs, "other.host.name"));
}
#endif
}