OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 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 "apps/app_shim/extension_app_shim_handler_mac.h" |
| 6 |
| 7 #include "apps/app_shim/app_shim_host_mac.h" |
| 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "chrome/common/chrome_notification_types.h" |
| 10 #include "chrome/test/base/testing_profile.h" |
| 11 #include "content/public/browser/notification_service.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 namespace apps { |
| 15 |
| 16 class TestingExtensionAppShimHandler : public ExtensionAppShimHandler { |
| 17 public: |
| 18 explicit TestingExtensionAppShimHandler() : fails_launch_(false) {} |
| 19 virtual ~TestingExtensionAppShimHandler() {} |
| 20 |
| 21 void set_fails_launch(bool fails_launch) { |
| 22 fails_launch_ = fails_launch; |
| 23 } |
| 24 |
| 25 AppShimHandler::Host* FindHost(Profile* profile, |
| 26 const std::string& app_id) { |
| 27 HostMap::const_iterator it = hosts().find(make_pair(profile, app_id)); |
| 28 return it == hosts().end() ? NULL : it->second; |
| 29 } |
| 30 |
| 31 content::NotificationRegistrar& GetRegistrar() { return registrar(); } |
| 32 |
| 33 protected: |
| 34 virtual bool LaunchApp(Profile* profile, const std::string& app_id) OVERRIDE { |
| 35 return !fails_launch_; |
| 36 } |
| 37 |
| 38 private: |
| 39 bool fails_launch_; |
| 40 |
| 41 DISALLOW_COPY_AND_ASSIGN(TestingExtensionAppShimHandler); |
| 42 }; |
| 43 |
| 44 const char kTestAppIdA[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; |
| 45 const char kTestAppIdB[] = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"; |
| 46 |
| 47 class FakeHost : public apps::AppShimHandler::Host { |
| 48 public: |
| 49 FakeHost(Profile* profile, |
| 50 std::string app_id, |
| 51 TestingExtensionAppShimHandler* handler) |
| 52 : profile_(profile), |
| 53 app_id_(app_id), |
| 54 handler_(handler), |
| 55 close_count_(0) {} |
| 56 |
| 57 virtual void OnAppClosed() OVERRIDE { |
| 58 handler_->OnShimClose(this); |
| 59 ++close_count_; |
| 60 } |
| 61 virtual Profile* GetProfile() const OVERRIDE { return profile_; } |
| 62 virtual std::string GetAppId() const OVERRIDE { return app_id_; } |
| 63 |
| 64 int close_count() { return close_count_; } |
| 65 |
| 66 private: |
| 67 Profile* profile_; |
| 68 std::string app_id_; |
| 69 TestingExtensionAppShimHandler* handler_; |
| 70 int close_count_; |
| 71 |
| 72 DISALLOW_COPY_AND_ASSIGN(FakeHost); |
| 73 }; |
| 74 |
| 75 class ExtensionAppShimHandlerTest : public testing::Test { |
| 76 protected: |
| 77 ExtensionAppShimHandlerTest() |
| 78 : handler_(new TestingExtensionAppShimHandler), |
| 79 host_aa_(&profile_a_, kTestAppIdA, handler_.get()), |
| 80 host_ab_(&profile_a_, kTestAppIdB, handler_.get()), |
| 81 host_bb_(&profile_b_, kTestAppIdB, handler_.get()), |
| 82 host_aa_duplicate_(&profile_a_, kTestAppIdA, handler_.get()) {} |
| 83 |
| 84 scoped_ptr<TestingExtensionAppShimHandler> handler_; |
| 85 TestingProfile profile_a_; |
| 86 TestingProfile profile_b_; |
| 87 FakeHost host_aa_; |
| 88 FakeHost host_ab_; |
| 89 FakeHost host_bb_; |
| 90 FakeHost host_aa_duplicate_; |
| 91 |
| 92 private: |
| 93 DISALLOW_COPY_AND_ASSIGN(ExtensionAppShimHandlerTest); |
| 94 }; |
| 95 |
| 96 TEST_F(ExtensionAppShimHandlerTest, LaunchAndCloseShim) { |
| 97 // If launch fails, the host is not added to the map. |
| 98 handler_->set_fails_launch(true); |
| 99 EXPECT_EQ(false, handler_->OnShimLaunch(&host_aa_)); |
| 100 EXPECT_FALSE(handler_->FindHost(&profile_a_, kTestAppIdA)); |
| 101 |
| 102 // Normal startup. |
| 103 handler_->set_fails_launch(false); |
| 104 EXPECT_EQ(true, handler_->OnShimLaunch(&host_aa_)); |
| 105 EXPECT_EQ(&host_aa_, handler_->FindHost(&profile_a_, kTestAppIdA)); |
| 106 EXPECT_TRUE(handler_->GetRegistrar().IsRegistered( |
| 107 handler_.get(), |
| 108 chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, |
| 109 content::Source<Profile>(&profile_a_))); |
| 110 EXPECT_EQ(true, handler_->OnShimLaunch(&host_ab_)); |
| 111 EXPECT_EQ(&host_ab_, handler_->FindHost(&profile_a_, kTestAppIdB)); |
| 112 EXPECT_EQ(true, handler_->OnShimLaunch(&host_bb_)); |
| 113 EXPECT_EQ(&host_bb_, handler_->FindHost(&profile_b_, kTestAppIdB)); |
| 114 EXPECT_TRUE(handler_->GetRegistrar().IsRegistered( |
| 115 handler_.get(), |
| 116 chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, |
| 117 content::Source<Profile>(&profile_b_))); |
| 118 |
| 119 // Starting and closing a second host does nothing. |
| 120 EXPECT_EQ(false, handler_->OnShimLaunch(&host_aa_duplicate_)); |
| 121 EXPECT_EQ(&host_aa_, handler_->FindHost(&profile_a_, kTestAppIdA)); |
| 122 handler_->OnShimClose(&host_aa_duplicate_); |
| 123 EXPECT_EQ(&host_aa_, handler_->FindHost(&profile_a_, kTestAppIdA)); |
| 124 |
| 125 // Normal close. |
| 126 handler_->OnShimClose(&host_aa_); |
| 127 EXPECT_FALSE(handler_->FindHost(&profile_a_, kTestAppIdA)); |
| 128 |
| 129 // Closing the second host afterward does nothing. |
| 130 handler_->OnShimClose(&host_aa_duplicate_); |
| 131 EXPECT_FALSE(handler_->FindHost(&profile_a_, kTestAppIdA)); |
| 132 |
| 133 // Destruction sends OnAppClose to remaining hosts. |
| 134 handler_.reset(); |
| 135 EXPECT_EQ(1, host_ab_.close_count()); |
| 136 EXPECT_EQ(1, host_bb_.close_count()); |
| 137 } |
| 138 |
| 139 } // namespace apps |
OLD | NEW |