• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "dex_cache.h"
18 
19 #include <stdio.h>
20 
21 #include "class_linker.h"
22 #include "common_runtime_test.h"
23 #include "linear_alloc.h"
24 #include "mirror/class_loader-inl.h"
25 #include "handle_scope-inl.h"
26 #include "scoped_thread_state_change.h"
27 
28 namespace art {
29 namespace mirror {
30 
31 class DexCacheTest : public CommonRuntimeTest {};
32 
TEST_F(DexCacheTest,Open)33 TEST_F(DexCacheTest, Open) {
34   ScopedObjectAccess soa(Thread::Current());
35   StackHandleScope<1> hs(soa.Self());
36   ASSERT_TRUE(java_lang_dex_file_ != nullptr);
37   Handle<DexCache> dex_cache(
38       hs.NewHandle(class_linker_->AllocDexCache(soa.Self(),
39                                                 *java_lang_dex_file_,
40                                                 Runtime::Current()->GetLinearAlloc())));
41   ASSERT_TRUE(dex_cache.Get() != nullptr);
42 
43   EXPECT_EQ(java_lang_dex_file_->NumStringIds(), dex_cache->NumStrings());
44   EXPECT_EQ(java_lang_dex_file_->NumTypeIds(),   dex_cache->NumResolvedTypes());
45   EXPECT_EQ(java_lang_dex_file_->NumMethodIds(), dex_cache->NumResolvedMethods());
46   EXPECT_EQ(java_lang_dex_file_->NumFieldIds(),  dex_cache->NumResolvedFields());
47 }
48 
TEST_F(DexCacheTest,LinearAlloc)49 TEST_F(DexCacheTest, LinearAlloc) {
50   ScopedObjectAccess soa(Thread::Current());
51   jobject jclass_loader(LoadDex("Main"));
52   ASSERT_TRUE(jclass_loader != nullptr);
53   Runtime* const runtime = Runtime::Current();
54   ClassLinker* const class_linker = runtime->GetClassLinker();
55   StackHandleScope<1> hs(soa.Self());
56   Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
57       soa.Decode<mirror::ClassLoader*>(jclass_loader)));
58   mirror::Class* klass = class_linker->FindClass(soa.Self(), "LMain;", class_loader);
59   ASSERT_TRUE(klass != nullptr);
60   LinearAlloc* const linear_alloc = klass->GetClassLoader()->GetAllocator();
61   EXPECT_NE(linear_alloc, runtime->GetLinearAlloc());
62   EXPECT_TRUE(linear_alloc->Contains(klass->GetDexCache()->GetResolvedMethods()));
63 }
64 
65 }  // namespace mirror
66 }  // namespace art
67