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(); | |
29 ~TestMessageLoopCondition(); | |
30 | |
31 void Signal(); | |
32 void Wait(); | |
Dan Beam
2013/08/13 00:30:43
doc comments
Noam Samuel
2013/08/13 18:45:38
Done.
| |
33 | |
34 private: | |
35 bool set_; | |
Dan Beam
2013/08/13 00:30:43
nit: signaled_ or signaling_
Noam Samuel
2013/08/13 18:45:38
Done.
| |
36 bool waiting_; | |
37 | |
38 DISALLOW_COPY_AND_ASSIGN(TestMessageLoopCondition); | |
39 }; | |
40 | |
41 class FakePrivetDeviceLister : public PrivetDeviceLister { | |
42 public: | |
43 explicit FakePrivetDeviceLister(base::Closure discover_devices_called); | |
44 virtual ~FakePrivetDeviceLister(); | |
45 | |
46 virtual void Start() OVERRIDE; | |
47 | |
48 virtual void DiscoverNewDevices(bool force_referesh) OVERRIDE; | |
49 | |
50 void set_delegate(Delegate* delegate) { delegate_ = delegate; } | |
51 Delegate* delegate() { return delegate_; } | |
52 | |
53 private: | |
54 Delegate* delegate_; | |
55 base::Closure discover_devices_called_; | |
Dan Beam
2013/08/13 00:30:43
indent off
Noam Samuel
2013/08/13 18:45:38
Done.
| |
56 | |
57 DISALLOW_COPY_AND_ASSIGN(FakePrivetDeviceLister); | |
58 }; | |
59 | |
60 class FakeLocalDiscoveryUIFactory : public LocalDiscoveryUIHandler::Factory { | |
61 public: | |
62 explicit FakeLocalDiscoveryUIFactory( | |
63 scoped_ptr<FakePrivetDeviceLister> privet_lister); | |
64 | |
65 virtual ~FakeLocalDiscoveryUIFactory(); | |
Dan Beam
2013/08/13 00:30:43
indent off
Noam Samuel
2013/08/13 18:45:38
Done.
| |
66 | |
67 virtual LocalDiscoveryUIHandler* CreateLocalDiscoveryUIHandler() OVERRIDE; | |
68 | |
69 FakePrivetDeviceLister* privet_lister() { return privet_lister_; } | |
70 | |
71 private: | |
72 // FakePrivetDeviceLister is owned either by the factory or, once it creates a | |
73 // LocalDiscoveryUI, by the LocalDiscoveryUI. |privet_lister_| points to the | |
74 // FakePrivetDeviceLister whereas |owned_privet_lister_| manages the ownership | |
75 // of the pointer when it is owned by the factory. | |
76 scoped_ptr<FakePrivetDeviceLister> owned_privet_lister_; | |
77 FakePrivetDeviceLister* privet_lister_; | |
78 | |
79 DISALLOW_COPY_AND_ASSIGN(FakeLocalDiscoveryUIFactory); | |
80 }; | |
81 | |
82 | |
83 class LocalDiscoveryUITest : public WebUIBrowserTest { | |
84 public: | |
85 LocalDiscoveryUITest(); | |
86 virtual ~LocalDiscoveryUITest(); | |
87 | |
88 virtual void SetUpOnMainThread() OVERRIDE; | |
89 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE; | |
90 | |
91 FakeLocalDiscoveryUIFactory* ui_factory() { return ui_factory_.get(); } | |
92 TestMessageLoopCondition& condition_devices_listed() { | |
93 return condition_devices_listed_; | |
94 } | |
95 | |
96 private: | |
97 scoped_ptr<FakeLocalDiscoveryUIFactory> ui_factory_; | |
98 TestMessageLoopCondition condition_devices_listed_; | |
99 | |
100 DISALLOW_COPY_AND_ASSIGN(LocalDiscoveryUITest); | |
101 }; | |
102 | |
103 | |
104 TestMessageLoopCondition::TestMessageLoopCondition() : set_(false), | |
105 waiting_(false) { | |
106 } | |
107 | |
108 TestMessageLoopCondition::~TestMessageLoopCondition() { | |
109 } | |
110 | |
111 void TestMessageLoopCondition::Signal() { | |
112 set_ = true; | |
113 if (waiting_) | |
114 base::MessageLoop::current()->Quit(); | |
115 } | |
116 | |
117 void TestMessageLoopCondition::Wait() { | |
118 if (!set_) { | |
119 waiting_ = true; | |
120 base::MessageLoop::current()->Run(); | |
121 waiting_ = false; | |
122 } | |
123 } | |
124 | |
125 FakePrivetDeviceLister::FakePrivetDeviceLister( | |
126 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.
| |
127 : discover_devices_called_(discover_devices_called) { | |
128 } | |
129 | |
130 FakePrivetDeviceLister::~FakePrivetDeviceLister() { | |
131 } | |
132 | |
133 void FakePrivetDeviceLister::Start() { | |
134 } | |
135 | |
136 void FakePrivetDeviceLister::DiscoverNewDevices(bool force_referesh) { | |
137 discover_devices_called_.Run(); | |
138 } | |
139 | |
140 FakeLocalDiscoveryUIFactory::FakeLocalDiscoveryUIFactory( | |
141 scoped_ptr<FakePrivetDeviceLister> privet_lister) { | |
142 owned_privet_lister_ = privet_lister.Pass(); | |
143 privet_lister_ = owned_privet_lister_.get(); | |
144 LocalDiscoveryUIHandler::SetFactory(this); | |
145 } | |
146 | |
147 FakeLocalDiscoveryUIFactory::~FakeLocalDiscoveryUIFactory() { | |
148 LocalDiscoveryUIHandler::SetFactory(NULL); | |
149 } | |
150 | |
151 LocalDiscoveryUIHandler* | |
152 FakeLocalDiscoveryUIFactory::CreateLocalDiscoveryUIHandler() { | |
153 DCHECK(owned_privet_lister_); // This factory is a one-use factory. | |
154 scoped_ptr<LocalDiscoveryUIHandler> handler( | |
155 new LocalDiscoveryUIHandler( | |
156 owned_privet_lister_.PassAs<PrivetDeviceLister>())); | |
157 privet_lister_->set_delegate(handler.get()); | |
158 return handler.release(); | |
159 } | |
160 | |
161 LocalDiscoveryUITest::LocalDiscoveryUITest() { | |
162 } | |
163 | |
164 LocalDiscoveryUITest::~LocalDiscoveryUITest() { | |
165 } | |
166 | |
167 void LocalDiscoveryUITest::SetUpOnMainThread() { | |
168 WebUIBrowserTest::SetUpOnMainThread(); | |
169 | |
170 scoped_ptr<FakePrivetDeviceLister> fake_lister; | |
171 fake_lister.reset(new FakePrivetDeviceLister( | |
172 base::Bind(&TestMessageLoopCondition::Signal, | |
173 base::Unretained(&condition_devices_listed_)))); | |
174 | |
175 ui_factory_.reset(new FakeLocalDiscoveryUIFactory( | |
176 fake_lister.Pass())); | |
177 | |
178 AddLibrary(base::FilePath("local_discovery_ui_test.js")); | |
179 } | |
180 | |
181 void LocalDiscoveryUITest::SetUpCommandLine(CommandLine* command_line) { | |
182 WebUIBrowserTest::SetUpCommandLine(command_line); | |
183 command_line->AppendSwitch(switches::kEnableDeviceDiscovery); | |
184 } | |
Dan Beam
2013/08/13 00:30:43
instead of
class ClassName {
Method(...) OV
Noam Samuel
2013/08/13 18:45:38
Done.
| |
185 | |
186 IN_PROC_BROWSER_TEST_F(LocalDiscoveryUITest, EmptyTest) { | |
187 ui_test_utils::NavigateToURL(browser(), GURL(kChromeDevicesPage)); | |
188 condition_devices_listed().Wait(); | |
189 EXPECT_TRUE(WebUIBrowserTest::RunJavascriptTest("checkTableHasNoRows")); | |
190 } | |
191 | |
192 IN_PROC_BROWSER_TEST_F(LocalDiscoveryUITest, AddRowTest) { | |
193 ui_test_utils::NavigateToURL(browser(), GURL(kChromeDevicesPage)); | |
194 condition_devices_listed().Wait(); | |
195 DeviceDescription description; | |
196 | |
197 description.id = kSampleDeviceID; | |
198 description.address = net::HostPortPair(kSampleDeviceHost, 8888); | |
199 description.ip_address.push_back(1); | |
200 description.ip_address.push_back(2); | |
201 description.ip_address.push_back(3); | |
202 description.ip_address.push_back(4); | |
203 | |
204 ui_factory()->privet_lister()->delegate()->DeviceChanged( | |
205 true, kSampleServiceName, description); | |
206 | |
207 EXPECT_TRUE(WebUIBrowserTest::RunJavascriptTest("checkTableHasOneRow")); | |
208 | |
209 ui_factory()->privet_lister()->delegate()->DeviceRemoved( | |
210 kSampleServiceName); | |
211 | |
212 EXPECT_TRUE(WebUIBrowserTest::RunJavascriptTest("checkTableHasNoRows")); | |
213 } | |
214 | |
215 } // namespace | |
216 | |
217 } // namespace local_discovery | |
OLD | NEW |