local_discovery_ui_browsertest.cc revision 58537e28ecd584eab876aee8be7156509866d23a
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 "base/basictypes.h" 6#include "base/bind.h" 7#include "base/callback.h" 8#include "base/command_line.h" 9#include "base/compiler_specific.h" 10#include "base/memory/scoped_ptr.h" 11#include "base/message_loop/message_loop.h" 12#include "chrome/browser/ui/webui/local_discovery/local_discovery_ui_handler.h" 13#include "chrome/common/chrome_switches.h" 14#include "chrome/common/url_constants.h" 15#include "chrome/test/base/ui_test_utils.cc" 16#include "chrome/test/base/web_ui_browsertest.h" 17 18namespace local_discovery { 19 20namespace { 21 22const char kSampleServiceName[] = "myService._privet._tcp.local"; 23const char kSampleDeviceID[] = "MyFakeID"; 24const char kSampleDeviceHost[] = "myservice.local"; 25 26class TestMessageLoopCondition { 27 public: 28 TestMessageLoopCondition() : signaled_(false), 29 waiting_(false) { 30 } 31 32 ~TestMessageLoopCondition() { 33 } 34 35 // Signal a waiting method that it can continue executing. 36 void Signal() { 37 signaled_ = true; 38 if (waiting_) 39 base::MessageLoop::current()->Quit(); 40 } 41 42 // Pause execution and recursively run the message loop until |Signal()| is 43 // called. Do not pause if |Signal()| has already been called. 44 void Wait() { 45 if (!signaled_) { 46 waiting_ = true; 47 base::MessageLoop::current()->Run(); 48 waiting_ = false; 49 } 50 } 51 52 private: 53 bool signaled_; 54 bool waiting_; 55 56 DISALLOW_COPY_AND_ASSIGN(TestMessageLoopCondition); 57}; 58 59class FakePrivetDeviceLister : public PrivetDeviceLister { 60 public: 61 explicit FakePrivetDeviceLister(const base::Closure& discover_devices_called) 62 : discover_devices_called_(discover_devices_called) { 63 } 64 65 virtual ~FakePrivetDeviceLister() { 66 } 67 68 // PrivetDeviceLister implementation. 69 virtual void Start() OVERRIDE { 70 } 71 72 virtual void DiscoverNewDevices(bool force_referesh) OVERRIDE { 73 discover_devices_called_.Run(); 74 } 75 76 void set_delegate(Delegate* delegate) { delegate_ = delegate; } 77 Delegate* delegate() { return delegate_; } 78 79 private: 80 Delegate* delegate_; 81 base::Closure discover_devices_called_; 82 83 DISALLOW_COPY_AND_ASSIGN(FakePrivetDeviceLister); 84}; 85 86class FakeLocalDiscoveryUIFactory : public LocalDiscoveryUIHandler::Factory { 87 public: 88 explicit FakeLocalDiscoveryUIFactory( 89 scoped_ptr<FakePrivetDeviceLister> privet_lister) { 90 owned_privet_lister_ = privet_lister.Pass(); 91 privet_lister_ = owned_privet_lister_.get(); 92 LocalDiscoveryUIHandler::SetFactory(this); 93 } 94 95 virtual ~FakeLocalDiscoveryUIFactory() { 96 LocalDiscoveryUIHandler::SetFactory(NULL); 97 } 98 99 // LocalDiscoveryUIHandler::Factory implementation. 100 virtual LocalDiscoveryUIHandler* CreateLocalDiscoveryUIHandler() OVERRIDE { 101 DCHECK(owned_privet_lister_); // This factory is a one-use factory. 102 scoped_ptr<LocalDiscoveryUIHandler> handler( 103 new LocalDiscoveryUIHandler( 104 owned_privet_lister_.PassAs<PrivetDeviceLister>())); 105 privet_lister_->set_delegate(handler.get()); 106 return handler.release(); 107 } 108 109 FakePrivetDeviceLister* privet_lister() { return privet_lister_; } 110 111 private: 112 // FakePrivetDeviceLister is owned either by the factory or, once it creates a 113 // LocalDiscoveryUI, by the LocalDiscoveryUI. |privet_lister_| points to the 114 // FakePrivetDeviceLister whereas |owned_privet_lister_| manages the ownership 115 // of the pointer when it is owned by the factory. 116 scoped_ptr<FakePrivetDeviceLister> owned_privet_lister_; 117 FakePrivetDeviceLister* privet_lister_; 118 119 DISALLOW_COPY_AND_ASSIGN(FakeLocalDiscoveryUIFactory); 120}; 121 122class LocalDiscoveryUITest : public WebUIBrowserTest { 123 public: 124 LocalDiscoveryUITest() { 125 } 126 virtual ~LocalDiscoveryUITest() { 127 } 128 129 virtual void SetUpOnMainThread() OVERRIDE { 130 WebUIBrowserTest::SetUpOnMainThread(); 131 132 scoped_ptr<FakePrivetDeviceLister> fake_lister; 133 fake_lister.reset(new FakePrivetDeviceLister( 134 base::Bind(&TestMessageLoopCondition::Signal, 135 base::Unretained(&condition_devices_listed_)))); 136 137 ui_factory_.reset(new FakeLocalDiscoveryUIFactory( 138 fake_lister.Pass())); 139 140 AddLibrary(base::FilePath(FILE_PATH_LITERAL("local_discovery_ui_test.js"))); 141 } 142 143 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { 144 WebUIBrowserTest::SetUpCommandLine(command_line); 145 } 146 147 FakeLocalDiscoveryUIFactory* ui_factory() { return ui_factory_.get(); } 148 TestMessageLoopCondition& condition_devices_listed() { 149 return condition_devices_listed_; 150 } 151 152 private: 153 scoped_ptr<FakeLocalDiscoveryUIFactory> ui_factory_; 154 TestMessageLoopCondition condition_devices_listed_; 155 156 DISALLOW_COPY_AND_ASSIGN(LocalDiscoveryUITest); 157}; 158 159IN_PROC_BROWSER_TEST_F(LocalDiscoveryUITest, EmptyTest) { 160 ui_test_utils::NavigateToURL(browser(), GURL( 161 chrome::kChromeUIDevicesFrameURL)); 162 condition_devices_listed().Wait(); 163 EXPECT_TRUE(WebUIBrowserTest::RunJavascriptTest("checkNoDevices")); 164} 165 166IN_PROC_BROWSER_TEST_F(LocalDiscoveryUITest, AddRowTest) { 167 ui_test_utils::NavigateToURL(browser(), GURL( 168 chrome::kChromeUIDevicesFrameURL)); 169 condition_devices_listed().Wait(); 170 DeviceDescription description; 171 172 description.name = "Sample device"; 173 description.description = "Sample device description"; 174 175 ui_factory()->privet_lister()->delegate()->DeviceChanged( 176 true, kSampleServiceName, description); 177 178 EXPECT_TRUE(WebUIBrowserTest::RunJavascriptTest("checkOneDevice")); 179 180 ui_factory()->privet_lister()->delegate()->DeviceRemoved( 181 kSampleServiceName); 182 183 EXPECT_TRUE(WebUIBrowserTest::RunJavascriptTest("checkNoDevices")); 184} 185 186} // namespace 187 188} // namespace local_discovery 189