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/bind.h"
6#include "base/command_line.h"
7#include "base/files/file_path.h"
8#include "base/memory/ref_counted.h"
9#include "base/metrics/field_trial.h"
10#include "base/path_service.h"
11#include "chrome/browser/infobars/infobar_service.h"
12#include "chrome/browser/net/url_request_mock_util.h"
13#include "chrome/browser/ui/browser.h"
14#include "chrome/browser/ui/tabs/tab_strip_model.h"
15#include "chrome/common/chrome_switches.h"
16#include "chrome/common/render_messages.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/browser_thread.h"
20#include "content/public/common/content_paths.h"
21#include "content/public/common/content_switches.h"
22#include "content/public/test/browser_test_utils.h"
23#include "content/public/test/test_utils.h"
24#include "net/test/url_request/url_request_mock_http_job.h"
25#include "url/gurl.h"
26
27using content::BrowserThread;
28using net::URLRequestMockHTTPJob;
29
30namespace {
31
32// Tests that the NPAPI unauthorized plugin infobar is:
33// (1) Shown when the Finch trial is set to "Enabled"
34// (2) Not shown when the Finch trial is set to "Disabled"
35// TODO(cthomp): Remove/simplify when Finch trial is completed crbug.com/381944
36class UnauthorizedPluginInfoBarBrowserTest : public InProcessBrowserTest {
37 protected:
38  UnauthorizedPluginInfoBarBrowserTest() {}
39
40  // Makes URLRequestMockHTTPJobs serve data from content::DIR_TEST_DATA
41  // instead of chrome::DIR_TEST_DATA.
42  void ServeContentTestData() {
43    base::FilePath root_http;
44    PathService::Get(content::DIR_TEST_DATA, &root_http);
45    scoped_refptr<content::MessageLoopRunner> runner =
46        new content::MessageLoopRunner;
47    BrowserThread::PostTaskAndReply(
48        BrowserThread::IO,
49        FROM_HERE,
50        base::Bind(URLRequestMockHTTPJob::AddUrlHandler,
51                   root_http,
52                   make_scoped_refptr(BrowserThread::GetBlockingPool())),
53        runner->QuitClosure());
54    runner->Run();
55  }
56
57 public:
58  // Verifies that the test page exists (only present with src-internal)
59  bool TestPageExists() {
60    return base::PathExists(ui_test_utils::GetTestFilePath(
61        base::FilePath(FILE_PATH_LITERAL("plugin")),
62        base::FilePath(FILE_PATH_LITERAL("quicktime.html"))));
63  }
64
65  void InfoBarCountTest(size_t number_infobars_expected,
66                        Browser* browser) {
67    ServeContentTestData();
68    content::WebContents* contents =
69        browser->tab_strip_model()->GetActiveWebContents();
70    ASSERT_TRUE(contents);
71    InfoBarService* infobar_service = InfoBarService::FromWebContents(contents);
72    ASSERT_TRUE(infobar_service);
73    EXPECT_EQ(0u, infobar_service->infobar_count());
74
75    base::FilePath path(FILE_PATH_LITERAL("plugin/quicktime.html"));
76    GURL url(URLRequestMockHTTPJob::GetMockUrl(path));
77    ui_test_utils::NavigateToURL(browser, url);
78    ASSERT_EQ(number_infobars_expected, infobar_service->infobar_count());
79  }
80
81 private:
82  DISALLOW_COPY_AND_ASSIGN(UnauthorizedPluginInfoBarBrowserTest);
83};
84
85// When "UnauthorizedPluginInfoBar" is "Enabled", infobar for NPAPI plugin
86// should be shown.
87IN_PROC_BROWSER_TEST_F(UnauthorizedPluginInfoBarBrowserTest, InfobarEnabled) {
88  if (!TestPageExists()) {
89    LOG(INFO) <<
90        "Test skipped because plugin/quicktime.html test file wasn't found.";
91    return;
92  }
93  base::FieldTrialList::CreateTrialsFromString(
94      "UnauthorizedPluginInfoBar/Enabled/",
95      base::FieldTrialList::ACTIVATE_TRIALS,
96       std::set<std::string>());
97  InfoBarCountTest(1u, browser());
98}
99
100// When "UnauthorizedPluginInfoBar" is "Disabled", infobar for NPAPI plugin
101// should not be shown.
102IN_PROC_BROWSER_TEST_F(UnauthorizedPluginInfoBarBrowserTest, InfobarDisabled) {
103  if (!TestPageExists()) {
104    LOG(INFO) <<
105        "Test skipped because plugin/quicktime.html test file wasn't found.";
106    return;
107  }
108  base::FieldTrialList::CreateTrialsFromString(
109      "UnauthorizedPluginInfoBar/Disabled/",
110      base::FieldTrialList::ACTIVATE_TRIALS,
111      std::set<std::string>());
112  InfoBarCountTest(0u, browser());
113}
114
115}  // namespace
116