Index: chrome/browser/ui/webui/local_discovery/local_discovery_ui_browsertest.cc |
diff --git a/chrome/browser/ui/webui/local_discovery/local_discovery_ui_browsertest.cc b/chrome/browser/ui/webui/local_discovery/local_discovery_ui_browsertest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..fa19bc46062fd3420221ca8ac0f941acf53727ec |
--- /dev/null |
+++ b/chrome/browser/ui/webui/local_discovery/local_discovery_ui_browsertest.cc |
@@ -0,0 +1,217 @@ |
+// Copyright 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "base/basictypes.h" |
+#include "base/bind.h" |
+#include "base/callback.h" |
+#include "base/command_line.h" |
+#include "base/compiler_specific.h" |
+#include "base/memory/scoped_ptr.h" |
+#include "base/message_loop/message_loop.h" |
+#include "chrome/browser/ui/webui/local_discovery/local_discovery_ui_handler.h" |
+#include "chrome/common/chrome_switches.h" |
+#include "chrome/test/base/ui_test_utils.cc" |
+#include "chrome/test/base/web_ui_browsertest.h" |
+ |
+namespace local_discovery { |
+ |
+namespace { |
+ |
+const char kChromeDevicesPage[] = "chrome://devices"; |
+const char kSampleServiceName[] = "myService._privet._tcp.local"; |
+const char kSampleDeviceID[] = "MyFakeID"; |
+const char kSampleDeviceHost[] = "myservice.local"; |
+ |
+class TestMessageLoopCondition { |
+ public: |
+ TestMessageLoopCondition(); |
+ ~TestMessageLoopCondition(); |
+ |
+ void Signal(); |
+ void Wait(); |
Dan Beam
2013/08/13 00:30:43
doc comments
Noam Samuel
2013/08/13 18:45:38
Done.
|
+ |
+ private: |
+ bool set_; |
Dan Beam
2013/08/13 00:30:43
nit: signaled_ or signaling_
Noam Samuel
2013/08/13 18:45:38
Done.
|
+ bool waiting_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(TestMessageLoopCondition); |
+}; |
+ |
+class FakePrivetDeviceLister : public PrivetDeviceLister { |
+ public: |
+ explicit FakePrivetDeviceLister(base::Closure discover_devices_called); |
+ virtual ~FakePrivetDeviceLister(); |
+ |
+ virtual void Start() OVERRIDE; |
+ |
+ virtual void DiscoverNewDevices(bool force_referesh) OVERRIDE; |
+ |
+ void set_delegate(Delegate* delegate) { delegate_ = delegate; } |
+ Delegate* delegate() { return delegate_; } |
+ |
+ private: |
+ Delegate* delegate_; |
+base::Closure discover_devices_called_; |
Dan Beam
2013/08/13 00:30:43
indent off
Noam Samuel
2013/08/13 18:45:38
Done.
|
+ |
+ DISALLOW_COPY_AND_ASSIGN(FakePrivetDeviceLister); |
+}; |
+ |
+class FakeLocalDiscoveryUIFactory : public LocalDiscoveryUIHandler::Factory { |
+ public: |
+ explicit FakeLocalDiscoveryUIFactory( |
+ scoped_ptr<FakePrivetDeviceLister> privet_lister); |
+ |
+virtual ~FakeLocalDiscoveryUIFactory(); |
Dan Beam
2013/08/13 00:30:43
indent off
Noam Samuel
2013/08/13 18:45:38
Done.
|
+ |
+ virtual LocalDiscoveryUIHandler* CreateLocalDiscoveryUIHandler() OVERRIDE; |
+ |
+ FakePrivetDeviceLister* privet_lister() { return privet_lister_; } |
+ |
+ private: |
+ // FakePrivetDeviceLister is owned either by the factory or, once it creates a |
+ // LocalDiscoveryUI, by the LocalDiscoveryUI. |privet_lister_| points to the |
+ // FakePrivetDeviceLister whereas |owned_privet_lister_| manages the ownership |
+ // of the pointer when it is owned by the factory. |
+ scoped_ptr<FakePrivetDeviceLister> owned_privet_lister_; |
+ FakePrivetDeviceLister* privet_lister_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(FakeLocalDiscoveryUIFactory); |
+}; |
+ |
+ |
+class LocalDiscoveryUITest : public WebUIBrowserTest { |
+ public: |
+ LocalDiscoveryUITest(); |
+ virtual ~LocalDiscoveryUITest(); |
+ |
+ virtual void SetUpOnMainThread() OVERRIDE; |
+ virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE; |
+ |
+ FakeLocalDiscoveryUIFactory* ui_factory() { return ui_factory_.get(); } |
+ TestMessageLoopCondition& condition_devices_listed() { |
+ return condition_devices_listed_; |
+ } |
+ |
+ private: |
+ scoped_ptr<FakeLocalDiscoveryUIFactory> ui_factory_; |
+ TestMessageLoopCondition condition_devices_listed_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(LocalDiscoveryUITest); |
+}; |
+ |
+ |
+TestMessageLoopCondition::TestMessageLoopCondition() : set_(false), |
+ waiting_(false) { |
+} |
+ |
+TestMessageLoopCondition::~TestMessageLoopCondition() { |
+} |
+ |
+void TestMessageLoopCondition::Signal() { |
+ set_ = true; |
+ if (waiting_) |
+ base::MessageLoop::current()->Quit(); |
+} |
+ |
+void TestMessageLoopCondition::Wait() { |
+ if (!set_) { |
+ waiting_ = true; |
+ base::MessageLoop::current()->Run(); |
+ waiting_ = false; |
+ } |
+} |
+ |
+FakePrivetDeviceLister::FakePrivetDeviceLister( |
+ base::Closure discover_devices_called) |
Dan Beam
2013/08/13 00:30:43
nit: const-ref, e.g.
const base::Closure& disco
Noam Samuel
2013/08/13 18:45:38
Done.
|
+ : discover_devices_called_(discover_devices_called) { |
+} |
+ |
+FakePrivetDeviceLister::~FakePrivetDeviceLister() { |
+} |
+ |
+void FakePrivetDeviceLister::Start() { |
+} |
+ |
+void FakePrivetDeviceLister::DiscoverNewDevices(bool force_referesh) { |
+ discover_devices_called_.Run(); |
+} |
+ |
+FakeLocalDiscoveryUIFactory::FakeLocalDiscoveryUIFactory( |
+ scoped_ptr<FakePrivetDeviceLister> privet_lister) { |
+ owned_privet_lister_ = privet_lister.Pass(); |
+ privet_lister_ = owned_privet_lister_.get(); |
+ LocalDiscoveryUIHandler::SetFactory(this); |
+} |
+ |
+FakeLocalDiscoveryUIFactory::~FakeLocalDiscoveryUIFactory() { |
+ LocalDiscoveryUIHandler::SetFactory(NULL); |
+} |
+ |
+LocalDiscoveryUIHandler* |
+FakeLocalDiscoveryUIFactory::CreateLocalDiscoveryUIHandler() { |
+ DCHECK(owned_privet_lister_); // This factory is a one-use factory. |
+ scoped_ptr<LocalDiscoveryUIHandler> handler( |
+ new LocalDiscoveryUIHandler( |
+ owned_privet_lister_.PassAs<PrivetDeviceLister>())); |
+ privet_lister_->set_delegate(handler.get()); |
+ return handler.release(); |
+} |
+ |
+LocalDiscoveryUITest::LocalDiscoveryUITest() { |
+} |
+ |
+LocalDiscoveryUITest::~LocalDiscoveryUITest() { |
+} |
+ |
+void LocalDiscoveryUITest::SetUpOnMainThread() { |
+ WebUIBrowserTest::SetUpOnMainThread(); |
+ |
+ scoped_ptr<FakePrivetDeviceLister> fake_lister; |
+ fake_lister.reset(new FakePrivetDeviceLister( |
+ base::Bind(&TestMessageLoopCondition::Signal, |
+ base::Unretained(&condition_devices_listed_)))); |
+ |
+ ui_factory_.reset(new FakeLocalDiscoveryUIFactory( |
+ fake_lister.Pass())); |
+ |
+ AddLibrary(base::FilePath("local_discovery_ui_test.js")); |
+} |
+ |
+void LocalDiscoveryUITest::SetUpCommandLine(CommandLine* command_line) { |
+ WebUIBrowserTest::SetUpCommandLine(command_line); |
+ command_line->AppendSwitch(switches::kEnableDeviceDiscovery); |
+} |
Dan Beam
2013/08/13 00:30:43
instead of
class ClassName {
Method(...) OV
Noam Samuel
2013/08/13 18:45:38
Done.
|
+ |
+IN_PROC_BROWSER_TEST_F(LocalDiscoveryUITest, EmptyTest) { |
+ ui_test_utils::NavigateToURL(browser(), GURL(kChromeDevicesPage)); |
+ condition_devices_listed().Wait(); |
+ EXPECT_TRUE(WebUIBrowserTest::RunJavascriptTest("checkTableHasNoRows")); |
+} |
+ |
+IN_PROC_BROWSER_TEST_F(LocalDiscoveryUITest, AddRowTest) { |
+ ui_test_utils::NavigateToURL(browser(), GURL(kChromeDevicesPage)); |
+ condition_devices_listed().Wait(); |
+ DeviceDescription description; |
+ |
+ description.id = kSampleDeviceID; |
+ description.address = net::HostPortPair(kSampleDeviceHost, 8888); |
+ description.ip_address.push_back(1); |
+ description.ip_address.push_back(2); |
+ description.ip_address.push_back(3); |
+ description.ip_address.push_back(4); |
+ |
+ ui_factory()->privet_lister()->delegate()->DeviceChanged( |
+ true, kSampleServiceName, description); |
+ |
+ EXPECT_TRUE(WebUIBrowserTest::RunJavascriptTest("checkTableHasOneRow")); |
+ |
+ ui_factory()->privet_lister()->delegate()->DeviceRemoved( |
+ kSampleServiceName); |
+ |
+ EXPECT_TRUE(WebUIBrowserTest::RunJavascriptTest("checkTableHasNoRows")); |
+} |
+ |
+} // namespace |
+ |
+} // namespace local_discovery |