1// Copyright (c) 2012 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/extensions/api/dial/dial_api.h"
7#include "chrome/browser/extensions/api/dial/dial_api_factory.h"
8#include "chrome/browser/extensions/api/dial/dial_registry.h"
9#include "chrome/browser/extensions/extension_apitest.h"
10#include "chrome/browser/extensions/extension_service.h"
11#include "extensions/common/switches.h"
12#include "extensions/test/extension_test_message_listener.h"
13#include "extensions/test/result_catcher.h"
14#include "testing/gmock/include/gmock/gmock.h"
15#include "url/gurl.h"
16
17using extensions::DialDeviceData;
18using extensions::Extension;
19
20namespace api = extensions::api;
21
22namespace {
23
24class DialAPITest : public ExtensionApiTest {
25 public:
26  DialAPITest() {}
27
28  virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
29    ExtensionApiTest::SetUpCommandLine(command_line);
30    command_line->AppendSwitchASCII(
31        extensions::switches::kWhitelistedExtensionID,
32        "ddchlicdkolnonkihahngkmmmjnjlkkf");
33  }
34};
35
36}  // namespace
37
38// http://crbug.com/177163
39#if defined(OS_WIN) && !defined(NDEBUG)
40#define MAYBE_DeviceListEvents DISABLED_DeviceListEvents
41#else
42#define MAYBE_DeviceListEvents DeviceListEvents
43#endif
44// Test receiving DIAL API events.
45IN_PROC_BROWSER_TEST_F(DialAPITest, MAYBE_DeviceListEvents) {
46  // Setup the test.
47  ASSERT_TRUE(RunExtensionSubtest("dial/experimental", "device_list.html"));
48
49  // Send three device list updates.
50  scoped_refptr<extensions::DialAPI> api =
51      extensions::DialAPIFactory::GetInstance()->GetForBrowserContext(
52          profile());
53  ASSERT_TRUE(api.get());
54  extensions::DialRegistry::DeviceList devices;
55
56  extensions::ResultCatcher catcher;
57
58  DialDeviceData device1;
59  device1.set_device_id("1");
60  device1.set_label("1");
61  device1.set_device_description_url(GURL("http://127.0.0.1/dd.xml"));
62
63  devices.push_back(device1);
64  api->SendEventOnUIThread(devices);
65
66  DialDeviceData device2;
67  device2.set_device_id("2");
68  device2.set_label("2");
69  device2.set_device_description_url(GURL("http://127.0.0.2/dd.xml"));
70
71  devices.push_back(device2);
72  api->SendEventOnUIThread(devices);
73
74  DialDeviceData device3;
75  device3.set_device_id("3");
76  device3.set_label("3");
77  device3.set_device_description_url(GURL("http://127.0.0.3/dd.xml"));
78
79  devices.push_back(device3);
80  api->SendEventOnUIThread(devices);
81
82  EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
83}
84
85IN_PROC_BROWSER_TEST_F(DialAPITest, Discovery) {
86  ASSERT_TRUE(RunExtensionSubtest("dial/experimental", "discovery.html"));
87}
88
89// discoverNow does not do discovery when there are no listeners; in that case
90// the DIAL service will not be active.
91IN_PROC_BROWSER_TEST_F(DialAPITest, DiscoveryNoListeners) {
92  ASSERT_TRUE(RunExtensionSubtest("dial/experimental",
93                                  "discovery_no_listeners.html"));
94}
95
96// Make sure this API is only accessible to whitelisted extensions.
97IN_PROC_BROWSER_TEST_F(DialAPITest, NonWhitelistedExtension) {
98  extensions::ResultCatcher catcher;
99  catcher.RestrictToBrowserContext(browser()->profile());
100
101  ExtensionTestMessageListener listener("ready", true);
102  const extensions::Extension* extension = LoadExtensionWithFlags(
103      test_data_dir_.AppendASCII("dial/whitelist"),
104      ExtensionBrowserTest::kFlagIgnoreManifestWarnings);
105  // We should have a DIAL API not available warning.
106  ASSERT_FALSE(extension->install_warnings().empty());
107
108  EXPECT_TRUE(listener.WaitUntilSatisfied());
109
110  listener.Reply("go");
111  EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
112}
113
114IN_PROC_BROWSER_TEST_F(DialAPITest, OnError) {
115  ASSERT_TRUE(RunExtensionSubtest("dial/experimental", "on_error.html"));
116}
117