1 // Copyright 2020 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/strings/strcat.h"
6 #include "base/test/bind.h"
7 #include "build/build_config.h"
8 #include "content/public/test/browser_test.h"
9 #include "content/public/test/content_browser_test.h"
10 #include "content/public/test/url_loader_interceptor.h"
11 #include "headless/public/headless_browser.h"
12 #include "headless/public/headless_web_contents.h"
13 #include "headless/test/headless_browser_test.h"
14 
15 using content::URLLoaderInterceptor;
16 
17 namespace {
18 constexpr char kBaseDataDir[] = "headless/test/data/";
19 
20 }  // namespace
21 
22 namespace headless {
23 
24 class HeadlessOriginTrialsBrowserTest : public HeadlessBrowserTest {
25  public:
26   HeadlessOriginTrialsBrowserTest() = default;
27   ~HeadlessOriginTrialsBrowserTest() override = default;
28 
SetUpOnMainThread()29   void SetUpOnMainThread() override {
30     HeadlessBrowserTest::SetUpOnMainThread();
31 
32     // We use a URLLoaderInterceptor, rather than the EmbeddedTestServer, since
33     // the origin trial token in the response is associated with a fixed
34     // origin, whereas EmbeddedTestServer serves content on a random port.
35     url_loader_interceptor_ =
36         std::make_unique<URLLoaderInterceptor>(base::BindLambdaForTesting(
37             [&](URLLoaderInterceptor::RequestParams* params) -> bool {
38               URLLoaderInterceptor::WriteResponse(
39                   base::StrCat(
40                       {kBaseDataDir, params->url_request.url.path_piece()}),
41                   params->client.get());
42               return true;
43             }));
44   }
45 
TearDownOnMainThread()46   void TearDownOnMainThread() override {
47     url_loader_interceptor_.reset();
48     HeadlessBrowserTest::TearDownOnMainThread();
49   }
50 
51  private:
52   std::unique_ptr<URLLoaderInterceptor> url_loader_interceptor_;
53 
54   DISALLOW_COPY_AND_ASSIGN(HeadlessOriginTrialsBrowserTest);
55 };
56 
IN_PROC_BROWSER_TEST_F(HeadlessOriginTrialsBrowserTest,TrialsDisabledByDefault)57 IN_PROC_BROWSER_TEST_F(HeadlessOriginTrialsBrowserTest,
58                        TrialsDisabledByDefault) {
59   HeadlessBrowserContext* browser_context =
60       browser()->CreateBrowserContextBuilder().Build();
61 
62   HeadlessWebContents* web_contents =
63       browser_context->CreateWebContentsBuilder()
64           .SetInitialURL(GURL("https://example.test/no_origin_trial.html"))
65           .Build();
66   EXPECT_TRUE(WaitForLoad(web_contents));
67 
68   // Ensures that createShadowRoot() is not defined, as no token is provided to
69   // enable the WebComponents V0 origin trial.
70   // TODO(crbug.com/1050190): Implement a permanent, sample trial so this test
71   // doesn't rely on WebComponents V0, which will eventually go away.
72   EXPECT_FALSE(
73       EvaluateScript(web_contents,
74                      "'createShadowRoot' in document.createElement('div')")
75           ->GetResult()
76           ->GetValue()
77           ->GetBool());
78 }
79 
80 }  // namespace headless
81