Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(781)

Side by Side Diff: apps/app_shim/extension_app_shim_handler_mac_unittest.cc

Issue 15269003: Refactor extension handling code from AppShimHost into ExtensionAppShimHandler. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Change to map of Hosts (no ObserverList). Improve test. Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 "chrome/common/chrome_notification_types.h"
9 #include "chrome/test/base/testing_profile.h"
10 #include "content/public/browser/notification_service.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12
13 namespace apps {
14
15 class TestingExtensionAppShimHandler : public ExtensionAppShimHandler {
16 public:
17 explicit TestingExtensionAppShimHandler() : fails_launch_(false) {}
18 virtual ~TestingExtensionAppShimHandler() {}
19
20 void set_fails_launch(bool fails_launch) {
21 fails_launch_ = fails_launch;
22 }
23
24 AppShimHandler::Host* FindHost(Profile* profile,
25 const std::string& app_id) {
26 HostMap::const_iterator it = hosts().find(make_pair(profile, app_id));
27 return it == hosts().end() ? NULL : it->second;
28 }
29
30 content::NotificationRegistrar& GetRegistrar() { return registrar(); }
31
32 protected:
33 virtual bool LaunchApp(Profile* profile, const std::string& app_id) OVERRIDE {
34 return !fails_launch_;
35 }
36
37 private:
38 bool fails_launch_;
39
40 DISALLOW_COPY_AND_ASSIGN(TestingExtensionAppShimHandler);
41 };
42
43 const char kTestAppIdA[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
44 const char kTestAppIdB[] = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
45
46 class FakeHost : public apps::AppShimHandler::Host {
47 public:
48 FakeHost(Profile* profile,
49 std::string app_id,
50 TestingExtensionAppShimHandler* handler)
51 : profile_(profile),
52 app_id_(app_id),
53 handler_(handler),
54 close_count_(0) {}
55
56 virtual void OnAppClosed() OVERRIDE {
57 handler_->OnShimClose(this);
58 ++close_count_;
59 }
60 virtual Profile* GetProfile() const OVERRIDE { return profile_; }
61 virtual std::string GetAppId() const OVERRIDE { return app_id_; }
62
63 int close_count() { return close_count_; }
64
65 private:
66 Profile* profile_;
67 std::string app_id_;
68 TestingExtensionAppShimHandler* handler_;
69 int close_count_;
70
71 DISALLOW_COPY_AND_ASSIGN(FakeHost);
72 };
73
74 class ExtensionAppShimHandlerTest : public testing::Test {
75 protected:
76 ExtensionAppShimHandlerTest()
77 : handler_(new TestingExtensionAppShimHandler),
78 host_aa_(&profile_a_, kTestAppIdA, handler_.get()),
79 host_ab_(&profile_a_, kTestAppIdB, handler_.get()),
80 host_bb_(&profile_b_, kTestAppIdB, handler_.get()),
81 host_aa_duplicate_(&profile_a_, kTestAppIdA, handler_.get()) {}
82
83 scoped_ptr<TestingExtensionAppShimHandler> handler_;
tapted 2013/05/23 07:03:11 scoped_ptr is back! --> #include :p
jackhou1 2013/05/24 00:12:27 Done.
84 TestingProfile profile_a_;
85 TestingProfile profile_b_;
86 FakeHost host_aa_;
87 FakeHost host_ab_;
88 FakeHost host_bb_;
89 FakeHost host_aa_duplicate_;
90
91 private:
92 DISALLOW_COPY_AND_ASSIGN(ExtensionAppShimHandlerTest);
93 };
94
95 TEST_F(ExtensionAppShimHandlerTest, LaunchAndCloseShim) {
96 // If launch fails, the host is not added to the map.
97 handler_->set_fails_launch(true);
98 EXPECT_EQ(false, handler_->OnShimLaunch(&host_aa_));
99 EXPECT_FALSE(handler_->FindHost(&profile_a_, kTestAppIdA));
100
101 // Normal startup.
102 handler_->set_fails_launch(false);
103 EXPECT_EQ(true, handler_->OnShimLaunch(&host_aa_));
104 EXPECT_EQ(&host_aa_, handler_->FindHost(&profile_a_, kTestAppIdA));
105 EXPECT_TRUE(handler_->GetRegistrar().IsRegistered(
106 handler_.get(),
107 chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED,
108 content::Source<Profile>(&profile_a_)));
109 EXPECT_EQ(true, handler_->OnShimLaunch(&host_ab_));
110 EXPECT_EQ(&host_ab_, handler_->FindHost(&profile_a_, kTestAppIdB));
111 EXPECT_EQ(true, handler_->OnShimLaunch(&host_bb_));
112 EXPECT_EQ(&host_bb_, handler_->FindHost(&profile_b_, kTestAppIdB));
113 EXPECT_TRUE(handler_->GetRegistrar().IsRegistered(
114 handler_.get(),
115 chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED,
116 content::Source<Profile>(&profile_b_)));
117
118 // Starting and closing a second host does nothing.
119 EXPECT_EQ(false, handler_->OnShimLaunch(&host_aa_duplicate_));
120 EXPECT_EQ(&host_aa_, handler_->FindHost(&profile_a_, kTestAppIdA));
121 handler_->OnShimClose(&host_aa_duplicate_);
122 EXPECT_EQ(&host_aa_, handler_->FindHost(&profile_a_, kTestAppIdA));
123
124 // Normal close.
125 handler_->OnShimClose(&host_aa_);
126 EXPECT_FALSE(handler_->FindHost(&profile_a_, kTestAppIdA));
127
128 // Closing the second host afterward does nothing.
129 handler_->OnShimClose(&host_aa_duplicate_);
130 EXPECT_FALSE(handler_->FindHost(&profile_a_, kTestAppIdA));
131
132 // Destruction sends OnAppClose to remaining hosts.
133 handler_.reset();
134 EXPECT_EQ(1, host_ab_.close_count());
135 EXPECT_EQ(1, host_bb_.close_count());
136 }
137
138 } // namespace apps
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698