summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/webui/local_discovery/local_discovery_ui_browsertest.cc
blob: 78d04ff8225ba64381646712a03a65ae88f79752 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// 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/common/url_constants.h"
#include "chrome/test/base/ui_test_utils.cc"
#include "chrome/test/base/web_ui_browsertest.h"

namespace local_discovery {

namespace {

const char kSampleServiceName[] = "myService._privet._tcp.local";

class TestMessageLoopCondition {
 public:
  TestMessageLoopCondition() : signaled_(false),
                               waiting_(false) {
  }

  ~TestMessageLoopCondition() {
  }

  // Signal a waiting method that it can continue executing.
  void Signal() {
    signaled_ = true;
    if (waiting_)
      base::MessageLoop::current()->Quit();
  }

  // Pause execution and recursively run the message loop until |Signal()| is
  // called. Do not pause if |Signal()| has already been called.
  void Wait() {
    if (!signaled_) {
      waiting_ = true;
      base::MessageLoop::current()->Run();
      waiting_ = false;
    }
  }

 private:
  bool signaled_;
  bool waiting_;

  DISALLOW_COPY_AND_ASSIGN(TestMessageLoopCondition);
};

class FakePrivetDeviceLister : public PrivetDeviceLister {
 public:
  explicit FakePrivetDeviceLister(const base::Closure& discover_devices_called)
      : discover_devices_called_(discover_devices_called) {
  }

  virtual ~FakePrivetDeviceLister() {
  }

  // PrivetDeviceLister implementation.
  virtual void Start() OVERRIDE {
  }

  virtual void DiscoverNewDevices(bool force_referesh) OVERRIDE {
    discover_devices_called_.Run();
  }

  void set_delegate(Delegate* delegate) { delegate_ = delegate; }
  Delegate* delegate() { return delegate_; }

 private:
  Delegate* delegate_;
  base::Closure discover_devices_called_;

  DISALLOW_COPY_AND_ASSIGN(FakePrivetDeviceLister);
};

class FakeLocalDiscoveryUIFactory : public LocalDiscoveryUIHandler::Factory {
 public:
  explicit FakeLocalDiscoveryUIFactory(
      scoped_ptr<FakePrivetDeviceLister> privet_lister) {
    owned_privet_lister_ = privet_lister.Pass();
    privet_lister_ = owned_privet_lister_.get();
    LocalDiscoveryUIHandler::SetFactory(this);
  }

  virtual ~FakeLocalDiscoveryUIFactory() {
    LocalDiscoveryUIHandler::SetFactory(NULL);
  }

  // LocalDiscoveryUIHandler::Factory implementation.
  virtual LocalDiscoveryUIHandler* CreateLocalDiscoveryUIHandler() OVERRIDE {
    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();
  }

  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 {
    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(FILE_PATH_LITERAL("local_discovery_ui_test.js")));
  }

  virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
    WebUIBrowserTest::SetUpCommandLine(command_line);
  }

  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);
};

IN_PROC_BROWSER_TEST_F(LocalDiscoveryUITest, EmptyTest) {
  ui_test_utils::NavigateToURL(browser(), GURL(
      chrome::kChromeUIDevicesURL));
  condition_devices_listed().Wait();
  EXPECT_TRUE(WebUIBrowserTest::RunJavascriptTest("checkNoDevices"));
}

IN_PROC_BROWSER_TEST_F(LocalDiscoveryUITest, AddRowTest) {
  ui_test_utils::NavigateToURL(browser(), GURL(
      chrome::kChromeUIDevicesURL));
  condition_devices_listed().Wait();
  DeviceDescription description;

  description.name = "Sample device";
  description.description = "Sample device description";

  ui_factory()->privet_lister()->delegate()->DeviceChanged(
      true, kSampleServiceName, description);

  EXPECT_TRUE(WebUIBrowserTest::RunJavascriptTest("checkOneDevice"));

  ui_factory()->privet_lister()->delegate()->DeviceRemoved(
      kSampleServiceName);

  EXPECT_TRUE(WebUIBrowserTest::RunJavascriptTest("checkNoDevices"));
}

}  // namespace

}  // namespace local_discovery