1// Copyright 2013 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 "chrome/browser/extensions/extension_test_message_listener.h"
6#include "chrome/browser/extensions/platform_app_browsertest_util.h"
7#include "chrome/common/chrome_switches.h"
8#include "content/test/net/url_request_prepackaged_interceptor.h"
9#include "net/url_request/url_fetcher.h"
10
11class AdViewTest : public extensions::PlatformAppBrowserTest {
12 protected:
13  virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
14    extensions::PlatformAppBrowserTest::SetUpCommandLine(command_line);
15    command_line->AppendSwitch(switches::kEnableAdview);
16    command_line->AppendSwitch(switches::kEnableAdviewSrcAttribute);
17  }
18};
19
20// This test checks the "loadcommit" event is called when the page inside an
21// <adview> is loaded.
22IN_PROC_BROWSER_TEST_F(AdViewTest, LoadCommitEventIsCalled) {
23  ASSERT_TRUE(StartEmbeddedTestServer());
24
25  ASSERT_TRUE(RunPlatformAppTest(
26      "platform_apps/ad_view/loadcommit_event")) << message_;
27}
28
29// This test checks the "loadabort" event is called when the "src" attribute
30// of an <adview> is an invalid URL.
31IN_PROC_BROWSER_TEST_F(AdViewTest, LoadAbortEventIsCalled) {
32  ASSERT_TRUE(StartEmbeddedTestServer());
33
34  ASSERT_TRUE(RunPlatformAppTest(
35      "platform_apps/ad_view/loadabort_event")) << message_;
36}
37
38// This test checks the page loaded inside an <adview> has the ability to
39// 1) receive "message" events from the application, and 2) use
40// "window.postMessage" to post back a message to the application.
41#if defined(OS_WIN)
42// Flaky, or takes too long time on Win7. (http://crbug.com/230271)
43#define MAYBE_CommitMessageFromAdNetwork DISABLED_CommitMessageFromAdNetwork
44#else
45#define MAYBE_CommitMessageFromAdNetwork CommitMessageFromAdNetwork
46#endif
47IN_PROC_BROWSER_TEST_F(AdViewTest, MAYBE_CommitMessageFromAdNetwork) {
48  ASSERT_TRUE(StartEmbeddedTestServer());
49
50  ASSERT_TRUE(RunPlatformAppTest(
51      "platform_apps/ad_view/onloadcommit_ack")) << message_;
52}
53
54// This test checks the page running inside an <adview> has the ability to load
55// and display an image inside an <iframe>.
56// Note: Disabled for initial checkin because the test depends on a binary
57//       file (image035.png) which the trybots don't process correctly when
58//       first checked-in.
59IN_PROC_BROWSER_TEST_F(AdViewTest, DISABLED_DisplayFirstAd) {
60  ASSERT_TRUE(StartEmbeddedTestServer());
61
62  ASSERT_TRUE(RunPlatformAppTest(
63      "platform_apps/ad_view/display_first_ad")) << message_;
64}
65
66// This test checks that <adview> attributes are also exposed as properties
67// (with the same name and value).
68#if defined(OS_WIN)
69// Flaky on Win XP. (http://crbug.com/264362)
70#define MAYBE_PropertiesAreInSyncWithAttributes \
71    DISABLED_PropertiesAreInSyncWithAttributes
72#else
73#define MAYBE_PropertiesAreInSyncWithAttributes \
74    PropertiesAreInSyncWithAttributes
75#endif
76IN_PROC_BROWSER_TEST_F(AdViewTest, MAYBE_PropertiesAreInSyncWithAttributes) {
77  ASSERT_TRUE(StartEmbeddedTestServer());
78
79  ASSERT_TRUE(RunPlatformAppTest(
80      "platform_apps/ad_view/properties_exposed")) << message_;
81}
82
83// This test checks an <adview> element has no behavior when the "adview"
84// permission is missing from the application manifest.
85IN_PROC_BROWSER_TEST_F(AdViewTest, AdViewPermissionIsRequired) {
86  ASSERT_TRUE(StartEmbeddedTestServer());
87
88  ASSERT_TRUE(RunPlatformAppTest(
89      "platform_apps/ad_view/permission_required")) << message_;
90}
91
92// This test checks that 1) it is possible change the value of the "ad-network"
93// attribute of an <adview> element and 2) changing the value will reset the
94// "src" attribute.
95// Broken test: http://crbug.com/257465.
96IN_PROC_BROWSER_TEST_F(AdViewTest, DISABLED_ChangeAdNetworkValue) {
97  ASSERT_TRUE(StartEmbeddedTestServer());
98
99  ASSERT_TRUE(RunPlatformAppTest(
100      "platform_apps/ad_view/change_ad_network")) << message_;
101}
102
103class AdViewNoSrcTest : public extensions::PlatformAppBrowserTest {
104 protected:
105  virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
106    extensions::PlatformAppBrowserTest::SetUpCommandLine(command_line);
107    command_line->AppendSwitch(switches::kEnableAdview);
108    //Note: The "kEnableAdviewSrcAttribute" flag is not here!
109  }
110};
111
112// This test checks an invalid "ad-network" value (i.e. not whitelisted)
113// is ignored.
114IN_PROC_BROWSER_TEST_F(AdViewNoSrcTest, InvalidAdNetworkIsIgnored) {
115  ASSERT_TRUE(StartEmbeddedTestServer());
116
117  ASSERT_TRUE(RunPlatformAppTest(
118      "platform_apps/ad_view/invalid_ad_network")) << message_;
119}
120
121// This test checks the "src" attribute is ignored when the
122// "kEnableAdviewSrcAttribute" is missing.
123IN_PROC_BROWSER_TEST_F(AdViewNoSrcTest, EnableAdviewSrcAttributeFlagRequired) {
124  ASSERT_TRUE(StartEmbeddedTestServer());
125
126  ASSERT_TRUE(RunPlatformAppTest(
127      "platform_apps/ad_view/src_flag_required")) << message_;
128}
129
130// This test checks 1) an <adview> works end-to-end (i.e. page is loaded) when
131// using a whitelisted ad-network, and 2) the "src" attribute is never exposed
132// to the application.
133IN_PROC_BROWSER_TEST_F(AdViewNoSrcTest, SrcNotExposed) {
134  base::FilePath file_path = test_data_dir_
135    .AppendASCII("platform_apps")
136    .AppendASCII("ad_view/src_not_exposed")
137    .AppendASCII("ad_network_fake_website.html");
138
139  // Note: The following URL is identical to the whitelisted url
140  //       for "admob" (see ad_view.js).
141  GURL url = GURL("https://admob-sdk.doubleclick.net/chromeapps");
142  std::string scheme = url.scheme();
143  std::string hostname = url.host();
144
145  content::URLRequestPrepackagedInterceptor interceptor(scheme, hostname);
146  interceptor.SetResponse(url, file_path);
147
148  ASSERT_TRUE(RunPlatformAppTest(
149      "platform_apps/ad_view/src_not_exposed")) << message_;
150  ASSERT_EQ(1, interceptor.GetHitCount());
151}
152
153class AdViewNotEnabledTest : public extensions::PlatformAppBrowserTest {
154 protected:
155  virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
156    extensions::PlatformAppBrowserTest::SetUpCommandLine(command_line);
157    //Note: The "kEnableAdview" flag is not here!
158  }
159};
160
161// This test checks an <adview> element has no behavior when the "kEnableAdview"
162// flag is missing.
163IN_PROC_BROWSER_TEST_F(AdViewNotEnabledTest, EnableAdviewFlagRequired) {
164  ASSERT_TRUE(StartEmbeddedTestServer());
165
166  ASSERT_TRUE(RunPlatformAppTest(
167      "platform_apps/ad_view/flag_required")) << message_;
168}
169