| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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 "chrome/browser/media/media_internals.h" | |
| 6 | |
| 7 #include "base/memory/scoped_ptr.h" | |
| 8 #include "base/message_loop.h" | |
| 9 #include "chrome/browser/media/media_internals_observer.h" | |
| 10 #include "content/public/test/test_browser_thread.h" | |
| 11 #include "testing/gmock/include/gmock/gmock.h" | |
| 12 #include "testing/gtest/include/gtest/gtest.h" | |
| 13 | |
| 14 class MockMediaInternalsObserver : public MediaInternalsObserver { | |
| 15 public: | |
| 16 MOCK_METHOD1(OnUpdate, void(const string16& javascript)); | |
| 17 }; | |
| 18 | |
| 19 class MediaInternalsTest : public testing::Test { | |
| 20 public: | |
| 21 MediaInternalsTest() : io_thread_(content::BrowserThread::IO, &loop_) {} | |
| 22 DictionaryValue* data() { | |
| 23 return &internals_->data_; | |
| 24 } | |
| 25 | |
| 26 void DeleteItem(const std::string& item) { | |
| 27 internals_->DeleteItem(item); | |
| 28 } | |
| 29 | |
| 30 void UpdateItem(const std::string& item, const std::string& property, | |
| 31 Value* value) { | |
| 32 internals_->UpdateItem("", item, property, value); | |
| 33 } | |
| 34 | |
| 35 void SendUpdate(const std::string& function, Value* value) { | |
| 36 internals_->SendUpdate(function, value); | |
| 37 } | |
| 38 | |
| 39 protected: | |
| 40 virtual void SetUp() { | |
| 41 internals_.reset(new MediaInternals()); | |
| 42 } | |
| 43 | |
| 44 MessageLoop loop_; | |
| 45 content::TestBrowserThread io_thread_; | |
| 46 scoped_ptr<MediaInternals> internals_; | |
| 47 }; | |
| 48 | |
| 49 TEST_F(MediaInternalsTest, UpdateAddsNewItem) { | |
| 50 UpdateItem("some.item", "testing", Value::CreateBooleanValue(true)); | |
| 51 bool testing = false; | |
| 52 std::string id; | |
| 53 | |
| 54 EXPECT_TRUE(data()->GetBoolean("some.item.testing", &testing)); | |
| 55 EXPECT_TRUE(testing); | |
| 56 | |
| 57 EXPECT_TRUE(data()->GetString("some.item.id", &id)); | |
| 58 EXPECT_EQ(id, "some.item"); | |
| 59 } | |
| 60 | |
| 61 TEST_F(MediaInternalsTest, UpdateModifiesExistingItem) { | |
| 62 UpdateItem("some.item", "testing", Value::CreateBooleanValue(true)); | |
| 63 UpdateItem("some.item", "value", Value::CreateIntegerValue(5)); | |
| 64 UpdateItem("some.item", "testing", Value::CreateBooleanValue(false)); | |
| 65 bool testing = true; | |
| 66 int value = 0; | |
| 67 std::string id; | |
| 68 | |
| 69 EXPECT_TRUE(data()->GetBoolean("some.item.testing", &testing)); | |
| 70 EXPECT_FALSE(testing); | |
| 71 | |
| 72 EXPECT_TRUE(data()->GetInteger("some.item.value", &value)); | |
| 73 EXPECT_EQ(value, 5); | |
| 74 | |
| 75 EXPECT_TRUE(data()->GetString("some.item.id", &id)); | |
| 76 EXPECT_EQ(id, "some.item"); | |
| 77 } | |
| 78 | |
| 79 TEST_F(MediaInternalsTest, ObserversReceiveNotifications) { | |
| 80 scoped_ptr<MockMediaInternalsObserver> observer( | |
| 81 new MockMediaInternalsObserver()); | |
| 82 | |
| 83 EXPECT_CALL(*observer.get(), OnUpdate(testing::_)).Times(1); | |
| 84 | |
| 85 internals_->AddObserver(observer.get()); | |
| 86 SendUpdate("fn", data()); | |
| 87 } | |
| 88 | |
| 89 TEST_F(MediaInternalsTest, RemovedObserversReceiveNoNotifications) { | |
| 90 scoped_ptr<MockMediaInternalsObserver> observer( | |
| 91 new MockMediaInternalsObserver()); | |
| 92 | |
| 93 EXPECT_CALL(*observer.get(), OnUpdate(testing::_)).Times(0); | |
| 94 | |
| 95 internals_->AddObserver(observer.get()); | |
| 96 internals_->RemoveObserver(observer.get()); | |
| 97 SendUpdate("fn", data()); | |
| 98 } | |
| 99 | |
| 100 TEST_F(MediaInternalsTest, DeleteRemovesItem) { | |
| 101 Value* out; | |
| 102 | |
| 103 UpdateItem("some.item", "testing", Value::CreateNullValue()); | |
| 104 EXPECT_TRUE(data()->Get("some.item", &out)); | |
| 105 EXPECT_TRUE(data()->Get("some", &out)); | |
| 106 | |
| 107 DeleteItem("some.item"); | |
| 108 EXPECT_FALSE(data()->Get("some.item", &out)); | |
| 109 EXPECT_TRUE(data()->Get("some", &out)); | |
| 110 | |
| 111 DeleteItem("some"); | |
| 112 EXPECT_FALSE(data()->Get("some.item", &out)); | |
| 113 EXPECT_FALSE(data()->Get("some", &out)); | |
| 114 } | |
| OLD | NEW |