Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / test / media_router / media_router_base_browsertest.cc
blobb89970a6c37648e3498c00d103b7645597e49bb8
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/test/media_router/media_router_base_browsertest.h"
7 #include "base/bind.h"
8 #include "base/command_line.h"
9 #include "base/path_service.h"
10 #include "base/timer/elapsed_timer.h"
11 #include "chrome/browser/extensions/unpacked_installer.h"
12 #include "chrome/common/chrome_switches.h"
13 #include "chrome/test/base/ui_test_utils.h"
14 #include "content/public/common/content_switches.h"
15 #include "content/public/test/test_utils.h"
16 #include "extensions/browser/process_manager.h"
17 #include "extensions/common/switches.h"
20 namespace {
21 // Command line argument to specify CRX extension location.
22 const char kExtensionCrx[] = "extension-crx";
23 // Command line argument to specify unpacked extension location.
24 const char kExtensionUnpacked[] = "extension-unpacked";
25 } // namespace
28 namespace media_router {
30 MediaRouterBaseBrowserTest::MediaRouterBaseBrowserTest()
31 : extension_load_event_(false, false), extension_host_created_(false) {
32 base::CommandLine::ForCurrentProcess()->AppendSwitch(
33 switches::kEnableMediaRouter);
34 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
35 switches::kEnableExperimentalWebPlatformFeatures, "Presentation");
38 MediaRouterBaseBrowserTest::~MediaRouterBaseBrowserTest() {
41 void MediaRouterBaseBrowserTest::SetUp() {
42 ParseCommandLine();
43 ExtensionBrowserTest::SetUp();
46 void MediaRouterBaseBrowserTest::TearDown() {
47 ExtensionBrowserTest::TearDown();
50 void MediaRouterBaseBrowserTest::SetUpOnMainThread() {
51 ExtensionBrowserTest::SetUpOnMainThread();
52 extensions::ProcessManager* process_manager =
53 extensions::ProcessManager::Get(browser()->profile());
54 DCHECK(process_manager);
55 process_manager->AddObserver(this);
56 InstallAndEnableMRExtension();
57 extension_load_event_.Wait();
60 void MediaRouterBaseBrowserTest::TearDownOnMainThread() {
61 UninstallMRExtension();
62 extensions::ProcessManager* process_manager =
63 extensions::ProcessManager::Get(browser()->profile());
64 DCHECK(process_manager);
65 process_manager->RemoveObserver(this);
66 ExtensionBrowserTest::TearDownOnMainThread();
69 void MediaRouterBaseBrowserTest::InstallAndEnableMRExtension() {
70 if (is_unpacked()) {
71 const extensions::Extension* extension = LoadExtension(extension_unpacked_);
72 extension_id_ = extension->id();
73 } else {
74 NOTIMPLEMENTED();
78 void MediaRouterBaseBrowserTest::UninstallMRExtension() {
79 if (!extension_id_.empty()) {
80 UninstallExtension(extension_id_);
84 bool MediaRouterBaseBrowserTest::ConditionalWait(
85 base::TimeDelta timeout,
86 base::TimeDelta interval,
87 const base::Callback<bool(void)>& callback) {
88 base::ElapsedTimer timer;
89 do {
90 if (callback.Run())
91 return true;
93 base::RunLoop run_loop;
94 base::MessageLoop::current()->PostDelayedTask(
95 FROM_HERE, run_loop.QuitClosure(), interval);
96 run_loop.Run();
97 } while (timer.Elapsed() < timeout);
99 return false;
102 void MediaRouterBaseBrowserTest::Wait(base::TimeDelta timeout) {
103 base::RunLoop run_loop;
104 base::MessageLoop::current()->PostDelayedTask(
105 FROM_HERE, run_loop.QuitClosure(), timeout);
106 run_loop.Run();
109 void MediaRouterBaseBrowserTest::OnBackgroundHostCreated(
110 extensions::ExtensionHost* host) {
111 extension_host_created_ = true;
112 DVLOG(0) << "Host created";
113 extension_load_event_.Signal();
116 void MediaRouterBaseBrowserTest::ParseCommandLine() {
117 DVLOG(0) << "ParseCommandLine";
118 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
120 extension_crx_ = command_line->GetSwitchValuePath(kExtensionCrx);
121 extension_unpacked_ = command_line->GetSwitchValuePath(kExtensionUnpacked);
123 // Check if there is mr_extension folder under PRODUCT_DIR folder.
124 if (extension_crx_.empty() && extension_unpacked_.empty()) {
125 base::FilePath base_dir;
126 ASSERT_TRUE(PathService::Get(base::DIR_MODULE, &base_dir));
127 base::FilePath extension_path =
128 base_dir.Append(FILE_PATH_LITERAL("mr_extension/"));
129 if (PathExists(extension_path)) {
130 extension_unpacked_ = extension_path;
134 // Exactly one of these two arguments should be provided.
135 ASSERT_NE(extension_crx_.empty(), extension_unpacked_.empty());
138 } // namespace media_router