Chromium Code Reviews| 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/app_shim_host.h" | |
| 6 | |
| 7 #include "apps/app_shim/app_shim_messages.h" | |
| 8 #include "base/memory/scoped_vector.h" | |
| 9 #include "chrome/test/base/testing_profile.h" | |
| 10 #include "ipc/ipc_message.h" | |
| 11 #include "testing/gtest/include/gtest/gtest.h" | |
| 12 | |
| 13 class TestingAppShimHost : public AppShimHost { | |
| 14 public: | |
| 15 explicit TestingAppShimHost(Profile* profile); | |
| 16 virtual ~TestingAppShimHost() {} | |
| 17 | |
| 18 bool SendFakeMessage(IPC::Message* message); | |
| 19 | |
| 20 const std::vector<IPC::Message*>& sent_messages() { | |
| 21 return sent_messages_.get(); | |
| 22 } | |
| 23 | |
| 24 void set_fails_profile(bool fails_profile) { | |
| 25 fails_profile_ = fails_profile; | |
| 26 } | |
| 27 | |
| 28 void set_fails_launch(bool fails_launch) { | |
| 29 fails_launch_ = fails_launch; | |
| 30 } | |
| 31 | |
| 32 protected: | |
| 33 virtual Profile* FetchProfileForDirectory(const std::string& profile_dir) | |
| 34 OVERRIDE; | |
| 35 virtual bool LaunchApp(Profile* profile, const std::string& app_id) OVERRIDE; | |
| 36 | |
| 37 virtual bool Send(IPC::Message* message) OVERRIDE; | |
| 38 | |
| 39 Profile* test_profile_; | |
| 40 bool fails_profile_; | |
| 41 bool fails_launch_; | |
| 42 | |
| 43 ScopedVector<IPC::Message> sent_messages_; | |
| 44 | |
| 45 DISALLOW_COPY_AND_ASSIGN(TestingAppShimHost); | |
| 46 }; | |
| 47 | |
| 48 TestingAppShimHost::TestingAppShimHost(Profile* profile) | |
| 49 : test_profile_(profile), | |
| 50 fails_profile_(false), | |
| 51 fails_launch_(false) { | |
| 52 } | |
| 53 | |
| 54 bool TestingAppShimHost::SendFakeMessage(IPC::Message* message) { | |
|
koz (OOO until 15th September)
2013/03/14 23:51:05
I think this would be better named as ReceiveFakeM
jeremya
2013/03/15 02:24:14
Done.
| |
| 55 bool handled = OnMessageReceived(*message); | |
| 56 delete message; | |
| 57 return handled; | |
| 58 } | |
| 59 | |
| 60 bool TestingAppShimHost::Send(IPC::Message* message) { | |
| 61 sent_messages_.push_back(message); | |
| 62 return true; | |
| 63 } | |
| 64 | |
| 65 Profile* TestingAppShimHost::FetchProfileForDirectory( | |
| 66 const std::string& profile_dir) { | |
| 67 return fails_profile_ ? NULL : test_profile_; | |
| 68 } | |
| 69 | |
| 70 bool TestingAppShimHost::LaunchApp( | |
| 71 Profile* profile, const std::string& app_id) { | |
| 72 return !fails_launch_; | |
| 73 } | |
| 74 | |
| 75 class AppShimHostTest : public testing::Test { | |
| 76 public: | |
| 77 TestingAppShimHost* host() { return host_; } | |
| 78 TestingProfile* profile() { return profile_; } | |
| 79 | |
| 80 bool LaunchWasSuccessful() { | |
| 81 EXPECT_EQ(1u, host()->sent_messages().size()); | |
| 82 IPC::Message* message = host()->sent_messages()[0]; | |
| 83 EXPECT_EQ(AppShimMsg_LaunchApp_Done::ID, message->type()); | |
| 84 AppShimMsg_LaunchApp_Done::Param param; | |
| 85 AppShimMsg_LaunchApp_Done::Read(message, ¶m); | |
| 86 return param.a; | |
| 87 } | |
| 88 | |
| 89 private: | |
| 90 virtual void SetUp() OVERRIDE { | |
| 91 profile_ = new TestingProfile; | |
| 92 host_ = new TestingAppShimHost(profile_); | |
| 93 } | |
| 94 virtual void TearDown() OVERRIDE { | |
| 95 delete host_; | |
| 96 delete profile_; | |
| 97 } | |
| 98 | |
| 99 TestingAppShimHost* host_; | |
|
koz (OOO until 15th September)
2013/03/14 23:51:05
scoped_ptrs means you don't need a TearDown().
jeremya
2013/03/15 02:24:14
Like magic!
| |
| 100 TestingProfile* profile_; | |
| 101 }; | |
| 102 | |
| 103 static const std::string kTestAppId = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; | |
| 104 static const std::string kTestProfileDir = "Default"; | |
| 105 | |
| 106 TEST_F(AppShimHostTest, TestLaunchApp) { | |
| 107 host()->SendFakeMessage( | |
| 108 new AppShimHostMsg_LaunchApp(kTestProfileDir, kTestAppId)); | |
| 109 ASSERT_EQ(kTestAppId, host()->app_id()); | |
| 110 ASSERT_EQ(profile(), host()->profile()); | |
| 111 ASSERT_EQ(true, LaunchWasSuccessful()); | |
|
koz (OOO until 15th September)
2013/03/14 23:51:05
nit: ASSERT_TRUE
jeremya
2013/03/15 02:24:14
Done.
| |
| 112 } | |
| 113 | |
| 114 TEST_F(AppShimHostTest, TestFailProfile) { | |
| 115 host()->set_fails_profile(true); | |
| 116 host()->SendFakeMessage( | |
| 117 new AppShimHostMsg_LaunchApp(kTestProfileDir, kTestAppId)); | |
| 118 ASSERT_EQ(0u, host()->app_id().length()); | |
|
koz (OOO until 15th September)
2013/03/14 23:51:05
nit: host()->app_id().empty()
jeremya
2013/03/15 02:24:14
Done.
| |
| 119 ASSERT_EQ(false, LaunchWasSuccessful()); | |
|
koz (OOO until 15th September)
2013/03/14 23:51:05
nit: ASSERT_FALSE
jeremya
2013/03/15 02:24:14
Done.
| |
| 120 } | |
| 121 | |
| 122 TEST_F(AppShimHostTest, TestFailLaunch) { | |
| 123 host()->set_fails_launch(true); | |
| 124 host()->SendFakeMessage( | |
| 125 new AppShimHostMsg_LaunchApp(kTestProfileDir, kTestAppId)); | |
| 126 ASSERT_EQ(0u, host()->app_id().length()); | |
|
koz (OOO until 15th September)
2013/03/14 23:51:05
s/length/empty()
jeremya
2013/03/15 02:24:14
Done.
| |
| 127 ASSERT_EQ(false, LaunchWasSuccessful()); | |
|
koz (OOO until 15th September)
2013/03/14 23:51:05
nit: ASSERT_FALSE
jeremya
2013/03/15 02:24:14
Done.
| |
| 128 } | |
| OLD | NEW |