summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/webui/bookmarks_ui_browsertest.cc
blob: 8b7c72fd038cbea75f98c0f0ba4ee1e5def1984f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/test/ui/ui_test.h"

#include "base/test/test_timeouts.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_tabstrip.h"
#include "chrome/browser/ui/chrome_pages.h"
#include "chrome/common/url_constants.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/test_navigation_observer.h"

class BookmarksTest : public InProcessBrowserTest {
 public:
  BookmarksTest() {
    EnableDOMAutomation();
  }

  void OpenBookmarksManager() {
    content::TestNavigationObserver navigation_observer(
        content::NotificationService::AllSources(), NULL, 2);

    // Bring up the bookmarks manager tab.
    chrome::ShowBookmarkManager(browser());
    navigation_observer.Wait();
  }

  void AssertIsBookmarksPage(content::WebContents* tab) {
    GURL url;
    std::string out;
    ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
        tab->GetRenderViewHost(), L"",
        L"domAutomationController.send(location.protocol)", &out));
    ASSERT_EQ("chrome-extension:", out);
    ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
        tab->GetRenderViewHost(), L"",
        L"domAutomationController.send(location.pathname)", &out));
    ASSERT_EQ("/main.html", out);
  }
};

IN_PROC_BROWSER_TEST_F(BookmarksTest, ShouldRedirectToExtension) {
  ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIBookmarksURL));
  AssertIsBookmarksPage(chrome::GetActiveWebContents(browser()));
}

IN_PROC_BROWSER_TEST_F(BookmarksTest, CommandOpensBookmarksTab) {
  ASSERT_EQ(1, browser()->tab_count());

  // Bring up the bookmarks manager tab.
  OpenBookmarksManager();
  ASSERT_EQ(1, browser()->tab_count());
  AssertIsBookmarksPage(chrome::GetActiveWebContents(browser()));
}

// If this flakes on Mac, use: http://crbug.com/87200
IN_PROC_BROWSER_TEST_F(BookmarksTest, CommandAgainGoesBackToBookmarksTab) {
  ui_test_utils::NavigateToURL(
      browser(),
      ui_test_utils::GetTestUrl(FilePath(),
                                FilePath().AppendASCII("simple.html")));
  ASSERT_EQ(1, browser()->tab_count());

  // Bring up the bookmarks manager tab.
  OpenBookmarksManager();
  ASSERT_EQ(2, browser()->tab_count());

  AssertIsBookmarksPage(chrome::GetActiveWebContents(browser()));

  // Switch to first tab and run command again.
  chrome::ActivateTabAt(browser(), 0, true);
  chrome::ShowBookmarkManager(browser());

  // Ensure the bookmarks ui tab is active.
  ASSERT_EQ(1, browser()->active_index());
  ASSERT_EQ(2, browser()->tab_count());
}

IN_PROC_BROWSER_TEST_F(BookmarksTest, TwoCommandsOneTab) {
  content::TestNavigationObserver navigation_observer(
      content::NotificationService::AllSources());
  chrome::ShowBookmarkManager(browser());
  chrome::ShowBookmarkManager(browser());
  navigation_observer.Wait();

  ASSERT_EQ(1, browser()->tab_count());
}

IN_PROC_BROWSER_TEST_F(BookmarksTest, BookmarksLoaded) {
  ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIBookmarksURL));
  ASSERT_EQ(1, browser()->tab_count());
  AssertIsBookmarksPage(chrome::GetActiveWebContents(browser()));
}