Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / views / media_router / media_router_ui_browsertest.cc
blob87a6f21bf8f3d558fb3914a3d103e8d2009057fa
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 "base/bind.h"
6 #include "base/thread_task_runner_handle.h"
7 #include "chrome/browser/ui/browser.h"
8 #include "chrome/browser/ui/browser_commands.h"
9 #include "chrome/browser/ui/tabs/tab_strip_model.h"
10 #include "chrome/browser/ui/toolbar/media_router_action.h"
11 #include "chrome/browser/ui/toolbar/toolbar_action_view_delegate.h"
12 #include "chrome/browser/ui/views/frame/browser_view.h"
13 #include "chrome/browser/ui/views/toolbar/browser_actions_container.h"
14 #include "chrome/browser/ui/views/toolbar/toolbar_action_view.h"
15 #include "chrome/browser/ui/views/toolbar/toolbar_view.h"
16 #include "chrome/common/url_constants.h"
17 #include "chrome/test/base/in_process_browser_test.h"
18 #include "chrome/test/base/ui_test_utils.h"
19 #include "content/public/browser/web_contents.h"
20 #include "content/public/test/test_navigation_observer.h"
21 #include "content/public/test/test_utils.h"
23 namespace media_router {
25 class MediaRouterUIBrowserTest : public InProcessBrowserTest {
26 public:
27 MediaRouterUIBrowserTest() {}
28 ~MediaRouterUIBrowserTest() override {}
30 void SetUpOnMainThread() override {
31 InProcessBrowserTest::SetUpOnMainThread();
33 BrowserActionsContainer* browser_actions_container =
34 BrowserView::GetBrowserViewForBrowser(browser())
35 ->toolbar()
36 ->browser_actions();
37 ASSERT_TRUE(browser_actions_container);
39 media_router_action_.reset(new MediaRouterAction(browser()));
41 // Sets delegate on |media_router_action_|.
42 toolbar_action_view_.reset(
43 new ToolbarActionView(media_router_action_.get(), browser()->profile(),
44 browser_actions_container));
47 void TearDownOnMainThread() override {
48 toolbar_action_view_.reset();
49 media_router_action_.reset();
50 InProcessBrowserTest::TearDownOnMainThread();
53 void OpenMediaRouterDialogAndWaitForNewWebContents() {
54 content::TestNavigationObserver nav_observer(NULL);
55 nav_observer.StartWatchingNewWebContents();
57 ToolbarView* toolbar =
58 BrowserView::GetBrowserViewForBrowser(browser())->toolbar();
60 // When the Media Router Action executes, it opens a dialog with web
61 // contents to chrome://media-router.
62 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
63 base::Bind(&MediaRouterUIBrowserTest::ExecuteMediaRouterAction,
64 this,
65 toolbar));
66 toolbar->ShowAppMenu(false);
68 EXPECT_FALSE(toolbar->IsWrenchMenuShowing());
69 nav_observer.Wait();
70 ASSERT_EQ(chrome::kChromeUIMediaRouterURL,
71 nav_observer.last_navigation_url().spec());
72 nav_observer.StopWatchingNewWebContents();
75 void ExecuteMediaRouterAction(ToolbarView* toolbar) {
76 EXPECT_TRUE(toolbar->IsWrenchMenuShowing());
77 media_router_action_->ExecuteAction(true);
80 protected:
81 // Must be initialized after |InProcessBrowserTest::SetUpOnMainThread|.
82 scoped_ptr<MediaRouterAction> media_router_action_;
84 // ToolbarActionView constructed to set the delegate on |mr_action|.
85 scoped_ptr<ToolbarActionView> toolbar_action_view_;
88 IN_PROC_BROWSER_TEST_F(MediaRouterUIBrowserTest,
89 OpenDialogWithMediaRouterAction) {
90 // We start off at about:blank page.
91 // Make sure there is 1 tab and media router is enabled.
92 ASSERT_EQ(1, browser()->tab_strip_model()->count());
94 OpenMediaRouterDialogAndWaitForNewWebContents();
96 // Reload the browser and wait.
97 content::TestNavigationObserver reload_observer(
98 browser()->tab_strip_model()->GetActiveWebContents());
99 chrome::Reload(browser(), CURRENT_TAB);
100 reload_observer.Wait();
102 // The reload should have removed the previously created dialog.
103 // We expect a new dialog WebContents to be created by calling this.
104 OpenMediaRouterDialogAndWaitForNewWebContents();
106 // Navigate away.
107 ui_test_utils::NavigateToURL(browser(), GURL("about:blank"));
109 // The navigation should have removed the previously created dialog.
110 // We expect a new dialog WebContents to be created by calling this.
111 OpenMediaRouterDialogAndWaitForNewWebContents();
114 } // namespace media_router