summaryrefslogtreecommitdiffstats
path: root/chrome/test/chromedriver/session_unittest.cc
blob: 095fe7e6b3b0bda30a028968eabd34c9455a0a3f (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
// Copyright (c) 2012 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 <list>
#include <string>

#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/synchronization/lock.h"
#include "chrome/test/chromedriver/session.h"
#include "chrome/test/chromedriver/status.h"
#include "chrome/test/chromedriver/stub_chrome.h"
#include "chrome/test/chromedriver/stub_web_view.h"
#include "testing/gtest/include/gtest/gtest.h"

TEST(SessionAccessorTest, LocksSession) {
  scoped_ptr<Session> scoped_session(new Session("id"));
  Session* session = scoped_session.get();
  scoped_refptr<SessionAccessor> accessor(
      new SessionAccessorImpl(scoped_session.Pass()));
  scoped_ptr<base::AutoLock> lock;
  ASSERT_EQ(session, accessor->Access(&lock));
  ASSERT_TRUE(lock.get());
}

namespace {

class MockChrome : public StubChrome {
 public:
  MockChrome() : web_view_("1") {}
  virtual ~MockChrome() {}

  virtual Status GetWebViews(std::list<WebView*>* web_views) OVERRIDE {
    web_views->clear();
    web_views->push_back(&web_view_);
    return Status(kOk);
  }

 private:
  StubWebView web_view_;
};

}  // namespace

TEST(Session, GetTargetWindowNoChrome) {
  Session session("1");
  WebView* web_view;
  ASSERT_EQ(kNoSuchWindow, session.GetTargetWindow(&web_view).code());
}

TEST(Session, GetTargetWindowTargetWindowClosed) {
  scoped_ptr<Chrome> chrome(new MockChrome());
  Session session("1", chrome.Pass());
  session.window = "2";
  WebView* web_view;
  ASSERT_EQ(kNoSuchWindow, session.GetTargetWindow(&web_view).code());
}

TEST(Session, GetTargetWindowTargetWindowStillOpen) {
  scoped_ptr<Chrome> chrome(new MockChrome());
  Session session("1", chrome.Pass());
  session.window = "1";
  WebView* web_view = NULL;
  ASSERT_EQ(kOk, session.GetTargetWindow(&web_view).code());
  ASSERT_TRUE(web_view);
}