summaryrefslogtreecommitdiffstats
path: root/content/browser/media/media_internals_unittest.cc
blob: 0db25803310dda5a08a26a973b358a2a2707c3bb (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
// Copyright (c) 2011 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 "content/browser/media/media_internals.h"

#include "base/bind.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop.h"
#include "content/public/test/test_browser_thread.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace content {
namespace {

class MockObserverBaseClass {
 public:
  ~MockObserverBaseClass() {}
  virtual void OnUpdate(const string16& javascript) = 0;
};

class MockMediaInternalsObserver : public MockObserverBaseClass {
 public:
  virtual ~MockMediaInternalsObserver() {}
  MOCK_METHOD1(OnUpdate, void(const string16& javascript));
};

}  // namespace

class MediaInternalsTest : public testing::Test {
 public:
  MediaInternalsTest() : io_thread_(BrowserThread::IO, &loop_) {}
  base::DictionaryValue* data() {
    return &internals_->data_;
  }

  void DeleteItem(const std::string& item) {
    internals_->DeleteItem(item);
  }

  void UpdateItem(const std::string& item, const std::string& property,
                  base::Value* value) {
    internals_->UpdateItem("", item, property, value);
  }

  void SendUpdate(const std::string& function, base::Value* value) {
    internals_->SendUpdate(function, value);
  }

 protected:
  virtual void SetUp() {
    internals_.reset(new MediaInternals());
  }

  MessageLoop loop_;
  TestBrowserThread io_thread_;
  scoped_ptr<MediaInternals> internals_;
};

TEST_F(MediaInternalsTest, UpdateAddsNewItem) {
  UpdateItem("some.item", "testing", new base::FundamentalValue(true));
  bool testing = false;
  std::string id;

  EXPECT_TRUE(data()->GetBoolean("some.item.testing", &testing));
  EXPECT_TRUE(testing);

  EXPECT_TRUE(data()->GetString("some.item.id", &id));
  EXPECT_EQ(id, "some.item");
}

TEST_F(MediaInternalsTest, UpdateModifiesExistingItem) {
  UpdateItem("some.item", "testing", new base::FundamentalValue(true));
  UpdateItem("some.item", "value", new base::FundamentalValue(5));
  UpdateItem("some.item", "testing", new base::FundamentalValue(false));
  bool testing = true;
  int value = 0;
  std::string id;

  EXPECT_TRUE(data()->GetBoolean("some.item.testing", &testing));
  EXPECT_FALSE(testing);

  EXPECT_TRUE(data()->GetInteger("some.item.value", &value));
  EXPECT_EQ(value, 5);

  EXPECT_TRUE(data()->GetString("some.item.id", &id));
  EXPECT_EQ(id, "some.item");
}

TEST_F(MediaInternalsTest, ObserversReceiveNotifications) {
  scoped_ptr<MockMediaInternalsObserver> observer(
      new MockMediaInternalsObserver());

  EXPECT_CALL(*observer.get(), OnUpdate(testing::_)).Times(1);

  MediaInternals::UpdateCallback callback = base::Bind(
      &MockMediaInternalsObserver::OnUpdate, base::Unretained(observer.get()));

  internals_->AddUpdateCallback(callback);
  SendUpdate("fn", data());
}

TEST_F(MediaInternalsTest, RemovedObserversReceiveNoNotifications) {
  scoped_ptr<MockMediaInternalsObserver> observer(
      new MockMediaInternalsObserver());

  EXPECT_CALL(*observer.get(), OnUpdate(testing::_)).Times(0);

  MediaInternals::UpdateCallback callback = base::Bind(
      &MockMediaInternalsObserver::OnUpdate, base::Unretained(observer.get()));

  internals_->AddUpdateCallback(callback);
  internals_->RemoveUpdateCallback(callback);
  SendUpdate("fn", data());
}

TEST_F(MediaInternalsTest, DeleteRemovesItem) {
  base::Value* out;

  UpdateItem("some.item", "testing", base::Value::CreateNullValue());
  EXPECT_TRUE(data()->Get("some.item", &out));
  EXPECT_TRUE(data()->Get("some", &out));

  DeleteItem("some.item");
  EXPECT_FALSE(data()->Get("some.item", &out));
  EXPECT_TRUE(data()->Get("some", &out));

  DeleteItem("some");
  EXPECT_FALSE(data()->Get("some.item", &out));
  EXPECT_FALSE(data()->Get("some", &out));
}

}  // namespace content