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

Side by Side Diff: base/android/activity_status_unittest.cc

Issue 14373019: Add base/android/activity_status.cc (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: fix bug Created 7 years, 8 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
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 "base/android/activity_status.h"
6 #include "base/logging.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/run_loop.h"
9
10 #include "testing/gtest/include/gtest/gtest.h"
11
12 namespace base {
gavinp 2013/04/26 07:37:53 Blank line here.
digit1 2013/04/26 12:32:40 Are you sure about that? I'm asking because _all_
gavinp 2013/04/26 12:38:48 I just checked. It's not required. It is allowed.
13 namespace android {
14
15 // This is equivalent to what the JNI generator would output if
16 // ActivityStatus.onStateChange() was tagged with @CalledByNative.
17 // because this is only useful for unit-testing, there is no point in
18 // making this permanent for production code.
19 using base::android::ScopedJavaLocalRef;
20
21 namespace {
gavinp 2013/04/26 07:37:53 Blank line here.
digit1 2013/04/26 12:32:40 Done.
22 const char kActivityStatusClassPath[] = "org/chromium/base/ActivityStatus";
23 jclass g_ActivityStatus_clazz = NULL;
24
25 base::subtle::AtomicWord g_ActivityStatus_onStateChange = 0;
26
27 void Java_ActivityStatus_onStateChange(JNIEnv* env, jobject activity,
28 jint newState) {
29 // Ensure g_ActivityStatus_clazz is initialized
30 if (g_ActivityStatus_clazz == NULL) {
31 g_ActivityStatus_clazz = reinterpret_cast<jclass>(env->NewGlobalRef(
32 base::android::GetClass(env, kActivityStatusClassPath).obj()));
33 DCHECK(g_ActivityStatus_clazz);
34 }
35 jmethodID method_id =
36 base::android::MethodID::LazyGet<
37 base::android::MethodID::TYPE_STATIC>(
38 env, g_ActivityStatus_clazz,
39 "onStateChange",
40 "("
41 "Landroid/app/Activity;"
42 "I"
43 ")"
44 "V",
45 &g_ActivityStatus_onStateChange);
46
47 env->CallStaticVoidMethod(g_ActivityStatus_clazz,
48 method_id, activity, newState);
49 base::android::CheckException(env);
50 }
gavinp 2013/04/26 07:37:53 Blank line above this, and comment: // namespace
digit1 2013/04/26 12:32:40 I think you're mistaken, this is the end of a func
51
52 void ForceActivityStateTo(ActivityState state) {
53 JNIEnv* env = base::android::AttachCurrentThread();
gavinp 2013/04/26 07:37:53 #include <jni.h> ?
digit1 2013/04/26 12:32:40 Added #include "base/android/jni_android.h"
54 DCHECK(env != NULL);
gavinp 2013/04/26 07:37:53 Don't DCHECK in unit tests. Either ASSERT or EXPEC
digit1 2013/04/26 12:32:40 Replaced with an ASSERT_TRUE(env). Thanks.
gavinp 2013/04/26 12:38:48 Since the ASSERT is inside a function, I think it
digit1 2013/04/26 13:25:55 Yes, I believe that's why I wanted to DCHECK() in
55 RunLoop run_loop;
56 Java_ActivityStatus_onStateChange(env, NULL, static_cast<int>(state));
57 run_loop.RunUntilIdle();
58 }
59
60 // Helper class that writes the new state at a dedicated location.
61 class StoringStateListener : public ActivityStatus::StateListener {
62 public:
63 // When creating an instance, pass the address of an ActivityState
64 // variable in |result_ptr|
65 StoringStateListener(ActivityState* result_ptr)
66 : result_ptr_(result_ptr) {}
67
68 void OnActivityStateChange(ActivityState new_state) OVERRIDE {
69 // Simply write the result to the correct address.
70 *result_ptr_ = new_state;
71 }
72 private:
73 ActivityState* result_ptr_;
74 };
75
76 typedef scoped_ptr<StoringStateListener> ScopedStoringStateListener;
gavinp 2013/04/26 07:37:53 This is never used. I prefer the naked scoped_ptr
digit1 2013/04/26 12:32:40 Done.
77
78 // An invalid ActivityState value.
79 const ActivityState kInvalidActivityState = static_cast<ActivityState>(100);
80
81 } // namespace
82
83 TEST(ActivityStatusTest, SingleThread) {
84 MessageLoop message_loop;
85
86 ActivityState result = kInvalidActivityState;
87
88 scoped_ptr<StoringStateListener> listener(new StoringStateListener(&result));
89 ASSERT_TRUE(listener.get() != NULL);
gavinp 2013/04/26 07:37:53 ASSERT_TRUE(listener); Looks cleaner to me, but t
digit1 2013/04/26 12:32:40 Done.
90
91 EXPECT_EQ(kInvalidActivityState, result);
92
93 ForceActivityStateTo(ACTIVITY_STATE_CREATED);
94 EXPECT_EQ(ACTIVITY_STATE_CREATED, result);
95
96 ForceActivityStateTo(ACTIVITY_STATE_DESTROYED);
97 EXPECT_EQ(ACTIVITY_STATE_DESTROYED, result);
98 }
99
100 } // namespace android
gavinp 2013/04/26 07:37:53 Blank line after this.
101 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698