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 "chrome/browser/extensions/extension_test_message_listener.h"
12 #include "extensions/common/switches.h"
13 #include "testing/gmock/include/gmock/gmock.h"
14 #include "url/gurl.h"
15
16 using extensions::DialDeviceData;
17 using extensions::Extension;
18
19 namespace api = extensions::api;
20
21 namespace {
22
23 class DialAPITest : public ExtensionApiTest {
24 public:
DialAPITest()25 DialAPITest() {}
26
SetUpCommandLine(CommandLine * command_line)27 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
28 ExtensionApiTest::SetUpCommandLine(command_line);
29 command_line->AppendSwitchASCII(
30 extensions::switches::kWhitelistedExtensionID,
31 "ddchlicdkolnonkihahngkmmmjnjlkkf");
32 }
33 };
34
35 } // namespace
36
37 // http://crbug.com/177163
38 #if defined(OS_WIN) && !defined(NDEBUG)
39 #define MAYBE_DeviceListEvents DISABLED_DeviceListEvents
40 #else
41 #define MAYBE_DeviceListEvents DeviceListEvents
42 #endif
43 // Test receiving DIAL API events.
IN_PROC_BROWSER_TEST_F(DialAPITest,MAYBE_DeviceListEvents)44 IN_PROC_BROWSER_TEST_F(DialAPITest, MAYBE_DeviceListEvents) {
45 // Setup the test.
46 ASSERT_TRUE(RunExtensionSubtest("dial/experimental", "device_list.html"));
47
48 // Send three device list updates.
49 scoped_refptr<extensions::DialAPI> api =
50 extensions::DialAPIFactory::GetInstance()->GetForBrowserContext(
51 profile());
52 ASSERT_TRUE(api.get());
53 extensions::DialRegistry::DeviceList devices;
54
55
56 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
85 // Test discoverNow fails if there are no listeners. When there are no listeners
86 // the DIAL API will not be active.
IN_PROC_BROWSER_TEST_F(DialAPITest,Discovery)87 IN_PROC_BROWSER_TEST_F(DialAPITest, Discovery) {
88 ASSERT_TRUE(RunExtensionSubtest("dial/experimental", "discovery.html"));
89 }
90
91 // Make sure this API is only accessible to whitelisted extensions.
IN_PROC_BROWSER_TEST_F(DialAPITest,NonWhitelistedExtension)92 IN_PROC_BROWSER_TEST_F(DialAPITest, NonWhitelistedExtension) {
93 ResultCatcher catcher;
94 catcher.RestrictToProfile(browser()->profile());
95
96 ExtensionTestMessageListener listener("ready", true);
97 const extensions::Extension* extension = LoadExtensionWithFlags(
98 test_data_dir_.AppendASCII("dial/whitelist"),
99 ExtensionBrowserTest::kFlagIgnoreManifestWarnings);
100 // We should have a DIAL API not available warning.
101 ASSERT_FALSE(extension->install_warnings().empty());
102
103 EXPECT_TRUE(listener.WaitUntilSatisfied());
104
105 listener.Reply("go");
106 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
107 }
108
IN_PROC_BROWSER_TEST_F(DialAPITest,OnError)109 IN_PROC_BROWSER_TEST_F(DialAPITest, OnError) {
110 ASSERT_TRUE(RunExtensionSubtest("dial/experimental", "on_error.html"));
111 }
112