Upstream version 10.38.222.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / bluetooth / bluetooth_event_router_unittest.cc
1 // Copyright (c) 2012 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 <string>
6
7 #include "base/memory/ref_counted.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/run_loop.h"
11 #include "chrome/browser/extensions/api/bluetooth/bluetooth_event_router.h"
12 #include "chrome/browser/extensions/extension_system_factory.h"
13 #include "chrome/browser/extensions/test_extension_system.h"
14 #include "chrome/common/extensions/api/bluetooth.h"
15 #include "chrome/test/base/testing_profile.h"
16 #include "content/public/test/test_browser_thread.h"
17 #include "content/public/test/test_browser_thread_bundle.h"
18 #include "device/bluetooth/bluetooth_uuid.h"
19 #include "device/bluetooth/test/mock_bluetooth_adapter.h"
20 #include "device/bluetooth/test/mock_bluetooth_device.h"
21 #include "extensions/browser/event_router.h"
22 #include "extensions/browser/extension_registry.h"
23 #include "extensions/common/extension_builder.h"
24 #include "testing/gmock/include/gmock/gmock.h"
25 #include "testing/gtest/include/gtest/gtest.h"
26
27 namespace {
28
29 const char kTestExtensionId[] = "test extension id";
30 const device::BluetoothUUID kAudioProfileUuid("1234");
31 const device::BluetoothUUID kHealthProfileUuid("4321");
32
33 }  // namespace
34
35 namespace extensions {
36
37 namespace bluetooth = api::bluetooth;
38
39 class BluetoothEventRouterTest : public testing::Test {
40  public:
41   BluetoothEventRouterTest()
42       : ui_thread_(content::BrowserThread::UI, &message_loop_),
43         mock_adapter_(new testing::StrictMock<device::MockBluetoothAdapter>()),
44         test_profile_(new TestingProfile()),
45         router_(new BluetoothEventRouter(test_profile_.get())) {
46     router_->SetAdapterForTest(mock_adapter_);
47   }
48
49   virtual void TearDown() OVERRIDE {
50     // Some profile-dependent services rely on UI thread to clean up. We make
51     // sure they are properly cleaned up by running the UI message loop until
52     // idle.
53     // It's important to destroy the router before the |test_profile_| so it
54     // removes itself as an observer.
55     router_.reset(NULL);
56     test_profile_.reset(NULL);
57     base::RunLoop run_loop;
58     run_loop.RunUntilIdle();
59   }
60
61  protected:
62   base::MessageLoopForUI message_loop_;
63   // Note: |ui_thread_| must be declared before |router_|.
64   content::TestBrowserThread ui_thread_;
65   testing::StrictMock<device::MockBluetoothAdapter>* mock_adapter_;
66   scoped_ptr<TestingProfile> test_profile_;
67   scoped_ptr<BluetoothEventRouter> router_;
68 };
69
70 TEST_F(BluetoothEventRouterTest, BluetoothEventListener) {
71   router_->OnListenerAdded();
72   EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1);
73   router_->OnListenerRemoved();
74 }
75
76 TEST_F(BluetoothEventRouterTest, MultipleBluetoothEventListeners) {
77   router_->OnListenerAdded();
78   router_->OnListenerAdded();
79   router_->OnListenerAdded();
80   router_->OnListenerRemoved();
81   router_->OnListenerRemoved();
82   EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1);
83   router_->OnListenerRemoved();
84 }
85
86 TEST_F(BluetoothEventRouterTest, UnloadExtension) {
87   scoped_refptr<const extensions::Extension> extension =
88       extensions::ExtensionBuilder()
89           .SetManifest(extensions::DictionaryBuilder()
90               .Set("name", "BT event router test")
91               .Set("version", "1.0")
92               .Set("manifest_version", 2))
93           .SetID(kTestExtensionId)
94           .Build();
95
96   ExtensionRegistry::Get(test_profile_.get())
97       ->TriggerOnUnloaded(extension, UnloadedExtensionInfo::REASON_DISABLE);
98
99   EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1);
100 }
101
102 }  // namespace extensions