15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <algorithm>
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector>
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/bind.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/bind_helpers.h"
117dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "base/callback.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/command_line.h"
13868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/files/file_enumerator.h"
142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/files/file_path.h"
151320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "base/files/file_util.h"
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/files/scoped_temp_dir.h"
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/ref_counted.h"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/path_service.h"
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/prefs/pref_service.h"
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/run_loop.h"
21868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/string16.h"
22868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/string_util.h"
23868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/stringprintf.h"
24868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/utf_string_conversions.h"
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/test/test_file_util.h"
261320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "base/threading/sequenced_worker_pool.h"
27eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "base/time/time.h"
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/values.h"
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/app/chrome_command_ids.h"
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/autocomplete/autocomplete_controller.h"
3158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "chrome/browser/background/background_contents_service.h"
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/browser_process.h"
337dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "chrome/browser/chrome_notification_types.h"
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/content_settings/tab_specific_content_settings.h"
35116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "chrome/browser/devtools/devtools_window_testing.h"
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/download/download_prefs.h"
375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/extensions/api/messaging/native_message_process_host.h"
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/extensions/crx_installer.h"
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/extensions/extension_service.h"
4090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "chrome/browser/extensions/unpacked_installer.h"
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/extensions/updater/extension_cache_fake.h"
4290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "chrome/browser/extensions/updater/extension_updater.h"
432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/infobars/infobar_service.h"
442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/media/media_capture_devices_dispatcher.h"
452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/media/media_stream_devices_controller.h"
46c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/browser/metrics/variations/variations_service.h"
475f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "chrome/browser/net/prediction_options.h"
481675a649fd7a8b3cb80ffddae2dc181f122353c5Ben Murdoch#include "chrome/browser/net/ssl_config_service_manager.h"
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/net/url_request_mock_util.h"
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/plugins/plugin_prefs.h"
5190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "chrome/browser/policy/cloud/test_request_interceptor.h"
52f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "chrome/browser/policy/profile_policy_connector.h"
53f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "chrome/browser/policy/profile_policy_connector_factory.h"
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/prefs/session_startup_pref.h"
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/profiles/profile.h"
56a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)#include "chrome/browser/search/instant_service.h"
57a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)#include "chrome/browser/search/instant_service_factory.h"
582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/search/search.h"
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/search_engines/template_url_service_factory.h"
6046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "chrome/browser/translate/chrome_translate_client.h"
61116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "chrome/browser/translate/cld_data_harness.h"
625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/translate/translate_service.h"
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/bookmarks/bookmark_bar.h"
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/browser.h"
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/browser_commands.h"
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/browser_list.h"
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/browser_tabstrip.h"
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/browser_window.h"
692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/host_desktop.h"
70116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "chrome/browser/ui/location_bar/location_bar.h"
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/omnibox/omnibox_edit_model.h"
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/omnibox/omnibox_view.h"
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/tabs/tab_strip_model.h"
742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/common/chrome_paths.h"
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/chrome_switches.h"
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/extensions/extension_constants.h"
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/pref_names.h"
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/url_constants.h"
7903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)#include "chrome/grit/generated_resources.h"
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/test/base/in_process_browser_test.h"
81a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)#include "chrome/test/base/test_switches.h"
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/test/base/ui_test_utils.h"
831320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "components/content_settings/core/common/content_settings.h"
841320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "components/content_settings/core/common/content_settings_pattern.h"
850529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "components/infobars/core/infobar.h"
865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "components/policy/core/browser/browser_policy_connector.h"
87f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "components/policy/core/common/external_data_fetcher.h"
88a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "components/policy/core/common/mock_configuration_policy_provider.h"
89f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "components/policy/core/common/policy_map.h"
905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "components/policy/core/common/policy_pref_names.h"
91a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "components/policy/core/common/policy_service.h"
92a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "components/policy/core/common/policy_service_impl.h"
93116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "components/search_engines/template_url.h"
94116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "components/search_engines/template_url_service.h"
95f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "components/translate/core/browser/language_state.h"
9646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "components/translate/core/browser/translate_infobar_delegate.h"
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/browser_child_process_host_iterator.h"
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/browser_context.h"
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/browser_thread.h"
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/child_process_data.h"
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/download_item.h"
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/download_manager.h"
103a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "content/public/browser/gpu_data_manager.h"
10458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "content/public/browser/notification_details.h"
10558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "content/public/browser/notification_observer.h"
106c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "content/public/browser/notification_registrar.h"
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/notification_service.h"
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/notification_source.h"
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/notification_types.h"
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/plugin_service.h"
1112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/render_process_host.h"
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/render_view_host.h"
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/web_contents.h"
114ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch#include "content/public/common/content_constants.h"
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/common/content_paths.h"
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/common/process_type.h"
11758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "content/public/common/result_codes.h"
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/common/url_constants.h"
119ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch#include "content/public/common/webplugininfo.h"
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/test/browser_test_utils.h"
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/test/download_test_observer.h"
122c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "content/public/test/mock_notification_observer.h"
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/test/test_navigation_observer.h"
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/test/test_utils.h"
12523730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)#include "extensions/browser/extension_host.h"
1265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "extensions/browser/extension_system.h"
127f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "extensions/browser/process_manager.h"
1285f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "extensions/browser/uninstall_reason.h"
129f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "extensions/common/constants.h"
130f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "extensions/common/extension.h"
1315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "extensions/common/extension_set.h"
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/net_errors.h"
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/net_util.h"
134c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "net/base/url_util.h"
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/http/http_stream_factory.h"
1361675a649fd7a8b3cb80ffddae2dc181f122353c5Ben Murdoch#include "net/ssl/ssl_config.h"
1371675a649fd7a8b3cb80ffddae2dc181f122353c5Ben Murdoch#include "net/ssl/ssl_config_service.h"
1381320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "net/test/url_request/url_request_failed_job.h"
1391320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "net/test/url_request/url_request_mock_http_job.h"
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/url_request/url_request.h"
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/url_request/url_request_filter.h"
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "policy/policy_constants.h"
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "testing/gmock/include/gmock/gmock.h"
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
1457d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "third_party/WebKit/public/web/WebInputEvent.h"
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/base/l10n/l10n_util.h"
1471320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "ui/base/page_transition_types.h"
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/base/resource/resource_bundle.h"
149eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "url/gurl.h"
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_CHROMEOS)
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/accelerators/accelerator_controller.h"
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/accelerators/accelerator_table.h"
154868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "ash/magnifier/magnifier_constants.h"
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/shell.h"
156868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "chrome/browser/chromeos/accessibility/accessibility_manager.h"
157868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "chrome/browser/chromeos/accessibility/magnification_manager.h"
1585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/profiles/profile_manager.h"
1595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/ui/ash/screenshot_taker.h"
160a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)#include "chromeos/audio/cras_audio_handler.h"
1616d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)#include "ui/keyboard/keyboard_util.h"
162ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch#endif
163ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
164d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#if !defined(OS_MACOSX)
165d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "base/basictypes.h"
166d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "base/compiler_specific.h"
167d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "chrome/browser/ui/extensions/application_launch.h"
1681320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "extensions/browser/app_window/app_window.h"
1691320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "extensions/browser/app_window/app_window_registry.h"
17003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)#include "extensions/browser/app_window/native_app_window.h"
171d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "ui/base/window_open_disposition.h"
172d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#endif
173d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using content::BrowserThread;
1751320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciusing net::URLRequestMockHTTPJob;
176c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)using testing::Mock;
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using testing::Return;
1782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)using testing::_;
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace policy {
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace {
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1841e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#if defined(OS_CHROMEOS)
1851e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)const int kOneHourInMs = 60 * 60 * 1000;
1861e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)const int kThreeHoursInMs = 180 * 60 * 1000;
1871e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#endif
1881e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const char kURL[] = "http://example.com";
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const char kCookieValue[] = "converted=true";
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Assigned to Philip J. Fry to fix eventually.
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const char kCookieOptions[] = ";expires=Wed Jan 01 3000 00:00:00 GMT";
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)const base::FilePath::CharType kTestExtensionsDir[] =
1952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    FILE_PATH_LITERAL("extensions");
1962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)const base::FilePath::CharType kGoodCrxName[] = FILE_PATH_LITERAL("good.crx");
1972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)const base::FilePath::CharType kAdBlockCrxName[] =
1982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    FILE_PATH_LITERAL("adblock.crx");
1992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)const base::FilePath::CharType kHostedAppCrxName[] =
2002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    FILE_PATH_LITERAL("hosted_app.crx");
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const char kGoodCrxId[] = "ldnnhddmnhbkjipkidpdiheffobcpfmf";
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const char kAdBlockCrxId[] = "dojnnbeimaimaojcialkkgajdnefpgcn";
2042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)const char kHostedAppCrxId[] = "kbmnembihfiondgfjekmnmcbddelicoi";
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
20690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)const base::FilePath::CharType kGood2CrxManifestName[] =
20790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    FILE_PATH_LITERAL("good2_update_manifest.xml");
20890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)const base::FilePath::CharType kGoodV1CrxManifestName[] =
20990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    FILE_PATH_LITERAL("good_v1_update_manifest.xml");
21090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)const base::FilePath::CharType kGoodUnpackedExt[] =
21190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    FILE_PATH_LITERAL("good_unpacked");
21290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)const base::FilePath::CharType kAppUnpackedExt[] =
21390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    FILE_PATH_LITERAL("app");
21468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
21568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)#if !defined(OS_MACOSX)
216d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)const base::FilePath::CharType kUnpackedFullscreenAppName[] =
217d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    FILE_PATH_LITERAL("fullscreen_app");
21868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)#endif  // !defined(OS_MACOSX)
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Filters requests to the hosts in |urls| and redirects them to the test data
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// dir through URLRequestMockHTTPJobs.
2222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void RedirectHostsToTestData(const char* const urls[], size_t size) {
2232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Map the given hosts to the test data dir.
2242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance();
2252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath base_path;
2262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  PathService::Get(chrome::DIR_TEST_DATA, &base_path);
2272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (size_t i = 0; i < size; ++i) {
2282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const GURL url(urls[i]);
2292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    EXPECT_TRUE(url.is_valid());
2301320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    filter->AddUrlInterceptor(url,
2311320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                              URLRequestMockHTTPJob::CreateInterceptor(
2321320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                  base_path, BrowserThread::GetBlockingPool()));
2332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
2342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Remove filters for requests to the hosts in |urls|.
2372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void UndoRedirectHostsToTestData(const char* const urls[], size_t size) {
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Map the given hosts to the test data dir.
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance();
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (size_t i = 0; i < size; ++i) {
2412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const GURL url(urls[i]);
2422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    EXPECT_TRUE(url.is_valid());
2432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    filter->RemoveUrlHandler(url);
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Fails requests using ERR_CONNECTION_RESET.
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)net::URLRequestJob* FailedJobFactory(
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    net::URLRequest* request,
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    net::NetworkDelegate* network_delegate,
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::string& scheme) {
2521320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  return new net::URLRequestFailedJob(
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      request, network_delegate, net::ERR_CONNECTION_RESET);
2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// While |MakeRequestFail| is in scope URLRequests to |host| will fail.
2572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class MakeRequestFail {
2582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) public:
2592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Sets up the filter on IO thread such that requests to |host| fail.
2602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  explicit MakeRequestFail(const std::string& host) : host_(host) {
2612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    BrowserThread::PostTaskAndReply(
2622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        BrowserThread::IO, FROM_HERE,
2632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        base::Bind(MakeRequestFailOnIO, host_),
26490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        base::MessageLoop::QuitClosure());
2652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    content::RunMessageLoop();
2662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
2672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ~MakeRequestFail() {
2682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    BrowserThread::PostTaskAndReply(
2692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        BrowserThread::IO, FROM_HERE,
2702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        base::Bind(UndoMakeRequestFailOnIO, host_),
27190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        base::MessageLoop::QuitClosure());
2722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    content::RunMessageLoop();
2732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) private:
2762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Filters requests to the |host| such that they fail. Run on IO thread.
2772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static void MakeRequestFailOnIO(const std::string& host) {
2782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance();
2792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    filter->AddHostnameHandler("http", host, &FailedJobFactory);
2802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    filter->AddHostnameHandler("https", host, &FailedJobFactory);
2812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
2822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Remove filters for requests to the |host|. Run on IO thread.
2842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static void UndoMakeRequestFailOnIO(const std::string& host) {
2852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance();
2862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    filter->RemoveHostnameHandler("http", host);
2872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    filter->RemoveHostnameHandler("https", host);
2882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
2895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const std::string host_;
2912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)};
2922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Verifies that the given url |spec| can be opened. This assumes that |spec|
2942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// points at empty.html in the test data dir.
2952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void CheckCanOpenURL(Browser* browser, const char* spec) {
2962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  GURL url(spec);
2975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ui_test_utils::NavigateToURL(browser, url);
2982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  content::WebContents* contents =
2992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      browser->tab_strip_model()->GetActiveWebContents();
3001320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  ASSERT_EQ(url, contents->GetURL());
3011320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  base::string16 spec16 = base::UTF8ToUTF16(url.spec());
3021320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  base::string16 title =
3031320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      l10n_util::GetStringFUTF16(IDS_ERRORPAGES_TITLE_BLOCKED, spec16);
3041320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  ASSERT_NE(title, contents->GetTitle());
3055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Verifies that access to the given url |spec| is blocked.
3082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void CheckURLIsBlocked(Browser* browser, const char* spec) {
3092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  GURL url(spec);
3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ui_test_utils::NavigateToURL(browser, url);
3112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  content::WebContents* contents =
3122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      browser->tab_strip_model()->GetActiveWebContents();
3131320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  ASSERT_EQ(url, contents->GetURL());
3141320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  base::string16 spec16 = base::UTF8ToUTF16(url.spec());
3151320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  base::string16 title =
3161320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      l10n_util::GetStringFUTF16(IDS_ERRORPAGES_TITLE_BLOCKED, spec16);
3171320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  ASSERT_EQ(title, contents->GetTitle());
3185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Verify that the expected error page is being displayed.
3205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool result = false;
3211320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
3222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      contents,
3232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      "var textContent = document.body.textContent;"
3242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      "var hasError = textContent.indexOf('ERR_BLOCKED_BY_ADMINISTRATOR') >= 0;"
3252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      "domAutomationController.send(hasError);",
3265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      &result));
3271320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  ASSERT_TRUE(result);
3285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Downloads a file named |file| and expects it to be saved to |dir|, which
3315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// must be empty.
3325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void DownloadAndVerifyFile(
3332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    Browser* browser, const base::FilePath& dir, const base::FilePath& file) {
3345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  content::DownloadManager* download_manager =
3355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      content::BrowserContext::GetDownloadManager(browser->profile());
3365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  content::DownloadTestObserverTerminal observer(
3375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      download_manager, 1,
3385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL);
3395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  GURL url(URLRequestMockHTTPJob::GetMockUrl(file));
3402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath downloaded = dir.Append(file);
3417dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  EXPECT_FALSE(base::PathExists(downloaded));
3425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ui_test_utils::NavigateToURLWithDisposition(
3435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      browser, url, CURRENT_TAB,
3445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
3455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  observer.WaitForFinished();
3465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(
3475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      1u, observer.NumDownloadsSeenInState(content::DownloadItem::COMPLETE));
3487dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  EXPECT_TRUE(base::PathExists(downloaded));
349868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  base::FileEnumerator enumerator(dir, false, base::FileEnumerator::FILES);
3505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(file, enumerator.Next().BaseName());
3512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(base::FilePath(), enumerator.Next());
3525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
35423730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)#if defined(OS_CHROMEOS)
3555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int CountScreenshots() {
3565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DownloadPrefs* download_prefs = DownloadPrefs::FromBrowserContext(
3575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      ProfileManager::GetActiveUserProfile());
358868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  base::FileEnumerator enumerator(download_prefs->DownloadPath(),
359868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                  false, base::FileEnumerator::FILES,
360868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                  "Screenshot*");
3615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int count = 0;
3625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  while (!enumerator.Next().empty())
3635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    count++;
3645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return count;
3655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
3675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Checks if WebGL is enabled in the given WebContents.
3695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool IsWebGLEnabled(content::WebContents* contents) {
3705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool result = false;
3712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
3722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      contents,
3732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      "var canvas = document.createElement('canvas');"
374f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      "var context = canvas.getContext('webgl');"
3752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      "domAutomationController.send(context != null);",
3765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      &result));
3775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return result;
3785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool IsJavascriptEnabled(content::WebContents* contents) {
3812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<base::Value> value = content::ExecuteScriptAndGetValue(
382a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      contents->GetMainFrame(), "123");
3835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int result = 0;
3845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!value->GetAsInteger(&result))
3855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_EQ(base::Value::TYPE_NULL, value->GetType());
3865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return result == 123;
3875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3895f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)bool IsNetworkPredictionEnabled(PrefService* prefs) {
3906e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  return chrome_browser_net::CanPrefetchAndPrerenderUI(prefs);
3915f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
3925f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
393ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdochvoid CopyPluginListAndQuit(std::vector<content::WebPluginInfo>* out,
394ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch                           const std::vector<content::WebPluginInfo>& in) {
3955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  *out = in;
39690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  base::MessageLoop::current()->QuitWhenIdle();
3975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)template<typename T>
4005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void CopyValueAndQuit(T* out, T in) {
4015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  *out = in;
40290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  base::MessageLoop::current()->QuitWhenIdle();
4035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
405ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdochvoid GetPluginList(std::vector<content::WebPluginInfo>* plugins) {
4065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  content::PluginService* service = content::PluginService::GetInstance();
4075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  service->GetPlugins(base::Bind(CopyPluginListAndQuit, plugins));
4085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  content::RunMessageLoop();
4095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
411ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdochconst content::WebPluginInfo* GetFlashPlugin(
412ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    const std::vector<content::WebPluginInfo>& plugins) {
413ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  const content::WebPluginInfo* flash = NULL;
4145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (size_t i = 0; i < plugins.size(); ++i) {
4155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (plugins[i].name == base::ASCIIToUTF16(content::kFlashPluginName)) {
4165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      flash = &plugins[i];
4175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
4185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
4195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OFFICIAL_BUILD)
4215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Official builds bundle Flash.
4225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(flash);
4235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#else
4245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!flash)
4255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LOG(INFO) << "Test skipped because the Flash plugin couldn't be found.";
4265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
4275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return flash;
4285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool SetPluginEnabled(PluginPrefs* plugin_prefs,
431ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch                      const content::WebPluginInfo* plugin,
4325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                      bool enabled) {
4335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool ok = false;
4345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  plugin_prefs->EnablePlugin(enabled, plugin->path,
4355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             base::Bind(CopyValueAndQuit<bool>, &ok));
4365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  content::RunMessageLoop();
4375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return ok;
4385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int CountPluginsOnIOThread() {
4415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int count = 0;
4425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (content::BrowserChildProcessHostIterator iter; !iter.Done(); ++iter) {
4432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (iter.GetData().process_type == content::PROCESS_TYPE_PLUGIN ||
4442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        iter.GetData().process_type == content::PROCESS_TYPE_PPAPI_PLUGIN) {
4455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      count++;
4465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
4475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return count;
4495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int CountPlugins() {
4525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int count = -1;
4535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  BrowserThread::PostTaskAndReplyWithResult(
4545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      BrowserThread::IO, FROM_HERE,
4555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      base::Bind(CountPluginsOnIOThread),
4565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      base::Bind(CopyValueAndQuit<int>, &count));
4575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  content::RunMessageLoop();
4585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_GE(count, 0);
4595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return count;
4605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void FlushBlacklistPolicy() {
4635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Updates of the URLBlacklist are done on IO, after building the blacklist
4641320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // on the blocking pool, which is initiated from IO.
4655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  content::RunAllPendingInMessageLoop(BrowserThread::IO);
4661320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  BrowserThread::GetBlockingPool()->FlushForTesting();
4675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  content::RunAllPendingInMessageLoop(BrowserThread::IO);
4685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool ContainsVisibleElement(content::WebContents* contents,
4712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                            const std::string& id) {
4722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool result;
4732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
4742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      contents,
4752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      "var elem = document.getElementById('" + id + "');"
476c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      "domAutomationController.send(!!elem && !elem.hidden);",
4772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      &result));
4782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return result;
4792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
4802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_CHROMEOS)
482a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)class TestAudioObserver : public chromeos::CrasAudioHandler::AudioObserver {
4835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
484a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  TestAudioObserver() : output_mute_changed_count_(0) {
485a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  }
4865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
487a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  int output_mute_changed_count() const {
488a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    return output_mute_changed_count_;
489a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  }
490a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
491a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  virtual ~TestAudioObserver() {}
492a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
493a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) protected:
494a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // chromeos::CrasAudioHandler::AudioObserver overrides.
495a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  virtual void OnOutputMuteChanged() OVERRIDE {
496a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    ++output_mute_changed_count_;
497a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  }
4985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
500a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  int output_mute_changed_count_;
501a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
502a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(TestAudioObserver);
5035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
5045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
5055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5068bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// This class waits until either a load stops or the WebContents is destroyed.
5078bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)class WebContentsLoadedOrDestroyedWatcher
5088bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    : public content::WebContentsObserver {
50958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) public:
5108bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  explicit WebContentsLoadedOrDestroyedWatcher(
5118bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      content::WebContents* web_contents);
5128bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  virtual ~WebContentsLoadedOrDestroyedWatcher();
51358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
5148bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Waits until the WebContents's load is done or until it is destroyed.
51558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  void Wait();
51658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
5178bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Overridden WebContentsObserver methods.
518010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  virtual void WebContentsDestroyed() OVERRIDE;
5198bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  virtual void DidStopLoading(
5208bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      content::RenderViewHost* render_view_host) OVERRIDE;
52158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
52258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) private:
52358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
52458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
5258bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(WebContentsLoadedOrDestroyedWatcher);
52658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)};
52758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
5288bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)WebContentsLoadedOrDestroyedWatcher::WebContentsLoadedOrDestroyedWatcher(
5298bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    content::WebContents* web_contents)
5308bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    : content::WebContentsObserver(web_contents),
5318bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      message_loop_runner_(new content::MessageLoopRunner) {
53258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
53358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
5348bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)WebContentsLoadedOrDestroyedWatcher::~WebContentsLoadedOrDestroyedWatcher() {}
53558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
5368bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void WebContentsLoadedOrDestroyedWatcher::Wait() {
53758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  message_loop_runner_->Run();
53858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
53958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
540010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)void WebContentsLoadedOrDestroyedWatcher::WebContentsDestroyed() {
5418bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  message_loop_runner_->Quit();
5428bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
5438bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
5448bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void WebContentsLoadedOrDestroyedWatcher::DidStopLoading(
5458bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    content::RenderViewHost* render_view_host) {
54658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  message_loop_runner_->Quit();
54758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
54858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
549d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#if !defined(OS_MACOSX)
550d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
5515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Observer used to wait for the creation of a new app window.
5521320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciclass TestAddAppWindowObserver
5531320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    : public extensions::AppWindowRegistry::Observer {
554d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) public:
5551320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  explicit TestAddAppWindowObserver(extensions::AppWindowRegistry* registry);
5565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual ~TestAddAppWindowObserver();
557d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
5581320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // extensions::AppWindowRegistry::Observer:
5591320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  virtual void OnAppWindowAdded(extensions::AppWindow* app_window) OVERRIDE;
560d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
5611320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  extensions::AppWindow* WaitForAppWindow();
562d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
563d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) private:
5641320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  extensions::AppWindowRegistry* registry_;  // Not owned.
5651320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  extensions::AppWindow* window_;            // Not owned.
566d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  base::RunLoop run_loop_;
567d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
5685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(TestAddAppWindowObserver);
569d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)};
570d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
5715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TestAddAppWindowObserver::TestAddAppWindowObserver(
5721320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    extensions::AppWindowRegistry* registry)
5735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    : registry_(registry), window_(NULL) {
574d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  registry_->AddObserver(this);
575d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)}
576d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
5775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TestAddAppWindowObserver::~TestAddAppWindowObserver() {
578d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  registry_->RemoveObserver(this);
579d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)}
580d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
5811320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccivoid TestAddAppWindowObserver::OnAppWindowAdded(
5821320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    extensions::AppWindow* app_window) {
5835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  window_ = app_window;
584d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  run_loop_.Quit();
585d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)}
586d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
5871320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciextensions::AppWindow* TestAddAppWindowObserver::WaitForAppWindow() {
588d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  run_loop_.Run();
589d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  return window_;
590d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)}
591d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
592d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#endif
593d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
5945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace
5955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class PolicyTest : public InProcessBrowserTest {
5975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected:
5985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PolicyTest() {}
5995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~PolicyTest() {}
6005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void SetUp() OVERRIDE {
6025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    test_extension_cache_.reset(new extensions::ExtensionCacheFake());
6035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    InProcessBrowserTest::SetUp();
6045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
6055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
6065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
60790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    CommandLine::ForCurrentProcess()->AppendSwitch("noerrdialogs");
6082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    EXPECT_CALL(provider_, IsInitializationComplete(_))
6095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        .WillRepeatedly(Return(true));
6105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_);
6115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
6125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void SetUpOnMainThread() OVERRIDE {
6145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    BrowserThread::PostTask(
6155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        BrowserThread::IO, FROM_HERE,
6165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        base::Bind(chrome_browser_net::SetUrlRequestMocksEnabled, true));
6175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
6185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Makes URLRequestMockHTTPJobs serve data from content::DIR_TEST_DATA
6205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // instead of chrome::DIR_TEST_DATA.
6215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void ServeContentTestData() {
6222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    base::FilePath root_http;
6235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    PathService::Get(content::DIR_TEST_DATA, &root_http);
6245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    BrowserThread::PostTaskAndReply(
6251320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        BrowserThread::IO,
6261320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        FROM_HERE,
6271320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        base::Bind(URLRequestMockHTTPJob::AddUrlHandler,
6281320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                   root_http,
6291320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                   make_scoped_refptr(BrowserThread::GetBlockingPool())),
63090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        base::MessageLoop::current()->QuitWhenIdleClosure());
6315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    content::RunMessageLoop();
6325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
6335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SetScreenshotPolicy(bool enabled) {
6355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    PolicyMap policies;
6367dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    policies.Set(key::kDisableScreenshots,
637116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                 POLICY_LEVEL_MANDATORY,
638116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                 POLICY_SCOPE_USER,
639116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                 new base::FundamentalValue(!enabled),
640116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                 NULL);
6412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    UpdateProviderPolicy(policies);
6425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
6435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_CHROMEOS)
6455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  class QuitMessageLoopAfterScreenshot : public ScreenshotTakerObserver {
6465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)   public:
6475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    virtual void OnScreenshotCompleted(
6485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        ScreenshotTakerObserver::Result screenshot_result,
6495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        const base::FilePath& screenshot_path) OVERRIDE {
6505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      BrowserThread::PostTaskAndReply(BrowserThread::IO,
6515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                      FROM_HERE,
6525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                      base::Bind(base::DoNothing),
6535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                      base::MessageLoop::QuitClosure());
6545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    }
6555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
6565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    virtual ~QuitMessageLoopAfterScreenshot() {}
6575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  };
6585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
6595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void TestScreenshotFile(bool enabled) {
6605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // AddObserver is an ash-specific method, so just replace the screenshot
6615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // taker with one we've created here.
6625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    scoped_ptr<ScreenshotTaker> screenshot_taker(new ScreenshotTaker);
6635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // ScreenshotTaker doesn't own this observer, so the observer's lifetime
6645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // is tied to the test instead.
6655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    screenshot_taker->AddObserver(&observer_);
6665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ash::Shell::GetInstance()->accelerator_controller()->SetScreenshotDelegate(
6675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        screenshot_taker.PassAs<ash::ScreenshotDelegate>());
6685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
6695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    SetScreenshotPolicy(enabled);
6705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ash::Shell::GetInstance()->accelerator_controller()->PerformAction(
6715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        ash::TAKE_SCREENSHOT, ui::Accelerator());
6725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    content::RunMessageLoop();
6745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
6755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
6765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ExtensionService* extension_service() {
6785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    extensions::ExtensionSystem* system =
6795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        extensions::ExtensionSystem::Get(browser()->profile());
6805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return system->extension_service();
6815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
6825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const extensions::Extension* InstallExtension(
6842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const base::FilePath::StringType& name) {
6852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    base::FilePath extension_path(ui_test_utils::GetTestFilePath(
6862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        base::FilePath(kTestExtensionsDir), base::FilePath(name)));
6875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    scoped_refptr<extensions::CrxInstaller> installer =
688ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch        extensions::CrxInstaller::CreateSilent(extension_service());
6895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    installer->set_allow_silent_install(true);
6905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    installer->set_install_cause(extension_misc::INSTALL_CAUSE_UPDATE);
6915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    installer->set_creation_flags(extensions::Extension::FROM_WEBSTORE);
6925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    content::WindowedNotificationObserver observer(
6945f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        extensions::NOTIFICATION_CRX_INSTALLER_DONE,
6955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        content::NotificationService::AllSources());
6965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    installer->InstallCrx(extension_path);
6975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    observer.Wait();
6985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    content::Details<const extensions::Extension> details = observer.details();
6995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return details.ptr();
7005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
7015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
702d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  const extensions::Extension* LoadUnpackedExtension(
70390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      const base::FilePath::StringType& name, bool expect_success) {
70490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    base::FilePath extension_path(ui_test_utils::GetTestFilePath(
70590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        base::FilePath(kTestExtensionsDir), base::FilePath(name)));
70690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    scoped_refptr<extensions::UnpackedInstaller> installer =
70790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        extensions::UnpackedInstaller::Create(extension_service());
70890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    content::WindowedNotificationObserver observer(
7095f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        expect_success ? extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED
7105f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                       : extensions::NOTIFICATION_EXTENSION_LOAD_ERROR,
71190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        content::NotificationService::AllSources());
71290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    installer->Load(extension_path);
71390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    observer.Wait();
714d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
7155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const extensions::ExtensionSet* extensions =
7165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        extension_service()->extensions();
7175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    for (extensions::ExtensionSet::const_iterator it = extensions->begin();
718d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)         it != extensions->end(); ++it) {
719d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      if ((*it)->path() == extension_path)
720d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        return it->get();
721d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    }
722d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    return NULL;
72390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
72490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
7255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void UninstallExtension(const std::string& id, bool expect_success) {
7265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    content::WindowedNotificationObserver observer(
7275f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        expect_success
7285f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)            ? extensions::NOTIFICATION_EXTENSION_UNINSTALLED_DEPRECATED
7295f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)            : extensions::NOTIFICATION_EXTENSION_UNINSTALL_NOT_ALLOWED,
7305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        content::NotificationService::AllSources());
731116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    extension_service()->UninstallExtension(
7325f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        id,
7335f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        extensions::UNINSTALL_REASON_FOR_TESTING,
7345f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        base::Bind(&base::DoNothing),
7355f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        NULL);
7365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    observer.Wait();
7375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
7385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void UpdateProviderPolicy(const PolicyMap& policy) {
7402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    provider_.UpdateChromePolicy(policy);
74190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    DCHECK(base::MessageLoop::current());
7422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    base::RunLoop loop;
7432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    loop.RunUntilIdle();
7442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
7452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
746c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Sends a mouse click at the given coordinates to the current renderer.
747c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  void PerformClick(int x, int y) {
748c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    content::WebContents* contents =
749c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        browser()->tab_strip_model()->GetActiveWebContents();
750f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    blink::WebMouseEvent click_event;
751f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    click_event.type = blink::WebInputEvent::MouseDown;
752f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    click_event.button = blink::WebMouseEvent::ButtonLeft;
753c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    click_event.clickCount = 1;
754c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    click_event.x = x;
755c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    click_event.y = y;
756c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    contents->GetRenderViewHost()->ForwardMouseEvent(click_event);
757f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    click_event.type = blink::WebInputEvent::MouseUp;
758c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    contents->GetRenderViewHost()->ForwardMouseEvent(click_event);
759c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
760c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
7615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MockConfigurationPolicyProvider provider_;
7625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  scoped_ptr<extensions::ExtensionCacheFake> test_extension_cache_;
7635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#if defined(OS_CHROMEOS)
7645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  QuitMessageLoopAfterScreenshot observer_;
7655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif
7665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
7675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_WIN)
7695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This policy only exists on Windows.
7705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Sets the locale policy before the browser is started.
7725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class LocalePolicyTest : public PolicyTest {
7735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
7745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LocalePolicyTest() {}
7755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~LocalePolicyTest() {}
7765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
7785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    PolicyTest::SetUpInProcessBrowserTestFixture();
7795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    PolicyMap policies;
7805f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    policies.Set(key::kApplicationLocaleValue,
7815f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                 POLICY_LEVEL_MANDATORY,
7825f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                 POLICY_SCOPE_USER,
7835f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                 new base::StringValue("fr"),
7845f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                 NULL);
7855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    provider_.UpdateChromePolicy(policies);
7865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // The "en-US" ResourceBundle is always loaded before this step for tests,
7875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // but in this test we want the browser to load the bundle as it
7885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // normally would.
7895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ResourceBundle::CleanupSharedInstance();
7905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
7915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
7925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(LocalePolicyTest, ApplicationLocaleValue) {
7945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Verifies that the default locale can be overridden with policy.
7955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ("fr", g_browser_process->GetApplicationLocale());
7965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUINewTabURL));
797a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 french_title = l10n_util::GetStringUTF16(IDS_NEW_TAB_TITLE);
798a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 title;
7995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(), &title));
8005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(french_title, title);
8015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Make sure this is really French and differs from the English title.
8035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string loaded =
8045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      ui::ResourceBundle::GetSharedInstance().ReloadLocaleResources("en-US");
8055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ("en-US", loaded);
806a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 english_title = l10n_util::GetStringUTF16(IDS_NEW_TAB_TITLE);
8075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_NE(french_title, english_title);
8085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
8095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
8105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(PolicyTest, BookmarkBarEnabled) {
812ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch#if defined(OS_WIN) && defined(USE_ASH)
813ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  // Disable this test in Metro+Ash for now (http://crbug.com/262796).
814a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
815ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    return;
816ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch#endif
817ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
8185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Verifies that the bookmarks bar can be forced to always or never show up.
8195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Test starts in about:blank.
8215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PrefService* prefs = browser()->profile()->GetPrefs();
8221320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  EXPECT_FALSE(prefs->IsManagedPreference(bookmarks::prefs::kShowBookmarkBar));
8231320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  EXPECT_FALSE(prefs->GetBoolean(bookmarks::prefs::kShowBookmarkBar));
8245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(BookmarkBar::HIDDEN, browser()->bookmark_bar_state());
8255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PolicyMap policies;
827116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  policies.Set(key::kBookmarkBarEnabled,
828116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch               POLICY_LEVEL_MANDATORY,
829116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch               POLICY_SCOPE_USER,
830116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch               new base::FundamentalValue(true),
831116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch               NULL);
8322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  UpdateProviderPolicy(policies);
8331320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  EXPECT_TRUE(prefs->IsManagedPreference(bookmarks::prefs::kShowBookmarkBar));
8341320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  EXPECT_TRUE(prefs->GetBoolean(bookmarks::prefs::kShowBookmarkBar));
8355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(BookmarkBar::SHOW, browser()->bookmark_bar_state());
8365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The NTP has special handling of the bookmark bar.
8385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUINewTabURL));
8395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(BookmarkBar::SHOW, browser()->bookmark_bar_state());
8405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
841116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  policies.Set(key::kBookmarkBarEnabled,
842116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch               POLICY_LEVEL_MANDATORY,
843116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch               POLICY_SCOPE_USER,
844116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch               new base::FundamentalValue(false),
845116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch               NULL);
8462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  UpdateProviderPolicy(policies);
8471320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  EXPECT_TRUE(prefs->IsManagedPreference(bookmarks::prefs::kShowBookmarkBar));
8481320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  EXPECT_FALSE(prefs->GetBoolean(bookmarks::prefs::kShowBookmarkBar));
8495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The bookmark bar is hidden in the NTP when disabled by policy.
8505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(BookmarkBar::HIDDEN, browser()->bookmark_bar_state());
8515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  policies.Clear();
8532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  UpdateProviderPolicy(policies);
8541320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  EXPECT_FALSE(prefs->IsManagedPreference(bookmarks::prefs::kShowBookmarkBar));
8551320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  EXPECT_FALSE(prefs->GetBoolean(bookmarks::prefs::kShowBookmarkBar));
8565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The bookmark bar is shown detached in the NTP, when disabled by prefs only.
8575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(BookmarkBar::DETACHED, browser()->bookmark_bar_state());
8585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
8595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
86090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)IN_PROC_BROWSER_TEST_F(PolicyTest, PRE_PRE_DefaultCookiesSetting) {
8615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Verifies that cookies are deleted on shutdown. This test is split in 3
8625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // parts because it spans 2 browser restarts.
8635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Profile* profile = browser()->profile();
8655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  GURL url(kURL);
8665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // No cookies at startup.
8675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(content::GetCookies(profile, url).empty());
8685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Set a cookie now.
8695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string value = std::string(kCookieValue) + std::string(kCookieOptions);
8705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(content::SetCookie(profile, url, value));
8715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Verify it was set.
8725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(kCookieValue, GetCookies(profile, url));
8735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
8745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
87590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)IN_PROC_BROWSER_TEST_F(PolicyTest, PRE_DefaultCookiesSetting) {
8765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Verify that the cookie persists across restarts.
8775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(kCookieValue, GetCookies(browser()->profile(), GURL(kURL)));
8785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Now set the policy and the cookie should be gone after another restart.
8795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PolicyMap policies;
8805f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  policies.Set(key::kDefaultCookiesSetting,
8815f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)               POLICY_LEVEL_MANDATORY,
8825f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)               POLICY_SCOPE_USER,
8835f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)               new base::FundamentalValue(4),
8845f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)               NULL);
8852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  UpdateProviderPolicy(policies);
8865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
8875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
88890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)IN_PROC_BROWSER_TEST_F(PolicyTest, DefaultCookiesSetting) {
8895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Verify that the cookie is gone.
8905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(GetCookies(browser()->profile(), GURL(kURL)).empty());
8915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
8925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(PolicyTest, DefaultSearchProvider) {
8942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  MakeRequestFail make_request_fail("search.example");
8955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Verifies that a default search is made using the provider configured via
8975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // policy. Also checks that default search can be completely disabled.
8985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const base::string16 kKeyword(base::ASCIIToUTF16("testsearch"));
8995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const std::string kSearchURL("http://search.example/search?q={searchTerms}");
9005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const std::string kAlternateURL0(
9015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      "http://search.example/search#q={searchTerms}");
9025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const std::string kAlternateURL1("http://search.example/#q={searchTerms}");
9032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const std::string kSearchTermsReplacementKey("zekey");
904a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  const std::string kImageURL("http://test.com/searchbyimage/upload");
905a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  const std::string kImageURLPostParams(
906a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      "image_content=content,image_url=http://test.com/test.png");
9073551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  const std::string kNewTabURL("http://search.example/newtab");
9085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TemplateURLService* service = TemplateURLServiceFactory::GetForProfile(
9105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      browser()->profile());
9115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ui_test_utils::WaitForTemplateURLServiceToLoad(service);
9125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TemplateURL* default_search = service->GetDefaultSearchProvider();
9135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ASSERT_TRUE(default_search);
9145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_NE(kKeyword, default_search->keyword());
9155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_NE(kSearchURL, default_search->url());
9165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_FALSE(
9175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    default_search->alternate_urls().size() == 2 &&
9185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    default_search->alternate_urls()[0] == kAlternateURL0 &&
9192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    default_search->alternate_urls()[1] == kAlternateURL1 &&
9202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    default_search->search_terms_replacement_key() ==
921a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)        kSearchTermsReplacementKey &&
922a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    default_search->image_url() == kImageURL &&
9233551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    default_search->image_url_post_params() == kImageURLPostParams &&
9243551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    default_search->new_tab_url() == kNewTabURL);
9255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Override the default search provider using policies.
9275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PolicyMap policies;
928116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  policies.Set(key::kDefaultSearchProviderEnabled,
929116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch               POLICY_LEVEL_MANDATORY,
930116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch               POLICY_SCOPE_USER,
931116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch               new base::FundamentalValue(true),
932116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch               NULL);
9337dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  policies.Set(key::kDefaultSearchProviderKeyword,
9345f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)               POLICY_LEVEL_MANDATORY,
9355f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)               POLICY_SCOPE_USER,
9365f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)               new base::StringValue(kKeyword),
937