Home | History | Annotate | Download | only in dial
      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 "chrome/common/chrome_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:
     25   DialAPITest() {}
     26 
     27   virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
     28     ExtensionApiTest::SetUpCommandLine(command_line);
     29     command_line->AppendSwitchASCII(
     30         switches::kWhitelistedExtensionID, "ddchlicdkolnonkihahngkmmmjnjlkkf");
     31   }
     32 };
     33 
     34 }  // namespace
     35 
     36 // http://crbug.com/177163
     37 #if defined(OS_WIN) && !defined(NDEBUG)
     38 #define MAYBE_DeviceListEvents DISABLED_DeviceListEvents
     39 #else
     40 #define MAYBE_DeviceListEvents DeviceListEvents
     41 #endif
     42 // Test receiving DIAL API events.
     43 IN_PROC_BROWSER_TEST_F(DialAPITest, MAYBE_DeviceListEvents) {
     44   // Setup the test.
     45   ASSERT_TRUE(RunExtensionSubtest("dial/experimental", "device_list.html"));
     46 
     47   // Send three device list updates.
     48   scoped_refptr<extensions::DialAPI> api =
     49       extensions::DialAPIFactory::GetInstance()->GetForProfile(profile());
     50   ASSERT_TRUE(api.get());
     51   extensions::DialRegistry::DeviceList devices;
     52 
     53 
     54   ResultCatcher catcher;
     55 
     56   DialDeviceData device1;
     57   device1.set_device_id("1");
     58   device1.set_label("1");
     59   device1.set_device_description_url(GURL("http://127.0.0.1/dd.xml"));
     60 
     61   devices.push_back(device1);
     62   api->SendEventOnUIThread(devices);
     63 
     64   DialDeviceData device2;
     65   device2.set_device_id("2");
     66   device2.set_label("2");
     67   device2.set_device_description_url(GURL("http://127.0.0.2/dd.xml"));
     68 
     69   devices.push_back(device2);
     70   api->SendEventOnUIThread(devices);
     71 
     72   DialDeviceData device3;
     73   device3.set_device_id("3");
     74   device3.set_label("3");
     75   device3.set_device_description_url(GURL("http://127.0.0.3/dd.xml"));
     76 
     77   devices.push_back(device3);
     78   api->SendEventOnUIThread(devices);
     79 
     80   EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
     81 }
     82 
     83 // Test discoverNow fails if there are no listeners. When there are no listeners
     84 // the DIAL API will not be active.
     85 IN_PROC_BROWSER_TEST_F(DialAPITest, Discovery) {
     86   ASSERT_TRUE(RunExtensionSubtest("dial/experimental", "discovery.html"));
     87 }
     88 
     89 // Make sure this API is only accessible to whitelisted extensions.
     90 IN_PROC_BROWSER_TEST_F(DialAPITest, NonWhitelistedExtension) {
     91   ResultCatcher catcher;
     92   catcher.RestrictToProfile(browser()->profile());
     93 
     94   ExtensionTestMessageListener listener("ready", true);
     95   const extensions::Extension* extension = LoadExtensionWithFlags(
     96       test_data_dir_.AppendASCII("dial/whitelist"),
     97       ExtensionBrowserTest::kFlagIgnoreManifestWarnings);
     98   // We should have a DIAL API not available warning.
     99   ASSERT_FALSE(extension->install_warnings().empty());
    100 
    101   EXPECT_TRUE(listener.WaitUntilSatisfied());
    102 
    103   listener.Reply("go");
    104   EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
    105 }
    106 
    107 IN_PROC_BROWSER_TEST_F(DialAPITest, OnError) {
    108   ASSERT_TRUE(RunExtensionSubtest("dial/experimental", "on_error.html"));
    109 }
    110