1// Copyright 2014 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.
4
5#include "base/command_line.h"
6#include "chrome/browser/profiles/profile.h"
7#include "chrome/browser/ui/browser.h"
8#include "chrome/browser/ui/tabs/tab_strip_model.h"
9#include "chrome/browser/ui/webui/password_manager_internals/password_manager_internals_ui.h"
10#include "chrome/common/url_constants.h"
11#include "chrome/test/base/ui_test_utils.h"
12#include "chrome/test/base/web_ui_browser_test.h"
13#include "components/password_manager/content/browser/password_manager_internals_service_factory.h"
14#include "components/password_manager/core/browser/password_manager_internals_service.h"
15#include "components/password_manager/core/common/password_manager_switches.h"
16#include "content/public/browser/web_contents.h"
17
18class PasswordManagerInternalsWebUIBrowserTest : public WebUIBrowserTest {
19 public:
20  PasswordManagerInternalsWebUIBrowserTest();
21  virtual ~PasswordManagerInternalsWebUIBrowserTest();
22
23  virtual void SetUpOnMainThread() OVERRIDE;
24
25 protected:
26  content::WebContents* GetWebContents();
27
28  // Navigates to the internals page in a tab specified by |disposition|. Also
29  // assigns the corresponding UI controller to |controller_|.
30  void OpenInternalsPage(WindowOpenDisposition disposition);
31
32 private:
33  PasswordManagerInternalsUI* controller_;
34};
35
36PasswordManagerInternalsWebUIBrowserTest::
37    PasswordManagerInternalsWebUIBrowserTest()
38    : controller_(NULL) {}
39
40PasswordManagerInternalsWebUIBrowserTest::
41    ~PasswordManagerInternalsWebUIBrowserTest() {}
42
43void PasswordManagerInternalsWebUIBrowserTest::SetUpOnMainThread() {
44  WebUIBrowserTest::SetUpOnMainThread();
45  OpenInternalsPage(CURRENT_TAB);
46}
47
48content::WebContents*
49PasswordManagerInternalsWebUIBrowserTest::GetWebContents() {
50  return browser()->tab_strip_model()->GetActiveWebContents();
51}
52
53void PasswordManagerInternalsWebUIBrowserTest::OpenInternalsPage(
54    WindowOpenDisposition disposition) {
55  std::string url_string("chrome://");
56  url_string += chrome::kChromeUIPasswordManagerInternalsHost;
57  ui_test_utils::NavigateToURLWithDisposition(
58      browser(),
59      GURL(url_string),
60      disposition,
61      ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
62  controller_ = static_cast<PasswordManagerInternalsUI*>(
63      GetWebContents()->GetWebUI()->GetController());
64  AddLibrary(base::FilePath(
65      FILE_PATH_LITERAL("password_manager_internals_browsertest.js")));
66}
67
68IN_PROC_BROWSER_TEST_F(PasswordManagerInternalsWebUIBrowserTest,
69                       LogSavePasswordProgress) {
70  password_manager::PasswordManagerInternalsService* service =
71      password_manager::PasswordManagerInternalsServiceFactory::
72          GetForBrowserContext(browser()->profile());
73  ASSERT_TRUE(service);
74  service->ProcessLog("<script> text for testing");
75  ASSERT_TRUE(RunJavascriptTest("testLogText"));
76}
77
78// Test that a single internals page is flushed on reload.
79IN_PROC_BROWSER_TEST_F(PasswordManagerInternalsWebUIBrowserTest,
80                       LogSavePasswordProgress_FlushedOnReload) {
81  password_manager::PasswordManagerInternalsService* service =
82      password_manager::PasswordManagerInternalsServiceFactory::
83          GetForBrowserContext(browser()->profile());
84  ASSERT_TRUE(service);
85  service->ProcessLog("<script> text for testing");
86  OpenInternalsPage(CURRENT_TAB);  // Reload.
87  ASSERT_TRUE(RunJavascriptTest("testLogTextNotPresent"));
88}
89
90// Test that if two tabs with the internals page are open, the second displays
91// the same logs. In particular, this checks that both the second tab gets the
92// logs created before the second tab was opened, and also that the second tab
93// waits with displaying until the internals page is ready (trying to display
94// the old logs just on construction time would fail).
95IN_PROC_BROWSER_TEST_F(PasswordManagerInternalsWebUIBrowserTest,
96                       LogSavePasswordProgress_MultipleTabsIdentical) {
97  // First, open one tab with the internals page, and log something.
98  password_manager::PasswordManagerInternalsService* service =
99      password_manager::PasswordManagerInternalsServiceFactory::
100          GetForBrowserContext(browser()->profile());
101  ASSERT_TRUE(service);
102  service->ProcessLog("<script> text for testing");
103  ASSERT_TRUE(RunJavascriptTest("testLogText"));
104  // Now open a second tab with the internals page, but do not log anything.
105  OpenInternalsPage(NEW_FOREGROUND_TAB);
106  // The previously logged text should have made it to the page.
107  ASSERT_TRUE(RunJavascriptTest("testLogText"));
108}
109
110// Test that in the presence of more internals pages, reload does not cause
111// flushing the logs.
112IN_PROC_BROWSER_TEST_F(PasswordManagerInternalsWebUIBrowserTest,
113                       LogSavePasswordProgress_NotFlushedOnReloadIfMultiple) {
114  // Open one more tab with the internals page.
115  OpenInternalsPage(NEW_FOREGROUND_TAB);
116  // Now log something.
117  password_manager::PasswordManagerInternalsService* service =
118      password_manager::PasswordManagerInternalsServiceFactory::
119          GetForBrowserContext(browser()->profile());
120  ASSERT_TRUE(service);
121  service->ProcessLog("<script> text for testing");
122  // Reload.
123  OpenInternalsPage(CURRENT_TAB);
124  // The text should still be there.
125  ASSERT_TRUE(RunJavascriptTest("testLogText"));
126}
127