OLD | NEW |
(Empty) | |
| 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/test/base/ui_test_utils.cc" |
| 15 #include "chrome/test/base/web_ui_browsertest.h" |
| 16 |
| 17 namespace local_discovery { |
| 18 |
| 19 namespace { |
| 20 |
| 21 const char kChromeDevicesPage[] = "chrome://devices"; |
| 22 const char kSampleServiceName[] = "myService._privet._tcp.local"; |
| 23 const char kSampleDeviceID[] = "MyFakeID"; |
| 24 const char kSampleDeviceHost[] = "myservice.local"; |
| 25 |
| 26 class 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 |
| 59 class 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 |
| 86 class 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 |
| 122 class 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 command_line->AppendSwitch(switches::kEnableDeviceDiscovery); |
| 146 } |
| 147 |
| 148 FakeLocalDiscoveryUIFactory* ui_factory() { return ui_factory_.get(); } |
| 149 TestMessageLoopCondition& condition_devices_listed() { |
| 150 return condition_devices_listed_; |
| 151 } |
| 152 |
| 153 private: |
| 154 scoped_ptr<FakeLocalDiscoveryUIFactory> ui_factory_; |
| 155 TestMessageLoopCondition condition_devices_listed_; |
| 156 |
| 157 DISALLOW_COPY_AND_ASSIGN(LocalDiscoveryUITest); |
| 158 }; |
| 159 |
| 160 IN_PROC_BROWSER_TEST_F(LocalDiscoveryUITest, EmptyTest) { |
| 161 ui_test_utils::NavigateToURL(browser(), GURL(kChromeDevicesPage)); |
| 162 condition_devices_listed().Wait(); |
| 163 EXPECT_TRUE(WebUIBrowserTest::RunJavascriptTest("checkTableHasNoRows")); |
| 164 } |
| 165 |
| 166 IN_PROC_BROWSER_TEST_F(LocalDiscoveryUITest, AddRowTest) { |
| 167 ui_test_utils::NavigateToURL(browser(), GURL(kChromeDevicesPage)); |
| 168 condition_devices_listed().Wait(); |
| 169 DeviceDescription description; |
| 170 |
| 171 description.id = kSampleDeviceID; |
| 172 description.address = net::HostPortPair(kSampleDeviceHost, 8888); |
| 173 description.ip_address.push_back(1); |
| 174 description.ip_address.push_back(2); |
| 175 description.ip_address.push_back(3); |
| 176 description.ip_address.push_back(4); |
| 177 |
| 178 ui_factory()->privet_lister()->delegate()->DeviceChanged( |
| 179 true, kSampleServiceName, description); |
| 180 |
| 181 EXPECT_TRUE(WebUIBrowserTest::RunJavascriptTest("checkTableHasOneRow")); |
| 182 |
| 183 ui_factory()->privet_lister()->delegate()->DeviceRemoved( |
| 184 kSampleServiceName); |
| 185 |
| 186 EXPECT_TRUE(WebUIBrowserTest::RunJavascriptTest("checkTableHasNoRows")); |
| 187 } |
| 188 |
| 189 } // namespace |
| 190 |
| 191 } // namespace local_discovery |
OLD | NEW |